Commit | Line | Data |
---|---|---|
e586807d H |
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 | ||
b42b2bf9 | 25 | $name = (isset($params['name'])) ? $params['name'] : 'default'; |
e586807d H |
26 | if (!isset($counters[$name])) { |
27 | $counters[$name] = array( | |
28 | 'start'=>1, | |
29 | 'skip'=>1, | |
30 | 'direction'=>'up', | |
31 | 'count'=>1 | |
32 | ); | |
33 | } | |
34 | $counter =& $counters[$name]; | |
35 | ||
b42b2bf9 H |
36 | if (isset($params['start'])) { |
37 | $counter['start'] = $counter['count'] = (int)$params['start']; | |
e586807d H |
38 | } |
39 | ||
b42b2bf9 H |
40 | if (!empty($params['assign'])) { |
41 | $counter['assign'] = $params['assign']; | |
e586807d H |
42 | } |
43 | ||
44 | if (isset($counter['assign'])) { | |
45 | $smarty->assign($counter['assign'], $counter['count']); | |
46 | } | |
47 | ||
b42b2bf9 H |
48 | if (isset($params['print'])) { |
49 | $print = (bool)$params['print']; | |
e586807d H |
50 | } else { |
51 | $print = empty($counter['assign']); | |
52 | } | |
53 | ||
54 | if ($print) { | |
55 | $retval = $counter['count']; | |
b42b2bf9 H |
56 | } else { |
57 | $retval = null; | |
58 | } | |
e586807d | 59 | |
b42b2bf9 H |
60 | if (isset($params['skip'])) { |
61 | $counter['skip'] = $params['skip']; | |
e586807d H |
62 | } |
63 | ||
b42b2bf9 H |
64 | if (isset($params['direction'])) { |
65 | $counter['direction'] = $params['direction']; | |
e586807d H |
66 | } |
67 | ||
68 | if ($counter['direction'] == "down") | |
69 | $counter['count'] -= $counter['skip']; | |
70 | else | |
71 | $counter['count'] += $counter['skip']; | |
72 | ||
b42b2bf9 | 73 | return $retval; |
e586807d H |
74 | |
75 | } | |
76 | ||
77 | /* vim: set expandtab: */ | |
78 | ||
79 | ?> |