forked from dinhtrung/yii2-setting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setting.php
263 lines (239 loc) · 6.65 KB
/
Setting.php
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
<?php
namespace tomaszkane\setting;
/**
* Settings Model
*
*
* DATABASE STRUCTURE:
*
* CREATE TABLE IF NOT EXISTS `setting` (
* [[category]] varchar(64) NOT NULL default 'system',
* [[key]] varchar(255) NOT NULL,
* [[value]] text NOT NULL,
* PRIMARY KEY (`id`),
* KEY `category_key` ([[category]],[[key]])
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
*/
use Yii;
use yii\db\Schema;
use yii\base\Application;
class Setting extends \yii\base\Component {
public $settingTable = 'setting';
protected $_toBeSave = array ();
protected $_toBeDelete = array ();
protected $_catToBeDel = array ();
protected $_cacheFlush = array ();
protected $_items = array ();
/**
* Setting::init()
*
* @return
*
*/
public function init() {
if (YII_DEBUG && ! Yii::$app->getDb ()->getTableSchema ( "{{%" . $this->settingTable . "}}" )) {
Yii::$app->db->createCommand (
\Yii::$app->getDb()->getQueryBuilder()->createTable ( "{{%" . $this->settingTable . "}}", [
'category' => Schema::TYPE_STRING,
'key' => Schema::TYPE_STRING,
'value' => Schema::TYPE_TEXT,
]
)
)->execute ();
Yii::$app->db->createCommand (
Yii::$app->getDb ()->getQueryBuilder()->addPrimaryKey( 'category_key', "{{%" . $this->settingTable . "}}", 'category,key' )
)->execute ();
}
$this->on (Application::EVENT_AFTER_REQUEST, [$this, 'commit']);
}
/**
* Setting::set()
*
* @param string $category
* @param mixed $key
* @param string $value
* @param bool $toDatabase
* @return
*
*/
public function set($category = 'system', $key, $value = '', $toDatabase = true) {
if (is_array ( $key )) {
foreach ( $key as $k => $v )
$this->set ( $category, $k, $v, $toDatabase );
} else {
if ($toDatabase)
$this->_toBeSave [$category] [$key] = $value;
$this->_items [$category] [$key] = $value;
}
}
/**
* Setting::get()
*
* @param string $category
* @param string $key
* @param string $default
* @return
*
*/
public function get($category = 'system', $key = '', $default = '') {
if (! isset ( $this->_items [$category] ))
$this->load ( $category );
if (empty ( $key ) && empty ( $default ) && ! empty ( $category ))
return isset ( $this->_items [$category] ) ? $this->_items [$category] : null;
if (isset ( $this->_items [$category] [$key] ))
return $this->_items [$category] [$key];
return ! empty ( $default ) ? $default : null;
}
/**
* Setting::delete()
*
* @param string $category
* @param string $key
* @return
*
*/
public function delete($category = 'system', $key = '') {
if (! empty ( $category ) && empty ( $key )) {
$this->_catToBeDel [] = $category;
return;
}
if (is_array ( $key )) {
foreach ( $key as $k )
$this->delete ( $category, $k );
} else {
if (isset ( $this->_items [$category] [$key] )) {
unset ( $this->_items [$category] [$key] );
$this->_toBeDelete [$category] [] = $key;
}
}
}
/**
* Setting::load()
*
* @param mixed $category
* @return
*
*/
public function load($category) {
$items = Yii::$app->cache->get ( $category . '_setting' );
if (! $items) {
$result = Yii::$app->getDb()->createCommand (
"SELECT * FROM {{%" . $this->settingTable . "}} WHERE [[category]]=:cat", [
':cat' => $category,
])->queryAll();
if (empty ( $result )) {
$this->set ( $category, '{empty}', '{empty}', false );
return;
}
$items = array ();
foreach ( $result as $row )
$items [$row ['key']] = @unserialize ( $row ['value'] );
Yii::$app->cache->add ( $category . '_setting', $items );
}
$this->set ( $category, $items, '', false );
return $items;
}
/**
* Setting::toArray()
*
* @return
*
*/
public function toArray() {
return $this->_items;
}
/**
* Setting::addDbItem()
*
* @param string $category
* @param mixed $key
* @param mixed $value
* @return integer
*
*/
private function addDbItem($category = 'system', $key, $value) {
$result = Yii::$app->getDb()->createCommand(
"SELECT * FROM {{%" . $this->settingTable . "}} WHERE [[category]]=:cat AND [[key]]=:key LIMIT 1", [
':cat' => $category,
':key' => $key
] )->queryOne();
$_value = @serialize ( $value );
\Yii::info(\yii\helpers\VarDumper::dumpAsString($result));
if (! $result ) {
$command = Yii::$app->getDb()->createCommand (
"INSERT INTO {{%" . $this->settingTable . "}} ([[category]], [[key]], [[value]]) VALUES(:cat,:key,:value)",
[
':cat' => $category,
':key' => $key,
':value' => $_value
] )->execute();
} else {
$command = Yii::$app->getDb()->createCommand (
"UPDATE {{%" . $this->settingTable . "}} SET [[value]]=:value WHERE [[category]]=:cat AND [[key]]=:key",
[
':cat' => $category,
':key' => $key,
':value' => $_value
] )->execute();
}
return $command;
}
/**
* @return boolean
*
*/
public function commit() {
$success = true;
$this->_cacheFlush = array ();
if (count ( $this->_catToBeDel ) > 0) {
foreach ( $this->_catToBeDel as $catName ) {
$command = Yii::$app->getDb()->createCommand (
"DELETE FROM {{%" . $this->settingTable . "}} WHERE [[category]]=:cat", [
':cat' => $catName
] )->execute();
$this->_cacheFlush [] = $catName;
if (isset ( $this->_toBeDelete [$catName] ))
unset ( $this->_toBeDelete [$catName] );
if (isset ( $this->_toBeSave [$catName] ))
unset ( $this->_toBeSave [$catName] );
}
}
if (count ( $this->_toBeDelete ) > 0) {
foreach ( $this->_toBeDelete as $catName => $keys ) {
$params = array ();
$i = 0;
foreach ( $keys as $v ) {
if (isset ( $this->_toBeSave [$catName] [$v] ))
unset ( $this->_toBeSave [$catName] [$v] );
$params [':p' . $i] = $v;
++ $i;
}
$names = implode ( ',', array_keys ( $params ) );
$command = Yii::$app->getDb()->createCommand(
"DELETE FROM {{%" . $this->settingTable . "}} WHERE [[category]]=:cat AND [[key]] IN ($names)", [
':cat' => $catName
]);
foreach ( $params as $key => $value )
$command->bindParam ( $key, $value );
$command->execute ();
$this->_cacheFlush [] = $catName;
}
}
/** @FIXME: Switch to batch mode... **/
if (count ( $this->_toBeSave ) > 0) {
foreach ( $this->_toBeSave as $catName => $keyValues ) {
foreach ( $keyValues as $k => $v ) {
if (!$this->addDbItem($catName, $k, $v)) {
$success = false;
}
}
$this->_cacheFlush [] = $catName;
}
}
if (count ( $this->_cacheFlush ) > 0) {
foreach ( $this->_cacheFlush as $catName )
Yii::$app->cache->delete ( $catName . '_setting' );
}
return $success;
}
}