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

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 5.7 KB
Line 
1<?php
2
3/**
4 * Compatibility plugin for smarty templates, do not use otherwise, this is deprecated.
5 *
6 * This software is provided 'as-is', without any express or implied warranty.
7 * In no event will the authors be held liable for any damages arising from the use of this software.
8 *
9 * @author     Jordi Boggiano <j.boggiano@seld.be>
10 * @copyright  Copyright (c) 2008, Jordi Boggiano
11 * @license    http://dwoo.org/LICENSE   Modified BSD License
12 * @link       http://dwoo.org/
13 * @version    1.0.0
14 * @date       2008-10-23
15 * @package    Dwoo
16 */
17class Dwoo_Plugin_section extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
18{
19        public static $cnt=0;
20
21        public function init($name, $loop, $start = null, $step = null, $max = null, $show = true)
22        {
23        }
24
25        public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
26        {
27                return '';
28        }
29
30        public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
31        {
32                $output = Dwoo_Compiler::PHP_OPEN;
33                $params = $compiler->getCompiledParams($params);
34
35                // assigns params
36                $loop = $params['loop'];
37                $start = $params['start'];
38                $max = $params['max'];
39                $name = $params['name'];
40                $step = $params['step'];
41                $show = $params['show'];
42
43                // gets unique id
44                $cnt = self::$cnt++;
45
46                $output .= '$this->globals[\'section\']['.$name.'] = array();'."\n".
47                '$_section'.$cnt.' =& $this->globals[\'section\']['.$name.'];'."\n";
48
49                if ($loop !== 'null') {
50                        $output .= '$_section'.$cnt.'[\'loop\'] = is_array($tmp = '.$loop.') ? count($tmp) : max(0, (int) $tmp);'."\n";
51                } else {
52                        $output .= '$_section'.$cnt.'[\'loop\'] = 1;'."\n";
53                }
54
55                if ($show !== 'null') {
56                        $output .= '$_section'.$cnt.'[\'show\'] = '.$show.";\n";
57                } else {
58                        $output .= '$_section'.$cnt.'[\'show\'] = true;'."\n";
59                }
60
61                if ($name !== 'null') {
62                        $output .= '$_section'.$cnt.'[\'name\'] = '.$name.";\n";
63                } else {
64                        $output .= '$_section'.$cnt.'[\'name\'] = true;'."\n";
65                }
66
67                if ($max !== 'null') {
68                        $output .= '$_section'.$cnt.'[\'max\'] = (int)'.$max.";\n".
69                                                'if($_section'.$cnt.'[\'max\'] < 0) { $_section'.$cnt.'[\'max\'] = $_section'.$cnt.'[\'loop\']; }'."\n";
70                } else {
71                        $output .= '$_section'.$cnt.'[\'max\'] = $_section'.$cnt.'[\'loop\'];'."\n";
72                }
73
74                if ($step !== 'null') {
75                        $output .= '$_section'.$cnt.'[\'step\'] = (int)'.$step.' == 0 ? 1 : (int) '.$step.";\n";
76                } else {
77                        $output .= '$_section'.$cnt.'[\'step\'] = 1;'."\n";
78                }
79
80                if ($start !== 'null') {
81                        $output .= '$_section'.$cnt.'[\'start\'] = (int)'.$start.";\n";
82                } else {
83                        $output .= '$_section'.$cnt.'[\'start\'] = $_section'.$cnt.'[\'step\'] > 0 ? 0 : $_section'.$cnt.'[\'loop\'] - 1;'."\n".
84                                                'if ($_section'.$cnt.'[\'start\'] < 0) { $_section'.$cnt.'[\'start\'] = max($_section'.$cnt.'[\'step\'] > 0 ? 0 : -1, $_section'.$cnt.'[\'loop\'] + $_section'.$cnt.'[\'start\']); } '."\n".
85                                                'else { $_section'.$cnt.'[\'start\'] = min($_section'.$cnt.'[\'start\'], $_section'.$cnt.'[\'step\'] > 0 ? $_section'.$cnt.'[\'loop\'] : $_section'.$cnt.'[\'loop\'] -1); }'."\n";
86                }
87
88/*              if ($usesAny) {
89                        $output .= "\n".'$this->globals["section"]['.$name.'] = array'."\n(";
90                        if ($usesIndex) $output .="\n\t".'"index"               => 0,';
91                        if ($usesIteration) $output .="\n\t".'"iteration"               => 1,';
92                        if ($usesFirst) $output .="\n\t".'"first"               => null,';
93                        if ($usesLast) $output .="\n\t".'"last"         => null,';
94                        if ($usesShow) $output .="\n\t".'"show"         => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
95                        if ($usesTotal) $output .="\n\t".'"total"               => $this->isArray($_for'.$cnt.'_from) ? $this->count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
96                        $out.="\n);\n".'$_section'.$cnt.'[\'glob\'] =& $this->globals["section"]['.$name.'];'."\n\n";
97                }
98*/
99
100                $output .= 'if ($_section'.$cnt.'[\'show\']) {'."\n";
101                if ($start === 'null' && $step === 'null' && $max === 'null') {
102                        $output .= '    $_section'.$cnt.'[\'total\'] = $_section'.$cnt.'[\'loop\'];'."\n";
103                } else {
104                        $output .= '    $_section'.$cnt.'[\'total\'] = min(ceil(($_section'.$cnt.'[\'step\'] > 0 ? $_section'.$cnt.'[\'loop\'] - $_section'.$cnt.'[\'start\'] : $_section'.$cnt.'[\'start\'] + 1) / abs($_section'.$cnt.'[\'step\'])), $_section'.$cnt.'[\'max\']);'."\n";
105                }
106                $output .= '    if ($_section'.$cnt.'[\'total\'] == 0) {'."\n".
107                                   '            $_section'.$cnt.'[\'show\'] = false;'."\n".
108                                   '    }'."\n".
109                                   '} else {'."\n".
110                                   '    $_section'.$cnt.'[\'total\'] = 0;'."\n}\n";
111                $output .= 'if ($_section'.$cnt.'[\'show\']) {'."\n";
112                $output .= "\t".'for ($this->scope['.$name.'] = $_section'.$cnt.'[\'start\'], $_section'.$cnt.'[\'iteration\'] = 1; '.
113                                        '$_section'.$cnt.'[\'iteration\'] <= $_section'.$cnt.'[\'total\']; '.
114                                        '$this->scope['.$name.'] += $_section'.$cnt.'[\'step\'], $_section'.$cnt.'[\'iteration\']++) {'."\n";
115                $output .= "\t\t".'$_section'.$cnt.'[\'rownum\'] = $_section'.$cnt.'[\'iteration\'];'."\n";
116                $output .= "\t\t".'$_section'.$cnt.'[\'index_prev\'] = $this->scope['.$name.'] - $_section'.$cnt.'[\'step\'];'."\n";
117                $output .= "\t\t".'$_section'.$cnt.'[\'index_next\'] = $this->scope['.$name.'] + $_section'.$cnt.'[\'step\'];'."\n";
118                $output .= "\t\t".'$_section'.$cnt.'[\'first\']      = ($_section'.$cnt.'[\'iteration\'] == 1);'."\n";
119                $output .= "\t\t".'$_section'.$cnt.'[\'last\']       = ($_section'.$cnt.'[\'iteration\'] == $_section'.$cnt.'[\'total\']);'."\n";
120
121                $output .= Dwoo_Compiler::PHP_CLOSE . $content . Dwoo_Compiler::PHP_OPEN;
122
123                $output .= "\n\t}\n} " . Dwoo_Compiler::PHP_CLOSE;
124
125                if (isset($params['hasElse'])) {
126                        $output .= $params['hasElse'];
127                }
128
129                return $output;
130        }
131}
Note: See TracBrowser for help on using the repository browser.