1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dominus77\maintenance\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
use yii\di\NotInstantiableException; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
use dominus77\maintenance\interfaces\StateFormInterface; |
10
|
|
|
use dominus77\maintenance\BackendMaintenance; |
11
|
|
|
use dominus77\maintenance\Maintenance; |
12
|
|
|
use Exception; |
13
|
|
|
use DateTime; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class FileStateForm |
17
|
|
|
* @package dominus77\maintenance\models |
18
|
|
|
* |
19
|
|
|
* @property string $dateTime |
20
|
|
|
* @property string $path |
21
|
|
|
* @property mixed $modeName |
22
|
|
|
* @property string $defaultTitle |
23
|
|
|
* @property string $defaultText |
24
|
|
|
* @property string|int $statusCode |
25
|
|
|
* @property int $timestamp |
26
|
|
|
*/ |
27
|
|
|
class FileStateForm extends BaseForm implements StateFormInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Select mode |
31
|
|
|
* @var int|string |
32
|
|
|
*/ |
33
|
|
|
public $mode; |
34
|
|
|
/** |
35
|
|
|
* Datetime |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $date; |
39
|
|
|
/** |
40
|
|
|
* Title |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $title; |
44
|
|
|
/** |
45
|
|
|
* Text |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $text; |
49
|
|
|
/** |
50
|
|
|
* Subscribe |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
public $subscribe = true; |
54
|
|
|
/** |
55
|
|
|
* CountDownWidget |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
public $countDown = true; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Path to file |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $_path; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws InvalidConfigException |
68
|
|
|
* @throws NotInstantiableException |
69
|
|
|
*/ |
70
|
|
|
public function init() |
71
|
|
|
{ |
72
|
|
|
parent::init(); |
73
|
|
|
$this->_path = $this->state->getFileStatePath(); |
74
|
|
|
$this->loadModel(); |
75
|
|
|
if ($this->mode === null) { |
76
|
|
|
$this->mode = $this->state->isEnabled() ? Maintenance::STATUS_CODE_MAINTENANCE : Maintenance::STATUS_CODE_OK; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function rules() |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
[['mode'], 'required'], |
88
|
|
|
[['mode'], 'number'], |
89
|
|
|
[['mode'], 'filter', 'filter' => [$this, 'integerTypeCast']], |
90
|
|
|
|
91
|
|
|
[['date'], 'string', 'min' => 19, 'max' => 19], |
92
|
|
|
['date', 'default', 'value' => function () { |
93
|
|
|
return $this->getDateTime(); |
94
|
|
|
}], |
95
|
|
|
[['date'], 'validateDateAttribute'], |
96
|
|
|
|
97
|
|
|
[['title', 'text'], 'string'], |
98
|
|
|
|
99
|
|
|
[['subscribe', 'countDown'], 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => false] |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Type Cast integer |
105
|
|
|
* |
106
|
|
|
* @param $value string|integer |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function integerTypeCast($value) |
110
|
|
|
{ |
111
|
|
|
return (int)$value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Validate date attribute |
116
|
|
|
* |
117
|
|
|
* @param $attribute |
118
|
|
|
* @throws InvalidConfigException |
119
|
|
|
*/ |
120
|
|
|
public function validateDateAttribute($attribute) |
121
|
|
|
{ |
122
|
|
|
if ($attribute && !$this->validDate($this->$attribute)) { |
123
|
|
|
$example = $this->getDateTime(); |
124
|
|
|
$this->addError($attribute, BackendMaintenance::t('app', 'Invalid date format. Use example: {:example}', [':example' => $example])); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Validate datetime |
130
|
|
|
* |
131
|
|
|
* @param $date |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function validDate($date) |
135
|
|
|
{ |
136
|
|
|
$date = $date ?: time(); |
137
|
|
|
$d = DateTime::createFromFormat($this->getDateFormat(), $date); |
138
|
|
|
return $d && $d->format($this->getDateFormat()) === $date; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function attributeLabels() |
146
|
|
|
{ |
147
|
|
|
return [ |
148
|
|
|
'mode' => BackendMaintenance::t('app', 'Mode'), |
149
|
|
|
'date' => BackendMaintenance::t('app', 'Date and Time'), |
150
|
|
|
'title' => BackendMaintenance::t('app', 'Title'), |
151
|
|
|
'text' => BackendMaintenance::t('app', 'Text'), |
152
|
|
|
'subscribe' => BackendMaintenance::t('app', 'Subscribe'), |
153
|
|
|
'countDown' => BackendMaintenance::t('app', 'Count Down'), |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Path to file |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getPath() |
162
|
|
|
{ |
163
|
|
|
return $this->_path; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set data model |
168
|
|
|
*/ |
169
|
|
|
public function loadModel() |
170
|
|
|
{ |
171
|
|
|
$stateArray = $this->prepareLoadModel($this->path); |
172
|
|
|
$this->setAttributes($stateArray); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getDefaultTitle() |
179
|
|
|
{ |
180
|
|
|
return $this->state->getDefaultTitle(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function getDefaultText() |
187
|
|
|
{ |
188
|
|
|
return $this->state->getDefaultContent(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Current Datetime |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
* @throws InvalidConfigException |
196
|
|
|
*/ |
197
|
|
|
public function getDateTime() |
198
|
|
|
{ |
199
|
|
|
return Yii::$app->formatter->asDatetime($this->getTimestamp(), 'php:' . $this->getDateFormat()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Mode name |
204
|
|
|
* |
205
|
|
|
* @return mixed |
206
|
|
|
*/ |
207
|
|
|
public function getModeName() |
208
|
|
|
{ |
209
|
|
|
return ArrayHelper::getValue(self::getModesArray(), $this->mode); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Modes |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public static function getModesArray() |
217
|
|
|
{ |
218
|
|
|
return [ |
219
|
|
|
Maintenance::STATUS_CODE_OK => BackendMaintenance::t('app', 'Mode normal'), |
220
|
|
|
Maintenance::STATUS_CODE_MAINTENANCE => BackendMaintenance::t('app', 'Mode maintenance'), |
221
|
|
|
]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Save this in file |
226
|
|
|
* |
227
|
|
|
* @return bool|int |
228
|
|
|
*/ |
229
|
|
|
public function save() |
230
|
|
|
{ |
231
|
|
|
$result = false; |
232
|
|
|
if ($this->mode === Maintenance::STATUS_CODE_MAINTENANCE) { |
233
|
|
|
$this->disable(); |
234
|
|
|
file_put_contents($this->path, |
235
|
|
|
$this->prepareSaveData()); |
236
|
|
|
chmod($this->path, 0765); |
237
|
|
|
$result = true; |
238
|
|
|
} |
239
|
|
|
if ($this->mode === Maintenance::STATUS_CODE_OK) { |
240
|
|
|
$model = new SubscribeForm(); |
241
|
|
|
$result = $model->send(); |
242
|
|
|
$this->disable(); |
243
|
|
|
} |
244
|
|
|
return $result; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Enable |
249
|
|
|
* |
250
|
|
|
* @return bool|mixed |
251
|
|
|
*/ |
252
|
|
|
public function enable() |
253
|
|
|
{ |
254
|
|
|
return $this->state->enable(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Disable |
259
|
|
|
* |
260
|
|
|
* @return bool|mixed |
261
|
|
|
*/ |
262
|
|
|
public function disable() |
263
|
|
|
{ |
264
|
|
|
return $this->state->disable(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return int |
269
|
|
|
* @throws Exception |
270
|
|
|
*/ |
271
|
|
|
public function getTimestamp() |
272
|
|
|
{ |
273
|
|
|
$date = new DateTime(date($this->getDateFormat())); |
274
|
|
|
if ($this->validDate($this->date)) { |
275
|
|
|
$date = new DateTime($this->date); |
276
|
|
|
} |
277
|
|
|
return $date->getTimestamp(); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
|
|
public function isTimer() |
284
|
|
|
{ |
285
|
|
|
return (bool)$this->countDown; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function isSubscribe() |
292
|
|
|
{ |
293
|
|
|
return (bool)$this->subscribe; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Return true is enable maintenance mode |
298
|
|
|
* |
299
|
|
|
* @return bool |
300
|
|
|
*/ |
301
|
|
|
public function isEnabled() |
302
|
|
|
{ |
303
|
|
|
return $this->state->isEnabled() && ($this->mode !== Maintenance::STATUS_CODE_OK); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* StatusCode |
308
|
|
|
* |
309
|
|
|
* @return int|string |
310
|
|
|
*/ |
311
|
|
|
public function getStatusCode() |
312
|
|
|
{ |
313
|
|
|
return $this->mode; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|