1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace backend\controllers; |
9
|
|
|
|
10
|
|
|
use common\components\MediaUploadHandler; |
11
|
|
|
use common\models\Media; |
12
|
|
|
use common\models\Option; |
13
|
|
|
use common\models\search\Media as MediaSearch; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\filters\AccessControl; |
16
|
|
|
use yii\filters\VerbFilter; |
17
|
|
|
use yii\helpers\ArrayHelper; |
18
|
|
|
use yii\web\Controller; |
19
|
|
|
use yii\web\ForbiddenHttpException; |
20
|
|
|
use yii\web\NotFoundHttpException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* MediaController, controlling the actions for for Media model. |
24
|
|
|
* |
25
|
|
|
* @author Agiel K. Saputra <[email protected]> |
26
|
|
|
* @since 0.1.0 |
27
|
|
|
*/ |
28
|
|
|
class MediaController extends Controller |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function behaviors() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'access' => [ |
37
|
|
|
'class' => AccessControl::className(), |
38
|
|
|
'rules' => [ |
39
|
|
|
[ |
40
|
|
|
'actions' => [ |
41
|
|
|
'index', |
42
|
|
|
'create', |
43
|
|
|
'update', |
44
|
|
|
'delete', |
45
|
|
|
'bulk-action', |
46
|
|
|
'ajax-upload', |
47
|
|
|
'ajax-update', |
48
|
|
|
'ajax-delete', |
49
|
|
|
], |
50
|
|
|
'allow' => true, |
51
|
|
|
'roles' => ['author'], |
52
|
|
|
], |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
'verbs' => [ |
56
|
|
|
'class' => VerbFilter::className(), |
57
|
|
|
'actions' => [ |
58
|
|
|
'delete' => ['post'], |
59
|
|
|
'bulk-action' => ['post'], |
60
|
|
|
'ajax-upload' => ['post'], |
61
|
|
|
'ajax-update' => ['post'], |
62
|
|
|
'ajax-delete' => ['post'], |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Lists all Media models. |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function actionIndex() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$searchModel = new MediaSearch(); |
76
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
77
|
|
|
|
78
|
|
|
return $this->render('index', [ |
79
|
|
|
'searchModel' => $searchModel, |
80
|
|
|
'dataProvider' => $dataProvider, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Creates a new Media model. |
87
|
|
|
* If creation is successful, the browser will display thumbnail and options to the 'create' page. |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
public function actionCreate() |
92
|
|
|
{ |
93
|
|
|
return $this->render('create', ['model' => new Media(['scenario' => 'upload'])]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Updates an existing Media model. |
98
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
99
|
|
|
* |
100
|
|
|
* @param integer $id |
101
|
|
|
* @return mixed |
102
|
|
|
* @throws ForbiddenHttpException |
103
|
|
|
* @throws NotFoundHttpException |
104
|
|
|
*/ |
105
|
|
|
public function actionUpdate($id) |
106
|
|
|
{ |
107
|
|
|
$model = $this->findModel($id); |
108
|
|
|
$this->getPermission($model); |
109
|
|
|
$metadata = $model->getMeta('metadata'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
|
|
|
112
|
|
|
$model->date = date('Y-m-d H:i:s', strtotime($model->date)); |
113
|
|
|
if ($model->save()) { |
|
|
|
|
114
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Media successfully saved.')); |
|
|
|
|
115
|
|
|
|
116
|
|
|
return $this->redirect(['update', 'id' => $id]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->render('update', [ |
121
|
|
|
'model' => $model, |
122
|
|
|
'metadata' => $metadata, |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Deletes an existing Media model. |
128
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
129
|
|
|
* |
130
|
|
|
* @param integer $id |
131
|
|
|
* @return mixed |
132
|
|
|
* @throws ForbiddenHttpException |
133
|
|
|
* @throws NotFoundHttpException |
134
|
|
|
*/ |
135
|
|
|
public function actionDelete($id) |
136
|
|
|
{ |
137
|
|
|
$this->getPermission($this->findModel($id)); |
138
|
|
|
$uploadHandler = new MediaUploadHandler(null, false); |
139
|
|
|
$uploadHandler->delete($id, MediaUploadHandler::NOT_PRINT_RESPONSE); |
140
|
|
|
|
141
|
|
|
return $this->redirect(['index']); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Bulk action for Media triggered when button 'Apply' clicked. |
146
|
|
|
* The action depends on the value of the dropdown next to the button. |
147
|
|
|
* Only accept POST HTTP method. |
148
|
|
|
*/ |
149
|
|
|
public function actionBulkAction() |
150
|
|
|
{ |
151
|
|
|
if (Yii::$app->request->post('action') == 'delete') { |
152
|
|
|
foreach (Yii::$app->request->post('ids', []) as $id) { |
153
|
|
|
$this->getPermission($this->findModel($id)); |
154
|
|
|
$uploadHandler = new MediaUploadHandler(null, false); |
155
|
|
|
$uploadHandler->delete($id, MediaUploadHandler::NOT_PRINT_RESPONSE); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Upload media file and store it to database. |
162
|
|
|
* Media versions can be set from application params. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function actionAjaxUpload() |
167
|
|
|
{ |
168
|
|
|
$versions = [ |
169
|
|
|
'large' => [ |
170
|
|
|
'max_width' => Option::get('large_width'), |
171
|
|
|
'max_height' => Option::get('large_height'), |
172
|
|
|
], |
173
|
|
|
'medium' => [ |
174
|
|
|
'max_width' => Option::get('medium_width'), |
175
|
|
|
'max_height' => Option::get('medium_height'), |
176
|
|
|
], |
177
|
|
|
'thumbnail' => [ |
178
|
|
|
'max_width' => Option::get('thumbnail_width'), |
179
|
|
|
'max_height' => Option::get('thumbnail_height'), |
180
|
|
|
'crop' => 1, |
181
|
|
|
], |
182
|
|
|
]; |
183
|
|
|
|
184
|
|
View Code Duplication |
if($userVersions = ArrayHelper::getValue(Yii::$app->params, 'media.versions', [])){ |
|
|
|
|
185
|
|
|
$versions = ArrayHelper::merge($versions, $userVersions); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$uploadHandler = new MediaUploadHandler([ |
189
|
|
|
'versions' => $versions, |
190
|
|
|
'user_dirs' => Option::get('uploads_username_based'), |
191
|
|
|
'year_month_dirs' => Option::get('uploads_yearmonth_based'), |
192
|
|
|
], MediaUploadHandler::NOT_PRINT_RESPONSE); |
193
|
|
|
$uploadHandler->post(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Update attributes of Media model via AJAX request. |
198
|
|
|
*/ |
199
|
|
|
public function actionAjaxUpdate() |
200
|
|
|
{ |
201
|
|
|
if ($model = $this->findModel(Yii::$app->request->post('id'))) { |
202
|
|
|
$this->getPermission($model); |
203
|
|
|
$model->{Yii::$app->request->post('attribute')} = Yii::$app->request->post('value'); |
204
|
|
|
$model->save(); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Delete Media model and its files based on media primary key. |
210
|
|
|
* |
211
|
|
|
* @param $id |
212
|
|
|
* @return array |
213
|
|
|
* @throws ForbiddenHttpException |
214
|
|
|
* @throws NotFoundHttpException |
215
|
|
|
*/ |
216
|
|
|
public function actionAjaxDelete($id) |
217
|
|
|
{ |
218
|
|
|
$this->getPermission($this->findModel($id)); |
219
|
|
|
$uploadHandler = new MediaUploadHandler(null, MediaUploadHandler::NOT_PRINT_RESPONSE); |
220
|
|
|
$uploadHandler->delete($id); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get permission to access model by current user. |
225
|
|
|
* If the user does not obtain the permission, a 403 exeption will be thrown. |
226
|
|
|
* |
227
|
|
|
* @param $model Media |
228
|
|
|
* @throws ForbiddenHttpException |
229
|
|
|
*/ |
230
|
|
|
public function getPermission($model) |
231
|
|
|
{ |
232
|
|
|
if (!$model->getPermission()) { |
233
|
|
|
throw new ForbiddenHttpException(Yii::t('writesdown', 'You are not allowed to perform this action.')); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Finds the Media model based on its primary key value. |
239
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
240
|
|
|
* |
241
|
|
|
* @param integer $id |
242
|
|
|
* @return Media the loaded model |
243
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
244
|
|
|
*/ |
245
|
|
|
protected function findModel($id) |
246
|
|
|
{ |
247
|
|
|
if (($model = Media::findOne($id)) !== null) { |
248
|
|
|
return $model; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.