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
|
<?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\Common;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Settings\Setting;
use Piwik\Settings\SystemSetting;
use Piwik\Settings\UserSetting;
class Settings
{
const TYPE_INT = 'integer';
const TYPE_FLOAT = 'float';
const TYPE_STRING = 'string';
const TYPE_BOOL = 'boolean';
const TYPE_ARRAY = 'array';
const FIELD_TEXT = 'text';
const FIELD_TEXTAREA = 'textarea';
const FIELD_CHECKBOX = 'checkbox';
const FIELD_PASSWORD = 'password';
const FIELD_MULTI_SELECT = 'multiselect';
const FIELD_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;
public function __construct($pluginName)
{
$this->pluginName = $pluginName;
$this->init();
$this->loadSettings();
}
protected function init()
{
// Define your settings and introduction here.
}
/**
* Sets (overwrites) the plugin settings introduction.
*
* @param string $introduction
*/
protected function setIntroduction($introduction)
{
$this->introduction = $introduction;
}
public function getIntroduction()
{
return $this->introduction;
}
/**
* Returns only settings that can be displayed for current user. For instance a regular user won't see get
* any settings that require super user permissions.
*
* @return Setting[]
*/
public function getSettingsForCurrentUser()
{
return array_values(array_filter($this->getSettings(), function (Setting $setting) {
return $setting->canBeDisplayedForCurrentUser();
}));
}
/**
* Get all available settings without checking any permissions.
*
* @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. Useful for instance while uninstalling the plugin.
*/
public function removeAllPluginSettings()
{
Option::delete($this->getOptionKey());
}
/**
* Gets the current value for this setting. If no value is specified, the default value will be returned.
*
* @param Setting $setting
*
* @return mixed
*
* @throws \Exception In case 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 for the given setting. Make sure to call `save()` afterwards, otherwise the change
* has no effect. Before the value is saved a possibly define `validate` closure and `filter` closure will be
* called. Alternatively the value will be casted to the specfied setting type.
*
* @param Setting $setting
* @param string $value
*
* @throws \Exception In case 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->filter && $setting->filter instanceof \Closure) {
$value = call_user_func($setting->filter, $value, $setting);
} else {
settype($value, $setting->type);
}
$this->settingsValues[$setting->getKey()] = $value;
}
/**
* Removes the value for the given setting. Make sure to call `save()` afterwards, otherwise the removal has no
* effect.
*
* @param Setting $setting
*/
public function removeSettingValue(Setting $setting)
{
$key = $setting->getKey();
if (array_key_exists($key, $this->settingsValues)) {
unset($this->settingsValues[$key]);
}
}
/**
* Adds a new setting.
*
* @param Setting $setting
* @throws \Exception In case a setting having the same name already exists.
* In case the name contains non-alnum 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));
}
if (!is_null($setting->field) && is_null($setting->type)) {
$setting->type = $setting->getDefaultType($setting->field);
} elseif (!is_null($setting->type) && is_null($setting->field)) {
$setting->field = $setting->getDefaultField($setting->type);
}
if (is_null($setting->validate) && !is_null($setting->fieldOptions)) {
$pluginName = $this->pluginName;
$setting->validate = function ($value) use ($setting, $pluginName) {
if (is_array($value) && $setting->type == Settings::TYPE_ARRAY) {
foreach ($value as $val) {
if (!array_key_exists($val, $setting->fieldOptions)) {
throw new \Exception(sprintf('The selected value for field "%s" in plugin "%s" is not allowed.', $setting->title, $pluginName));
}
}
} else {
if (!array_key_exists($value, $setting->fieldOptions)) {
throw new \Exception(sprintf('The selected value for field "%s" in plugin "%s" is not allowed.', $setting->title, $pluginName));
}
}
};
}
$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));
}
if (!$setting->canBeDisplayedForCurrentUser()) {
throw new \Exception(sprintf('You are not allowed to change the value of the setting %s', $name));
}
}
/**
* @param $name
* @return Setting|null
*/
private function getSetting($name)
{
if (array_key_exists($name, $this->settings)) {
return $this->settings[$name];
}
}
}
|