1
|
|
|
<?php |
2
|
|
|
namespace App\Model\Table; |
3
|
|
|
|
4
|
|
|
use Cake\Core\Configure; |
5
|
|
|
use Cake\Event\Event; |
6
|
|
|
use Cake\ORM\Entity; |
7
|
|
|
use Cake\ORM\Table; |
8
|
|
|
|
9
|
|
|
class SettingsTable extends Table |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Initialize method. |
14
|
|
|
* |
15
|
|
|
* @param array $config The configuration for the Table. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function initialize(array $config) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
parent::initialize($config); |
22
|
|
|
|
23
|
|
|
$this->table('settings'); |
|
|
|
|
24
|
|
|
$this->displayField('name'); |
|
|
|
|
25
|
|
|
$this->primaryKey('id'); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$this->addBehavior('Timestamp'); |
28
|
|
|
|
29
|
|
|
$this->belongsTo('LastUpdatedUser', [ |
30
|
|
|
'className' => 'Users', |
31
|
|
|
'foreignKey' => 'last_updated_user_id' |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set the settings from the database. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function setSettings() |
41
|
|
|
{ |
42
|
|
|
$settings = $this->find() |
43
|
|
|
->select([ |
44
|
|
|
'id', |
45
|
|
|
'name', |
46
|
|
|
'value_int', |
47
|
|
|
'value_str', |
48
|
|
|
'value_bool' |
49
|
|
|
]) |
50
|
|
|
->cache('settings', 'database'); |
51
|
|
|
|
52
|
|
|
if (empty($settings)) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($settings as $setting) { |
57
|
|
|
Configure::write($setting->name, $setting->value); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Handle the beforeSave event. |
63
|
|
|
* |
64
|
|
|
* @param \Cake\Event\Event $event The beforeSave event that was fired. |
65
|
|
|
* @param \Cake\ORM\Entity $entity The entity that is going to be saved. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function beforeSave(Event $event, Entity $entity) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->_setValue($entity); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Assign the right value for each `value_*` fields regarding to the value. |
76
|
|
|
* |
77
|
|
|
* @param \Cake\ORM\Entity $entity The entity that is going to be saved. |
78
|
|
|
* |
79
|
|
|
* @return \Cake\ORM\Entity |
80
|
|
|
*/ |
81
|
|
|
protected function _setValue(Entity $entity) |
82
|
|
|
{ |
83
|
|
|
if (empty($entity->value_str)) { |
84
|
|
|
$entity->value_str = null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!is_numeric($entity->value_int)) { |
88
|
|
|
$entity->value_int = null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (is_null($entity->value_str) && is_null($entity->value_int) && $entity->value_bool == false) { |
92
|
|
|
$entity->value_bool = 0; |
93
|
|
|
} elseif ((!is_null($entity->value_str) || !is_null($entity->value_int)) && $entity->value_bool == false) { |
94
|
|
|
$entity->value_bool = null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $entity; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.