1 | <?php |
||
2 | |||
3 | namespace dominus77\maintenance\actions\backend; |
||
4 | |||
5 | use Yii; |
||
6 | use yii\base\Action; |
||
7 | use yii\web\Response; |
||
8 | use dominus77\maintenance\models\FileStateForm; |
||
9 | use dominus77\maintenance\models\SubscribeForm; |
||
10 | use dominus77\maintenance\BackendMaintenance; |
||
11 | use yii\web\Session; |
||
12 | |||
13 | /** |
||
14 | * Class IndexAction |
||
15 | * @package dominus77\maintenance\actions |
||
16 | * |
||
17 | * @property array $viewRenderParams |
||
18 | */ |
||
19 | class IndexAction extends Action |
||
20 | { |
||
21 | /** @var string */ |
||
22 | public $defaultName; |
||
23 | |||
24 | /** @var string */ |
||
25 | public $layout; |
||
26 | |||
27 | /** @var string */ |
||
28 | public $viewPath; |
||
29 | |||
30 | /** @var string */ |
||
31 | public $view; |
||
32 | |||
33 | /** @var array */ |
||
34 | public $params = []; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function init() |
||
40 | { |
||
41 | parent::init(); |
||
42 | |||
43 | if ($this->defaultName === null) { |
||
44 | $this->defaultName = BackendMaintenance::t('app', 'Mode site'); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return string|Response |
||
50 | */ |
||
51 | public function run() |
||
52 | { |
||
53 | $this->setViewPath(); |
||
54 | $model = new FileStateForm(); |
||
55 | if (($post = Yii::$app->request->post()) && $model->load($post) && $model->validate()) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | $message = $model->isEnabled() ? BackendMaintenance::t('app', 'Maintenance mode successfully updated!') : ''; |
||
57 | $result = $model->save(); |
||
58 | $this->setMessage($message, $result); |
||
59 | return $this->controller->refresh(); |
||
60 | } |
||
61 | return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams($model)); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param $message string |
||
66 | * @param $result bool|int |
||
67 | */ |
||
68 | protected function setMessage($message, $result) |
||
69 | { |
||
70 | /** @var Session $session */ |
||
71 | $session = Yii::$app->session; |
||
72 | if (is_bool($result) && $result === true && $message) { |
||
73 | $session->setFlash('success', $message); |
||
74 | } |
||
75 | if (is_numeric($result)) { |
||
76 | $session->setFlash('success', BackendMaintenance::t('app', |
||
77 | '{n, plural, =0{no followers} =1{one message sent} other{# messages sent}}', |
||
78 | ['n' => $result]) |
||
79 | ); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param $model FileStateForm |
||
85 | * @return array |
||
86 | */ |
||
87 | protected function getViewRenderParams($model) |
||
88 | { |
||
89 | $subscribeModel = new SubscribeForm(); |
||
90 | return [ |
||
91 | 'name' => $this->defaultName, |
||
92 | 'model' => $model, |
||
93 | 'dataProvider' => $subscribeModel->getDataProvider() |
||
94 | ]; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Set View Path |
||
99 | */ |
||
100 | protected function setViewPath() |
||
101 | { |
||
102 | if ($this->viewPath !== null) { |
||
103 | $this->controller->setViewPath($this->viewPath); |
||
104 | } else { |
||
105 | $this->controller->setViewPath('@dominus77/maintenance/views/backend/maintenance'); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 |