]> git.mxchange.org Git - friendica.git/blob - library/Smarty/libs/plugins/function.counter.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git] / library / Smarty / libs / plugins / function.counter.php
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  */
24 function 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(
31             'start'     => 1,
32             'skip'      => 1,
33             'direction' => 'up',
34             'count'     => 1
35         );
36     }
37     $counter =& $counters[$name];
38
39     if (isset($params['start'])) {
40         $counter['start'] = $counter['count'] = (int) $params['start'];
41     }
42
43     if (!empty($params['assign'])) {
44         $counter['assign'] = $params['assign'];
45     }
46
47     if (isset($counter['assign'])) {
48         $template->assign($counter['assign'], $counter['count']);
49     }
50
51     if (isset($params['print'])) {
52         $print = (bool) $params['print'];
53     } else {
54         $print = empty($counter['assign']);
55     }
56
57     if ($print) {
58         $retval = $counter['count'];
59     } else {
60         $retval = null;
61     }
62
63     if (isset($params['skip'])) {
64         $counter['skip'] = $params['skip'];
65     }
66
67     if (isset($params['direction'])) {
68         $counter['direction'] = $params['direction'];
69     }
70
71     if ($counter['direction'] == "down") {
72         $counter['count'] -= $counter['skip'];
73     } else {
74         $counter['count'] += $counter['skip'];
75     }
76
77     return $retval;
78 }