set_search_order_by()

Description

Validate and set the order_by for the current search from the requested values passed to do_search()

Parameters

ColumnTypeDefaultDescription
$search string
$order_by string
$sort: string
$sort string

Return

string *

Location

include/search_functions.php lines 3379 to 3471

Definition

 
function set_search_order_by(string $searchstring $order_bystring $sort): string
{
    if (!
validate_sort_value($sort)) {
        
$sort 'asc';
    }
    
$order_by_date_sql_comma ",";
    
$order_by_date "r.ref $sort";
    if (
metadata_field_view_access($GLOBALS["date_field"])) {
        
$order_by_date_sql "field" . (int) $GLOBALS["date_field"] . " " $sort;
        
$order_by_date_sql_comma ", {$order_by_date_sql}, ";
        
$order_by_date "{$order_by_date_sql}, r.ref {$sort}";
    }

    
# Check if order_by is empty string as this avoids 'relevance' default
    
if ($order_by === "") {
        
$order_by "relevance";
    }

    
$order = [
        
"relevance"       => "score $sort, user_rating $sort, total_hit_count $sort {$order_by_date_sql_comma} r.ref $sort",
        
"popularity"      => "user_rating $sort, total_hit_count $sort {$order_by_date_sql_comma} r.ref $sort",
        
"rating"          => "r.rating $sort, user_rating $sort, score $sort, r.ref $sort",
        
"date"            => "$order_by_date, r.ref $sort",
        
"colour"          => "has_image $sort, image_blue $sort, image_green $sort, image_red $sort {$order_by_date_sql_comma} r.ref $sort",
        
"title"           => "field" . (int) $GLOBALS["view_title_field"] . " " $sort ", r.ref $sort",
        
"file_path"       => "file_path $sort, r.ref $sort",
        
"resourceid"      => "r.ref $sort",
        
"resourcetype"    => "order_by $sort, resource_type $sort, r.ref $sort",
        
"extension"       => "file_extension $sort, r.ref $sort",
        
"status"          => "archive $sort, r.ref $sort",
        
"modified"        => "modified $sort, r.ref $sort"
    
];

    
// Used for collection sort order as sortorder is ASC, date is DESC
    
$revsort = (strtoupper($sort) == 'DESC') ? "ASC" " DESC";

    
// These options are only supported if the default field 3 is still present
    
if (in_array(3get_resource_table_joins())) {
        
$order["country"] = "field3 $sort, r.ref $sort";
        
$order["titleandcountry"] = "field" $GLOBALS["view_title_field"] . $sort, field3 $sort, r.ref $sort";
    }

    
// Add collection sort option only if searching a collection
    
if (substr($search011) == '!collection') {
        
$order["collection"] = "c.sortorder $sort,c.date_added $revsort,r.ref $sort";
    }

    
# Check if date_field is being used as this will be needed in the inner select to be used in ordering
    
$GLOBALS["include_fieldx"] = false;
    if (isset(
$order_by_date_sql) && array_key_exists($order_by$order) && strpos($order[$order_by], $order_by_date_sql) !== false) {
        
$GLOBALS["include_fieldx"] = true;
    }

    
# Append order by field to the above array if absent and if named "fieldn" (where n is one or more digits)
    
if (!in_array($order_by$order) && (substr($order_by05) == "field")) {
        if (!
is_numeric(str_replace("field"""$order_by))) {
            exit(
"Order field incorrect.");
        }
        
# If fieldx is being used this will be needed in the inner select to be used in ordering
        
$GLOBALS["include_fieldx"] = true;
        
# Check for field type
        
$field_order_check ps_query(
            
"SELECT field_constraint, sort_method
            FROM resource_type_field
            WHERE ref = ?
            LIMIT 1"
,
            [
"i",str_replace("field"""$order_by)],
            
"",
            
"schema"
        
)[0];
        
# Establish sort order (numeric or otherwise)
        # Attach ref as a final key to foster stable result sets which should eliminate resequencing when moving <- and -> through resources (in view.php)
        
if ($field_order_check["sort_method"] == FIELD_SORT_METHODS['dot-notation']) {
            
$order[$order_by] =
                
"CASE WHEN TRIM($order_by) = '' THEN 0 ELSE 1 END $sort,
                ISNULL(REGEXP_SUBSTR(SUBSTRING_INDEX(
$order_by, '.', 1), '[^0-9]+')) $sort,
                IFNULL(REGEXP_SUBSTR(SUBSTRING_INDEX(
$order_by, '.', 1), '[^0-9]+'), '') $sort,
                CAST(REGEXP_SUBSTR(SUBSTRING_INDEX(
$order_by, '.', 1), '[0-9]+') AS UNSIGNED) $sort,";
            for (
$n 2$n <= 10$n++) {
                
$order[$order_by] .= "\nCAST(SUBSTRING_INDEX(SUBSTRING_INDEX($order_by, '.', $n), '.', -1) AS UNSIGNED) $sort,";
            }
            
$order[$order_by] .= "\nref $sort";
        } elseif (
$field_order_check["field_constraint"] == 1) {
            
$order[$order_by] = "$order_by +0 $sort,r.ref $sort";
        } else {
            
$order[$order_by] = "$order_by $sort,r.ref $sort";
        }
    }
    
hook("modifyorderarray");
    
$order_by = (isset($order[$order_by]) ? $order[$order_by] : (substr($search011) == '!collection' $order['collection'] : $order['relevance']));       // fail safe by falling back to default if not found

    
return $order_by;
}

This article was last updated 15th June 2025 10:05 Europe/London time based on the source file dated 4th June 2025 11:45 Europe/London time.