Method to set the child node value.
public setNodeValue ( string $value ) : |
||
$value | string | |
return |
/** * Constructor * * Instantiate the set of checkbox input form elements * * @param string $name * @param array $values * @param string $indent * @param string|array $marked * @return CheckboxSet */ public function __construct($name, array $values, $indent = null, $marked = null) { if (null !== $marked) { if (!is_array($marked)) { $marked = [$marked]; } } else { $marked = []; } parent::__construct('fieldset', null, null, false, $indent); $this->attributes['class'] = 'checkbox-fieldset'; $this->setMarked($marked); $this->setName($name . '[]'); // Create the checkbox elements and related span elements. $i = null; foreach ($values as $k => $v) { $checkbox = new Input\Checkbox($name . '[]', null, $indent); $checkbox->setAttributes(['class' => 'checkbox', 'id' => $name . $i, 'value' => $k]); // Determine if the current radio element is checked. if (in_array($k, $this->marked)) { $checkbox->setAttribute('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttribute('class', 'checkbox-span'); $span->setNodeValue($v); $this->addChildren([$checkbox, $span]); $this->checkboxes[] = $checkbox; $i++; } $this->value = $values; }
/** * Constructor * * Instantiate the radio input form elements * * @param string $name * @param array $values * @param string $indent * @param string $marked * @return RadioSet */ public function __construct($name, array $values, $indent = null, $marked = null) { parent::__construct('fieldset', null, null, false, $indent); $this->attributes['class'] = 'radio-fieldset'; $this->setMarked($marked); $this->setName($name); // Create the radio elements and related span elements. $i = null; foreach ($values as $k => $v) { $radio = new Input\Radio($name, null, $indent); $radio->setAttributes(['class' => 'radio', 'id' => $name . $i, 'value' => $k]); // Determine if the current radio element is checked. if (null !== $this->marked && $k == $this->marked) { $radio->setAttribute('checked', 'checked'); } $span = new Child('span', null, null, false, $indent); $span->setAttribute('class', 'radio-span'); $span->setNodeValue($v); $this->addChildren([$radio, $span]); $this->radios[] = $radio; $i++; } $this->value = $values; }
/** * Constructor * * Instantiate the select form element object * * @param string $name * @param string|array $values * @param string $indent * @param array $config * @return Select */ public function __construct($name, $values, $indent = null, array $config = null) { $marked = isset($config['marked']) ? $config['marked'] : null; $multiple = isset($config['multiple']) ? (bool) $config['multiple'] : false; $data = isset($config['data']) ? $config['data'] : null; parent::__construct($this->type, null, null, false, $indent); $this->setAttributes(['name' => $name, 'id' => $name]); $this->setAsMultiple($multiple); $this->setMarked($marked); $values = self::parseValues($values, $data); // Create the child option elements. foreach ($values as $k => $v) { if (is_array($v)) { $opt = new Child('optgroup', null, null, false, $indent); $opt->setAttribute('label', $k); foreach ($v as $ky => $vl) { $o = new Child('option', null, null, false, $indent); $o->setAttribute('value', $ky); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($ky, $this->marked, true)) { $o->setAttribute('selected', 'selected'); } } else { if (null !== $this->marked && $ky == $this->marked) { $o->setAttribute('selected', 'selected'); } } $o->setNodeValue($vl); $opt->addChild($o); } } else { $opt = new Child('option', null, null, false, $indent); $opt->setAttribute('value', $k); // Determine if the current option element is selected. if (is_array($this->marked)) { if (in_array($k, $this->marked, true)) { $opt->setAttribute('selected', 'selected'); } } else { if (null !== $this->marked && $k == $this->marked) { $opt->setAttribute('selected', 'selected'); } } $opt->setNodeValue($v); } $this->addChild($opt); } $this->setValue($values); $this->setName($name); }
/** * Method to render the form using the template * * @return void */ protected function renderWithTemplate() { // Initialize properties and variables. $isFile = !(stripos($this->template, '.phtml') === false && stripos($this->template, '.php') === false); $template = $this->template; $fileContents = $isFile ? file_get_contents($this->template) : null; $this->output = null; $children = $this->form->getChildren(); // Loop through the child elements of the form. foreach ($children as $child) { // Clear the password field from display. if ($child->getAttribute('type') == 'password') { $child->setValue(null); $child->setAttributes('value', null); } // Get the element name. if ($child->getNodeName() == 'fieldset') { $chdrn = $child->getChildren(); $attribs = $chdrn[0]->getAttributes(); } else { $attribs = $child->getAttributes(); } $name = isset($attribs['name']) ? $attribs['name'] : ''; $name = str_replace('[]', '', $name); // Set the element's label, if applicable. if (null !== $child->getLabel()) { // Format the label name. $label = new Child('label', $child->getLabel()); $label->setAttributes('for', $name); $labelAttributes = $child->getLabelAttributes(); if (null !== $labelAttributes) { foreach ($labelAttributes as $a => $v) { $label->setAttributes($a, $v); } } else { if ($child->isRequired()) { $label->setAttributes('class', 'required'); } } // Swap the element's label placeholder with the rendered label element. $labelSearch = '[{' . $name . '_label}]'; $labelReplace = $label->render(true); $labelReplace = substr($labelReplace, 0, -1); $template = str_replace($labelSearch, $labelReplace, $template); ${$name . '_label'} = $labelReplace; } // Calculate the element's indentation. if (null === $fileContents) { $childIndent = substr($template, 0, strpos($template, '[{' . $name . '}]')); $childIndent = substr($childIndent, strrpos($childIndent, "\n") + 1); } else { $childIndent = substr($fileContents, 0, strpos($fileContents, '$' . $name)); $childIndent = substr($childIndent, strrpos($childIndent, "\n") + 1); } // Some whitespace clean up $length = strlen($childIndent); $last = 0; $matches = array(); preg_match_all('/[^\\s]/', $childIndent, $matches, PREG_OFFSET_CAPTURE); if (isset($matches[0])) { foreach ($matches[0] as $str) { $childIndent = str_replace($str[0], null, $childIndent); $last = $str[1]; } } // Final whitespace clean up if (null !== $fileContents) { $childIndent = substr($childIndent, 0, 0 - abs($length - $last)); } // Set each child element's indentation. $childChildren = $child->getChildren(); $child->removeChildren(); foreach ($childChildren as $cChild) { $cChild->setIndent($childIndent . ' '); $child->addChild($cChild); } // Swap the element's placeholder with the rendered element. $elementSearch = '[{' . $name . '}]'; $elementReplace = $child->render(true, 0, null, $childIndent); $elementReplace = substr($elementReplace, 0, -1); $elementReplace = str_replace('</select>', $childIndent . '</select>', $elementReplace); $elementReplace = str_replace('</fieldset>', $childIndent . '</fieldset>', $elementReplace); $template = str_replace($elementSearch, $elementReplace, $template); ${$name} = $elementReplace; } // Set the rendered form content and remove the children. if (!$isFile) { $this->form->setNodeValue("\n" . $template . "\n" . $this->form->getIndent()); $this->form->removeChildren(); $this->output = $this->form->render(true); } else { $action = $this->action; $method = $this->method; ob_start(); include $this->template; $this->output = ob_get_clean(); } }
/** * 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; } }