Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {counter} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     counter
Chris PeBenito 696b41
 * Purpose:  print out a counter value
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.counter.php {counter}
Chris PeBenito 696b41
 *       (Smarty online manual)
Chris PeBenito 696b41
 * @param array parameters
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string|null
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_counter($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    static $counters = array();
Chris PeBenito 696b41
Chris PeBenito 696b41
    $name = (isset($params['name'])) ? $params['name'] : 'default';
Chris PeBenito 696b41
    if (!isset($counters[$name])) {
Chris PeBenito 696b41
        $counters[$name] = array(
Chris PeBenito 696b41
            'start'=>1,
Chris PeBenito 696b41
            'skip'=>1,
Chris PeBenito 696b41
            'direction'=>'up',
Chris PeBenito 696b41
            'count'=>1
Chris PeBenito 696b41
            );
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $counter =& $counters[$name];
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (isset($params['start'])) {
Chris PeBenito 696b41
        $counter['start'] = $counter['count'] = (int)$params['start'];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (!empty($params['assign'])) {
Chris PeBenito 696b41
        $counter['assign'] = $params['assign'];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (isset($counter['assign'])) {
Chris PeBenito 696b41
        $smarty->assign($counter['assign'], $counter['count']);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    if (isset($params['print'])) {
Chris PeBenito 696b41
        $print = (bool)$params['print'];
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $print = empty($counter['assign']);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($print) {
Chris PeBenito 696b41
        $retval = $counter['count'];
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $retval = null;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (isset($params['skip'])) {
Chris PeBenito 696b41
        $counter['skip'] = $params['skip'];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    if (isset($params['direction'])) {
Chris PeBenito 696b41
        $counter['direction'] = $params['direction'];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($counter['direction'] == "down")
Chris PeBenito 696b41
        $counter['count'] -= $counter['skip'];
Chris PeBenito 696b41
    else
Chris PeBenito 696b41
        $counter['count'] += $counter['skip'];
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    return $retval;
Chris PeBenito 696b41
    
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>