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