Advertisement
Guest User

eventcontroller.php

a guest
Apr 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\controllers;
  4.  
  5. use Yii;
  6. use common\components\MyController;
  7. use backend\models\Event;
  8. use backend\models\EventSearch;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use yii\web\UploadedFile;
  13.  
  14. /**
  15. * EventController implements the CRUD actions for Event model.
  16. */
  17. class EventController extends MyController
  18. {
  19.  
  20. /** @inheritdoc */
  21. public function behaviors()
  22. {
  23. return [
  24. 'verbs' => [
  25. 'class' => VerbFilter::class,
  26. 'actions' => [
  27. 'delete' => ['post'],
  28. ],
  29. ],
  30. ];
  31. }
  32.  
  33. /**
  34. * Lists all Event models.
  35. * @return mixed
  36. */
  37. public function actionIndex()
  38. {
  39. $searchModel = new EventSearch();
  40. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41.  
  42. return $this->render('index', [
  43. 'searchModel' => $searchModel,
  44. 'dataProvider' => $dataProvider,
  45. ]);
  46. }
  47.  
  48. /**
  49. * Displays a single Event model.
  50. * @param integer $id
  51. * @return mixed
  52. */
  53. public function actionView($id)
  54. {
  55. $viewData = [
  56. 'model' => $this->findModel($id),
  57. ];
  58.  
  59. if (Yii::$app->request->isAjax) {
  60. return $this->renderAjax('view', $viewData);
  61. }
  62.  
  63. return $this->render('view', $viewData);
  64. }
  65.  
  66. /**
  67. * Creates a new Event model.
  68. * If creation is successful, the browser will be redirected to the 'view' page.
  69. * @return mixed
  70. */
  71. public function actionCreate()
  72. {
  73. $model = new Event();
  74.  
  75. if ($model->load(Yii::$app->request->post())) {
  76. if ($model->save()) {
  77. alertSuccess(translateText('Data saved successfully'));
  78. return $this->redirect(['index']);
  79. } else {
  80. alertDanger(translateText('Failed to save data'));
  81. }
  82. }
  83.  
  84. $viewData = [
  85. 'model' => $model,
  86. ];
  87.  
  88. if (Yii::$app->request->isAjax) {
  89. return $this->renderAjax('create', $viewData);
  90. }
  91.  
  92. return $this->render('create', $viewData);
  93. }
  94.  
  95. /**
  96. * Updates an existing Event model.
  97. * If update is successful, the browser will be redirected to the 'view' page.
  98. * @param integer $id
  99. * @return mixed
  100. */
  101. public function actionUpdate($id)
  102. {
  103. $model = $this->findModel($id);
  104.  
  105. if ($model->load(Yii::$app->request->post())) {
  106. if ($model->save()) {
  107. alertSuccess(translateText('Data saved successfully'));
  108.  
  109. if (@explode("?", Yii::$app->request->referrer)[0] == Yii::$app->urlManager->createAbsoluteUrl(['event/index'])) {
  110. return $this->redirect(['index', 'modal' => $id]);
  111. }
  112.  
  113. return $this->redirect(['view', 'id' => $id]);
  114. } else {
  115. alertDanger(translateText('Failed to save data'));
  116. }
  117. }
  118.  
  119. $viewData = [
  120. 'model' => $model,
  121. ];
  122.  
  123. if (Yii::$app->request->isAjax) {
  124. return $this->renderAjax('update', $viewData);
  125. }
  126.  
  127. return $this->render('update', $viewData);
  128. }
  129.  
  130. /**
  131. * Deletes an existing Event model.
  132. * If deletion is successful, the browser will be redirected to the 'index' page.
  133. * @param integer $id
  134. * @return mixed
  135. */
  136. public function actionDelete($id)
  137. {
  138. $model = $this->findModel($id);
  139.  
  140. if ($model->delete()) {
  141. alertSuccess(translateText('Data deleted successfully'));
  142. } else {
  143. alertDanger(translateText('Failed to delete data'));
  144. }
  145. return $this->redirect(['index']);
  146. }
  147.  
  148. /**
  149. * Finds the Event model based on its primary key value.
  150. * If the model is not found, a 404 HTTP exception will be thrown.
  151. * @param integer $id
  152. * @return Event the loaded model
  153. * @throws NotFoundHttpException if the model cannot be found
  154. */
  155. protected function findModel($id)
  156. {
  157. if (($model = Event::findOne($id)) !== null) {
  158.  
  159. return $model;
  160. }
  161. throw new NotFoundHttpException('The requested page does not exist.');
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement