/** * Constructor * * Instantiate the form element object * * @param string $type * @param string $name * @param string $value * @param string|array $marked * @param string $indent * @throws Exception * @return \Pop\Form\Element */ public function __construct($type, $name, $value = null, $marked = null, $indent = null) { $this->name = $name; $this->type = $type; // Check the element type, else set the properties. if (!in_array($type, $this->allowedTypes)) { throw new Exception('Error: That input type is not allowed.'); } // Create the element based on the type passed. switch ($type) { // Textarea element case 'textarea': parent::__construct('textarea', null, null, false, $indent); $this->setAttributes(array('name' => $name, 'id' => $name)); $this->nodeValue = $value; $this->value = $value; break; // Select element // Select element case 'select': parent::__construct('select', null, null, false, $indent); $this->setAttributes(array('name' => $name, 'id' => $name)); // Create the child option elements. foreach ($value as $k => $v) { if (is_array($v)) { $opt = new Child('optgroup', null, null, false, $indent); $opt->setAttributes('label', $k); foreach ($v as $ky => $vl) { $o = new Child('option', null, null, false, $indent); $o->setAttributes('value', $ky); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($ky, $this->marked)) { $o->setAttributes('selected', 'selected'); } } else { if ($ky == $this->marked) { $o->setAttributes('selected', 'selected'); } } $o->setNodeValue($vl); $opt->addChild($o); } } else { $opt = new Child('option', null, null, false, $indent); $opt->setAttributes('value', $k); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($k, $this->marked)) { $opt->setAttributes('selected', 'selected'); } } else { if ($k == $this->marked) { $opt->setAttributes('selected', 'selected'); } } $opt->setNodeValue($v); } $this->addChild($opt); } $this->value = $value; break; // Radio element(s) // Radio element(s) case 'radio': parent::__construct('fieldset', null, null, false, $indent); $this->setAttributes('class', 'radio-btn-fieldset'); // Create the radio elements and related span elements. $i = null; foreach ($value as $k => $v) { $rad = new Child('input', null, null, false, $indent); $rad->setAttributes(array('type' => $type, 'class' => 'radio-btn', 'name' => $name, 'id' => $name . $i, 'value' => $k)); // Determine if the current radio element is checked. if ($k == $this->marked) { $rad->setAttributes('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttributes('class', 'radio-span'); $span->setNodeValue($v); $this->addChildren(array($rad, $span)); $i++; } $this->value = $value; break; // Checkbox element(s) // Checkbox element(s) case 'checkbox': parent::__construct('fieldset', null, null, false, $indent); $this->setAttributes('class', 'check-box-fieldset'); // Create the checkbox elements and related span elements. $i = null; foreach ($value as $k => $v) { $chk = new Child('input', null, null, false, $indent); $chk->setAttributes(array('type' => $type, 'class' => 'check-box', 'name' => $name . '[]', 'id' => $name . $i, 'value' => $k)); // Determine if the current radio element is checked. if (in_array($k, $this->marked)) { $chk->setAttributes('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttributes('class', 'check-span'); $span->setNodeValue($v); $this->addChildren(array($chk, $span)); $i++; } $this->value = $value; break; // Input element // Input element default: if ($type == 'button') { $nodeType = 'button'; $type = 'submit'; } else { $nodeType = 'input'; } parent::__construct($nodeType, null, null, false, $indent); $this->setAttributes(array('type' => $type, 'name' => $name, 'id' => $name)); if (!is_array($value)) { if ($nodeType == 'button') { $this->nodeValue = $value; } $this->setAttributes('value', $value); } $this->value = $value; } // If a certain value is marked (selected or checked), set the property here. if (null !== $marked) { $this->marked = $marked; } }