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_resource_tools_size_download_options()

Description

Render the resource tools for preview sizes

Parameters

ColumnTypeDefaultDescription
$resource array Resource data - {@see get_resource_data()}
$ctx: array
false
$resource['file_extension']
false;
bool, array{'download_multisize': "sizes"?: array, 'urlparams': array} $ctx Contextual information.

Location

include/render_functions.php lines 7334 to 7486

Definition

 
function render_resource_tools_size_download_options(array $resource, array $ctx): void
{
    
$ref $resource['ref'];
    
$download_multisize $ctx['download_multisize'];
    
$sizes $ctx['sizes'] ?? get_image_sizes($resource['ref'], false$resource['file_extension'], false);
    
$urlparams $ctx['urlparams'];
    
    
/** @var string Client side namespace to prevent clashes */
    
$ns "Resource{$ref}{$ctx['context']}";

    if (!((
$resource['has_image'] !== RESOURCE_PREVIEWS_NONE) && $download_multisize)) {
        return;
    }

    
$allowed_sizes = [];
    foreach (
$sizes as $size) {
        if (
$size['id'] === '') {
            continue;
        }

        
$downloadthissize resource_download_allowed($ref$size['id'], $resource['resource_type']);
        if (!
$downloadthissize && !checkperm('q')) {
            continue;
        }

        
// Fake $size key entry used in JS land
        
$size['html'] = [
            
// to display the preview size info to the user when selecting a size
            
'size_info' => get_size_info($size),
            
// to update the download button when selecting a size
            
'download_column' => cast_echo_to_string('add_download_column', [$ref$size$downloadthissize]),
        ];

        if (
$downloadthissize && $size['allow_preview'] == && !hook('previewlinkbar')) {
            
$GLOBALS['data_viewsize'] = $size['id']; # relied upon by some plugins (e.g lightbox)

            // Fake $size key entry used in JS land to update the View button before showing it to the user
            
$size['html']['view_btn'] = [
                
'viewsizeurl' => (string) hook('getpreviewurlforsize'),
                
'href' => generateURL(
                        
"{$GLOBALS['baseurl']}/pages/preview.php",
                        
$urlparams,
                        [
'ext' => $resource['file_extension']]
                    )
                    . 
'&' hook('previewextraurl'),
            ];
        }

        
$allowed_sizes[$size['id']] = $size;
    }

    if (
$allowed_sizes === []) {
        return;
    }

    
$use_selector count($allowed_sizes) > 1;
    
$render = function(bool $show_selector) use ($ns$allowed_sizes$resource): void {
        if (
$show_selector) {
            
?>
            <select id=" echo escape($ns); ?>size">
                
                
foreach ($allowed_sizes as $allowed_size) {
                    echo 
render_dropdown_option($allowed_size['id'], $allowed_size['name']);
                }
                
?>
            </select>
             hook('append_to_download_filename_td''', [$resource$ns]); ?>
            <div id=" echo escape($ns); ?>sizeInfo">
                
echo $allowed_sizes[array_key_first($allowed_sizes)]['html']['size_info'];
            
?></div>
            
            
return;
        }

        
// If only one size is available, render in the same way as the original file (ie not as a drop down selector).
        
$allowed_size $allowed_sizes[array_key_first($allowed_sizes)];
        
?>
        <h2> echo escape($allowed_size['name']); ?></h2>
         hook('append_to_download_filename_td''', [$resource$ns]); ?>
        <div id=" echo escape($ns); ?>sizeInfo"> echo $allowed_size['html']['size_info']; ?></div>
        
    
};
    
$render_js_picker = function(bool $show_selector) use ($ns$allowed_sizes) {
        if (
$show_selector) {
            
?>
            const picker = jQuery('select# echo escape($ns); ?>size');
            const selected_size = picker.val();
            updateSizeInfo(' echo escape($ns); ?>', selected_size);
            updatePreviewLink(' echo escape($ns); ?>', selected_size, picker);
            updateDownloadLink(' echo escape($ns); ?>', selected_size, picker);
            
            
return;
        }

        
// If only one size is available, there's no "size" to select from so ensure functions get called correctly to
        // the context.
        
$allowed_size $allowed_sizes[array_key_first($allowed_sizes)];
        
?>
        const picker = jQuery('.Picker # echo escape($ns); ?>sizeInfo');
        updatePreviewLink(' echo escape($ns); ?>', ' echo escape($allowed_size['id']); ?>', picker);
        updateDownloadLink(' echo escape($ns); ?>', ' echo escape($allowed_size['id']); ?>', picker);
        
    
};
    
?>
    <tr class="DownloadDBlend">
        <td class="DownloadFileName Picker"> $render($use_selector); ?></td>
        <td class="DownloadButton">
            <a id=" echo escape($ns); ?>downloadlink" onclick="return CentralSpaceLoad(this, true);">
                
echo escape($GLOBALS['lang']['action-download']);
            
?></a>
            <a
                id=" echo escape($ns); ?>previewlink"
                class="enterLink previewsizelink DisplayNone"
                href="#"
                data-viewsize=""
                data-viewsizeurl=""
            > echo escape($GLOBALS['lang']["action-view"]); ?></a>
        </td>
    </tr>
    <script>
    function  echo escape($ns); ?>_get_preview_size_info()
    {
        return 
            
echo json_encode(
                
array_map(
                    
get_sub_array_with(
                        [
                            
'allow_preview',
                            
'html',
                        ]
                    ),
                    
$allowed_sizes
                
),
                
JSON_NUMERIC_CHECK
            
);
        
?>;
    }

    jQuery(document).ready(function() {
         $render_js_picker($use_selector); ?>
    });

    jQuery('select# echo escape($ns); ?>size').change(function() {
        const picker = jQuery(this);
        const selected_size = picker.val();
        updateSizeInfo(' echo escape($ns); ?>', selected_size);
        updatePreviewLink(' echo escape($ns); ?>', selected_size, picker);
        updateDownloadLink(' echo escape($ns); ?>', selected_size, picker);
    });
     hook('append_to_resource_tools_size_download_options_script''', [$ns$allowed_sizes$resource]); ?>
    </script>

}

This article was last updated 11th December 2024 12:35 Europe/London time based on the source file dated 11th December 2024 10:05 Europe/London time.