-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSetting.php
More file actions
351 lines (300 loc) · 7.7 KB
/
Setting.php
File metadata and controls
351 lines (300 loc) · 7.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
<?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 = [];
/**
* 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 {
return null;
}
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 (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();
$_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->getSchemaCollection()->listTables();
if (in_array('settings_configurations', $tables)) {
return true;
}
return false;
}
}