SettingOnoff.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace dokuwiki\plugin\config\core\Setting;
  3. /**
  4. * Class setting_onoff
  5. */
  6. class SettingOnoff extends SettingNumeric {
  7. /**
  8. * We treat the strings 'false' and 'off' as false
  9. * @inheritdoc
  10. */
  11. protected function cleanValue($value) {
  12. if($value === null) return null;
  13. if(is_string($value)) {
  14. if(strtolower($value) === 'false') return 0;
  15. if(strtolower($value) === 'off') return 0;
  16. if(trim($value) === '') return 0;
  17. }
  18. return (int) (bool) $value;
  19. }
  20. /** @inheritdoc */
  21. public function html(\admin_plugin_config $plugin, $echo = false) {
  22. $disable = '';
  23. if($this->isProtected()) {
  24. $value = $this->protected;
  25. $disable = ' disabled="disabled"';
  26. } else {
  27. $value = is_null($this->local) ? $this->default : $this->local;
  28. }
  29. $key = htmlspecialchars($this->key);
  30. $checked = ($value) ? ' checked="checked"' : '';
  31. $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
  32. $input = '<div class="input"><input id="config___' . $key . '" name="config[' . $key .
  33. ']" type="checkbox" class="checkbox" value="1"' . $checked . $disable . '/></div>';
  34. return array($label, $input);
  35. }
  36. /** @inheritdoc */
  37. public function update($input) {
  38. if($this->isProtected()) return false;
  39. $input = ($input) ? 1 : 0;
  40. $value = is_null($this->local) ? $this->default : $this->local;
  41. if($value == $input) return false;
  42. $this->local = $input;
  43. return true;
  44. }
  45. }