You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

83 lines
2.2 KiB

  1. <?php
  2. namespace dokuwiki\Form;
  3. /**
  4. * Class CheckableElement
  5. *
  6. * For Radio- and Checkboxes
  7. *
  8. * @package dokuwiki\Form
  9. */
  10. class CheckableElement extends InputElement
  11. {
  12. /**
  13. * @param string $type The type of this element
  14. * @param string $name The name of this form element
  15. * @param string $label The label text for this element
  16. */
  17. public function __construct($type, $name, $label)
  18. {
  19. parent::__construct($type, $name, $label);
  20. // default value is 1
  21. $this->attr('value', 1);
  22. }
  23. /**
  24. * Handles the useInput flag and sets the checked attribute accordingly
  25. */
  26. protected function prefillInput()
  27. {
  28. global $INPUT;
  29. [$name, $key] = $this->getInputName();
  30. $myvalue = $this->val();
  31. if (!$INPUT->has($name)) return;
  32. if ($key === null) {
  33. // no key - single value
  34. $value = $INPUT->str($name);
  35. if ($value == $myvalue) {
  36. $this->attr('checked', 'checked');
  37. } else {
  38. $this->rmattr('checked');
  39. }
  40. } else {
  41. // we have an array, there might be several values in it
  42. $input = $INPUT->arr($name);
  43. if (isset($input[$key])) {
  44. $this->rmattr('checked');
  45. // values seem to be in another sub array
  46. if (is_array($input[$key])) {
  47. $input = $input[$key];
  48. }
  49. foreach ($input as $value) {
  50. if ($value == $myvalue) {
  51. $this->attr('checked', 'checked');
  52. }
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * The HTML representation of this element wrapped in a label
  59. * Note: allow HTML tags in label text
  60. *
  61. * @return string
  62. */
  63. public function toHTML()
  64. {
  65. if ($this->label) {
  66. return '<label ' . buildAttributes($this->label->attrs()) . '>' . DOKU_LF
  67. . $this->mainElementHTML() . DOKU_LF
  68. . '<span>' . $this->label->val() . '</span>' . DOKU_LF
  69. . '</label>';
  70. } else {
  71. return $this->mainElementHTML();
  72. }
  73. }
  74. }