|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yii2mod\settings\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\behaviors\TimestampBehavior; |
|
7
|
|
|
use yii\db\ActiveRecord; |
|
8
|
|
|
use yii\helpers\ArrayHelper; |
|
9
|
|
|
use yii2mod\settings\models\enumerables\SettingStatus; |
|
10
|
|
|
use yii2mod\settings\models\enumerables\SettingType; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This is the model class for table "{{%setting}}". |
|
14
|
|
|
* |
|
15
|
|
|
* @property int $id |
|
16
|
|
|
* @property string $type |
|
17
|
|
|
* @property string $section |
|
18
|
|
|
* @property string $key |
|
19
|
|
|
* @property string $value |
|
20
|
|
|
* @property bool $status |
|
21
|
|
|
* @property string $description |
|
22
|
|
|
* @property string $created_at |
|
23
|
|
|
* @property string $updated_at |
|
24
|
|
|
*/ |
|
25
|
|
|
class SettingModel extends ActiveRecord |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function tableName(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return '{{%setting}}'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
*/ |
|
38
|
|
|
public function rules(): array |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
[['section', 'key', 'value'], 'required'], |
|
42
|
|
|
[['section', 'key'], 'unique', 'targetAttribute' => ['section', 'key']], |
|
43
|
|
|
[['value', 'type'], 'string'], |
|
44
|
|
|
[['section', 'key', 'description'], 'string', 'max' => 255], |
|
45
|
|
|
[['status'], 'integer'], |
|
46
|
|
|
['status', 'default', 'value' => SettingStatus::ACTIVE], |
|
47
|
|
|
['status', 'in', 'range' => SettingStatus::getConstantsByName()], |
|
48
|
|
|
[['type'], 'safe'], |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function attributeLabels(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
'id' => Yii::t('yii2mod.settings', 'ID'), |
|
59
|
|
|
'type' => Yii::t('yii2mod.settings', 'Type'), |
|
60
|
|
|
'section' => Yii::t('yii2mod.settings', 'Section'), |
|
61
|
|
|
'key' => Yii::t('yii2mod.settings', 'Key'), |
|
62
|
|
|
'value' => Yii::t('yii2mod.settings', 'Value'), |
|
63
|
|
|
'status' => Yii::t('yii2mod.settings', 'Status'), |
|
64
|
|
|
'description' => Yii::t('yii2mod.settings', 'Description'), |
|
65
|
|
|
'created_at' => Yii::t('yii2mod.settings', 'Created Date'), |
|
66
|
|
|
'updated_at' => Yii::t('yii2mod.settings', 'Updated Date'), |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
*/ |
|
73
|
|
|
public function behaviors(): array |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
TimestampBehavior::class, |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Creates an [[ActiveQueryInterface]] instance for query purpose. |
|
82
|
|
|
* |
|
83
|
|
|
* @return SettingQuery |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function find(): SettingQuery |
|
86
|
|
|
{ |
|
87
|
|
|
return new SettingQuery(get_called_class()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @inheritdoc |
|
92
|
|
|
*/ |
|
93
|
|
|
public function afterDelete() |
|
94
|
|
|
{ |
|
95
|
|
|
parent::afterDelete(); |
|
96
|
|
|
|
|
97
|
|
|
Yii::$app->settings->invalidateCache(); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritdoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function afterSave($insert, $changedAttributes) |
|
104
|
|
|
{ |
|
105
|
|
|
parent::afterSave($insert, $changedAttributes); |
|
106
|
|
|
|
|
107
|
|
|
Yii::$app->settings->invalidateCache(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return array of settings |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getSettings(): array |
|
116
|
|
|
{ |
|
117
|
|
|
$result = []; |
|
118
|
|
|
$settings = static::find()->select(['type', 'section', 'key', 'value'])->active()->asArray()->all(); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($settings as $setting) { |
|
121
|
|
|
$section = $setting['section']; |
|
122
|
|
|
$key = $setting['key']; |
|
123
|
|
|
$settingOptions = ['type' => $setting['type'], 'value' => $setting['value']]; |
|
124
|
|
|
|
|
125
|
|
|
if (isset($result[$section][$key])) { |
|
126
|
|
|
ArrayHelper::merge($result[$section][$key], $settingOptions); |
|
127
|
|
|
} else { |
|
128
|
|
|
$result[$section][$key] = $settingOptions; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $result; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Set setting |
|
137
|
|
|
* |
|
138
|
|
|
* @param $section |
|
139
|
|
|
* @param $key |
|
140
|
|
|
* @param $value |
|
141
|
|
|
* @param null $type |
|
|
|
|
|
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setSetting($section, $key, $value, $type = null): bool |
|
146
|
|
|
{ |
|
147
|
|
|
$model = static::findOne(['section' => $section, 'key' => $key]); |
|
148
|
|
|
|
|
149
|
|
|
if (empty($model)) { |
|
150
|
|
|
$model = new static(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$model->section = $section; |
|
154
|
|
|
$model->key = $key; |
|
155
|
|
|
$model->value = strval($value); |
|
156
|
|
|
|
|
157
|
|
|
if ($type !== null && ArrayHelper::keyExists($type, SettingType::getConstantsByValue())) { |
|
|
|
|
|
|
158
|
|
|
$model->type = $type; |
|
159
|
|
|
} else { |
|
160
|
|
|
$model->type = gettype($value); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $model->save(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Remove setting |
|
168
|
|
|
* |
|
169
|
|
|
* @param $section |
|
170
|
|
|
* @param $key |
|
171
|
|
|
* |
|
172
|
|
|
* @return bool|int|null |
|
173
|
|
|
* |
|
174
|
|
|
* @throws \Exception |
|
175
|
|
|
*/ |
|
176
|
|
|
public function removeSetting($section, $key) |
|
177
|
|
|
{ |
|
178
|
|
|
$model = static::findOne(['section' => $section, 'key' => $key]); |
|
179
|
|
|
|
|
180
|
|
|
if (!empty($model)) { |
|
181
|
|
|
return $model->delete(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return false; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Remove all settings |
|
189
|
|
|
* |
|
190
|
|
|
* @return int |
|
191
|
|
|
*/ |
|
192
|
|
|
public function removeAllSettings(): int |
|
193
|
|
|
{ |
|
194
|
|
|
return static::deleteAll(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Activates a setting |
|
199
|
|
|
* |
|
200
|
|
|
* @param $section |
|
201
|
|
|
* @param $key |
|
202
|
|
|
* |
|
203
|
|
|
* @return bool |
|
204
|
|
|
*/ |
|
205
|
|
|
public function activateSetting($section, $key): bool |
|
206
|
|
|
{ |
|
207
|
|
|
$model = static::findOne(['section' => $section, 'key' => $key]); |
|
208
|
|
|
|
|
209
|
|
|
if ($model && $model->status === SettingStatus::INACTIVE) { |
|
210
|
|
|
$model->status = SettingStatus::ACTIVE; |
|
211
|
|
|
|
|
212
|
|
|
return $model->save(true, ['status']); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return false; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Deactivates a setting |
|
220
|
|
|
* |
|
221
|
|
|
* @param $section |
|
222
|
|
|
* @param $key |
|
223
|
|
|
* |
|
224
|
|
|
* @return bool |
|
225
|
|
|
*/ |
|
226
|
|
|
public function deactivateSetting($section, $key): bool |
|
227
|
|
|
{ |
|
228
|
|
|
$model = static::findOne(['section' => $section, 'key' => $key]); |
|
229
|
|
|
|
|
230
|
|
|
if ($model && $model->status === SettingStatus::ACTIVE) { |
|
231
|
|
|
$model->status = SettingStatus::INACTIVE; |
|
232
|
|
|
|
|
233
|
|
|
return $model->save(true, ['status']); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return false; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.