diff options
author | Andreas Gohr <andi@splitbrain.org> | 2015-08-03 18:47:19 +0200 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2015-08-03 18:47:19 +0200 |
commit | 8909ab7629ec67708b589c35d380c68da04a8b44 (patch) | |
tree | a35ec57bebc7c0fcc45853fe377b5b0fcc2d5846 /inc/Form/InputElement.php | |
parent | b20571a714d774d231f80f95643c6f84ef9833ed (diff) | |
parent | 3c8e094acdcf6d556f022bc224b81ef17e449281 (diff) | |
download | rpg-8909ab7629ec67708b589c35d380c68da04a8b44.tar.gz rpg-8909ab7629ec67708b589c35d380c68da04a8b44.tar.bz2 |
Merge pull request #1265 from splitbrain/form2
Form 2
Diffstat (limited to 'inc/Form/InputElement.php')
-rw-r--r-- | inc/Form/InputElement.php | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/inc/Form/InputElement.php b/inc/Form/InputElement.php new file mode 100644 index 000000000..5908f7d11 --- /dev/null +++ b/inc/Form/InputElement.php @@ -0,0 +1,160 @@ +<?php +namespace dokuwiki\Form; + +/** + * Class InputElement + * + * Base class for all input elements. Uses a wrapping label when label + * text is given. + * + * @todo figure out how to make wrapping or related label configurable + * @package dokuwiki\Form + */ +class InputElement extends Element { + /** + * @var Label + */ + protected $label = null; + + /** + * @var bool if the element should reflect posted values + */ + protected $useInput = true; + + /** + * @param string $type The type of this element + * @param string $name The name of this form element + * @param string $label The label text for this element + */ + public function __construct($type, $name, $label = '') { + parent::__construct($type, array('name' => $name)); + $this->attr('name', $name); + if($label) $this->label = new Label($label); + } + + /** + * Returns the label element if there's one set + * + * @return Label|null + */ + public function getLabel() { + return $this->label; + } + + /** + * Should the user sent input be used to initialize the input field + * + * The default is true. Any set values will be overwritten by the INPUT + * provided values. + * + * @param bool $useinput + * @return $this + */ + public function useInput($useinput) { + $this->useInput = (bool) $useinput; + return $this; + } + + /** + * Get or set the element's ID + * + * @param null|string $id + * @return string|$this + */ + public function id($id = null) { + if($this->label) $this->label->attr('for', $id); + return parent::id($id); + } + + /** + * Adds a class to the class attribute + * + * This is the preferred method of setting the element's class + * + * @param string $class the new class to add + * @return $this + */ + public function addClass($class) { + if($this->label) $this->label->addClass($class); + return parent::addClass($class); + } + + /** + * Figures out how to access the value for this field from INPUT data + * + * The element's name could have been given as a simple string ('foo') + * or in array notation ('foo[bar]'). + * + * Note: this function only handles one level of arrays. If your data + * is nested deeper, you should call useInput(false) and set the + * correct value yourself + * + * @return array name and array key (null if not an array) + */ + protected function getInputName() { + $name = $this->attr('name'); + parse_str("$name=1", $parsed); + + $name = array_keys($parsed); + $name = array_shift($name); + + if(is_array($parsed[$name])) { + $key = array_keys($parsed[$name]); + $key = array_shift($key); + } else { + $key = null; + } + + return array($name, $key); + } + + /** + * Handles the useInput flag and set the value attribute accordingly + */ + protected function prefillInput() { + global $INPUT; + + list($name, $key) = $this->getInputName(); + if(!$INPUT->has($name)) return; + + if($key === null) { + $value = $INPUT->str($name); + } else { + $value = $INPUT->arr($name); + if(isset($value[$key])) { + $value = $value[$key]; + } else { + $value = ''; + } + } + if($value !== '') { + $this->val($value); + } + } + + /** + * The HTML representation of this element + * + * @return string + */ + protected function mainElementHTML() { + if($this->useInput) $this->prefillInput(); + return '<input ' . buildAttributes($this->attrs()) . ' />'; + } + + /** + * The HTML representation of this element wrapped in a label + * + * @return string + */ + public function toHTML() { + if($this->label) { + return '<label ' . buildAttributes($this->label->attrs()) . '>' . DOKU_LF . + '<span>' . hsc($this->label->val()) . '</span>' . DOKU_LF . + $this->mainElementHTML() . DOKU_LF . + '</label>'; + } else { + return $this->mainElementHTML(); + } + } +} |