Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

render_sort_order()

Description

Renders sort order functionality as a dropdown box


if (!function_exists("render_sort_order")){

Parameters

ColumnTypeDefaultDescription
$order_fields array
$default_sort_order

Location

include/render_functions.php lines 706 to 798

Definition

 
function render_sort_order(array $order_fields,$default_sort_order)
    {
    global 
$order_by$baseurl_short$lang$search$archive$restypes$k$sort$date_field;

    
// use query strings here as this is used to render elements and sometimes it
    // can depend on other params
    
$modal  = ('true' == getval('modal'''));
    
$sort = (in_array(mb_strtoupper($sort), array("ASC""DESC")) ? mb_strtoupper($sort) : "DESC");
    
?>
    <select id="sort_order_selection" onChange="UpdateResultOrder();">
    
    
    $options 
'';
    foreach(
$order_fields as $name => $label)
        {
        
// date shows as 'field'.$date_field rather than 'date' for collection searches so let's fix it
        
if($name=='field'.$date_field)
            {
            
$name='date';
            }
        
        
// Are we constructing the option for the default order (ie. the first entry in the order_fields array)
        
$current_is_default = ($name == $default_sort_order);

        
// Is the currently set order that of the current field
        
$selected = ($order_by == $name || ($name=='date' && $order_by=='field'.$date_field));
        
        
// Build the option:
        
$option '<option value="' $name '"';

        
// Set selection attribute if necessary
        
if(($selected && $current_is_default) || $selected)
            {
            
$option .= ' selected';
            }

        
$option .= sprintf('
                data-url="%spages/search.php?search=%s&amp;order_by=%s&amp;archive=%s&amp;k=%s&amp;restypes=%s"
            '
,
            
$baseurl_short,
            
urlencode($search),
            
$name,
            
urlencode($archive),
            
urlencode($k),
            
urlencode($restypes)
        );

        
$option .= '>';
        
$option .= $label;
        
$option .= '</option>';

        
// Add option to the options list
        
$options .= $option;
        }

        
hook('render_sort_order_add_option''', array($options));
        echo 
$options;
    
?>
    </select>
    &nbsp;
    <a href="#" onClick="UpdateResultOrder(true);">
        <i id="sort_selection_toggle" class="fa fa-sort-amount- echo mb_strtolower($sort); ?>"></i>
    </a>

    <script>
    function UpdateResultOrder(toggle_order)
        {
        var selected_option      = jQuery('#sort_order_selection :selected');
        var option_url           = selected_option.data('url');
        
        if (toggle_order)
            {
            var selected_sort_option=' echo ($sort=='ASC'?'DESC':'ASC'); ?>';
            }
        else
            {
            if(selected_option='resourcetype')
                {
                // The default sort should be ascending when sorting by resource type
                var selected_sort_option=' echo ($sort=='ASC'?'DESC':'ASC'); ?>';
                }
            else
                {
                var selected_sort_option=' echo ($sort=='ASC'?'ASC':'DESC'); ?>';
                }
            }
        option_url += '&sort=' + selected_sort_option;
          echo $modal 'Modal' 'CentralSpace'?>Load(option_url);
        }
    </script>
    
    
return;
    }

This article was last updated 7th July 2020 10:35 Europe/London time based on the source file dated 3rd July 2020 14:29 Europe/London time.