1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\MFUploader\controllers\album; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use yii\data\Pagination; |
8
|
|
|
use yii\base\UnknownMethodException; |
9
|
|
|
use yii\web\{Controller, BadRequestHttpException, NotFoundHttpException}; |
10
|
|
|
use yii\filters\{VerbFilter, AccessControl}; |
11
|
|
|
use Itstructure\MFUploader\Module; |
12
|
|
|
use Itstructure\MFUploader\models\album\{Album, AlbumSearch}; |
13
|
|
|
use Itstructure\MFUploader\traits\MediaFilesTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* AlbumController implements the CRUD actions for Album model. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AlbumController implements the CRUD actions for Album model. |
21
|
|
|
* |
22
|
|
|
* @property Album $model Model object record. |
23
|
|
|
* @property Module $module |
24
|
|
|
* @property string $urlPrefix Url prefix for redirect and view links. |
25
|
|
|
* @property string $urlPrefixNeighbor Url prefix for redirect and view links of neighbor entity. |
26
|
|
|
* |
27
|
|
|
* @package Itstructure\MFUploader\controllers\album |
28
|
|
|
* |
29
|
|
|
* @author Andrey Girnik <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
abstract class AlbumController extends Controller |
32
|
|
|
{ |
33
|
|
|
use MediaFilesTrait; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Url prefix for redirect and view links. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $urlPrefix = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Url prefix for redirect and view links of neighbor entity. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $urlPrefixNeighbor = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Model object record. |
51
|
|
|
* |
52
|
|
|
* @var Album |
53
|
|
|
*/ |
54
|
|
|
private $model; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the name of the base model. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
abstract protected function getModelName():string; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the type of album. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
abstract protected function getAlbumType():string; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initializer. |
72
|
|
|
*/ |
73
|
|
|
public function init() |
74
|
|
|
{ |
75
|
|
|
$this->view->params['user'] = \Yii::$app->user->identity; |
76
|
|
|
|
77
|
|
|
$this->viewPath = '@'.Module::MODULE_NAME.'/views/albums'; |
78
|
|
|
|
79
|
|
|
parent::init(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param \yii\base\Action $action |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function beforeAction($action) |
88
|
|
|
{ |
89
|
|
|
$this->view->params['urlPrefix'] = $this->urlPrefix; |
90
|
|
|
$this->view->params['urlPrefixNeighbor'] = $this->urlPrefixNeighbor; |
91
|
|
|
|
92
|
|
|
return parent::beforeAction($action); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function behaviors() |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
'access' => [ |
102
|
|
|
'class' => AccessControl::class, |
103
|
|
|
'rules' => [ |
104
|
|
|
[ |
105
|
|
|
'allow' => true, |
106
|
|
|
'roles' => $this->module->accessRoles, |
107
|
|
|
], |
108
|
|
|
], |
109
|
|
|
], |
110
|
|
|
'verbs' => [ |
111
|
|
|
'class' => VerbFilter::class, |
112
|
|
|
'actions' => [ |
113
|
|
|
'delete' => ['POST'], |
114
|
|
|
], |
115
|
|
|
], |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set model. |
121
|
|
|
* |
122
|
|
|
* @param Album $model |
123
|
|
|
*/ |
124
|
|
|
public function setModel(Album $model): void |
125
|
|
|
{ |
126
|
|
|
$this->model = $model; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns model. |
131
|
|
|
* |
132
|
|
|
* @return Album |
133
|
|
|
*/ |
134
|
|
|
public function getModel(): Album |
135
|
|
|
{ |
136
|
|
|
return $this->model; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Give ability of configure view to the module class. |
141
|
|
|
* |
142
|
|
|
* @return \yii\base\View|\yii\web\View |
143
|
|
|
*/ |
144
|
|
|
public function getView() |
145
|
|
|
{ |
146
|
|
|
if (method_exists($this->module, 'getView')) { |
147
|
|
|
return $this->module->getView(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return parent::getView(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Lists all Album models. |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function actionIndex() |
159
|
|
|
{ |
160
|
|
|
$searchModel = new AlbumSearch(); |
161
|
|
|
$searchParams = ArrayHelper::merge(Yii::$app->request->queryParams, [ |
162
|
|
|
$searchModel->formName() => [ |
163
|
|
|
'type' => $this->getAlbumType() |
164
|
|
|
] |
165
|
|
|
]); |
166
|
|
|
$dataProvider = $searchModel->search($searchParams); |
167
|
|
|
|
168
|
|
|
return $this->render('index', [ |
169
|
|
|
'searchModel' => $searchModel, |
170
|
|
|
'dataProvider' => $dataProvider, |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Displays a single Album model. |
176
|
|
|
* |
177
|
|
|
* @param integer $id |
178
|
|
|
* |
179
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
180
|
|
|
* |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
|
|
public function actionView($id) |
184
|
|
|
{ |
185
|
|
|
$model = $this->findModel($id); |
186
|
|
|
|
187
|
|
|
$mediafilesQuery = $model->getMediaFilesQuery($model->getFileType($model->type)); |
188
|
|
|
$pages = new Pagination([ |
189
|
|
|
'defaultPageSize' => 6, |
190
|
|
|
'totalCount' => $mediafilesQuery->count() |
191
|
|
|
]); |
192
|
|
|
|
193
|
|
|
return $this->render('view', [ |
194
|
|
|
'model' => $model, |
195
|
|
|
'mediafiles' => $mediafilesQuery->offset($pages->offset) |
196
|
|
|
->limit($pages->limit) |
197
|
|
|
->all(), |
198
|
|
|
'pages' => $pages, |
199
|
|
|
]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Creates a new Album model. |
204
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
205
|
|
|
* |
206
|
|
|
* @return mixed |
207
|
|
|
*/ |
208
|
|
|
public function actionCreate() |
209
|
|
|
{ |
210
|
|
|
$this->setModelByConditions(); |
211
|
|
|
|
212
|
|
|
if ($this->model->load(Yii::$app->request->post()) && $this->model->save()) { |
213
|
|
|
return $this->redirect([ |
214
|
|
|
$this->urlPrefix.'view', |
215
|
|
|
'id' => $this->model->id |
216
|
|
|
]); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this->render('create', [ |
220
|
|
|
'model' => $this->model, |
221
|
|
|
'albumType' => $this->getAlbumType(), |
222
|
|
|
]); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Updates an existing Album model. |
227
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
228
|
|
|
* |
229
|
|
|
* @param integer $id |
230
|
|
|
* |
231
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
232
|
|
|
* |
233
|
|
|
* @return mixed |
234
|
|
|
*/ |
235
|
|
|
public function actionUpdate($id) |
236
|
|
|
{ |
237
|
|
|
$this->setModelByConditions($id); |
238
|
|
|
|
239
|
|
|
$post = Yii::$app->request->post(); |
240
|
|
|
if ($this->model->load($post) && $this->model->save()) { |
241
|
|
|
if (isset($post['delete']) && is_array($post)) { |
242
|
|
|
$this->deleteMediafileEntry($post['delete'], $this->module); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $this->redirect([ |
246
|
|
|
$this->urlPrefix.'view', |
247
|
|
|
'id' => $this->model->id |
248
|
|
|
]); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
$mediafilesQuery = $this->model->getMediaFilesQuery($this->model->getFileType($this->model->type)); |
252
|
|
|
$pages = new Pagination([ |
253
|
|
|
'defaultPageSize' => 6, |
254
|
|
|
'totalCount' => $mediafilesQuery->count() |
255
|
|
|
]); |
256
|
|
|
|
257
|
|
|
return $this->render('update', [ |
258
|
|
|
'model' => $this->model, |
259
|
|
|
'mediafiles' => $mediafilesQuery->offset($pages->offset) |
260
|
|
|
->limit($pages->limit) |
261
|
|
|
->all(), |
262
|
|
|
'pages' => $pages, |
263
|
|
|
'albumType' => $this->model->type, |
264
|
|
|
'ownerParams' => [ |
265
|
|
|
'owner' => $this->model->type, |
266
|
|
|
'ownerId' => $this->model->primaryKey, |
267
|
|
|
] |
268
|
|
|
]); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Deletes an existing Album model. |
273
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
274
|
|
|
* |
275
|
|
|
* @param integer $id |
276
|
|
|
* |
277
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
278
|
|
|
* @throws BadRequestHttpException |
279
|
|
|
* |
280
|
|
|
* @return mixed |
281
|
|
|
*/ |
282
|
|
|
public function actionDelete($id) |
283
|
|
|
{ |
284
|
|
|
$model = $this->findModel($id); |
285
|
|
|
|
286
|
|
|
$this->deleteMediafiles($model->type, $model->id, $this->module); |
287
|
|
|
|
288
|
|
|
if (false !== $model->delete()) { |
289
|
|
|
return $this->redirect([$this->urlPrefix.'index']); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
throw new BadRequestHttpException('Record is not deleted.'); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Finds the Album model based on its primary key value. |
297
|
|
|
* |
298
|
|
|
* @param $key |
299
|
|
|
* |
300
|
|
|
* @throws BadRequestHttpException |
301
|
|
|
* @throws NotFoundHttpException |
302
|
|
|
* |
303
|
|
|
* @return Album |
304
|
|
|
*/ |
305
|
|
|
protected function findModel($key): Album |
306
|
|
|
{ |
307
|
|
|
if (null === $key) { |
308
|
|
|
throw new BadRequestHttpException('Key parameter is not defined in findModel method.'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$modelObject = $this->getNewModel(); |
312
|
|
|
|
313
|
|
|
if (!method_exists($modelObject, 'findOne')) { |
314
|
|
|
$class = (new\ReflectionClass($modelObject)); |
|
|
|
|
315
|
|
|
throw new UnknownMethodException('Method findOne does not exists in ' . $class->getNamespaceName() . '\\' . |
316
|
|
|
$class->getShortName().' class.'); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$result = call_user_func([ |
320
|
|
|
$modelObject, |
321
|
|
|
'findOne', |
322
|
|
|
], $key); |
323
|
|
|
|
324
|
|
|
if ($result !== null) { |
325
|
|
|
return $result; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Returns new object of main Album model. |
333
|
|
|
* |
334
|
|
|
* @return Album |
335
|
|
|
*/ |
336
|
|
|
protected function getNewModel(): Album |
337
|
|
|
{ |
338
|
|
|
$modelName = $this->getModelName(); |
339
|
|
|
return new $modelName; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns an intermediate model for working with the main. |
344
|
|
|
* |
345
|
|
|
* @param int|string|null $key |
346
|
|
|
* |
347
|
|
|
* @return void |
348
|
|
|
*/ |
349
|
|
|
protected function setModelByConditions($key = null): void |
350
|
|
|
{ |
351
|
|
|
$model = null === $key ? $this->getNewModel() : $this->findModel($key); |
352
|
|
|
|
353
|
|
|
$this->setModel($model); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|