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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*/
namespace Piwik\Plugin;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Settings\Setting;
use Piwik\Settings\StorageInterface;
/**
* Base class of all plugin settings providers. Plugins that define their own configuration settings
* can extend this class to easily make their settings available to Piwik users.
*
* Descendants of this class should implement the {@link init()} method and call the
* {@link addSetting()} method for each of the plugin's settings.
*
* For an example, see the {@link Piwik\Plugins\ExampleSettingsPlugin\ExampleSettingsPlugin} plugin.
*
* @package Piwik\Plugin
* @api
*/
abstract class Settings implements StorageInterface
{
const TYPE_INT = 'integer';
const TYPE_FLOAT = 'float';
const TYPE_STRING = 'string';
const TYPE_BOOL = 'boolean';
const TYPE_ARRAY = 'array';
const CONTROL_RADIO = 'radio';
const CONTROL_TEXT = 'text';
const CONTROL_TEXTAREA = 'textarea';
const CONTROL_CHECKBOX = 'checkbox';
const CONTROL_PASSWORD = 'password';
const CONTROL_MULTI_SELECT = 'multiselect';
const CONTROL_SINGLE_SELECT = 'select';
/**
* An array containing all available settings: Array ( [setting-name] => [setting] )
*
* @var Settings[]
*/
private $settings = array();
/**
* Array containing all plugin settings values: Array( [setting-key] => [setting-value] ).
*
* @var array
*/
private $settingsValues = array();
private $introduction;
private $pluginName;
/**
* Constructor.
*
* @param string $pluginName The name of the plugin these settings are for.
*/
public function __construct($pluginName)
{
$this->pluginName = $pluginName;
$this->init();
$this->loadSettings();
}
/**
* Implemented by descendants. This method should define plugin settings (via the
* {@link addSetting()}) method and set the introduction text (via the
* {@link setIntroduction()}).
*/
abstract protected function init();
/**
* Sets the text used to introduce this plugin's settings in the _Plugin Settings_ page.
*
* @param string $introduction
*/
protected function setIntroduction($introduction)
{
$this->introduction = $introduction;
}
/**
* Returns the introduction text for this plugin's settings.
*
* @return string
*/
public function getIntroduction()
{
return $this->introduction;
}
/**
* Returns the settings that can be displayed for the current user.
*
* @return Setting[]
*/
public function getSettingsForCurrentUser()
{
$settings = array_filter($this->getSettings(), function (Setting $setting) {
return $setting->canBeDisplayedForCurrentUser();
});
uasort($settings, function ($setting1, $setting2) use ($settings) {
/** @var Setting $setting1 */ /** @var Setting $setting2 */
if ($setting1->getOrder() == $setting2->getOrder()) {
// preserve order for settings having same order
foreach ($settings as $setting) {
if ($setting1 === $setting) {
return -1;
}
if ($setting2 === $setting) {
return 1;
}
}
return 0;
}
return $setting1->getOrder() > $setting2->getOrder() ? -1 : 1;
});
return $settings;
}
/**
* Returns all available settings. This will include settings that are not available
* to the current user (such as settings available only to the Super User).
*
* @return Setting[]
*/
public function getSettings()
{
return $this->settings;
}
/**
* Saves (persists) the current setting values in the database.
*/
public function save()
{
Option::set($this->getOptionKey(), serialize($this->settingsValues));
}
/**
* Removes all settings for this plugin from the database. Useful when uninstalling
* a plugin.
*/
public function removeAllPluginSettings()
{
Piwik::checkUserIsSuperUser();
Option::delete($this->getOptionKey());
$this->settingsValues = array();
}
/**
* Returns the current value for a setting. If no value is stored, the default value
* is be returned.
*
* @param Setting $setting
* @return mixed
* @throws \Exception If the setting does not exist or if the current user is not allowed to change the value
* of this setting.
*/
public function getSettingValue(Setting $setting)
{
$this->checkIsValidSetting($setting->getName());
if (array_key_exists($setting->getKey(), $this->settingsValues)) {
return $this->settingsValues[$setting->getKey()];
}
return $setting->defaultValue;
}
/**
* Sets (overwrites) the value of a setting in memory. To persist the change, {@link save()} must be
* called afterwards, otherwise the change has no effect.
*
* Before the setting is changed, the {@link Piwik\Settings\Setting::$validate} and
* {@link Piwik\Settings\Setting::$transform} closures will be invoked (if defined). If there is no validation
* filter, the setting value will be casted to the appropriate data type.
*
* @param Setting $setting
* @param string $value
* @throws \Exception If the setting does not exist or if the current user is not allowed to change the value
* of this setting.
*/
public function setSettingValue(Setting $setting, $value)
{
$this->checkIsValidSetting($setting->getName());
if ($setting->validate && $setting->validate instanceof \Closure) {
call_user_func($setting->validate, $value, $setting);
}
if ($setting->transform && $setting->transform instanceof \Closure) {
$value = call_user_func($setting->transform, $value, $setting);
} elseif (isset($setting->type)) {
settype($value, $setting->type);
}
$this->settingsValues[$setting->getKey()] = $value;
}
/**
* Unsets a setting value in memory. To persist the change, {@link save()} must be
* called afterwards, otherwise the change has no effect.
*
* @param Setting $setting
*/
public function removeSettingValue(Setting $setting)
{
$this->checkHasEnoughPermission($setting);
$key = $setting->getKey();
if (array_key_exists($key, $this->settingsValues)) {
unset($this->settingsValues[$key]);
}
}
/**
* Makes a new plugin setting available.
*
* @param Setting $setting
* @throws \Exception If there is a setting with the same name that already exists.
* If the name contains non-alphanumeric characters.
*/
protected function addSetting(Setting $setting)
{
if (!ctype_alnum($setting->getName())) {
$msg = sprintf('The setting name "%s" in plugin "%s" is not valid. Only alpha and numerical characters are allowed', $setting->getName(), $this->pluginName);
throw new \Exception($msg);
}
if (array_key_exists($setting->getName(), $this->settings)) {
throw new \Exception(sprintf('A setting with name "%s" does already exist for plugin "%s"', $setting->getName(), $this->pluginName));
}
$this->setDefaultTypeAndFieldIfNeeded($setting);
$this->addValidatorIfNeeded($setting);
$setting->setStorage($this);
$this->settings[$setting->getName()] = $setting;
}
private function getOptionKey()
{
return 'Plugin_' . $this->pluginName . '_Settings';
}
private function loadSettings()
{
$values = Option::get($this->getOptionKey());
if (!empty($values)) {
$this->settingsValues = unserialize($values);
}
}
private function checkIsValidSetting($name)
{
$setting = $this->getSetting($name);
if (empty($setting)) {
throw new \Exception(sprintf('The setting %s does not exist', $name));
}
$this->checkHasEnoughPermission($setting);
}
/**
* @param $name
* @return Setting|null
*/
private function getSetting($name)
{
if (array_key_exists($name, $this->settings)) {
return $this->settings[$name];
}
}
private function getDefaultType($controlType)
{
$defaultTypes = array(
static::CONTROL_TEXT => static::TYPE_STRING,
static::CONTROL_TEXTAREA => static::TYPE_STRING,
static::CONTROL_PASSWORD => static::TYPE_STRING,
static::CONTROL_CHECKBOX => static::TYPE_BOOL,
static::CONTROL_MULTI_SELECT => static::TYPE_ARRAY,
static::CONTROL_RADIO => static::TYPE_STRING,
static::CONTROL_SINGLE_SELECT => static::TYPE_STRING,
);
return $defaultTypes[$controlType];
}
private function getDefaultCONTROL($type)
{
$defaultControlTypes = array(
static::TYPE_INT => static::CONTROL_TEXT,
static::TYPE_FLOAT => static::CONTROL_TEXT,
static::TYPE_STRING => static::CONTROL_TEXT,
static::TYPE_BOOL => static::CONTROL_CHECKBOX,
static::TYPE_ARRAY => static::CONTROL_MULTI_SELECT,
);
return $defaultControlTypes[$type];
}
/**
* @param $setting
* @throws \Exception
*/
private function checkHasEnoughPermission(Setting $setting)
{
if (!$setting->canBeDisplayedForCurrentUser()) {
$errorMsg = Piwik::translate('CoreAdminHome_PluginSettingChangeNotAllowed', array($setting->getName(), $this->pluginName));
throw new \Exception($errorMsg);
}
}
private function setDefaultTypeAndFieldIfNeeded(Setting $setting)
{
if (!is_null($setting->uiControlType) && is_null($setting->type)) {
$setting->type = $this->getDefaultType($setting->uiControlType);
} elseif (!is_null($setting->type) && is_null($setting->uiControlType)) {
$setting->uiControlType = $this->getDefaultCONTROL($setting->type);
} elseif (is_null($setting->uiControlType) && is_null($setting->type)) {
$setting->type = static::TYPE_STRING;
$setting->uiControlType = static::CONTROL_TEXT;
}
}
private function addValidatorIfNeeded(Setting $setting)
{
if (!is_null($setting->validate) || is_null($setting->availableValues)) {
return;
}
$pluginName = $this->pluginName;
$setting->validate = function ($value) use ($setting, $pluginName) {
$errorMsg = Piwik::translate('CoreAdminHome_PluginSettingsValueNotAllowed',
array($setting->title, $pluginName));
if (is_array($value) && $setting->type == Settings::TYPE_ARRAY) {
foreach ($value as $val) {
if (!array_key_exists($val, $setting->availableValues)) {
throw new \Exception($errorMsg);
}
}
} else {
if (!array_key_exists($value, $setting->availableValues)) {
throw new \Exception($errorMsg);
}
}
};
}
}
|