1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use yii\base\Model; |
6
|
|
|
use yii\db\Connection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This is the model class for table "settings". |
10
|
|
|
* |
11
|
|
|
* @property Connection $db |
12
|
|
|
*/ |
13
|
|
|
class Setting extends Model |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
public $initUserStatus; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $initUserRole; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
public $regBlock; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $tableName = 'settings'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Data base connection driver. |
37
|
|
|
* |
38
|
|
|
* @var Connection |
39
|
|
|
*/ |
40
|
|
|
private $db; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function rules() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
[ |
49
|
|
|
[ |
50
|
|
|
'initUserStatus', |
51
|
|
|
'initUserRole', |
52
|
|
|
'regBlock', |
53
|
|
|
], |
54
|
|
|
'required', |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
[ |
58
|
|
|
'initUserStatus', |
59
|
|
|
'regBlock', |
60
|
|
|
], |
61
|
|
|
'integer', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
[ |
65
|
|
|
'initUserRole', |
66
|
|
|
], |
67
|
|
|
'string', |
68
|
|
|
'max' => 64 |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function attributeLabels() |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
|
|
'initUserStatus' => 'User status after registration', |
80
|
|
|
'initUserRole' => 'User role after registration', |
81
|
|
|
'regBlock' => 'Block registration', |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set db driver. |
87
|
|
|
* |
88
|
|
|
* @param Connection $db |
89
|
|
|
*/ |
90
|
|
|
public function setDb(Connection $db): void |
91
|
|
|
{ |
92
|
|
|
$this->db = $db; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set table name. |
97
|
|
|
* |
98
|
|
|
* @param string $tableName |
99
|
|
|
*/ |
100
|
|
|
public function setTableName(string $tableName): void |
101
|
|
|
{ |
102
|
|
|
$this->tableName = $tableName; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns this object with field's values. |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function getSettings() |
111
|
|
|
{ |
112
|
|
|
$result = $this->db->createCommand('SELECT * FROM ' . $this->tableName) |
113
|
|
|
->queryOne(); |
114
|
|
|
|
115
|
|
|
$this->setAttributes($result, false); |
|
|
|
|
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Save settings data. |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function save(): bool |
126
|
|
|
{ |
127
|
|
|
if (!$this->validate()) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$count = (int)$this->db->createCommand('SELECT COUNT(*) FROM ' . $this->tableName)->queryScalar(); |
132
|
|
|
|
133
|
|
|
if ($count > 0) { |
134
|
|
|
$result = $this->db->createCommand('UPDATE ' . $this->tableName. ' SET ' . $this->queryUpdateAttributes()) |
135
|
|
|
->bindValues($this->attributesToBind()) |
136
|
|
|
->execute(); |
137
|
|
|
} else { |
138
|
|
|
$result = $this->db->createCommand('INSERT INTO ' . $this->tableName. ' ' . $this->queryInsertAttributes()) |
139
|
|
|
->bindValues($this->attributesToBind()) |
140
|
|
|
->execute(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!$result) { |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
private function attributesToBind(): array |
154
|
|
|
{ |
155
|
|
|
$attributesToBind = []; |
156
|
|
|
|
157
|
|
|
foreach ($this->getAttributes() as $name => $value) { |
158
|
|
|
$attributesToBind[':' . $name] = $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $attributesToBind; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
private function queryUpdateAttributes(): string |
168
|
|
|
{ |
169
|
|
|
$toUpdateArray = []; |
170
|
|
|
|
171
|
|
|
foreach ($this->getAttributes() as $name => $value) { |
172
|
|
|
$toUpdateArray[] = $name . ' = :' . $name; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return implode(', ', $toUpdateArray); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
private function queryInsertAttributes(): string |
182
|
|
|
{ |
183
|
|
|
$toInsertArrayNames = []; |
184
|
|
|
$toInsertArrayValues = []; |
185
|
|
|
|
186
|
|
|
foreach ($this->getAttributes() as $name => $value) { |
187
|
|
|
$toInsertArrayNames[] = $name; |
188
|
|
|
$toInsertArrayValues[] = ':' . $name; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return |
192
|
|
|
' (' . implode(', ', $toInsertArrayNames) . ') VALUES ' . |
193
|
|
|
' (' . implode(', ', $toInsertArrayValues) . ') '; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|