SettingArray.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace dokuwiki\plugin\config\core\Setting;
  3. /**
  4. * Class setting_array
  5. */
  6. class SettingArray extends Setting {
  7. /**
  8. * Create an array from a string
  9. *
  10. * @param string $string
  11. * @return array
  12. */
  13. protected function fromString($string) {
  14. $array = explode(',', $string);
  15. $array = array_map('trim', $array);
  16. $array = array_filter($array);
  17. $array = array_unique($array);
  18. return $array;
  19. }
  20. /**
  21. * Create a string from an array
  22. *
  23. * @param array $array
  24. * @return string
  25. */
  26. protected function fromArray($array) {
  27. return join(', ', (array) $array);
  28. }
  29. /**
  30. * update setting with user provided value $input
  31. * if value fails error check, save it
  32. *
  33. * @param string $input
  34. * @return bool true if changed, false otherwise (incl. on error)
  35. */
  36. public function update($input) {
  37. if(is_null($input)) return false;
  38. if($this->isProtected()) return false;
  39. $input = $this->fromString($input);
  40. $value = is_null($this->local) ? $this->default : $this->local;
  41. if($value == $input) return false;
  42. foreach($input as $item) {
  43. if($this->pattern && !preg_match($this->pattern, $item)) {
  44. $this->error = true;
  45. $this->input = $input;
  46. return false;
  47. }
  48. }
  49. $this->local = $input;
  50. return true;
  51. }
  52. /** @inheritdoc */
  53. public function html(\admin_plugin_config $plugin, $echo = false) {
  54. $disable = '';
  55. if($this->isProtected()) {
  56. $value = $this->protected;
  57. $disable = 'disabled="disabled"';
  58. } else {
  59. if($echo && $this->error) {
  60. $value = $this->input;
  61. } else {
  62. $value = is_null($this->local) ? $this->default : $this->local;
  63. }
  64. }
  65. $key = htmlspecialchars($this->key);
  66. $value = htmlspecialchars($this->fromArray($value));
  67. $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
  68. $input = '<input id="config___' . $key . '" name="config[' . $key .
  69. ']" type="text" class="edit" value="' . $value . '" ' . $disable . '/>';
  70. return array($label, $input);
  71. }
  72. }