Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

search_form_to_search_query()

Description

Constructs a search query string from the posted search form data.

This function takes the advanced search form fields and assembles them
into a search query string that can be used for a standard search. It
processes various input fields, including dates, keywords, and resource IDs,
while respecting user permissions and field visibility settings.

Parameters

ColumnTypeDefaultDescription
$fields array An array of fields used in the search form.
$fromsearchbar bool false Indicates if the search is initiated from a search bar.

Return

string The constructed search query string based on the input data.

Location

include/search_functions.php lines 174 to 546

Definition

 
function search_form_to_search_query($fields,$fromsearchbar=false)
    {
    global 
$auto_order_checkbox,$checkbox_and,$resource_field_verbatim_keyword_regex;
    
$search="";
    if (
getval("basicyear","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="basicyear:" getval("basicyear","");
        }
    if (
getval("basicmonth","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="basicmonth:" getval("basicmonth","");
        }
    if (
getval("basicday","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="basicday:" getval("basicday","");
        }
    if (
getval("startdate","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="startdate:" getval("startdate","");
        }
    if (
getval("enddate","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="enddate:" getval("enddate","");
        }
    if (
getval("start-y","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="startdate:" getval("start-y","");
        if (
getval("start-m","")!="")
            {
            
$search.="-" getval("start-m","");
            if (
getval("start-d","")!="")
                {
                
$search.="-" getval("start-d","");
                }
            else
                {
                
$search.="-01";
                }
            }
        else
            {
            
$search.="-01-01";
            }
        }
    if (
getval("end-y","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="enddate:" getval("end-y","");
        if (
getval("end-m","")!="")
            {
            
$search.="-" getval("end-m","");
            if (
getval("end-d","")!="")
                {
                
$search.="-" getval("end-d","");
                }
            else
                {
                
$search.="-31";
                }
            }
        else
            {
            
$search.="-12-31";
            }
        }
    if (
getval("allfields","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.=join(", ",explode(" ",getval("allfields",""))); # prepend 'all fields' option
        
}
    if (
getval("resourceids","")!="")
        {
        
$listsql="!list" join(":",trim_array(split_keywords(getval("resourceids",""))));
        
$search=$listsql " " $search;
        }
    
$full_text_search getval(FULLTEXT_SEARCH_PREFIX,"");
    if (
$full_text_search != "")
        {
        if (
$search!="") {$search.=" ";}
        
$full_text_search str_replace("\"",FULLTEXT_SEARCH_QUOTES_PLACEHOLDER,$full_text_search);
        
$search .= '"' FULLTEXT_SEARCH_PREFIX ':' $full_text_search '"';
        }

    for (
$n=0;$n<count($fields);$n++)
        {
        switch (
$fields[$n]["type"])
            {
            case 
FIELD_TYPE_TEXT_BOX_MULTI_LINE:
            case 
FIELD_TYPE_TEXT_BOX_LARGE_MULTI_LINE:
            case 
FIELD_TYPE_TEXT_BOX_FORMATTED_AND_CKEDITOR:
            
$name="field_" $fields[$n]["ref"];
            
$value=getval($name,"");
            if (
$value!="")
                {
                if(isset(
$resource_field_verbatim_keyword_regex[$fields[$n]["ref"]])
                    && 
preg_match(
                        
$resource_field_verbatim_keyword_regex[$fields[$n]["ref"]],
                        
str_replace('*'''$value)
                        ))
                    {
                    
// Keyword matches verbatim regex, do not split
                    
$vs=[$value];
                    }
                else
                    {
                    
$vs=split_keywords($valuefalsefalsefalsefalsetrue);
                    }
                for (
$m=0;$m<count($vs);$m++)
                    {
                    if (
$search!="") {$search.=", ";}
                    
$search.= ((strpos($vs[$m],"\"")===false)?$fields[$n]["name"] . ":" $vs[$m]:"\"" $fields[$n]["name"] . ":" substr($vs[$m],1,-1) . "\""); // Move any quotes around whole field:value element so that they are kept together
                    
}
                }
            break;

            case 
FIELD_TYPE_DROP_DOWN_LIST# -------- Dropdowns / check lists
            
case FIELD_TYPE_CHECK_BOX_LIST:
            if (
$fields[$n]["display_as_dropdown"])
                {
                
# Process dropdown box
                
$name="field_" $fields[$n]["ref"];
                
$value=getval($name,"");
                if (
$value!=="")
                    {
                    if (
$search!="") {$search.=", ";}
                    
$search.= ((strpos($value," ")===false)?$fields[$n]["name"] . ":" $value:"\"" $fields[$n]["name"] . ":" .substr($value,1,-1) . "\"");
                    }
                }
            else
                {
                
# Process checkbox list
                
$options=array();
                
node_field_options_override($options,$fields[$n]['ref']);
                
$p="";
                
$c=0;
                for (
$m=0;$m<count($options);$m++)
                    {
                    
$name=$fields[$n]["ref"] . "_" md5($options[$m]);
                    
$value=getval($name,"");
                    if (
$value=="yes")
                        {
                        
$c++;
                        if (
$p!="") {$p.=";";}
                        
$p.=mb_strtolower(i18n_get_translated($options[$m]), 'UTF-8');
                        }
                    }

                if ((
$c==count($options) && !$checkbox_and) && (count($options)>1))
                    {
                    
# all options ticked - omit from the search (unless using AND matching, or there is only one option intended as a boolean selection)
                    
$p="";
                    }
                if (
$p!="")
                    {
                    if (
$search!="") {$search.=", ";}
                    if(
$checkbox_and)
                        {
                        
$p=str_replace(";",", {$fields[$n]["name"]}:",$p); // this will force each and condition into a separate union in do_search (which will AND)
                        
if ($search!="") {$search.=", ";}
                        }
                    
$search.=$fields[$n]["name"] . ":" $p;
                    }
                }
            break;

            case 
FIELD_TYPE_DATE_AND_OPTIONAL_TIME:
            case 
FIELD_TYPE_EXPIRY_DATE:
            case 
FIELD_TYPE_DATE:
            case 
FIELD_TYPE_DATE_RANGE:
            
$name="field_" $fields[$n]["ref"];
            
$datepart="";
            
$value="";
            if (
strpos($search$name.":")===false)
                {
                
// Get each part of the date
                
$key_year=$name."-y";
                
$value_year=getval($key_year,"");

                
$key_month=$name."-m";
                
$value_month=getval($key_month,"");

                
$key_day=$name."-d";
                
$value_day=getval($key_day,"");

                
// The following constructs full date yyyy-mm-dd or partial dates yyyy-mm or yyyy
                // However yyyy-00-dd is interpreted as yyyy because its not a valid partial date

                
$value_date_final="";
                
// Process the valid combinations, otherwise treat it as an empty date
                
if ($value_year !="" && $value_month !="" && $value_day !="") {
                    
$value_date_final=$value_year."-".$value_month."-".$value_day;
                }
                elseif (
$value_year !="" && $value_month !="") {
                    
$value_date_final=$value_year."-".$value_month;
                }
                elseif (
$value_year !="") {
                    
$value_date_final=$value_year;
                }

                if (
$value_date_final !="") {
                    
// If search already has value, then attach this value separated by a comma
                    
if ($search!="") {$search.=", ";}
                    
$search.=$fields[$n]["name"] . ":" $value_date_final;
                }

                }

            if((
$date_edtf=getval("field_" $fields[$n]["ref"] . "_edtf",""))!=="")
                {
                
// We have been passed the range in EDTF format, check it is in the correct format
                
$rangeregex="/^(\d{4})(-\d{2})?(-\d{2})?\/(\d{4})(-\d{2})?(-\d{2})?/";
                if(!
preg_match($rangeregex,$date_edtf,$matches))
                    {
                    
//ignore this string as it is not a valid EDTF string
                    
continue 2;
                    }
                
$rangedates explode("/",$date_edtf);
                
$rangestart=str_pad($rangedates[0], 10"-00");
                
$rangeendparts=explode("-",$rangedates[1]);
                
$rangeend=$rangeendparts[0] . "-" . (isset($rangeendparts[1])?$rangeendparts[1]:"12") . "-" . (isset($rangeendparts[2])?$rangeendparts[2]:"99");
                
$datepart "start" $rangestart "end" $rangeend;
                }
            else
                {
                
#Date range search - start date
                
if (getval($name "_start-y","")!="")
                    {
                    
$datepart.= "start" getval($name "_start-y","");
                    if (
getval($name "_start-m","")!="")
                        {
                        
$datepart.="-" getval($name "_start-m","");
                        if (
getval($name "_start-d","")!="")
                            {
                            
$datepart.="-" getval($name "_start-d","");
                            }
                        else
                            {
                            
$datepart.="";
                            }
                        }
                    else
                        {
                        
$datepart.="";
                        }
                    }

                
#Date range search - end date
                
if (getval($name "_end-y","")!="")
                    {
                    
$datepart.= "end" getval($name "_end-y","");
                    if (
getval($name "_end-m","")!="")
                        {
                        
$datepart.="-" getval($name "_end-m","");
                        if (
getval($name "_end-d","")!="")
                            {
                            
$datepart.="-" getval($name "_end-d","");
                            }
                        else
                            {
                            
$datepart.="-31";
                            }
                        }
                    else
                        {
                        
$datepart.="-12-31";
                        }
                    }
                }
            if (
$datepart!="")
                {
                if (
$search!="") {$search.=", ";}
                
$search.=$fields[$n]["name"] . ":range" $datepart;
                }

            break;

            case 
FIELD_TYPE_TEXT_BOX_SINGLE_LINE# -------- Text boxes
            
default:
                
$value=getval('field_'.$fields[$n]["ref"],'');
                if (
$value!="")
                    {
                    if(isset(
$resource_field_verbatim_keyword_regex[$fields[$n]["ref"]])
                        && 
preg_match(
                            
$resource_field_verbatim_keyword_regex[$fields[$n]["ref"]],
                            
str_replace('*'''$value)
                            ))
                        {
                        
// Keyword matches verbatim regex, do not split
                        
$valueparts=[$value];
                        }
                    else
                        {
                        
$valueparts=split_keywords($valuefalsefalsefalsefalsetrue);
                        }
                    foreach(
$valueparts as $valuepart)
                        {
                        if (
$search!="") {$search.=", ";}
                        
// Move any quotes around whole field:value element so that they are kept together
                        
$search.= (strpos($valuepart,"\"")===false)?($fields[$n]["name"] . ":" $valuepart):("\"" $fields[$n]["name"] . ":" .substr($valuepart,1,-1) . "\"");
                        }
                    }
            break;
            }
        }

        
##### NODES #####
        // Fixed lists will be handled separately as we don't care about the field
        // they belong to
        
$node_ref '';

        foreach(
getval('nodes_searched', [], false'is_array') as $searchedfield => $searched_field_nodes)
            {
            
// Fields that are displayed as a dropdown will only pass one node ID
            
if(!is_array($searched_field_nodes) && '' == $searched_field_nodes)
                {
                continue;
                }
            elseif(!
is_array($searched_field_nodes))
                {
                
$node_ref .= ', ' NODE_TOKEN_PREFIX $searched_field_nodes;
                continue;
                }

            
$fieldinfo get_resource_type_field($searchedfield);

            
// For fields that are displayed as checkboxes
            
$node_ref .= ', ';

            foreach(
$searched_field_nodes as $searched_node_ref)
                {
                if(
$fieldinfo["type"] == FIELD_TYPE_CHECK_BOX_LIST && $checkbox_and)
                    {
                    
// Split into an additional search element to force a join since this is a separate condition
                    
$node_ref .= ', ';
                    }
                
$node_ref .= NODE_TOKEN_PREFIX $searched_node_ref;
                }
            }

        
$search = ('' == $search '' join(', 'split_keywords($search,false,false,false,false,true))) . $node_ref;
        
##### END OF NODES #####

        
$propertysearchcodes=array();
        global 
$advanced_search_properties;
        foreach(
$advanced_search_properties as $advanced_search_property=>$code)
            {
            
$propval=getval($advanced_search_property,"");
            if(
$propval!="")
                {
$propertysearchcodes[] =$code ":" $propval;}
            }
        if(
count($propertysearchcodes)>0)
            {
            
$search '!properties' implode(';'$propertysearchcodes) . ' ,' $search;
            }
        else
            {
            
// Allow a single special search to be prepended to the search string. For example, !contributions<user id>
            
foreach ($_POST as $key=>$value)
                {
                if (
$key[0]=='!' && strlen($value) > 0)
                    {
                    
$search=$key $value ',' $search;
                    }
                }
            }
        return 
$search;
    }

This article was last updated 17th November 2024 15:05 Europe/London time based on the source file dated 8th November 2024 11:45 Europe/London time.