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