10a5b05ebSAndreas Gohr<?php 20a5b05ebSAndreas Gohr 30a5b05ebSAndreas Gohrnamespace dokuwiki\plugin\config\core\Setting; 40a5b05ebSAndreas Gohr 50a5b05ebSAndreas Gohr/** 60a5b05ebSAndreas Gohr * Class setting_array 70a5b05ebSAndreas Gohr */ 88c7c53b0SAndreas Gohrclass SettingArray extends Setting 98c7c53b0SAndreas Gohr{ 100a5b05ebSAndreas Gohr /** 110a5b05ebSAndreas Gohr * Create an array from a string 120a5b05ebSAndreas Gohr * 130a5b05ebSAndreas Gohr * @param string $string 140a5b05ebSAndreas Gohr * @return array 150a5b05ebSAndreas Gohr */ 16*d868eb89SAndreas Gohr protected function fromString($string) 17*d868eb89SAndreas Gohr { 180a5b05ebSAndreas Gohr $array = explode(',', $string); 190a5b05ebSAndreas Gohr $array = array_map('trim', $array); 200a5b05ebSAndreas Gohr $array = array_filter($array); 210a5b05ebSAndreas Gohr $array = array_unique($array); 220a5b05ebSAndreas Gohr return $array; 230a5b05ebSAndreas Gohr } 240a5b05ebSAndreas Gohr 250a5b05ebSAndreas Gohr /** 260a5b05ebSAndreas Gohr * Create a string from an array 270a5b05ebSAndreas Gohr * 280a5b05ebSAndreas Gohr * @param array $array 290a5b05ebSAndreas Gohr * @return string 300a5b05ebSAndreas Gohr */ 31*d868eb89SAndreas Gohr protected function fromArray($array) 32*d868eb89SAndreas Gohr { 33467c1427SAndreas Gohr return implode(', ', (array) $array); 340a5b05ebSAndreas Gohr } 350a5b05ebSAndreas Gohr 360a5b05ebSAndreas Gohr /** 370a5b05ebSAndreas Gohr * update setting with user provided value $input 380a5b05ebSAndreas Gohr * if value fails error check, save it 390a5b05ebSAndreas Gohr * 400a5b05ebSAndreas Gohr * @param string $input 410a5b05ebSAndreas Gohr * @return bool true if changed, false otherwise (incl. on error) 420a5b05ebSAndreas Gohr */ 43*d868eb89SAndreas Gohr public function update($input) 44*d868eb89SAndreas Gohr { 450a5b05ebSAndreas Gohr if (is_null($input)) return false; 460a5b05ebSAndreas Gohr if ($this->isProtected()) return false; 470a5b05ebSAndreas Gohr 480a5b05ebSAndreas Gohr $input = $this->fromString($input); 490a5b05ebSAndreas Gohr 500a5b05ebSAndreas Gohr $value = is_null($this->local) ? $this->default : $this->local; 510a5b05ebSAndreas Gohr if ($value == $input) return false; 520a5b05ebSAndreas Gohr 530a5b05ebSAndreas Gohr foreach ($input as $item) { 540a5b05ebSAndreas Gohr if ($this->pattern && !preg_match($this->pattern, $item)) { 550a5b05ebSAndreas Gohr $this->error = true; 560a5b05ebSAndreas Gohr $this->input = $input; 570a5b05ebSAndreas Gohr return false; 580a5b05ebSAndreas Gohr } 590a5b05ebSAndreas Gohr } 600a5b05ebSAndreas Gohr 610a5b05ebSAndreas Gohr $this->local = $input; 620a5b05ebSAndreas Gohr return true; 630a5b05ebSAndreas Gohr } 640a5b05ebSAndreas Gohr 650a5b05ebSAndreas Gohr /** @inheritdoc */ 66*d868eb89SAndreas Gohr public function html(\admin_plugin_config $plugin, $echo = false) 67*d868eb89SAndreas Gohr { 680a5b05ebSAndreas Gohr $disable = ''; 690a5b05ebSAndreas Gohr 700a5b05ebSAndreas Gohr if ($this->isProtected()) { 710a5b05ebSAndreas Gohr $value = $this->protected; 720a5b05ebSAndreas Gohr $disable = 'disabled="disabled"'; 73467c1427SAndreas Gohr } elseif ($echo && $this->error) { 740a5b05ebSAndreas Gohr $value = $this->input; 750a5b05ebSAndreas Gohr } else { 760a5b05ebSAndreas Gohr $value = is_null($this->local) ? $this->default : $this->local; 770a5b05ebSAndreas Gohr } 780a5b05ebSAndreas Gohr 790a5b05ebSAndreas Gohr $key = htmlspecialchars($this->key); 800a5b05ebSAndreas Gohr $value = htmlspecialchars($this->fromArray($value)); 810a5b05ebSAndreas Gohr 820a5b05ebSAndreas Gohr $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>'; 830a5b05ebSAndreas Gohr $input = '<input id="config___' . $key . '" name="config[' . $key . 840a5b05ebSAndreas Gohr ']" type="text" class="edit" value="' . $value . '" ' . $disable . '/>'; 85467c1427SAndreas Gohr return [$label, $input]; 860a5b05ebSAndreas Gohr } 870a5b05ebSAndreas Gohr} 88