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