Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / plugins / function.counter.php
1 <?php
2 /**
3  * Smarty plugin
4  * @package Smarty
5  * @subpackage PluginsFunction
6  */
7
8 /**
9  * Smarty {counter} function plugin
10  *
11  * Type:     function<br>
12  * Name:     counter<br>
13  * Purpose:  print out a counter value
14  * @author Monte Ohrt <monte at ohrt dot com>
15  * @link http://smarty.php.net/manual/en/language.function.counter.php {counter}
16  *       (Smarty online manual)
17  * @param array parameters
18  * @param Smarty
19  * @param object $template template object
20  * @return string|null
21  */
22 function smarty_function_counter($params, $template)
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         $template->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 ?>