PageRenderTime 89ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/frontend/controllers/ContentController.php

https://gitlab.com/jafari/resana
PHP | 220 lines | 153 code | 20 blank | 47 comment | 23 complexity | 1c91b8748bc001c0a844160b5e0c41f0 MD5 | raw file
  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use common\models\Group;
  5. use common\models\Content;
  6. use frontend\models\ContentSearch;
  7. use yii\filters\AccessControl;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. /**
  12. * ContentController implements the CRUD actions for Content model.
  13. */
  14. class ContentController extends Controller
  15. {
  16. public function behaviors()
  17. {
  18. return [
  19. 'access' => [
  20. 'class' => AccessControl::className(),
  21. 'rules' => [
  22. [
  23. 'allow' => true,
  24. 'roles' => ['updateProject'],
  25. 'actions' => ['delete'],
  26. ],
  27. [
  28. 'allow' => true,
  29. 'roles' => ['readProject'],
  30. ],
  31. ],
  32. ],
  33. 'verbs' => [
  34. 'class' => VerbFilter::className(),
  35. 'actions' => [
  36. 'delete' => ['post'],
  37. 'delete-attachment' => ['post'],
  38. ],
  39. ],
  40. ];
  41. }
  42. /**
  43. * Lists all Content models.
  44. * @return mixed
  45. */
  46. public function actionIndex($project_id)
  47. {
  48. $searchModel = new ContentSearch();
  49. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  50. return $this->render('index', [
  51. 'searchModel' => $searchModel,
  52. 'dataProvider' => $dataProvider,
  53. 'projectId' => $project_id,
  54. ]);
  55. }
  56. /**
  57. * Displays a single Content model.
  58. * @param integer $id
  59. * @return mixed
  60. */
  61. public function actionView($project_id, $id)
  62. {
  63. $model = $this->findModel($id, $project_id);
  64. if (Yii::$app->request->isAjax) {
  65. return $this->renderAjax('view', [
  66. 'model' => $model,
  67. 'projectId' => $project_id,
  68. ]);
  69. } else {
  70. return $this->render('view', [
  71. 'model' => $model,
  72. 'projectId' => $project_id,
  73. ]);
  74. }
  75. }
  76. /**
  77. * Creates a new Content model.
  78. * If creation is successful, the browser will be redirected to the 'view' page.
  79. * @return mixed
  80. */
  81. public function actionCreate($project_id)
  82. {
  83. $model = new Content(compact('project_id'));
  84. $model->scenario = Content::SCENARIO_CREATE;
  85. if (Yii::$app->request->getBodyParam('draft') !== null) {
  86. $model->status = Content::STATUS_PENDING;
  87. }
  88. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  89. if (in_array($model->status, [Content::STATUS_SCHEDULED, Content::STATUS_PENDING], true)) {
  90. $urlParams = ['update', 'id' => $model->id, 'project_id' => $project_id];
  91. } else {
  92. $urlParams = ['index', 'project_id' => $project_id];
  93. }
  94. return $this->redirect($urlParams);
  95. } else {
  96. return $this->render('create', [
  97. 'model' => $model,
  98. 'projectId' => $project_id,
  99. ]);
  100. }
  101. }
  102. /**
  103. * Updates an existing Content model.
  104. * If update is successful, the browser will be redirected to the 'view' page.
  105. * @param integer $id
  106. * @return mixed
  107. */
  108. public function actionUpdate($project_id, $id)
  109. {
  110. $model = $this->findModel($id, $project_id);
  111. $model->scenario = Content::SCENARIO_UPDATE;
  112. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  113. return $this->redirect(['index', 'project_id' => $project_id]);
  114. } else {
  115. return $this->render('update', [
  116. 'model' => $model,
  117. 'projectId' => $project_id,
  118. ]);
  119. }
  120. }
  121. /**
  122. * Deletes an existing Content model.
  123. * If deletion is successful, the browser will be redirected to the 'index' page.
  124. * @param integer $id
  125. * @return mixed
  126. */
  127. public function actionDelete($project_id, $id)
  128. {
  129. $this->findModel($id, $project_id)->delete();
  130. return $this->redirect(['index', 'project_id' => $project_id]);
  131. }
  132. /**
  133. * Delete an attachment file from attachments json field
  134. * @param $project_id
  135. * @return bool|int
  136. * @throws NotFoundHttpException
  137. * @throws \Exception
  138. */
  139. public function actionDeleteAttachment($project_id)
  140. {
  141. $request = Yii::$app->request;
  142. if ($request->isAjax) {
  143. $id = $request->getBodyParam('id');
  144. $url = $request->getBodyParam('key');
  145. $model = $this->findModel($id, $project_id);
  146. if ($model->attachments) {
  147. $files = json_decode($model->attachments);
  148. foreach ($files as $file) {
  149. if ($file->url != $url) {
  150. $newFiles[] = $file;
  151. }
  152. }
  153. if (isset($newFiles)) {
  154. $model->attachments = json_encode($newFiles);
  155. } else {
  156. $model->attachments = '';
  157. $model->media_type = Content::MEDIA_TYPE_TEXT;
  158. }
  159. return $model->update(false);
  160. }
  161. }
  162. return false;
  163. }
  164. /**
  165. * Returns groups of selected channels in json format
  166. * @return string
  167. */
  168. public function actionGetGroups($project_id, $id = null)
  169. {
  170. $request = Yii::$app->request;
  171. if ($request->isAjax) {
  172. if ($channelIds = $request->getBodyParam('channelIds')) {
  173. $groups = Group::findAllByChannels($channelIds);
  174. if ($id) {
  175. $model = $this->findModel($id, $project_id);
  176. $activeGroups = $model->groupsList;
  177. } else {
  178. $activeGroups = [];
  179. }
  180. return json_encode(['items' => $groups, 'selected' => $activeGroups]);
  181. }
  182. }
  183. return '[]'; // array in json format
  184. }
  185. /**
  186. * Finds the Content model based on its primary key value.
  187. * If the model is not found, a 404 HTTP exception will be thrown.
  188. * @param integer $id
  189. * @return Content the loaded model
  190. * @throws NotFoundHttpException if the model cannot be found
  191. */
  192. protected function findModel($id, $project_id)
  193. {
  194. if (($model = Content::findOne(compact('id', 'project_id'))) !== null) {
  195. return $model;
  196. } else {
  197. throw new NotFoundHttpException('The requested page does not exist.');
  198. }
  199. }
  200. }