-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSetting.php
More file actions
464 lines (391 loc) · 10.7 KB
/
Setting.php
File metadata and controls
464 lines (391 loc) · 10.7 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/**
* CakeManager (http://cakemanager.org)
* Copyright (c) http://cakemanager.org
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) http://cakemanager.org
* @link http://cakemanager.org CakeManager Project
* @since 1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Settings\Core;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
class Setting
{
/**
* List of loaded data
*
* @var array
*/
protected static $_data = [];
/**
* Array of values currently stored in Configure.
*
* @var array
*/
protected static $_values = [];
/**
* Options
*
* @var array
*/
protected static $_options = [];
/**
* Holder for the model
*
* @var \Cake\ORM\Table
*/
protected static $_model = null;
/**
* Keeps the boolean if the autoload method has been loaded
*
* @var bool
*/
protected static $_autoloaded = false;
/**
* read
*
* Method to read the data.
*
* @param string $key Key with the name of the setting.
* @param string $type The type to return in.
* @return mixed
*/
public static function read($key = null, $type = null)
{
if (!self::_tableExists()) {
return;
}
self::autoLoad();
if (!$key) {
return self::$_data;
}
if (key_exists($key, self::$_data)) {
if ($type) {
$value = self::$_data[$key];
settype($value, $type);
return $value;
}
return self::$_data[$key];
}
$model = self::model();
$data = $model->findByName($key)->select('value');
if ($data->count() > 0) {
$data = $data->first()->toArray();
} else {
$data = $model->find()
->select(['name', 'value'])
->where(['name LIKE' => $key.'.%']);
if ($data->count() > 0) {
$data = $data->toArray();
foreach($data as $data_set)
{
if(self::_serialized($data_set->value)) {
$data_set->value = unserialize($data_set->value);
}
static::$_values = Hash::insert(static::$_values, $data_set->name, $data_set->value);
}
$data['value'] = static::$_values;
}
else {
return null;
}
}
if(self::_serialized($data['value'])) {
$data['value'] = unserialize($data['value']);
}
self::_store($key, $data['value']);
$value = $data['value'];
if ($type) {
settype($value, $type);
}
return $value;
}
/**
* write
*
* Method to write data to database.
*
* ### Example
*
* Setting::write('Plugin.Autoload', true);
*
* ### Options
* - editable value if the setting is editable in the admin-area. Default 1 (so, editable)
* - overrule boolean if the setting should be written if it already exists. Default true.
*
* Example:
* Setting::write('Plugin.Autoload', false, [
* 'overrule' => true,
* 'editable' => 0,
* ]
*
* @param string $key Key of the value. Must contain an prefix.
* @param mixed $value The value of the key.
* @param array $options Options array.
* @return void|bool
*/
public static function write($key, $value = null, $options = [])
{
if (!self::_tableExists()) {
return;
}
self::autoLoad();
$_options = [
'editable' => 1,
'overrule' => true,
];
$options = Hash::merge($_options, $options);
$model = self::model();
if(is_array($value) && !empty($value)) {
$value = serialize($value);
}
if (self::check($key)) {
if ($options['overrule']) {
$data = $model->findByName($key)->first();
if ($data) {
$data->set('value', $value);
$model->save($data);
} else {
return false;
}
} else {
return false;
}
} else {
$data = $model->newEntity($options);
$data->name = $key;
$data->value = $value;
$model->save($data);
}
self::_store($key, $value);
return true;
}
/**
* check
*
* Checks if an specific key exists.
* Returns boolean.
*
* @param string $key Key.
* @return bool|void
*/
public static function check($key)
{
if (!self::_tableExists()) {
return;
}
self::autoLoad();
$model = self::model();
if (key_exists($key, self::$_data)) {
return true;
}
$query = $model->findByName($key);
if (!$query->Count()) {
return false;
}
return true;
}
/**
* model
*
* Returns an instance of the Configurations-model (Table).
* Also used as setter for the instance of the model.
*
* @param \Cake\ORM\Table|null $model Model to use.
* @return \Cake\ORM\Table
*/
public static function model($model = null)
{
if ($model) {
self::$_model = $model;
}
if (!self::$_model) {
self::$_model = TableRegistry::get('Settings.Configurations');
}
return self::$_model;
}
/**
* register
*
* Registers a setting and its default values.
*
* @param string $key The key.
* @param mixed $value The default value.
* @param array $data Custom data.
* @return void
*/
public static function register($key, $value, $data = [])
{
if (!self::_tableExists()) {
return;
}
self::autoLoad();
if(is_array($value))
{
$value = seralize($value);
}
$_data = [
'value' => $value,
'editable' => 1,
'autoload' => true,
'options' => [],
'description' => null,
];
$data = array_merge($_data, $data);
// Don't overrule because we register
$data['overrule'] = false;
self::options($key, $data['options']);
self::write($key, $data['value'], $data);
}
/**
* options
*
* @param string $key Key for options.
* @param array $value Options to use.
* @return mixed
*/
public static function options($key, $value = null)
{
if (!self::_tableExists()) {
return;
}
if ($value) {
self::$_options[$key] = $value;
}
if (array_key_exists($key, self::$_options)) {
return self::$_options[$key];
} else {
return false;
}
}
/**
* autoLoad
*
* AutoLoad method.
* Loads all configurations who are autoloaded.
*
* @return void
*/
public static function autoLoad()
{
if (!self::_tableExists()) {
return;
}
if (self::$_autoloaded) {
return;
}
self::$_autoloaded = true;
$model = self::model();
$query = $model->find('all')->where(['autoload' => 1])->select(['name', 'value']);
foreach ($query as $configure) {
self::_store($configure->get('name'), $configure->get('value'));
}
}
/**
* clear
*
* Clears all settings out of the class. Settings
* won't be deleted from database.
*
* @param bool $reload Bool if settings should be reloaded
* @return void
*/
public static function clear($reload = false)
{
self::$_autoloaded = !$reload;
self::$_data = [];
}
/**
* _store
*
* Stores recent data in the $_data-variable.
*
* @param string $key The key.
* @param mixed $value The value.
* @return void
*/
protected static function _store($key, $value)
{
self::$_data[$key] = $value;
}
/**
* _tableExists
*
* @return bool
*/
protected static function _tableExists()
{
$db = ConnectionManager::get('default');
$tables = $db->schemaCollection()->listTables();
if (in_array('settings_configurations', $tables)) {
return true;
}
return false;
}
/**
* _serialized
*
* @return bool
*/
protected static function _serialized( $value, &$result = null ) {
if ( ! is_string( $value ) ) {
return false;
}
if ( 'b:0;' === $value ) {
$result = false;
return true;
}
$length = strlen($value);
$end = '';
if ( isset( $value[0] ) ) {
switch ($value[0]) {
case 's':
if ( '"' !== $value[$length - 2] )
return false;
case 'b':
case 'i':
case 'd':
$end .= ';';
case 'a':
case 'O':
$end .= '}';
if ( ':' !== $value[1] )
return false;
switch ( $value[2] ) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
break;
default:
return false;
}
case 'N':
$end .= ';';
if ( $value[$length - 1] !== $end[0] )
return false;
break;
default:
return false;
}
}
if ( ( $result = unserialize($value) ) === false ) {
$result = null;
return false;
}
return true;
}
}