Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

search_form_to_search_query()

Parameters

ColumnTypeDefaultDescription
$fields
$fromsearchbar false

Location

include/search_functions.php lines 127 to 505

Definition

 
function search_form_to_search_query($fields,$fromsearchbar=false)
    {
    
# Take the data in the the posted search form that contained $fields, and assemble
    # a search query string that can be used for a standard search.
    #
    # This is used to take the advanced search form and assemble it into a search query.
    
    
global $auto_order_checkbox,$checkbox_and,$dynamic_keyword_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_year","")!="")
        {       
        if (
$search!="") {$search.=", ";}
        
$search.="startdate:" getval("start_year","");
        if (
getval("start_month","")!="")
            {
            
$search.="-" getval("start_month","");
            if (
getval("start_day","")!="")
                {
                
$search.="-" getval("start_day","");
                }
            else
                {
                
$search.="-01";
                }
            }
        else
            {
            
$search.="-01-01";
            }
        }   
    if (
getval("end_year","")!="")
        {
        if (
$search!="") {$search.=", ";}
        
$search.="enddate:" getval("end_year","");
        if (
getval("end_month","")!="")
            {
            
$search.="-" getval("end_month","");
            if (
getval("end_day","")!="")
                {
                
$search.="-" getval("end_day","");
                }
            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."_year";
                
$value_year=getval($key_year,"");
                
                
$key_month=$name."_month";
                
$value_month=getval($key_month,"");
                
                
$key_day=$name."_day";
                
$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_year","")!="")
                    {
                    
$datepart.= "start" getval($name "_start_year","");
                    if (
getval($name "_start_month","")!="")
                        {
                        
$datepart.="-" getval($name "_start_month","");
                        if (
getval($name "_start_day","")!="")
                            {
                            
$datepart.="-" getval($name "_start_day","");
                            }
                        else
                            {
                            
$datepart.="";
                            }
                        }
                    else
                        {
                        
$datepart.="";
                        }
                    }
                    
                
#Date range search - end date
                
if (getval($name "_end_year","")!="")
                    {
                    
$datepart.= "end" getval($name "_end_year","");
                    if (
getval($name "_end_month","")!="")
                        {
                        
$datepart.="-" getval($name "_end_month","");
                        if (
getval($name "_end_day","")!="")
                            {
                            
$datepart.="-" getval($name "_end_day","");
                            }
                        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 (except when $checkbox_and and $dynamic_keyword_and)
        // we know exactly what we are searching for.
        
$node_ref '';

        foreach(
getval('nodes_searched', 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) || ($fieldinfo["type"] == FIELD_TYPE_DYNAMIC_KEYWORDS_LIST && $dynamic_keyword_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 19th March 2024 11:05 Europe/London time based on the source file dated 15th March 2024 17:00 Europe/London time.