source: branches/0.4/web/addons/job_monarch/dwoo/plugins/builtin/blocks/for.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 6.3 KB
Line 
1<?php
2
3/**
4 * Similar to the php for block
5 * <pre>
6 *  * name : for name to access it's iterator variables through {$.for.name.var} see {@link http://wiki.dwoo.org/index.php/IteratorVariables} for details
7 *  * from : array to iterate from (which equals 0) or a number as a start value
8 *  * to : value to stop iterating at (equals count($array) by default if you set an array in from)
9 *  * step : defines the incrementation of the pointer at each iteration
10 * </pre>
11 * This software is provided 'as-is', without any express or implied warranty.
12 * In no event will the authors be held liable for any damages arising from the use of this software.
13 *
14 * @author     Jordi Boggiano <j.boggiano@seld.be>
15 * @copyright  Copyright (c) 2008, Jordi Boggiano
16 * @license    http://dwoo.org/LICENSE   Modified BSD License
17 * @link       http://dwoo.org/
18 * @version    1.1.0
19 * @date       2009-07-18
20 * @package    Dwoo
21 */
22class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
23{
24        public static $cnt=0;
25
26        public function init($name, $from, $to=null, $step=1, $skip=0)
27        {
28        }
29
30        public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
31        {
32                // get block params and save the current template pointer to use it in the postProcessing method
33                $currentBlock =& $compiler->getCurrentBlock();
34                $currentBlock['params']['tplPointer'] = $compiler->getPointer();
35
36                return '';
37        }
38
39        public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
40        {
41                $params = $compiler->getCompiledParams($params);
42                $tpl = $compiler->getTemplateSource($params['tplPointer']);
43
44                // assigns params
45                $from = $params['from'];
46                $name = $params['name'];
47                $step = $params['step'];
48                $to = $params['to'];
49
50                // evaluates which global variables have to be computed
51                $varName = '$dwoo.for.'.trim($name, '"\'').'.';
52                $shortVarName = '$.for.'.trim($name, '"\'').'.';
53                $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
54                $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false;
55                $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false;
56                $usesIndex = strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false;
57                $usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false;
58                $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false;
59                $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false;
60
61                if (strpos($name, '$this->scope[') !== false) {
62                        $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true;
63                }
64
65                // gets foreach id
66                $cnt = self::$cnt++;
67
68                // builds pre processing output for
69                $out = Dwoo_Compiler::PHP_OPEN . "\n".'$_for'.$cnt.'_from = '.$from.';'.
70                                                                                "\n".'$_for'.$cnt.'_to = '.$to.';'.
71                                                                                "\n".'$_for'.$cnt.'_step = abs('.$step.');'.
72                                                                                "\n".'if (is_numeric($_for'.$cnt.'_from) && !is_numeric($_for'.$cnt.'_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }'.
73                                                                                "\n".'$tmp_shows = $this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && (abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0 || $_for'.$cnt.'_from == $_for'.$cnt.'_to));';
74                // adds for properties
75                if ($usesAny) {
76                        $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n(";
77                        if ($usesIndex) $out .="\n\t".'"index"          => 0,';
78                        if ($usesIteration) $out .="\n\t".'"iteration"          => 1,';
79                        if ($usesFirst) $out .="\n\t".'"first"          => null,';
80                        if ($usesLast) $out .="\n\t".'"last"            => null,';
81                        if ($usesShow) $out .="\n\t".'"show"            => $tmp_shows,';
82                        if ($usesTotal) $out .="\n\t".'"total"          => $this->isArray($_for'.$cnt.'_from) ? floor($this->count($_for'.$cnt.'_from) / $_for'.$cnt.'_step) : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
83                        $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
84                }
85                // checks if for must be looped
86                $out .= "\n".'if ($tmp_shows)'."\n{";
87                // set from/to to correct values if an array was given
88                $out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from'.(isset($params['hasElse']) ? ', true' : '').') == true) {
89                $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : $this->count($_for'.$cnt.'_from) - 1;
90                $_for'.$cnt.'_from = 0;
91        }';
92
93                // if input are pure numbers it shouldn't reorder them, if it's variables it gets too messy though so in that case a counter should be used
94                $reverse = false;
95                $condition = '<=';
96                $incrementer = '+';
97
98                if (preg_match('{^(["\']?)([0-9]+)\1$}', $from, $mN1) && preg_match('{^(["\']?)([0-9]+)\1$}', $to, $mN2)) {
99                        $from = (int) $mN1[2];
100                        $to = (int) $mN2[2];
101                        if ($from > $to) {
102                                $reverse = true;
103                                $condition = '>=';
104                                $incrementer = '-';
105                        }
106                }
107
108                // reverse from and to if needed
109                if (!$reverse) {
110                        $out .= "\n\t".'if ($_for'.$cnt.'_from > $_for'.$cnt.'_to) {
111                                $tmp = $_for'.$cnt.'_from;
112                                $_for'.$cnt.'_from = $_for'.$cnt.'_to;
113                                $_for'.$cnt.'_to = $tmp;
114                        }';
115                }
116
117                $out .= "\n\t".'for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] '.$condition.' $_for'.$cnt.'_to; $this->scope['.$name.'] '.$incrementer.'= $_for'.$cnt.'_step)'."\n\t{";
118                // updates properties
119                if ($usesIndex) {
120                        $out .="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
121                }
122                if ($usesFirst) {
123                        $out .= "\n\t\t".'$_for'.$cnt.'_glob["first"] = (string) ($_for'.$cnt.'_glob["iteration"] === 1);';
124                }
125                if ($usesLast) {
126                        $out .= "\n\t\t".'$_for'.$cnt.'_glob["last"] = (string) ($_for'.$cnt.'_glob["iteration"] === $_for'.$cnt.'_glob["total"]);';
127                }
128                $out .= "\n/* -- for start output */\n".Dwoo_Compiler::PHP_CLOSE;
129
130
131                // build post processing output and cache it
132                $postOut = Dwoo_Compiler::PHP_OPEN . '/* -- for end output */';
133                // update properties
134                if ($usesIteration) {
135                        $postOut .= "\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;';
136                }
137                // end loop
138                $postOut .= "\n\t}\n}\n".Dwoo_Compiler::PHP_CLOSE;
139
140                if (isset($params['hasElse'])) {
141                        $postOut .= $params['hasElse'];
142                }
143
144                return $out . $content . $postOut;
145        }
146}
Note: See TracBrowser for help on using the repository browser.