PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/controllers/MediaController.php

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