1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace carono\yii2crud; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use carono\yii2crud\actions\CreateAction; |
8
|
|
|
use carono\yii2crud\actions\DeleteAction; |
9
|
|
|
use carono\yii2crud\actions\DeleteBatch; |
10
|
|
|
use carono\yii2crud\actions\IndexAction; |
11
|
|
|
use carono\yii2crud\actions\UpdateAction; |
12
|
|
|
use carono\yii2helpers\QueryHelper; |
13
|
|
|
use Yii; |
14
|
|
|
use yii\data\ActiveDataProvider; |
15
|
|
|
use yii\data\BaseDataProvider; |
16
|
|
|
use yii\db\ActiveQuery; |
17
|
|
|
use yii\db\ActiveRecord; |
18
|
|
|
use yii\filters\VerbFilter; |
19
|
|
|
use yii\web\Controller; |
20
|
|
|
use yii\web\NotFoundHttpException; |
21
|
|
|
|
22
|
|
|
abstract class CrudController extends Controller |
23
|
|
|
{ |
24
|
|
|
public const SCENARIO_CREATE = 'create'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ActiveRecord |
28
|
|
|
*/ |
29
|
|
|
public $modelClass; |
30
|
|
|
/** |
31
|
|
|
* @var ActiveRecord |
32
|
|
|
*/ |
33
|
|
|
public $modelSearchClass; |
34
|
|
|
|
35
|
|
|
public $createClass; |
36
|
|
|
|
37
|
|
|
public $updateClass; |
38
|
|
|
|
39
|
|
|
public $updateView = 'update'; |
40
|
|
|
public $indexView = 'index'; |
41
|
|
|
public $viewView = 'view'; |
42
|
|
|
public $createView = 'create'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ActiveRecord|string $class |
46
|
|
|
* @return ActiveQuery |
47
|
|
|
*/ |
48
|
|
|
public function getModelQuery($class) |
49
|
|
|
{ |
50
|
|
|
return $class::find(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $id |
55
|
|
|
* @param null $class |
|
|
|
|
56
|
|
|
* @return ActiveRecord |
57
|
|
|
* @throws NotFoundHttpException |
58
|
|
|
*/ |
59
|
|
|
public function findModel($id, $class = null): ActiveRecord |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* @var ActiveRecord $class |
63
|
|
|
*/ |
64
|
|
|
$class = $class ?? $this->modelClass; |
65
|
|
|
$query = $this->getModelQuery($class)->andWhere(['id' => (int)$id]); |
66
|
|
|
$this->findModelCondition($query); |
67
|
|
|
if (($model = $query->one()) !== null) { |
68
|
|
|
return $model; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ActiveQuery $query |
75
|
|
|
* @return ActiveDataProvider |
76
|
|
|
*/ |
77
|
|
|
public function queryToDataProvider($query): ActiveDataProvider |
78
|
|
|
{ |
79
|
|
|
/** |
80
|
|
|
* @var ActiveRecord $modelClass |
81
|
|
|
*/ |
82
|
|
|
$modelClass = $query->modelClass; |
83
|
|
|
$table = $modelClass::tableName(); |
84
|
|
|
$options = [ |
85
|
|
|
'sort' => [ |
86
|
|
|
'defaultOrder' => [ |
87
|
|
|
current(Yii::$app->db->getTableSchema($table)->primaryKey) => SORT_ASC |
88
|
|
|
] |
89
|
|
|
], |
90
|
|
|
]; |
91
|
|
|
return new ActiveDataProvider(array_merge(['query' => $query], $options)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function behaviors() |
98
|
|
|
{ |
99
|
|
|
return array_merge(parent::behaviors(), [ |
100
|
|
|
'verbs' => [ |
101
|
|
|
'class' => VerbFilter::class, |
102
|
|
|
'actions' => [ |
103
|
|
|
'delete' => ['POST'], |
104
|
|
|
'delete-batch' => ['POST'] |
105
|
|
|
], |
106
|
|
|
], |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function applySearch(ActiveQuery $query, BaseDataProvider $dataProvider, $searchModel): void |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
QueryHelper::regular($searchModel, $query); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param ActiveQuery $query |
117
|
|
|
* @return ActiveQuery |
118
|
|
|
*/ |
119
|
|
|
public function findModelCondition($query): ActiveQuery |
120
|
|
|
{ |
121
|
|
|
return $query; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ActiveQuery $query |
126
|
|
|
* @return ActiveQuery |
127
|
|
|
*/ |
128
|
|
|
public function indexCondition($query): ActiveQuery |
129
|
|
|
{ |
130
|
|
|
return $query; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $params |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function indexParams($params): array |
138
|
|
|
{ |
139
|
|
|
return $params; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $id |
144
|
|
|
* @return mixed|string |
145
|
|
|
*/ |
146
|
|
|
public function actionView($id) |
147
|
|
|
{ |
148
|
|
|
if (Yii::$app->request->isPost) { |
149
|
|
|
return $this->runAction('update', ['id' => $this->id]); |
150
|
|
|
} |
151
|
|
|
return $this->render('view', [ |
152
|
|
|
'model' => $this->findModel($id), |
153
|
|
|
]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $model |
158
|
|
|
*/ |
159
|
|
|
public function beforeCreate($model): void |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $model |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
|
|
public function createRedirect($model): array |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
return ['index']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $model |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
public function updateRedirect($model): array |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
return ['index']; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param $model |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function deleteRedirect($model): array |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
return ['index']; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
public function actions() |
194
|
|
|
{ |
195
|
|
|
return [ |
196
|
|
|
'update' => [ |
197
|
|
|
'class' => UpdateAction::class, |
198
|
|
|
], |
199
|
|
|
'index' => [ |
200
|
|
|
'class' => IndexAction::class |
201
|
|
|
], |
202
|
|
|
'create' => [ |
203
|
|
|
'class' => CreateAction::class |
204
|
|
|
], |
205
|
|
|
'delete' => [ |
206
|
|
|
'class' => DeleteAction::class |
207
|
|
|
], |
208
|
|
|
'delete-batch' => [ |
209
|
|
|
'class' => DeleteBatch::class |
210
|
|
|
] |
211
|
|
|
]; |
212
|
|
|
} |
213
|
|
|
} |