1 |
cvsjoko |
1.1 |
<?php |
2 |
joko |
1.2 |
/** |
3 |
|
|
* Smarty plugin |
4 |
|
|
* @package Smarty |
5 |
|
|
* @subpackage plugins |
6 |
|
|
*/ |
7 |
|
|
|
8 |
cvsjoko |
1.1 |
|
9 |
joko |
1.2 |
/** |
10 |
|
|
* Smarty {counter} function plugin |
11 |
|
|
* |
12 |
|
|
* Type: function<br> |
13 |
|
|
* Name: counter<br> |
14 |
cvsjoko |
1.1 |
* Purpose: print out a counter value |
15 |
joko |
1.2 |
* @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 |
cvsjoko |
1.1 |
*/ |
21 |
|
|
function smarty_function_counter($params, &$smarty) |
22 |
|
|
{ |
23 |
joko |
1.2 |
static $counters = array(); |
24 |
|
|
|
25 |
|
|
$name = (isset($params['name'])) ? $params['name'] : 'default'; |
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 |
|
|
|
36 |
|
|
if (isset($params['start'])) { |
37 |
|
|
$counter['start'] = $counter['count'] = (int)$params['start']; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
if (!empty($params['assign'])) { |
41 |
|
|
$counter['assign'] = $params['assign']; |
42 |
|
|
} |
43 |
cvsjoko |
1.1 |
|
44 |
joko |
1.2 |
if (isset($counter['assign'])) { |
45 |
|
|
$smarty->assign($counter['assign'], $counter['count']); |
46 |
|
|
} |
47 |
cvsjoko |
1.1 |
|
48 |
joko |
1.2 |
if (isset($params['print'])) { |
49 |
|
|
$print = (bool)$params['print']; |
50 |
|
|
} else { |
51 |
|
|
$print = empty($counter['assign']); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
if ($print) { |
55 |
|
|
$retval = $counter['count']; |
56 |
|
|
} else { |
57 |
|
|
$retval = null; |
58 |
cvsjoko |
1.1 |
} |
59 |
|
|
|
60 |
joko |
1.2 |
if (isset($params['skip'])) { |
61 |
|
|
$counter['skip'] = $params['skip']; |
62 |
|
|
} |
63 |
cvsjoko |
1.1 |
|
64 |
joko |
1.2 |
if (isset($params['direction'])) { |
65 |
|
|
$counter['direction'] = $params['direction']; |
66 |
|
|
} |
67 |
cvsjoko |
1.1 |
|
68 |
joko |
1.2 |
if ($counter['direction'] == "down") |
69 |
|
|
$counter['count'] -= $counter['skip']; |
70 |
cvsjoko |
1.1 |
else |
71 |
joko |
1.2 |
$counter['count'] += $counter['skip']; |
72 |
|
|
|
73 |
|
|
return $retval; |
74 |
|
|
|
75 |
cvsjoko |
1.1 |
} |
76 |
|
|
|
77 |
|
|
/* vim: set expandtab: */ |
78 |
|
|
|
79 |
|
|
?> |