/packages/Output/Smarty/Core/plugins/function.counter.php
PHP | 73 lines | 41 code | 11 blank | 21 comment | 13 complexity | 672316d5893326c4026f9b8980179c9b MD5 | raw file
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 */
24function smarty_function_counter($params, $template)
25{
26 static $counters = array();
27
28 $name = (isset($params[ 'name' ])) ? $params[ 'name' ] : 'default';
29 if (!isset($counters[ $name ])) {
30 $counters[ $name ] = array('start' => 1, 'skip' => 1, 'direction' => 'up', 'count' => 1);
31 }
32 $counter =& $counters[ $name ];
33
34 if (isset($params[ 'start' ])) {
35 $counter[ 'start' ] = $counter[ 'count' ] = (int) $params[ 'start' ];
36 }
37
38 if (!empty($params[ 'assign' ])) {
39 $counter[ 'assign' ] = $params[ 'assign' ];
40 }
41
42 if (isset($counter[ 'assign' ])) {
43 $template->assign($counter[ 'assign' ], $counter[ 'count' ]);
44 }
45
46 if (isset($params[ 'print' ])) {
47 $print = (bool) $params[ 'print' ];
48 } else {
49 $print = empty($counter[ 'assign' ]);
50 }
51
52 if ($print) {
53 $retval = $counter[ 'count' ];
54 } else {
55 $retval = null;
56 }
57
58 if (isset($params[ 'skip' ])) {
59 $counter[ 'skip' ] = $params[ 'skip' ];
60 }
61
62 if (isset($params[ 'direction' ])) {
63 $counter[ 'direction' ] = $params[ 'direction' ];
64 }
65
66 if ($counter[ 'direction' ] == "down") {
67 $counter[ 'count' ] -= $counter[ 'skip' ];
68 } else {
69 $counter[ 'count' ] += $counter[ 'skip' ];
70 }
71
72 return $retval;
73}