1 | <?php |
||
32 | class BaseSetting extends ActiveRecord implements SettingInterface |
||
33 | { |
||
34 | /** |
||
35 | * @inheritdoc |
||
36 | */ |
||
37 | 24 | public static function tableName() |
|
38 | { |
||
39 | 24 | return '{{%settings}}'; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * @inheritdoc |
||
44 | */ |
||
45 | 6 | public function rules() |
|
46 | { |
||
47 | return [ |
||
48 | 6 | [['value'], 'string'], |
|
49 | 6 | [['section', 'key'], 'string', 'max' => 255], |
|
50 | [ |
||
51 | 6 | ['key'], |
|
52 | 6 | 'unique', |
|
53 | 6 | 'targetAttribute' => ['section', 'key'], |
|
54 | 6 | ], |
|
55 | 6 | ['type', 'in', 'range' => ['string', 'integer', 'boolean', 'float', 'double', 'object', 'null']], |
|
56 | 6 | [['created', 'modified'], 'safe'], |
|
57 | 6 | [['active'], 'boolean'], |
|
58 | 6 | ]; |
|
59 | } |
||
60 | |||
61 | 22 | public function afterSave($insert, $changedAttributes) |
|
62 | { |
||
63 | 22 | parent::afterSave($insert, $changedAttributes); |
|
64 | 22 | Yii::$app->settings->clearCache(); |
|
65 | 22 | } |
|
66 | |||
67 | 4 | public function afterDelete() |
|
68 | { |
||
69 | 4 | parent::afterDelete(); |
|
70 | 4 | Yii::$app->settings->clearCache(); |
|
71 | 4 | } |
|
72 | |||
73 | /** |
||
74 | * @return array |
||
75 | */ |
||
76 | 24 | public function behaviors() |
|
77 | { |
||
78 | return [ |
||
79 | 'timestamp' => [ |
||
80 | 24 | 'class' => TimestampBehavior::className(), |
|
81 | 'attributes' => [ |
||
82 | 24 | ActiveRecord::EVENT_BEFORE_INSERT => 'created', |
|
83 | 24 | ActiveRecord::EVENT_BEFORE_UPDATE => 'modified', |
|
84 | 24 | ], |
|
85 | 24 | 'value' => new Expression('NOW()'), |
|
86 | 24 | ], |
|
87 | 24 | ]; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | 4 | public function getSettings() |
|
94 | { |
||
95 | 4 | $settings = static::find()->where(['active' => true])->asArray()->all(); |
|
96 | 4 | return array_merge_recursive( |
|
97 | 4 | ArrayHelper::map($settings, 'key', 'value', 'section'), |
|
98 | 4 | ArrayHelper::map($settings, 'key', 'type', 'section') |
|
99 | 4 | ); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * @inheritdoc |
||
104 | */ |
||
105 | 2 | public function setSetting($section, $key, $value, $type = null) |
|
106 | { |
||
107 | 2 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
108 | |||
109 | 2 | if ($model === null) { |
|
110 | 1 | $model = new static(); |
|
111 | 1 | $model->active = 1; |
|
112 | 1 | } |
|
113 | 2 | $model->section = $section; |
|
114 | 2 | $model->key = $key; |
|
115 | 2 | $model->value = strval($value); |
|
116 | |||
117 | 2 | if ($type !== null) { |
|
118 | 1 | $model->type = $type; |
|
119 | 1 | } else { |
|
120 | 1 | $t = gettype($value); |
|
121 | 1 | if ($t == 'string') { |
|
122 | 1 | $error = false; |
|
123 | try { |
||
124 | 1 | Json::decode($value); |
|
125 | 1 | } catch (InvalidParamException $e) { |
|
126 | 1 | $error = true; |
|
127 | } |
||
128 | 1 | if (!$error) { |
|
129 | $t = 'object'; |
||
130 | } |
||
131 | 1 | } |
|
132 | 1 | $model->type = $t; |
|
133 | } |
||
134 | |||
135 | 2 | return $model->save(); |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 6 | public function activateSetting($section, $key) |
|
142 | { |
||
143 | 6 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
144 | |||
145 | 6 | if ($model && $model->active == 0) { |
|
146 | 6 | $model->active = 1; |
|
147 | 6 | return $model->save(); |
|
148 | } |
||
149 | 1 | return false; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 3 | public function deactivateSetting($section, $key) |
|
156 | { |
||
157 | 3 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
158 | |||
159 | 3 | if ($model && $model->active == 1) { |
|
160 | 3 | $model->active = 0; |
|
161 | 3 | return $model->save(); |
|
162 | } |
||
163 | 1 | return false; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | 2 | public function deleteSetting($section, $key) |
|
170 | { |
||
171 | 2 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
172 | |||
173 | 2 | if ($model) { |
|
174 | 2 | return $model->delete(); |
|
|
|||
175 | } |
||
176 | return true; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @inheritdoc |
||
181 | */ |
||
182 | 2 | public function deleteAllSettings() |
|
183 | { |
||
184 | 2 | return static::deleteAll(); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @inheritdoc |
||
189 | */ |
||
190 | 1 | public function findSetting($key, $section = null) |
|
203 | } |
||
204 |