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

set_search_order_by()

Description

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

Parameters

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

Return

string *

Location

include/search_functions.php lines 3382 to 3463

Definition

 
function set_search_order_by(string $search,string $order_bystring $sort): string
    
{
    if(
strtolower($sort)!=='desc') {
        
$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(3,get_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_by,0,5)=="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_value("SELECT field_constraint value FROM resource_type_field WHERE ref = ?",["i",str_replace("field","",$order_by)],"""schema");
        
# 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==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 27th April 2024 13:35 Europe/London time based on the source file dated 25th April 2024 17:25 Europe/London time.