khanat-code-old/code/web/private_php/ams/smarty/libs/plugins/function.counter.php

79 lines
1.8 KiB
PHP
Raw Normal View History

2014-09-08 06:57:31 +00:00
<?php
/**
* Smarty plugin
2014-09-08 09:07:42 +00:00
*
* @package Smarty
2014-09-08 06:57:31 +00:00
* @subpackage PluginsFunction
*/
/**
* Smarty {counter} function plugin
* Type: function<br>
* Name: counter<br>
* Purpose: print out a counter value
*
* @author Monte Ohrt <monte at ohrt dot com>
2014-09-08 09:07:42 +00:00
* @link http://www.smarty.net/manual/en/language.function.counter.php {counter}
* (Smarty online manual)
*
2014-09-08 06:57:31 +00:00
* @param array $params parameters
* @param Smarty_Internal_Template $template template object
2014-09-08 09:07:42 +00:00
*
2014-09-08 06:57:31 +00:00
* @return string|null
*/
function smarty_function_counter($params, $template)
{
static $counters = array();
$name = (isset($params['name'])) ? $params['name'] : 'default';
if (!isset($counters[$name])) {
$counters[$name] = array(
2014-09-08 09:07:42 +00:00
'start' => 1,
'skip' => 1,
'direction' => 'up',
'count' => 1
);
2014-09-08 06:57:31 +00:00
}
$counter =& $counters[$name];
if (isset($params['start'])) {
2014-09-08 09:07:42 +00:00
$counter['start'] = $counter['count'] = (int) $params['start'];
2014-09-08 06:57:31 +00:00
}
if (!empty($params['assign'])) {
$counter['assign'] = $params['assign'];
}
if (isset($counter['assign'])) {
$template->assign($counter['assign'], $counter['count']);
}
2014-09-08 09:07:42 +00:00
2014-09-08 06:57:31 +00:00
if (isset($params['print'])) {
2014-09-08 09:07:42 +00:00
$print = (bool) $params['print'];
2014-09-08 06:57:31 +00:00
} else {
$print = empty($counter['assign']);
}
if ($print) {
$retval = $counter['count'];
} else {
$retval = null;
}
if (isset($params['skip'])) {
$counter['skip'] = $params['skip'];
}
2014-09-08 09:07:42 +00:00
2014-09-08 06:57:31 +00:00
if (isset($params['direction'])) {
$counter['direction'] = $params['direction'];
}
2014-09-08 09:07:42 +00:00
if ($counter['direction'] == "down") {
2014-09-08 06:57:31 +00:00
$counter['count'] -= $counter['skip'];
2014-09-08 09:07:42 +00:00
} else {
2014-09-08 06:57:31 +00:00
$counter['count'] += $counter['skip'];
2014-09-08 09:07:42 +00:00
}
2014-09-08 06:57:31 +00:00
return $retval;
}