container.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\abstracts;
11 
12 use depage\htmlform\elements;
13 use depage\htmlform\exceptions;
14 
21 abstract class container extends element
22 {
26  protected $elements = array();
30  protected $elementsAndHtml = array();
34  protected $form;
35 
44  public function __construct($name, $parameters, $form)
45  {
46  $this->form = $form;
47 
48  parent::__construct($name, $parameters, $form);
49  }
50 
67  public function __call($function, $arguments)
68  {
69  if (substr($function, 0, 3) === 'add') {
70  $type = strtolower(str_replace('add', '\\depage\\htmlform\\elements\\', $function));
71  $name = isset($arguments[0]) ? $arguments[0] : '';
72  $parameters = isset($arguments[1]) ? $arguments[1] : array();
73 
74  return $this->addElement($type, $name, $parameters);
75  } else {
76  return parent::__call($function, $arguments);
77  }
78  }
79 
93  protected function addElement($type, $name, $parameters)
94  {
95  $this->checkElementType($type);
96  $this->checkParameters($parameters);
97 
98  $parameters['log'] = $this->log;
99 
100  $newElement = new $type($name, $parameters, $this->form);
101 
102  $this->elements[] = $newElement;
103  $this->elementsAndHtml[] = $newElement;
104 
105  if ($newElement instanceof container) {
106  $newElement->addChildElements();
107  }
108 
109  return $newElement;
110  }
111 
125  public function addChildElements() {}
126 
135  public function addHtml($html)
136  {
137  $htmlElement = new elements\html($html);
138 
139  $this->elementsAndHtml[] = $htmlElement;
140 
141  return $htmlElement;
142  }
143 
149  public function addStepNav($parameter = array())
150  {
151  $htmlElement = new elements\stepnav($parameter, $this->form);
152 
153  $this->elementsAndHtml[] = $htmlElement;
154 
155  return $htmlElement;
156  }
157 
166  public function validate()
167  {
168  if (!$this->validated) {
169  $this->validated = true;
170 
171  $this->valid = true;
172  foreach ($this->elements as $element) {
173  $element->validate();
174  $this->valid = (($this->valid) && ($element->validate()));
175  }
176  }
177 
178  return $this->valid;
179  }
180 
189  public function setRequired($required = true)
190  {
191  $required = (bool) $required;
192 
193  foreach ($this->elements as $element) {
194  $element->setRequired($required);
195  }
196  }
197 
206  private function checkElementType($type)
207  {
208  if (!class_exists($type)) {
210  }
211  }
212 
222  public function getElements($includeFieldsets = false)
223  {
224  $allElements = array();
225  foreach ($this->elements as $element) {
226  if ($element instanceof elements\fieldset) {
227  $allElements = array_merge($allElements, $element->getElements());
228  if ($includeFieldsets) {
229  $allElements[] = $element;
230  }
231  } else {
232  $allElements[] = $element;
233  }
234  }
235 
236  return $allElements;
237  }
238 
248  public function getElement($name)
249  {
250  foreach ($this->getElements() as $element) {
251  if ($name === $element->getName()) {
252  return $element;
253  }
254  }
255 
256  return false;
257  }
258 
264  public function clearValue()
265  {
266  foreach ($this->getElements(true) as $element) {
267  $element->clearValue();
268  }
269  }
270 }
271 
272 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
getElements($includeFieldsets=false)
Returns containers subelements.
Definition: container.php:222
thrown when attemting to instantiate an inexistent element class
addHtml($html)
Adds a new custom HTML element to the container.
Definition: container.php:135
clearValue()
Deletes values of all child elements.
Definition: container.php:264
$form
Parent form object reference.
Definition: container.php:34
$elementsAndHtml
Input element, fieldset and custom HTML object references.
Definition: container.php:30
$log
Log object reference.
Definition: element.php:37
addStepNav($parameter=array())
Adds automatic step navigation to output.
Definition: container.php:149
validate()
Validates container and its contents.
Definition: container.php:166
addElement($type, $name, $parameters)
Generates sub-elements.
Definition: container.php:93
Can be used to insert a step navigation.
Definition: stepnav.php:17
The fieldset class holds HTML-fieldset specific attributes and methods.
Definition: fieldset.php:50
container element base class
Definition: container.php:21
$valid
Contains element validation status/result.
Definition: element.php:29
behold: the ├╝ber-class
Definition: element.php:20
$elements
References to input elements and fieldsets.
Definition: container.php:26
setRequired($required=true)
Sets required-attribute.
Definition: container.php:189
Can be used to insert custom HTML between rendered HTML elements.
Definition: html.php:34
getElement($name)
Gets subelement by name.
Definition: container.php:248
__call($function, $arguments)
HTML escaping and add subelements.
Definition: container.php:67
addChildElements()
Sub-element generator hook.
Definition: container.php:125
checkParameters($parameters)
checks element parameters
Definition: element.php:131
__construct($name, $parameters, $form)
container class constructor
Definition: container.php:44