SettingMultichoice.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace dokuwiki\plugin\config\core\Setting;
  3. /**
  4. * Class setting_multichoice
  5. */
  6. class SettingMultichoice extends SettingString {
  7. protected $choices = array();
  8. public $lang; //some custom language strings are stored in setting
  9. /** @inheritdoc */
  10. public function html(\admin_plugin_config $plugin, $echo = false) {
  11. $disable = '';
  12. $nochoice = '';
  13. if($this->isProtected()) {
  14. $value = $this->protected;
  15. $disable = ' disabled="disabled"';
  16. } else {
  17. $value = is_null($this->local) ? $this->default : $this->local;
  18. }
  19. // ensure current value is included
  20. if(!in_array($value, $this->choices)) {
  21. $this->choices[] = $value;
  22. }
  23. // disable if no other choices
  24. if(!$this->isProtected() && count($this->choices) <= 1) {
  25. $disable = ' disabled="disabled"';
  26. $nochoice = $plugin->getLang('nochoice');
  27. }
  28. $key = htmlspecialchars($this->key);
  29. $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
  30. $input = "<div class=\"input\">\n";
  31. $input .= '<select class="edit" id="config___' . $key . '" name="config[' . $key . ']"' . $disable . '>' . "\n";
  32. foreach($this->choices as $choice) {
  33. $selected = ($value == $choice) ? ' selected="selected"' : '';
  34. $option = $plugin->getLang($this->key . '_o_' . $choice);
  35. if(!$option && isset($this->lang[$this->key . '_o_' . $choice])) {
  36. $option = $this->lang[$this->key . '_o_' . $choice];
  37. }
  38. if(!$option) $option = $choice;
  39. $choice = htmlspecialchars($choice);
  40. $option = htmlspecialchars($option);
  41. $input .= ' <option value="' . $choice . '"' . $selected . ' >' . $option . '</option>' . "\n";
  42. }
  43. $input .= "</select> $nochoice \n";
  44. $input .= "</div>\n";
  45. return array($label, $input);
  46. }
  47. /** @inheritdoc */
  48. public function update($input) {
  49. if(is_null($input)) return false;
  50. if($this->isProtected()) return false;
  51. $value = is_null($this->local) ? $this->default : $this->local;
  52. if($value == $input) return false;
  53. if(!in_array($input, $this->choices)) return false;
  54. $this->local = $input;
  55. return true;
  56. }
  57. }