2012-07-04 07:23:08 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Smarty plugin
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
|
|
|
* @package Smarty
|
2012-07-04 07:23:08 +02: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-07 13:38:28 +02:00
|
|
|
* @link http://www.smarty.net/manual/en/language.function.counter.php {counter}
|
|
|
|
* (Smarty online manual)
|
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @param array $params parameters
|
|
|
|
* @param Smarty_Internal_Template $template template object
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
2012-07-04 07:23:08 +02: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-07 13:38:28 +02:00
|
|
|
'start' => 1,
|
|
|
|
'skip' => 1,
|
|
|
|
'direction' => 'up',
|
|
|
|
'count' => 1
|
|
|
|
);
|
2012-07-04 07:23:08 +02:00
|
|
|
}
|
|
|
|
$counter =& $counters[$name];
|
|
|
|
|
|
|
|
if (isset($params['start'])) {
|
2014-09-07 13:38:28 +02:00
|
|
|
$counter['start'] = $counter['count'] = (int) $params['start'];
|
2012-07-04 07:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($params['assign'])) {
|
|
|
|
$counter['assign'] = $params['assign'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($counter['assign'])) {
|
|
|
|
$template->assign($counter['assign'], $counter['count']);
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
if (isset($params['print'])) {
|
2014-09-07 13:38:28 +02:00
|
|
|
$print = (bool) $params['print'];
|
2012-07-04 07:23:08 +02:00
|
|
|
} else {
|
|
|
|
$print = empty($counter['assign']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($print) {
|
|
|
|
$retval = $counter['count'];
|
|
|
|
} else {
|
|
|
|
$retval = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params['skip'])) {
|
|
|
|
$counter['skip'] = $params['skip'];
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
if (isset($params['direction'])) {
|
|
|
|
$counter['direction'] = $params['direction'];
|
|
|
|
}
|
|
|
|
|
2014-09-07 13:38:28 +02:00
|
|
|
if ($counter['direction'] == "down") {
|
2012-07-04 07:23:08 +02:00
|
|
|
$counter['count'] -= $counter['skip'];
|
2014-09-07 13:38:28 +02:00
|
|
|
} else {
|
2012-07-04 07:23:08 +02:00
|
|
|
$counter['count'] += $counter['skip'];
|
2014-09-07 13:38:28 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return $retval;
|
|
|
|
}
|