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