Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

search_form_to_search_query()

Parameters

ColumnTypeDefaultDescription
$fields
$fromsearchbar false

Location

include/search_functions.php lines 122 to 470

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;
    
$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(" ",getvalescaped("allfields",""))); # prepend 'all fields' option
        
}
    if (
getval("resourceids","")!="")
        {
        
$listsql="!list" join(":",trim_array(split_keywords(getvalescaped("resourceids",""))));
        
$search=$listsql " " $search;
        }

    
// Disabled as was killing search
    //$tmp = hook("richeditsearchquery", "", array($search, $fields, $n)); if($tmp) $search .= $tmp;
    
    
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=getvalescaped($name,"");
            if (
$value!="")
                {
                
$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=getvalescaped($name,"");
                if (
$value!=="")
                    {
                    
/*
                    $vs=split_keywords($value);
                    for ($m=0;$m<count($vs);$m++)
                        {
                        if ($search!="") {$search.=", ";}
                        $search.=$fields[$n]["name"] . ":" . strtolower($vs[$m]);
                        }
                    */
                    
if ($search!="") {$search.=", ";}
                    
$search.= ((strpos($value," ")===false)?$fields[$n]["name"] . ":" $value:"\"" $fields[$n]["name"] . ":" .substr($value,1,-1) . "\"");
                    }
                }
            else
                {
                
# Process checkbox list
                //$options=trim_array(explode(",",$fields[$n]["options"]));
                
$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=getvalescaped($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
                {
                
$key_year=$name."_year";
                
$value_year=getvalescaped($key_year,"");
                if (
$value_year!=""$value=$value_year;
                else 
$value="nnnn";
                
                
$key_month=$name."_month";
                
$value_month=getvalescaped($key_month,"");
                if (
$value_month==""$value_month.="nn";
                
                
$key_day=$name."_day";
                
$value_day=getvalescaped($key_day,"");
                if (
$value_day!=""$value.="|" $value_month "|" $value_day;
                elseif (
$value_month!="nn"$value.="|" $value_month;
                
                if ((
$value!=="nnnn|nn|nn")&&($value!=="nnnn")) 
                    {
                    if (
$search!="") {$search.=", ";}
                    
$search.=$fields[$n]["name"] . ":" $value;
                    }
                }

            if((
$date_edtf=getvalescaped("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 Need help? Talk to our expert team     Book a call