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