|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\backgroundtasks\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use app\backgroundtasks\models\Task; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\filters\AccessControl; |
|
8
|
|
|
use yii\filters\VerbFilter; |
|
9
|
|
|
use yii\web\Controller; |
|
10
|
|
|
use yii\web\NotFoundHttpException; |
|
11
|
|
|
use yii\helpers\Url; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ManageController |
|
15
|
|
|
* @package app\backgroundtasks\controllers |
|
16
|
|
|
* @author evgen-d <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ManageController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
public function behaviors() |
|
22
|
|
|
{ |
|
23
|
|
|
return [ |
|
24
|
|
|
'access' => [ |
|
25
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
|
|
26
|
|
|
'rules' => [ |
|
27
|
|
|
[ |
|
28
|
|
|
'allow' => true, |
|
29
|
|
|
'roles' => Yii::$app->getModule('background')->manageRoles, |
|
30
|
|
|
], |
|
31
|
|
|
], |
|
32
|
|
|
], |
|
33
|
|
|
'verbs' => [ |
|
34
|
|
|
'class' => VerbFilter::className(), |
|
|
|
|
|
|
35
|
|
|
'actions' => [ |
|
36
|
|
|
], |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* View all Task models. |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function actionIndex() |
|
46
|
|
|
{ |
|
47
|
|
|
$searchModel = new Task(['scenario' => 'search']); |
|
48
|
|
|
$dataProvider = $searchModel->search($_GET); |
|
49
|
|
|
|
|
50
|
|
|
return $this->render( |
|
51
|
|
|
'index', |
|
52
|
|
|
[ |
|
53
|
|
|
'dataProvider' => $dataProvider, |
|
54
|
|
|
'searchModel' => $searchModel, |
|
55
|
|
|
] |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Updates an existing Task model. |
|
61
|
|
|
* If update is successful, the browser will be redirected to the 'index' page. |
|
62
|
|
|
* @param $id |
|
63
|
|
|
* @return string |
|
|
|
|
|
|
64
|
|
|
* @throws \yii\web\NotFoundHttpException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function actionUpdate($id) |
|
67
|
|
|
{ |
|
68
|
|
|
/* @var $model Task|null */ |
|
69
|
|
|
$model = Task::find()->where( |
|
70
|
|
|
[ |
|
71
|
|
|
'id' => $id, |
|
72
|
|
|
'type' => Task::TYPE_REPEAT, |
|
73
|
|
|
] |
|
74
|
|
|
)->one(); |
|
75
|
|
|
if ($model !== null) { |
|
76
|
|
|
$model->scenario = 'repeat'; |
|
77
|
|
|
if ($model->load($_POST) && $model->validate()) { |
|
78
|
|
|
$model->fail_counter = 0; |
|
79
|
|
|
if ($model->save()) { |
|
80
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
|
81
|
|
|
$returnUrl = Yii::$app->request->get('returnUrl', ['background/manage/index']); |
|
82
|
|
|
switch (Yii::$app->request->post('action', 'save')) { |
|
83
|
|
|
case 'next': |
|
84
|
|
|
return $this->redirect( |
|
85
|
|
|
[ |
|
86
|
|
|
'/background/manage/create', |
|
87
|
|
|
'returnUrl' => $returnUrl, |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
case 'back': |
|
91
|
|
|
return $this->redirect($returnUrl); |
|
92
|
|
|
default: |
|
93
|
|
|
return $this->redirect( |
|
94
|
|
|
Url::toRoute( |
|
95
|
|
|
[ |
|
96
|
|
|
'/background/manage/update', |
|
97
|
|
|
'id' => $model->id, |
|
98
|
|
|
'returnUrl' => $returnUrl, |
|
99
|
|
|
] |
|
100
|
|
|
) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} else { |
|
106
|
|
|
return $this->render( |
|
107
|
|
|
'update', |
|
108
|
|
|
[ |
|
109
|
|
|
'model' => $model, |
|
110
|
|
|
] |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} else { |
|
114
|
|
|
throw new NotFoundHttpException('repeated task #'.$id.' not found'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Creates a new Task model. |
|
120
|
|
|
* If creation is successful, the browser will be redirected to the 'index' page. |
|
121
|
|
|
* @return string|\yii\web\Response |
|
122
|
|
|
*/ |
|
123
|
|
|
public function actionCreate() |
|
124
|
|
|
{ |
|
125
|
|
|
$model = new Task(['scenario' => 'repeat']); |
|
126
|
|
|
$model->initiator = Yii::$app->user->id; |
|
127
|
|
|
$model->type = Task::TYPE_REPEAT; |
|
128
|
|
|
|
|
129
|
|
|
if ($model->load($_POST) && $model->save()) { |
|
130
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved')); |
|
131
|
|
|
$returnUrl = Yii::$app->request->get('returnUrl', ['background/manage/index']); |
|
132
|
|
|
switch (Yii::$app->request->post('action', 'save')) { |
|
133
|
|
|
case 'next': |
|
134
|
|
|
return $this->redirect( |
|
135
|
|
|
[ |
|
136
|
|
|
'/background/manage/create', |
|
137
|
|
|
'returnUrl' => $returnUrl, |
|
138
|
|
|
] |
|
139
|
|
|
); |
|
140
|
|
|
case 'back': |
|
141
|
|
|
return $this->redirect($returnUrl); |
|
142
|
|
|
default: |
|
143
|
|
|
return $this->redirect( |
|
144
|
|
|
Url::toRoute( |
|
145
|
|
|
[ |
|
146
|
|
|
'/background/manage/update', |
|
147
|
|
|
'id' => $model->id, |
|
148
|
|
|
'returnUrl' => $returnUrl, |
|
149
|
|
|
] |
|
150
|
|
|
) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} else { |
|
155
|
|
|
return $this->render( |
|
156
|
|
|
'create', |
|
157
|
|
|
[ |
|
158
|
|
|
'model' => $model, |
|
159
|
|
|
] |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Deletes an existing Task model. |
|
166
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
|
167
|
|
|
* @param $id |
|
168
|
|
|
* @return \yii\web\Response |
|
169
|
|
|
*/ |
|
170
|
|
|
public function actionDelete($id) |
|
171
|
|
|
{ |
|
172
|
|
|
Task::deleteAll('id = :id', [':id' => $id]); |
|
173
|
|
|
return $this->redirect(['index']); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Deletes an existing Task models. |
|
178
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
|
179
|
|
|
* |
|
180
|
|
|
* @return int|false number of tasks deleted or false if there was no task provided for deletion |
|
181
|
|
|
*/ |
|
182
|
|
|
public function actionDeleteTasks() |
|
183
|
|
|
{ |
|
184
|
|
|
if (isset($_POST['tasks'])) { |
|
185
|
|
|
return Task::deleteAll([ |
|
186
|
|
|
'in', |
|
187
|
|
|
'id', |
|
188
|
|
|
$_POST['tasks'], |
|
189
|
|
|
]); |
|
190
|
|
|
} |
|
191
|
|
|
return false; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.