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

convert_search_form_date_to_search_query()

Description

Utility converter between sumitted date input and ResourceSpace search query

Parameters

ColumnTypeDefaultDescription
$field array
$ctx array []: string { if !in_array$field['type']
$GLOBALS['DATE_FIELD_TYPES']
int, array{ref: name: string, type: int} $field Field information relevant for the submitted data
string} array{html_field_name?: $ctx

Return

string The constructed search query string (based on the input/submitted data).

Location

include/search_functions.php lines 3260 to 3356

Definition

 
function convert_search_form_date_to_search_query(array $field, array $ctx = []): string
{
    if (!
in_array($field['type'], $GLOBALS['DATE_FIELD_TYPES'])) {
        return 
'';
    }

    
$search '';
    
$name $ctx['html_field_name'] ?? "field_{$field['ref']}";
    
$datepart '';

    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 .= $field["name"] . ":" $value_date_final;
        }
    }

    if ((
$date_edtf getval("field_" $field["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
            
return '';
        }

        
$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 .= $field["name"] . ":range" $datepart;
    }

    return 
$search;
}

This article was last updated 28th May 2026 17:35 Europe/London time based on the source file dated 28th May 2026 09:35 Europe/London time.