Failed Conditions
Push — psr2-config ( e063ba...8ea568 )
by Andreas
06:51 queued 03:32
created

Setting::shouldHaveDefault()   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
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\config\core;
4
5
/**
6
 * Class setting
7
 */
8
class Setting {
9
10
    protected $_key = '';
11
    protected $_default = null;
12
    protected $_local = null;
13
    protected $_protected = null;
14
15
    protected $_pattern = '';
16
    protected $_error = false;            // only used by those classes which error check
17
    protected $_input = null;             // only used by those classes which error check
18
    protected $_caution = null;           // used by any setting to provide an alert along with the setting
19
    // valid alerts, 'warning', 'danger', 'security'
20
    // images matching the alerts are in the plugin's images directory
21
22
    static protected $_validCautions = array('warning', 'danger', 'security');
23
24
    /**
25
     * @param string $key
26
     * @param array|null $params array with metadata of setting
27
     */
28
    public function __construct($key, $params = null) {
29
        $this->_key = $key;
30
31
        if(is_array($params)) {
32
            foreach($params as $property => $value) {
33
                $this->$property = $value;
34
            }
35
        }
36
    }
37
38
    /**
39
     * Receives current values for the setting $key
40
     *
41
     * @param mixed $default default setting value
42
     * @param mixed $local local setting value
43
     * @param mixed $protected protected setting value
44
     */
45
    public function initialize($default, $local, $protected) {
46
        if(isset($default)) $this->_default = $default;
47
        if(isset($local)) $this->_local = $local;
48
        if(isset($protected)) $this->_protected = $protected;
49
    }
50
51
    /**
52
     * Should this type of config have a default?
53
     *
54
     * @return bool
55
     */
56
    public function shouldHaveDefault() {
57
        return true;
58
    }
59
60
    /**
61
     * update changed setting with user provided value $input
62
     * - if changed value fails error check, save it to $this->_input (to allow echoing later)
63
     * - if changed value passes error check, set $this->_local to the new value
64
     *
65
     * @param  mixed $input the new value
66
     * @return boolean          true if changed, false otherwise (also on error)
67
     */
68
    public function update($input) {
69
        if(is_null($input)) return false;
70
        if($this->is_protected()) return false;
71
72
        $value = is_null($this->_local) ? $this->_default : $this->_local;
73
        if($value == $input) return false;
74
75
        if($this->_pattern && !preg_match($this->_pattern, $input)) {
76
            $this->_error = true;
77
            $this->_input = $input;
78
            return false;
79
        }
80
81
        $this->_local = $input;
82
        return true;
83
    }
84
85
    /**
86
     * Build html for label and input of setting
87
     *
88
     * @param \admin_plugin_config $plugin object of config plugin
89
     * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting
90
     * @return string[] with content array(string $label_html, string $input_html)
91
     */
92
    public function html(\admin_plugin_config $plugin, $echo = false) {
93
        $disable = '';
94
95
        if($this->is_protected()) {
96
            $value = $this->_protected;
97
            $disable = 'disabled="disabled"';
98
        } else {
99
            if($echo && $this->_error) {
100
                $value = $this->_input;
101
            } else {
102
                $value = is_null($this->_local) ? $this->_default : $this->_local;
103
            }
104
        }
105
106
        $key = htmlspecialchars($this->_key);
107
        $value = formText($value);
108
109
        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
110
        $input = '<textarea rows="3" cols="40" id="config___' . $key .
111
            '" name="config[' . $key . ']" class="edit" ' . $disable . '>' . $value . '</textarea>';
112
        return array($label, $input);
113
    }
114
115
    /**
116
     * Generate string to save setting value to file according to $fmt
117
     *
118
     * @param string $var name of variable
119
     * @param string $fmt save format
120
     * @return string
121
     */
122
    public function out($var, $fmt = 'php') {
123
124
        if($this->is_protected()) return '';
125
        if(is_null($this->_local) || ($this->_default == $this->_local)) return '';
126
127
        $out = '';
128
129
        if($fmt == 'php') {
130
            $tr = array("\\" => '\\\\', "'" => '\\\'');
131
132
            $out = '$' . $var . "['" . $this->_out_key() . "'] = '" . strtr(cleanText($this->_local), $tr) . "';\n";
133
        }
134
135
        return $out;
136
    }
137
138
    /**
139
     * Returns the localized prompt
140
     *
141
     * @param \admin_plugin_config $plugin object of config plugin
142
     * @return string text
143
     */
144
    public function prompt(\admin_plugin_config $plugin) {
145
        $prompt = $plugin->getLang($this->_key);
146
        if(!$prompt) $prompt = htmlspecialchars(str_replace(array('____', '_'), ' ', $this->_key));
147
        return $prompt;
148
    }
149
150
    /**
151
     * Is setting protected
152
     *
153
     * @return bool
154
     */
155
    public function is_protected() {
156
        return !is_null($this->_protected);
157
    }
158
159
    /**
160
     * Is setting the default?
161
     *
162
     * @return bool
163
     */
164
    public function is_default() {
165
        return !$this->is_protected() && is_null($this->_local);
166
    }
167
168
    /**
169
     * Has an error?
170
     *
171
     * @return bool
172
     */
173
    public function error() {
174
        return $this->_error;
175
    }
176
177
    /**
178
     * Returns caution
179
     *
180
     * @return false|string caution string, otherwise false for invalid caution
181
     */
182
    public function caution() {
183
        if(!empty($this->_caution)) {
184
            if(!in_array($this->_caution, Setting::$_validCautions)) {
185
                trigger_error(
186
                    'Invalid caution string (' . $this->_caution . ') in metadata for setting "' . $this->_key . '"',
187
                    E_USER_WARNING
188
                );
189
                return false;
190
            }
191
            return $this->_caution;
192
        }
193
        // compatibility with previous cautionList
194
        // TODO: check if any plugins use; remove
195
        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...
196
            $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...
197
            unset($this->_cautionList);
198
199
            return $this->caution();
200
        }
201
        return false;
202
    }
203
204
    /**
205
     * Returns setting key, eventually with referer to config: namespace at dokuwiki.org
206
     *
207
     * @param bool $pretty create nice key
208
     * @param bool $url provide url to config: namespace
209
     * @return string key
210
     */
211
    public function _out_key($pretty = false, $url = false) {
212
        if($pretty) {
213
            $out = str_replace(Configuration::KEYMARKER, "»", $this->_key);
214
            if($url && !strstr($out, '»')) {//provide no urls for plugins, etc.
215
                if($out == 'start') //one exception
216
                    return '<a href="http://www.dokuwiki.org/config:startpage">' . $out . '</a>';
217
                else
218
                    return '<a href="http://www.dokuwiki.org/config:' . $out . '">' . $out . '</a>';
219
            }
220
            return $out;
221
        } else {
222
            return str_replace(Configuration::KEYMARKER, "']['", $this->_key);
223
        }
224
    }
225
}
226