1 | <?php |
||
31 | class BaseSetting extends ActiveRecord implements SettingInterface |
||
32 | { |
||
33 | /** |
||
34 | * @inheritdoc |
||
35 | */ |
||
36 | 36 | public static function tableName() |
|
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 18 | public function rules() |
|
45 | { |
||
46 | return [ |
||
47 | 18 | [['value'], 'string'], |
|
48 | [['section', 'key'], 'string', 'max' => 255], |
||
49 | [ |
||
50 | ['key'], |
||
51 | 'unique', |
||
52 | 'targetAttribute' => ['section', 'key'], |
||
53 | ], |
||
54 | ['type', 'in', 'range' => ['string', 'integer', 'boolean', 'float', 'double', 'object', 'null', 'ip', 'email', 'url']], |
||
55 | [['created', 'modified'], 'safe'], |
||
56 | [['active'], 'boolean'], |
||
57 | ]; |
||
58 | } |
||
59 | |||
60 | 34 | public function afterSave($insert, $changedAttributes) |
|
65 | |||
66 | 4 | public function afterDelete() |
|
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | 36 | public function behaviors() |
|
76 | { |
||
77 | return [ |
||
78 | 'timestamp' => [ |
||
79 | 36 | 'class' => TimestampBehavior::className(), |
|
|
|||
80 | 'attributes' => [ |
||
81 | 36 | ActiveRecord::EVENT_BEFORE_INSERT => 'created', |
|
82 | 36 | ActiveRecord::EVENT_BEFORE_UPDATE => 'modified', |
|
83 | ], |
||
84 | 36 | 'value' => date('Y-m-d H:i:s'),//new Expression('NOW()'), |
|
85 | ], |
||
86 | ]; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | 7 | public function getSettings() |
|
93 | { |
||
94 | 7 | $settings = static::find()->where(['active' => true])->asArray()->all(); |
|
95 | 7 | return array_merge_recursive( |
|
96 | 7 | ArrayHelper::map($settings, 'key', 'value', 'section'), |
|
97 | 7 | ArrayHelper::map($settings, 'key', 'type', 'section') |
|
98 | ); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @inheritdoc |
||
103 | */ |
||
104 | 14 | public function setSetting($section, $key, $value, $type = null) |
|
105 | { |
||
106 | 14 | $model = static::findOne(['section' => $section, 'key' => $key]); |
|
107 | |||
108 | 14 | if ($model === null) { |
|
109 | 13 | $model = new static(); |
|
110 | 13 | $model->active = 1; |
|
111 | } |
||
112 | 14 | $model->section = $section; |
|
113 | 14 | $model->key = $key; |
|
114 | 14 | $model->value = strval($value); |
|
115 | |||
116 | 14 | if ($type !== null) { |
|
117 | 2 | $model->type = $type; |
|
118 | 12 | } elseif ( ! isset($model->type) ) { |
|
119 | 11 | $type = $this->getValueType($value); |
|
120 | 11 | $model->type = $type; |
|
121 | } |
||
122 | |||
123 | 14 | return $model->save(); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * @inheritdoc |
||
128 | */ |
||
129 | 6 | public function activateSetting($section, $key) |
|
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | 4 | public function deactivateSetting($section, $key) |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 2 | public function deleteSetting($section, $key) |
|
165 | |||
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | 2 | public function deleteAllSettings() |
|
173 | |||
174 | /** |
||
175 | * @inheritdoc |
||
176 | */ |
||
177 | 2 | public function findSetting($key, $section = null) |
|
190 | |||
191 | /** |
||
192 | * @param $value |
||
193 | * @return string|void |
||
194 | */ |
||
195 | 11 | protected function getValueType($value) |
|
237 | } |
||
238 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.