]> git.sur5r.net Git - contagged/blob - smarty/plugins/function.counter.php
initial checkin
[contagged] / smarty / plugins / function.counter.php
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  * @link http://smarty.php.net/manual/en/language.function.counter.php {counter}
16  *       (Smarty online manual)
17  * @param array parameters
18  * @param Smarty
19  * @return string|null
20  */
21 function smarty_function_counter($params, &$smarty)
22 {
23     static $counters = array();
24
25     extract($params);
26
27     if (!isset($name)) {
28                 if(isset($id)) {
29                         $name = $id;
30                 } else {                
31                 $name = "default";
32                 }
33         }
34
35     if (!isset($counters[$name])) {
36         $counters[$name] = array(
37             'start'=>1,
38             'skip'=>1,
39             'direction'=>'up',
40             'count'=>1
41             );
42     }
43     $counter =& $counters[$name];
44
45     if (isset($start)) {
46         $counter['start'] = $counter['count'] = $start;
47     }
48
49     if (!empty($assign)) {
50         $counter['assign'] = $assign;
51     }
52
53     if (isset($counter['assign'])) {
54         $smarty->assign($counter['assign'], $counter['count']);
55     }
56     
57     if (isset($print)) {
58         $print = (bool)$print;
59     } else {
60         $print = empty($counter['assign']);
61     }
62
63     if ($print) {
64         $retval = $counter['count'];
65         } else {
66                 $retval = null;
67         }
68
69     if (isset($skip)) {
70         $counter['skip'] = $skip;
71     }
72     
73     if (isset($direction)) {
74         $counter['direction'] = $direction;
75     }
76
77     if ($counter['direction'] == "down")
78         $counter['count'] -= $counter['skip'];
79     else
80         $counter['count'] += $counter['skip'];
81         
82         return $retval;
83         
84 }
85
86 /* vim: set expandtab: */
87
88 ?>