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