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

render_actions()

Description

Renders search actions functionality as a dropdown box

Parameters

ColumnTypeDefaultDescription
$collection_data array Collection data
$top_actions boolean true Set to true if actions are to be rendered in the search filter bar (above results)
$two_line boolean true Display on two lines
$id string '' Selector HTML ID
$resource_data array array Resource data
$optionsonly boolean false Render only options
$forpage string "" Specifically target for which page actions apply

Return

void

Location

include/render_functions.php lines 1077 to 1541

Definition

 
function render_actions(array $collection_data$top_actions true$two_line true$id '',$resource_data=array(),$optionsonly=false$forpage="")
    {
    if(
hook('prevent_running_render_actions'))
        {
        return;
        }

    global 
$baseurl$lang$k$pagename$order_by$sort;
    
    
// globals that could also be passed as a reference
    
global $result /*search result*/;

    
$action_selection_id = ($forpage!=""?$forpage:$pagename) . '_action_selection' $id;
    if(!
$top_actions)
        {
        
$action_selection_id .= '_bottom';
        }
    if(isset(
$collection_data['ref']))
        {
        
$action_selection_id .= '_' str_replace("-","_",$collection_data['ref']);
        }
    

    if(!
$optionsonly)
            {
?>
    
            <div class="ActionsContainer   if($top_actions) { echo 'InpageNavLeftBlock'; } ?>"
                data-actions-loaded="0"
            >
                
        
                
if($two_line)
                    {
                    
?>
                    <br />
                    
                    
}
                    
?>
                <select onchange="action_onchange_ echo escape($action_selection_id); ?>(this.value);"
                    id=" echo escape($action_selection_id); ?>"
                     if(!$top_actions) { ?>
                        class="SearchWidth"
                     } else { ?>
                        accesskey="A"
                     ?>
                    aria-label=" echo escape($lang["actions"]) ?>">
             ?>
            <option class="SelectAction" selected disabled hidden value=""> echo escape($lang["actions-select"])?></option>
            

            
// Collection Actions
            
$collection_actions_array compile_collection_actions($collection_data$top_actions$resource_data);
            
// Usual search actions
            
$search_actions_array compile_search_actions($top_actions);

            
// Remove certain actions that apply only to searches
            
if(!$top_actions)
                {
                
$action_index_to_remove array_search('search_items_disk_usage'array_column($search_actions_array'value'));
                unset(
$search_actions_array[$action_index_to_remove]);
                
$search_actions_array array_values($search_actions_array);
                
                
$action_index_to_remove array_search('save_search_items_to_collection'array_column($search_actions_array'value'));
                unset(
$search_actions_array[$action_index_to_remove]);
                
$search_actions_array array_values($search_actions_array);

                if(
$forpage === "themes")
                    {
                    
$action_index_to_remove array_search('remove_collection'array_column($collection_actions_array'value'));
                    unset(
$collection_actions_array[$action_index_to_remove]);
                    
$collection_actions_array array_values($collection_actions_array);
                    }
                }
    
            
/**
            * @var A global variable that allows other parts in ResourceSpace to append extra options to the actions 
            * unified dropdown (plugins can use existing hooks).
            */
            
$render_actions_extra_options = array();
            if(
                isset(
$GLOBALS["render_actions_extra_options"])
                && 
is_array($GLOBALS["render_actions_extra_options"])
                && !empty(
$GLOBALS["render_actions_extra_options"]))
                {
                
$render_actions_extra_options $GLOBALS["render_actions_extra_options"];
                }

            
$actions_array array_merge($collection_actions_array$search_actions_array$render_actions_extra_options);
            unset(
$render_actions_extra_options);

            
$modify_actions_array hook('modify_unified_dropdown_actions_options''', array($actions_array,$top_actions));

            if(!empty(
$modify_actions_array))
                {
                
$actions_array $modify_actions_array;
                }

            
/**
            * @var A global variable that allows other parts in ResourceSpace to filter actions options (plugins can use 
            * existing hooks).
            */
            
if(isset($GLOBALS["render_actions_filter"]) && is_callable($GLOBALS["render_actions_filter"]))
                {
                
$actions_array array_filter($actions_array$GLOBALS["render_actions_filter"]);
                unset(
$GLOBALS["render_actions_filter"]);
                }

            
// Sort array into category groups
            
usort($actions_array, function($a$b){
               if(isset(
$a['category']) && isset($b['category']))
                    {
                    if(
$a['category'] == $b['category'])
                        {
                        
// Same category, check for order_by. If no order_by add to end of category
                        
if(isset($a['order_by']) && (!isset($b['order_by']) || ($b['order_by'] > $a['order_by'])))
                            {
                            return -
1;
                            }
                        return 
1;
                        }
                    else
                        {
                        return  
$a['category'] - $b['category'];
                        }
                    }
                else
                    {
                    return isset(
$a['category']) ? -1;
                    }
                });
                                    
            
// loop and display
            
$options='';
            
$lastcategory 0;
            for(
$a 0$a count($actions_array); $a++)
                {
                
// Is this a new category?
                
if(!isset($actions_array[$a]['category']))
                    {
                    
$actions_array[$a]['category'] = 999;  
                    }
                if(
$lastcategory != $actions_array[$a]['category'])
                    {
                    if(
$a 0)
                        {
                        
$options .= "</optgroup>\n";
                        }
                    
$options .= "<optgroup label='" escape($lang["collection_actiontype_" $actions_array[$a]['category']]) . "'>\n";
                    }

                if(!isset(
$actions_array[$a]['data_attr']))
                    {
                    
$actions_array[$a]['data_attr'] = array();
                    }

                if(!isset(
$actions_array[$a]['extra_tag_attributes']))
                    {
                    
$actions_array[$a]['extra_tag_attributes'] = '';
                    }

                
$options .= render_dropdown_option($actions_array[$a]['value'], $actions_array[$a]['label'], $actions_array[$a]['data_attr'], $actions_array[$a]['extra_tag_attributes']);

                
$add_to_options hook('after_render_dropdown_option''', array($actions_array$a));
                if(
$add_to_options != '')
                    {
                    
$options .= $add_to_options;
                    }
                if(
$a == count($actions_array))
                    {
                    
$options .= "\n</optgroup>\n";
                    }
                
$lastcategory $actions_array[$a]['category'];
                }

            echo 
$options;
            
            if(!
$optionsonly)
                { 
?>
                </select>
                 ?>
        <script>
        function action_onchange_ echo $action_selection_id?>(v)
            {
            if(v == '')
                {
                return false;
                }

            v = v.match(/^[^~]*/i)[0]; // Remove unique value identifier: ~id

            switch(v)
                {
            
            
if(!== count($collection_data) && collection_readable($collection_data['ref']))
                {
                
?>
                case 'select_collection':
                    ChangeCollection( echo $collection_data['ref']; ?>, '');
                    break;

                case 'remove_collection':
                    if(confirm(" echo escape($lang['removecollectionareyousure']); ?>")) {
                        // most likely will need to be done the same way as delete_collection
                        var post_data = {
                            ajax: true,
                            dropdown_actions: true,
                            remove:  echo urlencode($collection_data['ref']); ?>,
                             echo generateAjaxToken("remove_collection"); ?>
                        };

                        jQuery.post(' echo $baseurl?>/pages/collection_manage.php', post_data, 'json')
                        .always(function(){
                            CollectionDivLoad(' echo $baseurl?>/pages/collections.php');
                        }); 
                        
                    }
                    break;

                case 'purge_collection':
                    if(confirm(' echo escape($lang["purgecollectionareyousure"]); ?>'))
                        {
                        document.getElementById('collectionpurge').value='".urlencode($collections[$n]["ref"])."';
                        document.getElementById('collectionform').submit();
                        }
                    break;

                case 'relate_all':
                    var collection =  echo urlencode($collection_data['ref']);?>;
                    jQuery.ajax({
                        type: 'POST',
                        url: baseurl_short + 'pages/ajax/relate_resources.php?collection=' + collection,
                        data: { echo generateAjaxToken("relate_resources"); ?>},
                        success: function(data) {
                            if (data.trim() == "SUCCESS") {
                                styledalert(' echo escape($lang["complete"])?>', ' echo escape($lang['relateallresources_confirmation'])?>');
                            }
                        },
                        error: function (err) {
                            console.log("AJAX error : " + JSON.stringify(err, null, 2));
                        }
                    }); 
                    break;

                case 'unrelate_all':
                    var collection =  echo urlencode($collection_data['ref']);?>;
                    jQuery.ajax({
                        type: 'POST',
                        url: baseurl_short + 'pages/ajax/unrelate_resources.php?collection=' + collection,
                        data: { echo generateAjaxToken("unrelate_resources"); ?>},
                        success: function(data) {
                            if (data.trim() == "SUCCESS") {
                                styledalert(' echo escape($lang["complete"])?>', ' echo escape($lang['unrelateallresources_confirmation'])?>');
                            }
                        },
                        error: function (err) {
                            console.log("AJAX error : " + JSON.stringify(err, null, 2));
                        }
                    }); 
                    break;
                
                
}

            if((!
$top_actions || !empty($collection_data)) &&  $collection_data['type'] != COLLECTION_TYPE_REQUEST)
                {
                global 
$search;
                
$search_collection='';
                if(
substr($search,0,11)=='!collection')
                    {
                    
$search_trimmed substr($search,11); // The collection search must always be the first part of the search string
                    
$search_elements split_keywords($search_trimmedfalsefalsefalsefalsetrue);
                    
$search_collection = (int)$search_elements[0];
                    }
                
?>
                case 'delete_collection':
                    if(confirm(' echo escape($lang["collectiondeleteconfirm"]); ?>')) {
                        var post_data = {
                            ajax: true,
                            dropdown_actions: true,
                            delete:  echo urlencode($collection_data['ref']); ?>,
                             echo generateAjaxToken("delete_collection"); ?>
                        };

                        jQuery.post(' echo $baseurl?>/pages/collection_manage.php', post_data, function(response) {
                            if(response.success === 'Yes')
                                {
                                CollectionDivLoad(' echo $baseurl?>/pages/collections.php?collection=' + response.redirect_to_collection + '&k=' + response.k + '&nc=' + response.nc);

                                if(basename(document.URL).substr(0, 17) === 'collection_manage')
                                    {
                                    CentralSpaceLoad(document.URL);
                                    }
                                else if(basename(document.URL).substr(0, 6) === 'search' && ' echo $search_collection?>'==' echo $collection_data["ref"]; ?>')
                                    {
                                    CentralSpaceLoad(' echo $baseurl?>/pages/search.php?search=!collection' + response.redirect_to_collection, true);
                                    }
                                else if(basename(document.URL).substr(0, 20) === 'collections_featured'){
                                    CentralSpaceLoad(document.URL);
                                    }
                                }
                        }, 'json');    
                    }
                    break;
                
                
}

            
// Add extra collection actions javascript case through plugins
            // Note: if you are just going to a different page, it should be easily picked by the default case
            
$extra_options_js_case hook('render_actions_add_option_js_case''', array($action_selection_id));
            if(
trim($extra_options_js_case) !== '')
                {
                echo 
$extra_options_js_case;
                }
            
?>

                case 'save_search_to_collection':
                    var option_url = jQuery('# echo $action_selection_id?> option:selected').data('url');
                    CollectionDivLoad(option_url);
                    break;

                case 'save_search_to_dash':
                    var option_url  = jQuery('# echo $action_selection_id?> option:selected').data('url');
                    var option_link = jQuery('# echo $action_selection_id?> option:selected').data('link');
                    
                    // Dash requires to have some search parameters (even if they are the default ones)
                    if((basename(option_link).substr(0, 10)) != 'search.php')
                        {
                        option_link = (window.location.href).replace(window.baseurl, '');
                        }

                    option_url    += '&link=' + option_link;

                    CentralSpaceLoad(option_url);
                    break;

                case 'save_search_smart_collection':
                    var option_url = jQuery('# echo $action_selection_id?> option:selected').data('url');
                    CollectionDivLoad(option_url);
                    break;

                case 'save_search_items_to_collection':
                    var option_url = jQuery('# echo $action_selection_id?> option:selected').data('url');
                    CollectionDivLoad(option_url);
                    break;

                case 'empty_collection':
                    if(!confirm(' echo escape($lang["emptycollectionareyousure"]); ?>'))
                        {
                        break;
                        }

                    var option_url = jQuery('# echo $action_selection_id?> option:selected').data('url');
                    CollectionDivLoad(option_url);
                    break;

                case 'copy_collection':
                    var option_url = jQuery('# echo $action_selection_id?> option:selected').data('url');
                    ModalLoad(option_url, false, true);
                    break;

            
            
if(!$top_actions)
                {
                global 
$delete_requires_password;
                
?>
                case 'delete_all_in_collection':
                    if(confirm(' echo escape($lang["deleteallsure"]); ?>'))
                        {
                        
if ($delete_requires_password)
                            {
                            
$delete_all_url_params = [
                                
"ref"       => $collection_data["ref"],
                                
"name"      => $collection_data["name"],
                                
"public"    => ($collection_data["type"] == COLLECTION_TYPE_PUBLIC 0),
                                
"deleteall" => 'on'
                            
];
                            
$delete_all_url generateURL("{$baseurl}/pages/collection_edit.php",$delete_all_url_params);
                            echo 
"ModalLoad('$delete_all_url');";
                            }
                        else
                            {
                            
?>
                            var post_data = {
                                submitted: true,
                                ref: ' echo $collection_data["ref"]; ?>',
                                name:  echo json_encode($collection_data["name"]); ?>,
                                public: ' echo $collection_data["type"] == COLLECTION_TYPE_PUBLIC 0?>',
                                deleteall: 'on',
                                 echo generateAjaxToken("delete_all_in_collection"); ?>
                            };

                            jQuery.post(' echo $baseurl?>/pages/collection_edit.php?ajax=true', post_data, function()
                                {
                                CollectionDivLoad(' echo $baseurl?>/pages/collections.php?collection= echo $collection_data["ref"]; ?>');
                                });
                            
                            
}?>
                        }
                    break;

                    case 'hide_collection':
                        var action = 'hidecollection';
                        var collection =  echo urlencode($collection_data['ref']);?>;
                        var mycol = jQuery('# echo $action_selection_id?> option:selected').data('mycol');

                        jQuery.ajax({
                            type: 'POST',
                            url: baseurl_short + 'pages/ajax/showhide_collection.php?action=' + action + '&collection=' + collection,
                            data: { echo generateAjaxToken("hide_collection"); ?>},
                            success: function(data) {
                                if (data.trim() == "HIDDEN") {
                                    CollectionDivLoad(' echo $baseurl?>/pages/collections.php?collection='+mycol);
                                }
                            },
                            error: function (err) {
                                console.log("AJAX error : " + JSON.stringify(err, null, 2));
                            }
                        }); 
                        break;

                
                
}
                
?>

                default:
                    var option_url = jQuery('# echo $action_selection_id?> option:selected').data('url');
                    var option_callback = jQuery('# echo $action_selection_id?> option:selected').data('callback');
                    var option_no_ajax = jQuery('# echo $action_selection_id?> option:selected').data('no-ajax');

                    // If action option has a defined data-callback attribute, then we can call it
                    // IMPORTANT: never allow callback data attribute to be input/saved by user. Only ResourceSpace should
                    // generate the callbacks - key point is "generate"
                    if(typeof option_callback !== "undefined")
                        {
                        eval(option_callback);
                        }

                    // If action option has a defined data-url attribute, then we can CentralSpaceLoad it
                    if(typeof option_url !== "undefined")
                        {
                        if (typeof option_no_ajax == "undefined")
                            {
                            CentralSpaceLoad(option_url, true);
                            }
                        else
                            {
                            window.location.href = option_url;
                            }
                        }
    
                    break;
                }
                
                // Go back to no action option
                jQuery('# echo $action_selection_id?> option[value=""]').prop('selected', true);
                

        }
        </script>
        
     if (!$optionsonly)
        {
?>
        </div>
        
        
}
    }

This article was last updated 4th December 2024 08:35 Europe/London time based on the source file dated 3rd December 2024 11:55 Europe/London time.