Failed Conditions
Push — psr2-config ( c6639e )
by Andreas
06:39 queued 03:33
created

Setting   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 209
rs 8.2857
c 0
b 0
f 0
wmc 39
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A initialize() 0 5 4
B update() 0 16 7
B html() 0 22 5
B out() 0 15 5
A prompt() 0 5 2
A is_protected() 0 3 1
A is_default() 0 3 2
A error() 0 3 1
A caution() 0 21 4
B _out_key() 0 14 5
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
     * update changed setting with user provided value $input
53
     * - if changed value fails error check, save it to $this->_input (to allow echoing later)
54
     * - if changed value passes error check, set $this->_local to the new value
55
     *
56
     * @param  mixed $input the new value
57
     * @return boolean          true if changed, false otherwise (also on error)
58
     */
59
    public function update($input) {
60
        if(is_null($input)) return false;
61
        if($this->is_protected()) return false;
62
63
        $value = is_null($this->_local) ? $this->_default : $this->_local;
64
        if($value == $input) return false;
65
66
        if($this->_pattern && !preg_match($this->_pattern, $input)) {
67
            $this->_error = true;
68
            $this->_input = $input;
69
            return false;
70
        }
71
72
        $this->_local = $input;
73
        return true;
74
    }
75
76
    /**
77
     * Build html for label and input of setting
78
     *
79
     * @param \admin_plugin_config $plugin object of config plugin
80
     * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting
81
     * @return string[] with content array(string $label_html, string $input_html)
82
     */
83
    public function html(\admin_plugin_config $plugin, $echo = false) {
84
        $disable = '';
85
86
        if($this->is_protected()) {
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 = formText($value);
99
100
        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
101
        $input = '<textarea rows="3" cols="40" id="config___' . $key .
102
            '" name="config[' . $key . ']" class="edit" ' . $disable . '>' . $value . '</textarea>';
103
        return array($label, $input);
104
    }
105
106
    /**
107
     * Generate string to save setting value to file according to $fmt
108
     *
109
     * @param string $var name of variable
110
     * @param string $fmt save format
111
     * @return string
112
     */
113
    public function out($var, $fmt = 'php') {
114
115
        if($this->is_protected()) return '';
116
        if(is_null($this->_local) || ($this->_default == $this->_local)) return '';
117
118
        $out = '';
119
120
        if($fmt == 'php') {
121
            $tr = array("\\" => '\\\\', "'" => '\\\'');
122
123
            $out = '$' . $var . "['" . $this->_out_key() . "'] = '" . strtr(cleanText($this->_local), $tr) . "';\n";
124
        }
125
126
        return $out;
127
    }
128
129
    /**
130
     * Returns the localized prompt
131
     *
132
     * @param \admin_plugin_config $plugin object of config plugin
133
     * @return string text
134
     */
135
    public function prompt(\admin_plugin_config $plugin) {
136
        $prompt = $plugin->getLang($this->_key);
137
        if(!$prompt) $prompt = htmlspecialchars(str_replace(array('____', '_'), ' ', $this->_key));
138
        return $prompt;
139
    }
140
141
    /**
142
     * Is setting protected
143
     *
144
     * @return bool
145
     */
146
    public function is_protected() {
147
        return !is_null($this->_protected);
148
    }
149
150
    /**
151
     * Is setting the default?
152
     *
153
     * @return bool
154
     */
155
    public function is_default() {
156
        return !$this->is_protected() && is_null($this->_local);
157
    }
158
159
    /**
160
     * Has an error?
161
     *
162
     * @return bool
163
     */
164
    public function error() {
165
        return $this->_error;
166
    }
167
168
    /**
169
     * Returns caution
170
     *
171
     * @return false|string caution string, otherwise false for invalid caution
172
     */
173
    public function caution() {
174
        if(!empty($this->_caution)) {
175
            if(!in_array($this->_caution, Setting::$_validCautions)) {
176
                trigger_error(
177
                    'Invalid caution string (' . $this->_caution . ') in metadata for setting "' . $this->_key . '"',
178
                    E_USER_WARNING
179
                );
180
                return false;
181
            }
182
            return $this->_caution;
183
        }
184
        // compatibility with previous cautionList
185
        // TODO: check if any plugins use; remove
186
        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...
187
            $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...
188
            unset($this->_cautionList);
189
190
            return $this->caution();
191
        }
192
        return false;
193
    }
194
195
    /**
196
     * Returns setting key, eventually with referer to config: namespace at dokuwiki.org
197
     *
198
     * @param bool $pretty create nice key
199
     * @param bool $url provide url to config: namespace
200
     * @return string key
201
     */
202
    public function _out_key($pretty = false, $url = false) {
203
        if($pretty) {
204
            $out = str_replace(Configuration::KEYMARKER, "»", $this->_key);
205
            if($url && !strstr($out, '»')) {//provide no urls for plugins, etc.
206
                if($out == 'start') //one exception
207
                    return '<a href="http://www.dokuwiki.org/config:startpage">' . $out . '</a>';
208
                else
209
                    return '<a href="http://www.dokuwiki.org/config:' . $out . '">' . $out . '</a>';
210
            }
211
            return $out;
212
        } else {
213
            return str_replace(Configuration::KEYMARKER, "']['", $this->_key);
214
        }
215
    }
216
}
217