Container.php
Go to the documentation of this file.
1<?php
11
14
21abstract class Container extends Element
22{
26 protected $elements = array();
30 protected $elementsAndHtml = array();
34 protected $form;
43 public function __construct($name, $parameters, $form)
44 {
45 $this->form = $form;
46
47 parent::__construct($name, $parameters, $form);
48 }
49
66 public function __call($function, $arguments)
67 {
68 if (substr($function, 0, 3) === 'add') {
69 $type = str_replace('add', '', $function);
70
71 foreach($this->form->getNamespaces() as $namespace) {
72 $class = $namespace . '\\' . $type;
73 if (class_exists($class)) {
74 $name = isset($arguments[0]) ? $arguments[0] : '';
75 $parameters = isset($arguments[1]) ? $arguments[1] : array();
76 return $this->addElement($class, $name, $parameters);
77 }
78 }
79
81 } else {
82 return parent::__call($function, $arguments);
83 }
84 }
85
99 protected function addElement($type, $name, $parameters)
100 {
101 $this->checkParameters($parameters);
102
103 $parameters['log'] = $this->log;
104
105 $newElement = new $type($name, $parameters, $this->form);
106
107 $this->elements[] = $newElement;
108 $this->elementsAndHtml[] = $newElement;
109
110 if ($newElement instanceof container) {
111 $newElement->addChildElements();
112 }
113
114 return $newElement;
115 }
129 public function addChildElements() {}
139 public function getElements($includeFieldsets = false)
140 {
141 $allElements = array();
142 foreach ($this->elements as $element) {
143 if ($element instanceof Container) {
144 if ($includeFieldsets) {
145 $allElements[] = $element;
146 }
147 $allElements = array_merge($allElements, $element->getElements($includeFieldsets));
148 } else {
149 $allElements[] = $element;
150 }
151 }
152
153 return $allElements;
154 }
164 public function getElement($name, $includeFieldsets = false)
165 {
166 foreach ($this->getElements($includeFieldsets) as $element) {
167 if ($name === $element->getName()) {
168 return $element;
169 }
170 }
171
172 return false;
173 }
174
183 public function addHtml($html)
184 {
185 $htmlElement = new Elements\Html($html);
186
187 $this->elementsAndHtml[] = $htmlElement;
188
189 return $htmlElement;
190 }
196 public function addStepNav($parameter = array())
197 {
198 $htmlElement = new Elements\Stepnav($parameter, $this->form);
199
200 $this->elementsAndHtml[] = $htmlElement;
201
202 return $htmlElement;
203 }
204
213 public function setRequired($required = true)
214 {
215 $required = (bool) $required;
216
217 foreach ($this->elements as $element) {
218 $element->setRequired($required);
219 }
220 }
229 public function validate()
230 {
231 if (!$this->validated) {
232 $this->validated = true;
233
234 $this->valid = true;
235 foreach ($this->elements as $element) {
236 $valid = $element->validate();
237 $this->valid = $this->valid && $valid;
238 }
239 }
240
241 return $this->valid;
242 }
248 public function clearValue()
249 {
250 foreach ($this->getElements(true) as $element) {
251 $element->clearValue();
252 }
253 }
254}
255
256/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
container element base class
Definition Container.php:22
getElements($includeFieldsets=false)
Returns containers subelements.
addElement($type, $name, $parameters)
Generates sub-elements.
Definition Container.php:99
addChildElements()
Sub-element generator hook.
setRequired($required=true)
Sets required-attribute.
validate()
Validates container and its contents.
$form
Parent form object reference.
Definition Container.php:34
__construct($name, $parameters, $form)
container class constructor
Definition Container.php:43
clearValue()
Deletes values of all child elements.
addStepNav($parameter=array())
Adds automatic step navigation to output.
addHtml($html)
Adds a new custom HTML element to the container.
__call($function, $arguments)
HTML escaping and add subelements.
Definition Container.php:66
getElement($name, $includeFieldsets=false)
Gets subelement by name.
$elementsAndHtml
Input element, fieldset and custom HTML object references.
Definition Container.php:30
$elements
References to input elements and fieldsets.
Definition Container.php:26
behold: the über-class
Definition Element.php:21
$valid
Contains element validation status/result.
Definition Element.php:30
$class
CSS class of the container element.
Definition Element.php:79
$log
Log object reference.
Definition Element.php:40
checkParameters($parameters)
checks element parameters
Definition Element.php:205
Can be used to insert custom HTML between rendered HTML elements.
Definition Html.php:35
Can be used to insert a step navigation.
Definition Stepnav.php:18
thrown when attemting to instantiate an inexistent element class
Abstract element classes.
Definition Container.php:10
Classes for HTML input-elements.
Definition Address.php:9