eesyphp/includes/templates.php

104 lines
3.3 KiB
PHP
Raw Normal View History

<?php
use EesyPHP\Config;
use EesyPHP\Date;
use EesyPHP\Hook;
use EesyPHP\Log;
use EesyPHP\Tpl;
use function EesyPHP\format_size;
if (php_sapi_name() == "cli")
return true;
// Enable Smarty security
Tpl :: enable_security_mode(
// Allow some functions in IF clauses
array(
'isset', 'empty', 'count', 'in_array', 'is_array', 'array_key_exists', 'is_null',
'can_modify', 'can_archive', 'can_delete'
),
// Allow some modifier functions
array('range', 'implode', 'stripslashes')
);
// Defined some global template variables
Tpl :: assign('public_root_url', Config::get('public_root_url', '/', 'string'));
Tpl :: assign('main_pagetitle', Config::get('main_pagetitle', null, 'string'));
Tpl :: assign('session_key', $_SESSION['session_key']);
// Handle CSS & JS files included
Tpl :: add_css_file(Config::get('included_css_files', array(), 'array'));
Tpl :: add_js_file(Config::get('included_js_files', array(), 'array'));
function define_common_template_variables($event) {
global $status_list, $admin;
Tpl :: assign(
'status_list',
isset($status_list) && is_array($status_list)?
$status_list:array()
);
Tpl :: assign('admin', isset($admin) && $admin);
Tpl :: assign(
'webstats_js_code',
Config::get('webstats_js_code', null, 'string'));
}
Hook :: register('before_displaying_template', 'define_common_template_variables');
// Templates functions
function smarty_item_status($params) {
global $status_list;
$status2class = array (
'pending' => 'info',
'validated' => 'success',
'refused' => 'danger',
'archived' => 'secondary',
);
if (array_key_exists($params['item']['status'], $status2class)) {
$class = $status2class[$params['item']['status']];
}
else
$class='danger';
echo "<span class='badge badge-$class'>";
echo (
array_key_exists($params['item']['status'], $status_list)?
$status_list[$params['item']['status']]:
"Inconnu (".$params['item']['status'].")"
);
echo "</span>";
}
Tpl :: register_function('item_status','smarty_item_status');
function smarty_format_time($params) {
echo Date :: format($params['time'], (!isset($params['with_time']) || (bool)$params['with_time']));
}
Tpl :: register_function('format_time','smarty_format_time');
function smarty_format_size($params, $smarty) {
if(!isset($params['digit'])) $params['digit'] = 2;
echo format_size($params['size'],$params['digit']);
}
Tpl :: register_function('format_size','smarty_format_size');
function smarty_table_ordered_th($params, $smarty) {
if ($params['order'] && $params['url'] && $params['text'] && is_array($params['search'])) {
$params['url'] .= (strpos($params['url'], '?') === false?'?':'&')."order=".$params['order'];
echo "<a href='".$params['url']."'>".$params['text']."</a>";
}
if ($params['order']==$params['search']['order']) {
echo (
' <i class="fa fa-sort-'.
(strtolower($params['search']['order_direction'])=='asc'?'up':'down').
'" aria-hidden="true"></i>');
}
}
Tpl :: register_function('table_ordered_th','smarty_table_ordered_th');
function smarty_encodeJsonBase64($params, $smarty) {
if (isset($params['data']))
echo base64_encode(json_encode($params['data']));
}
Tpl :: register_function('encodeJsonBase64','smarty_encodeJsonBase64');
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab