smarty/plugins/function.counter.php
changeset 0 c63de9eb7045
equal deleted inserted replaced
-1:000000000000 0:c63de9eb7045
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  * @package Smarty
       
     5  * @subpackage plugins
       
     6  */
       
     7 
       
     8 
       
     9 /**
       
    10  * Smarty {counter} function plugin
       
    11  *
       
    12  * Type:     function<br>
       
    13  * Name:     counter<br>
       
    14  * Purpose:  print out a counter value
       
    15  * @author Monte Ohrt <monte at ohrt dot com>
       
    16  * @link http://smarty.php.net/manual/en/language.function.counter.php {counter}
       
    17  *       (Smarty online manual)
       
    18  * @param array parameters
       
    19  * @param Smarty
       
    20  * @return string|null
       
    21  */
       
    22 function smarty_function_counter($params, &$smarty)
       
    23 {
       
    24     static $counters = array();
       
    25 
       
    26     $name = (isset($params['name'])) ? $params['name'] : 'default';
       
    27     if (!isset($counters[$name])) {
       
    28         $counters[$name] = array(
       
    29             'start'=>1,
       
    30             'skip'=>1,
       
    31             'direction'=>'up',
       
    32             'count'=>1
       
    33             );
       
    34     }
       
    35     $counter =& $counters[$name];
       
    36 
       
    37     if (isset($params['start'])) {
       
    38         $counter['start'] = $counter['count'] = (int)$params['start'];
       
    39     }
       
    40 
       
    41     if (!empty($params['assign'])) {
       
    42         $counter['assign'] = $params['assign'];
       
    43     }
       
    44 
       
    45     if (isset($counter['assign'])) {
       
    46         $smarty->assign($counter['assign'], $counter['count']);
       
    47     }
       
    48     
       
    49     if (isset($params['print'])) {
       
    50         $print = (bool)$params['print'];
       
    51     } else {
       
    52         $print = empty($counter['assign']);
       
    53     }
       
    54 
       
    55     if ($print) {
       
    56         $retval = $counter['count'];
       
    57     } else {
       
    58         $retval = null;
       
    59     }
       
    60 
       
    61     if (isset($params['skip'])) {
       
    62         $counter['skip'] = $params['skip'];
       
    63     }
       
    64     
       
    65     if (isset($params['direction'])) {
       
    66         $counter['direction'] = $params['direction'];
       
    67     }
       
    68 
       
    69     if ($counter['direction'] == "down")
       
    70         $counter['count'] -= $counter['skip'];
       
    71     else
       
    72         $counter['count'] += $counter['skip'];
       
    73     
       
    74     return $retval;
       
    75     
       
    76 }
       
    77 
       
    78 /* vim: set expandtab: */
       
    79 
       
    80 ?>