1: <?php
2: /*************************************************************************************/
3: /* */
4: /* Thelia */
5: /* */
6: /* Copyright (c) OpenStudio */
7: /* email : info@thelia.net */
8: /* web : http://www.thelia.net */
9: /* */
10: /* This program is free software; you can redistribute it and/or modify */
11: /* it under the terms of the GNU General Public License as published by */
12: /* the Free Software Foundation; either version 3 of the License */
13: /* */
14: /* This program is distributed in the hope that it will be useful, */
15: /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16: /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17: /* GNU General Public License for more details. */
18: /* */
19: /* You should have received a copy of the GNU General Public License */
20: /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
21: /* */
22: /*************************************************************************************/
23:
24: namespace Thelia\Tpex\Element;
25:
26: use Thelia\Tpex\TpexToken;
27: use Thelia\Tpex\Exception\TokenSyntaxException;
28:
29: class ConditionalLoopElement extends BaseElement
30: {
31:
32: public $name;
33:
34: public $value;
35:
36: public function __construct($name)
37: {
38: $this->name = $name;
39: $this->content = array();
40: }
41:
42: public function addContent($content)
43: {
44: $this->content[] = $content;
45: }
46:
47: public function exec($substitutions = array())
48: {
49: //exec first ElementCollection.
50: $ifCondition = $this->content[0]->exec($substitutions);
51:
52: //Find affected loop or first loop if name doesn't match
53: $firstLoop = false;
54: $testLoop = false;
55: $countLoop = 0;
56:
57: foreach ($this->content[0]->getElements() as $element) {
58: if ($element->type() == TpexToken::TPEX_SIMPLE_LOOP) {
59: $countLoop++;
60:
61: if ($firstLoop === false) { $firstLoop = $element; }
62:
63: if ($element->name == $this->name) {
64: $testLoop = $element;
65:
66: break;
67: }
68: }
69: }
70:
71: $empty = true;
72:
73: if ($countLoop == 0) {
74: $empty = trim($ifCondition) != '';
75:
76: } elseif ($testLoop === false) {
77: if ($firstLoop === false) {
78: throw new TokenSyntaxException(sprintf("loop thelia_%s not found for conditional loop T_%s",
79: $this->name, $this->name));
80: } else {
81: $empty = $firstLoop->empty;
82: }
83: } else {
84: $empty = $testLoop->empty;
85: }
86:
87: if ($empty) {
88: return $this->content[1]->exec($substitutions);
89: } else {
90: return $ifCondition;
91: }
92: }
93:
94: public function type()
95: {
96: return TpexToken::TPEX_COND_LOOP;
97: }
98: }
99: