PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/controllers/MediaCommentController.php

https://gitlab.com/makkooz/nikestreetbeat
PHP | 218 lines | 134 code | 21 blank | 63 comment | 12 complexity | 292c1d84f8c40db8c34f11e87fccc0b3 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\models\Media;
  9. use common\models\MediaComment;
  10. use common\models\search\MediaComment as MediaCommentSearch;
  11. use Yii;
  12. use yii\filters\AccessControl;
  13. use yii\filters\VerbFilter;
  14. use yii\web\Controller;
  15. use yii\web\NotFoundHttpException;
  16. /**
  17. * Class MediaCommentController, controlling the actions for Media model.
  18. *
  19. * @author Agiel K. Saputra <13nightevil@gmail.com>
  20. * @since 0.1.0
  21. */
  22. class MediaCommentController extends Controller
  23. {
  24. /**
  25. * @inheritdoc
  26. */
  27. public function behaviors()
  28. {
  29. return [
  30. 'access' => [
  31. 'class' => AccessControl::className(),
  32. 'rules' => [
  33. [
  34. 'actions' => ['index', 'update', 'delete', 'bulk-action', 'reply'],
  35. 'allow' => true,
  36. 'roles' => ['editor'],
  37. ],
  38. ],
  39. ],
  40. 'verbs' => [
  41. 'class' => VerbFilter::className(),
  42. 'actions' => [
  43. 'delete' => ['post'],
  44. 'bulk-action' => ['post'],
  45. ],
  46. ],
  47. ];
  48. }
  49. /**
  50. * Lists all MediaComment models.
  51. * If there is media, the action will generate list of all MediaComment models based on Media ID.
  52. *
  53. * @param null|integer $media Media ID
  54. * @return mixed
  55. */
  56. public function actionIndex($media = null)
  57. {
  58. $mediaId = null;
  59. if ($media) {
  60. $media = $this->findMedia($media);
  61. $mediaId = $media->id;
  62. }
  63. $searchModel = new MediaCommentSearch();
  64. $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $mediaId);
  65. return $this->render('index', [
  66. 'searchModel' => $searchModel,
  67. 'dataProvider' => $dataProvider,
  68. 'media' => $media,
  69. ]);
  70. }
  71. /**
  72. * Updates an existing MediaComment model.
  73. * If update is successful, the browser will be redirected to the 'update' page.
  74. *
  75. * @param integer $id
  76. * @return mixed
  77. */
  78. public function actionUpdate($id)
  79. {
  80. $model = $this->findModel($id);
  81. if ($model->load(Yii::$app->request->post())) {
  82. $model->date = date('Y-m-d H:i:s', strtotime($model->date));
  83. if ($model->save()) {
  84. return $this->redirect(['update', 'id' => $model->id]);
  85. }
  86. }
  87. return $this->render('update', [
  88. 'model' => $model,
  89. ]);
  90. }
  91. /**
  92. * Deletes an existing MediaComment model.
  93. * If deletion is successful, the browser will be redirected to the 'index' page.
  94. *
  95. * @param integer $id
  96. * @return mixed
  97. */
  98. public function actionDelete($id)
  99. {
  100. $model = $this->findModel($id);
  101. $media = $model->commentMedia;
  102. if ($model->delete()) {
  103. if (!$model->parent) {
  104. $media->updateAttributes(['comment_count', --$media->comment_count]);
  105. }
  106. MediaComment::deleteAll(['parent' => $model->id]);
  107. }
  108. return $this->redirect(['index']);
  109. }
  110. /**
  111. * Bulk action for MediaComment triggered when button 'Apply' clicked.
  112. * The action depends on the value of the dropdown next to the button.
  113. * Only accept POST HTTP method.
  114. */
  115. public function actionBulkAction()
  116. {
  117. if (Yii::$app->request->post('action') === MediaComment::STATUS_APPROVED) {
  118. foreach (Yii::$app->request->post('ids', []) as $id) {
  119. $this->findModel($id)->updateAttributes(['status' => MediaComment::STATUS_APPROVED]);
  120. }
  121. } elseif (Yii::$app->request->post('action') === MediaComment::STATUS_NOT_APPROVED) {
  122. foreach (Yii::$app->request->post('ids', []) as $id) {
  123. $this->findModel($id)->updateAttributes(['status' => MediaComment::STATUS_NOT_APPROVED]);
  124. }
  125. } elseif (Yii::$app->request->post('action') === MediaComment::STATUS_TRASHED) {
  126. foreach (Yii::$app->request->post('ids', []) as $id) {
  127. $this->findModel($id)->updateAttributes(['status' => MediaComment::STATUS_TRASHED]);
  128. }
  129. } elseif (Yii::$app->request->post('action') === 'delete') {
  130. foreach (Yii::$app->request->post('ids', []) as $id) {
  131. $model = $this->findModel($id);
  132. $media = $model->commentMedia;
  133. if ($model->delete()) {
  134. if (!$model->parent) {
  135. $media->updateAttributes(['comment_count', --$media->comment_count]);
  136. }
  137. MediaComment::deleteAll(['parent' => $model->id]);
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Reply an existing MediaComment model.
  144. * If reply is successful, the browser will be redirected to 'update' page.
  145. *
  146. * @param int $id Find MediaComment model based on id as its parent
  147. * @return string
  148. */
  149. public function actionReply($id)
  150. {
  151. $commentParent = $this->findModel($id);
  152. $model = new MediaComment(['scenario' => 'reply']);
  153. if ($model->load(Yii::$app->request->post())) {
  154. $model->setAttributes([
  155. 'media_id' => $commentParent->media_id,
  156. 'parent' => $commentParent->id,
  157. ]);
  158. if ($model->save()) {
  159. $this->redirect(['/media-comment/update', 'id' => $model->id]);
  160. }
  161. }
  162. return $this->render('reply', [
  163. 'commentParent' => $commentParent,
  164. 'model' => $model,
  165. ]);
  166. }
  167. /**
  168. * Finds the MediaComment model based on its primary key value.
  169. * If the model is not found, a 404 HTTP exception will be thrown.
  170. *
  171. * @param integer $id
  172. * @return MediaComment the loaded model
  173. * @throws NotFoundHttpException if the model cannot be found
  174. */
  175. protected function findModel($id)
  176. {
  177. if (($model = MediaComment::findOne($id)) !== null) {
  178. return $model;
  179. }
  180. throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
  181. }
  182. /**
  183. * Finds the Media model based on its primary key value.
  184. * If the model is not found, a 404 HTTP exception will be thrown.
  185. *
  186. * @param integer $id
  187. * @return Media the loaded model
  188. * @throws NotFoundHttpException if the model cannot be found
  189. */
  190. protected function findMedia($id)
  191. {
  192. if (($model = Media::findOne($id)) !== null) {
  193. return $model;
  194. }
  195. throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
  196. }
  197. }