Failed Conditions
Push — psr2-config ( 5675a0...a1ef8b )
by Andreas
06:37 queued 03:29
created

Setting::getArrayKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace dokuwiki\plugin\config\core;
4
5
/**
6
 * Class setting
7
 */
8
class Setting {
9
    /** @var string unique identifier of this setting */
10
    protected $key = '';
11
12
    /** @var mixed the default value of this setting */
13
    protected $default = null;
14
    /** @var mixed the local value of this setting */
15
    protected $local = null;
16
    /** @var mixed the protected value of this setting */
17
    protected $protected = null;
18
19
    /** @var array valid alerts, images matching the alerts are in the plugin's images directory */
20
    static protected $validCautions = array('warning', 'danger', 'security');
21
22
    protected $pattern = '';
23
    protected $error = false;            // only used by those classes which error check
24
    protected $input = null;             // only used by those classes which error check
25
    protected $caution = null;           // used by any setting to provide an alert along with the setting
26
27
    /**
28
     * Constructor.
29
     *
30
     * The given parameters will be set up as class properties
31
     *
32
     * @param string $key
33
     * @param array|null $params array with metadata of setting
34
     */
35
    public function __construct($key, $params = null) {
36
        $this->key = $key;
37
38
        if(is_array($params)) {
39
            foreach($params as $property => $value) {
40
                $property = trim($property, '_'); // we don't use underscores anymore
41
                $this->$property = $value;
42
            }
43
        }
44
    }
45
46
    /**
47
     * Receives current values for the setting $key
48
     *
49
     * @param mixed $default default setting value
50
     * @param mixed $local local setting value
51
     * @param mixed $protected protected setting value
52
     */
53
    public function initialize($default, $local, $protected) {
54
        if(isset($default)) $this->default = $default;
55
        if(isset($local)) $this->local = $local;
56
        if(isset($protected)) $this->protected = $protected;
57
    }
58
59
    /**
60
     * Should this type of config have a default?
61
     *
62
     * @return bool
63
     */
64
    public function shouldHaveDefault() {
65
        return true;
66
    }
67
68
    /**
69
     * Get this setting's unique key
70
     *
71
     * @return string
72
     */
73
    public function getKey() {
74
        return $this->key;
75
    }
76
77
    /**
78
     * Get the key of this setting marked up human readable
79
     *
80
     * @param bool $url link to dokuwiki.org manual?
81
     * @return string
82
     */
83
    public function getPrettyKey($url = true) {
84
        $out = str_replace(Configuration::KEYMARKER, "»", $this->key);
85
        if($url && !strstr($out, '»')) {//provide no urls for plugins, etc.
86
            if($out == 'start') {
87
                // exception, because this config name is clashing with our actual start page
88
                return '<a href="http://www.dokuwiki.org/config:startpage">' . $out . '</a>';
89
            } else {
90
                return '<a href="http://www.dokuwiki.org/config:' . $out . '">' . $out . '</a>';
91
            }
92
        }
93
        return $out;
94
    }
95
96
    /**
97
     * Returns setting key as an array key separator
98
     *
99
     * This is used to create form output
100
     *
101
     * @return string key
102
     */
103
    public function getArrayKey() {
104
        return str_replace(Configuration::KEYMARKER, "']['", $this->key);
105
    }
106
107
    /**
108
     * What type of configuration is this
109
     *
110
     * Returns one of
111
     *
112
     * 'plugin' for plugin configuration
113
     * 'template' for template configuration
114
     * 'conf' for core configuration
115
     *
116
     * @return string
117
     */
118
    public function getType() {
119
        if(substr($this->getKey(), 0, 10) == 'plugin' . Configuration::KEYMARKER) {
120
            return 'plugin';
121
        } else if(substr($this->getKey(), 0, 7) == 'tpl' . Configuration::KEYMARKER) {
122
            return 'template';
123
        } else {
124
            return 'conf';
125
        }
126
    }
127
128
    /**
129
     * update changed setting with user provided value $input
130
     * - if changed value fails error check, save it to $this->_input (to allow echoing later)
131
     * - if changed value passes error check, set $this->_local to the new value
132
     *
133
     * @param  mixed $input the new value
134
     * @return boolean          true if changed, false otherwise (also on error)
135
     */
136
    public function update($input) {
137
        if(is_null($input)) return false;
138
        if($this->isProtected()) return false;
139
140
        $value = is_null($this->local) ? $this->default : $this->local;
141
        if($value == $input) return false;
142
143
        if($this->pattern && !preg_match($this->pattern, $input)) {
144
            $this->error = true;
145
            $this->input = $input;
146
            return false;
147
        }
148
149
        $this->local = $input;
150
        return true;
151
    }
152
153
    /**
154
     * Build html for label and input of setting
155
     *
156
     * @param \admin_plugin_config $plugin object of config plugin
157
     * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting
158
     * @return string[] with content array(string $label_html, string $input_html)
159
     */
160
    public function html(\admin_plugin_config $plugin, $echo = false) {
161
        $disable = '';
162
163
        if($this->isProtected()) {
164
            $value = $this->protected;
165
            $disable = 'disabled="disabled"';
166
        } else {
167
            if($echo && $this->error) {
168
                $value = $this->input;
169
            } else {
170
                $value = is_null($this->local) ? $this->default : $this->local;
171
            }
172
        }
173
174
        $key = htmlspecialchars($this->key);
175
        $value = formText($value);
176
177
        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
178
        $input = '<textarea rows="3" cols="40" id="config___' . $key .
179
            '" name="config[' . $key . ']" class="edit" ' . $disable . '>' . $value . '</textarea>';
180
        return array($label, $input);
181
    }
182
183
    /**
184
     * Generate string to save setting value to file according to $fmt
185
     *
186
     * @param string $var name of variable
187
     * @param string $fmt save format
188
     * @return string
189
     */
190
    public function out($var, $fmt = 'php') {
191
192
        if($this->isProtected()) return '';
193
        if(is_null($this->local) || ($this->default == $this->local)) return '';
194
195
        $out = '';
196
197
        if($fmt == 'php') {
198
            $tr = array("\\" => '\\\\', "'" => '\\\'');
199
200
            $out = '$' . $var . "['" . $this->getArrayKey() . "'] = '" . strtr(cleanText($this->local), $tr) . "';\n";
201
        }
202
203
        return $out;
204
    }
205
206
    /**
207
     * Returns the localized prompt
208
     *
209
     * @param \admin_plugin_config $plugin object of config plugin
210
     * @return string text
211
     */
212
    public function prompt(\admin_plugin_config $plugin) {
213
        $prompt = $plugin->getLang($this->key);
214
        if(!$prompt) $prompt = htmlspecialchars(str_replace(array('____', '_'), ' ', $this->key));
215
        return $prompt;
216
    }
217
218
    /**
219
     * Is setting protected
220
     *
221
     * @return bool
222
     */
223
    public function isProtected() {
224
        return !is_null($this->protected);
225
    }
226
227
    /**
228
     * Is setting the default?
229
     *
230
     * @return bool
231
     */
232
    public function isDefault() {
233
        return !$this->isProtected() && is_null($this->local);
234
    }
235
236
    /**
237
     * Has an error?
238
     *
239
     * @return bool
240
     */
241
    public function hasError() {
242
        return $this->error;
243
    }
244
245
    /**
246
     * Returns caution
247
     *
248
     * @return false|string caution string, otherwise false for invalid caution
249
     */
250
    public function caution() {
251
        if(!empty($this->caution)) {
252
            if(!in_array($this->caution, Setting::$validCautions)) {
253
                trigger_error(
254
                    'Invalid caution string (' . $this->caution . ') in metadata for setting "' . $this->key . '"',
255
                    E_USER_WARNING
256
                );
257
                return false;
258
            }
259
            return $this->caution;
260
        }
261
        // compatibility with previous cautionList
262
        // TODO: check if any plugins use; remove
263
        if(!empty($this->cautionList[$this->key])) {
0 ignored issues
show
Bug introduced by
The property cautionList does not seem to exist. Did you mean caution?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
264
            $this->caution = $this->cautionList[$this->key];
0 ignored issues
show
Bug introduced by
The property cautionList does not seem to exist. Did you mean caution?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
265
            unset($this->cautionList);
266
267
            return $this->caution();
268
        }
269
        return false;
270
    }
271
272
}
273