InputElement.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace dokuwiki\Form;
  3. /**
  4. * Class InputElement
  5. *
  6. * Base class for all input elements. Uses a wrapping label when label
  7. * text is given.
  8. *
  9. * @todo figure out how to make wrapping or related label configurable
  10. * @package dokuwiki\Form
  11. */
  12. class InputElement extends Element
  13. {
  14. /**
  15. * @var LabelElement
  16. */
  17. protected $label = null;
  18. /**
  19. * @var bool if the element should reflect posted values
  20. */
  21. protected $useInput = true;
  22. /**
  23. * @param string $type The type of this element
  24. * @param string $name The name of this form element
  25. * @param string $label The label text for this element (will be autoescaped)
  26. */
  27. public function __construct($type, $name, $label = '')
  28. {
  29. parent::__construct($type, array('name' => $name));
  30. $this->attr('name', $name);
  31. $this->attr('type', $type);
  32. if ($label) $this->label = new LabelElement($label);
  33. }
  34. /**
  35. * Returns the label element if there's one set
  36. *
  37. * @return LabelElement|null
  38. */
  39. public function getLabel()
  40. {
  41. return $this->label;
  42. }
  43. /**
  44. * Should the user sent input be used to initialize the input field
  45. *
  46. * The default is true. Any set values will be overwritten by the INPUT
  47. * provided values.
  48. *
  49. * @param bool $useinput
  50. * @return $this
  51. */
  52. public function useInput($useinput)
  53. {
  54. $this->useInput = (bool) $useinput;
  55. return $this;
  56. }
  57. /**
  58. * Get or set the element's ID
  59. *
  60. * @param null|string $id
  61. * @return string|$this
  62. */
  63. public function id($id = null)
  64. {
  65. if ($this->label) $this->label->attr('for', $id);
  66. return parent::id($id);
  67. }
  68. /**
  69. * Adds a class to the class attribute
  70. *
  71. * This is the preferred method of setting the element's class
  72. *
  73. * @param string $class the new class to add
  74. * @return $this
  75. */
  76. public function addClass($class)
  77. {
  78. if ($this->label) $this->label->addClass($class);
  79. return parent::addClass($class);
  80. }
  81. /**
  82. * Figures out how to access the value for this field from INPUT data
  83. *
  84. * The element's name could have been given as a simple string ('foo')
  85. * or in array notation ('foo[bar]').
  86. *
  87. * Note: this function only handles one level of arrays. If your data
  88. * is nested deeper, you should call useInput(false) and set the
  89. * correct value yourself
  90. *
  91. * @return array name and array key (null if not an array)
  92. */
  93. protected function getInputName()
  94. {
  95. $name = $this->attr('name');
  96. parse_str("$name=1", $parsed);
  97. $name = array_keys($parsed);
  98. $name = array_shift($name);
  99. if (isset($parsed[$name]) && is_array($parsed[$name])) {
  100. $key = array_keys($parsed[$name]);
  101. $key = array_shift($key);
  102. } else {
  103. $key = null;
  104. }
  105. return array($name, $key);
  106. }
  107. /**
  108. * Handles the useInput flag and set the value attribute accordingly
  109. */
  110. protected function prefillInput()
  111. {
  112. global $INPUT;
  113. list($name, $key) = $this->getInputName();
  114. if (!$INPUT->has($name)) return;
  115. if ($key === null) {
  116. $value = $INPUT->str($name);
  117. } else {
  118. $value = $INPUT->arr($name);
  119. if (isset($value[$key])) {
  120. $value = $value[$key];
  121. } else {
  122. $value = '';
  123. }
  124. }
  125. $this->val($value);
  126. }
  127. /**
  128. * The HTML representation of this element
  129. *
  130. * @return string
  131. */
  132. protected function mainElementHTML()
  133. {
  134. if ($this->useInput) $this->prefillInput();
  135. return '<input '. buildAttributes($this->attrs()) .' />';
  136. }
  137. /**
  138. * The HTML representation of this element wrapped in a label
  139. *
  140. * @return string
  141. */
  142. public function toHTML()
  143. {
  144. if ($this->label) {
  145. return '<label '. buildAttributes($this->label->attrs()) .'>'.DOKU_LF
  146. .'<span>'. hsc($this->label->val()) .'</span>'.DOKU_LF
  147. . $this->mainElementHTML() .DOKU_LF
  148. .'</label>';
  149. } else {
  150. return $this->mainElementHTML();
  151. }
  152. }
  153. }