Advertisement
Esforeal

EventsController

May 31st, 2016
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use app\models\Timetable;
  6. use Yii;
  7. use app\models\Events;
  8. use app\models\EventsSearch;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use yii2fullcalendar\models\Event;
  13.  
  14. /**
  15.  * EventsController implements the CRUD actions for Events model.
  16.  */
  17. class EventsController extends Controller
  18. {
  19.     /**
  20.      * @inheritdoc
  21.      */
  22.     public function behaviors()
  23.     {
  24.         return [
  25.             'verbs' => [
  26.                 'class' => VerbFilter::className(),
  27.                 'actions' => [
  28.                     'delete' => ['POST'],
  29.                 ],
  30.             ],
  31.         ];
  32.     }
  33.  
  34.     /**
  35.      * Lists all Events models.
  36.      * @return mixed
  37.      */
  38.     public function actionIndex()
  39.     {
  40.         $lessons = Events::find()->all();
  41.  
  42.         foreach ($lessons as $lesson)
  43.         {
  44.                 $time = Timetable::findOne($lesson->timetable_id);
  45.  
  46.                 $event = new Event();
  47.                 $event->id = $lesson->id;
  48.                 $event->title = $lesson->topic;
  49.                 $event->start = $lesson->date . 'T' . $time->start;
  50.                 $event->end = $lesson->date . 'T' . $time->finish;
  51. //                $event->url = 'events/view?id=' . $event->id;
  52.                 $events[] = $event;
  53.         }
  54.  
  55.         return $this->render('index', [
  56.             'events' => $events
  57.         ]);
  58.     }
  59.  
  60.     /**
  61.      * Displays a single Events model.
  62.      * @param integer $id
  63.      * @return mixed
  64.      */
  65.     public function actionView($id)
  66.     {
  67.         return $this->render('view', [
  68.             'model' => $this->findModel($id),
  69.         ]);
  70.     }
  71.  
  72.     /**
  73.      * Creates a new Events model.
  74.      * If creation is successful, the browser will be redirected to the 'view' page.
  75.      * @param $date
  76.      * @return mixed
  77.      */
  78.     public function actionCreate($date)
  79.     {
  80.         $model = new Events();
  81.         $model->date = $date;
  82.  
  83.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  84.             return $this->redirect(['index']);
  85.         } else {
  86.             return $this->renderAjax('create', [
  87.                 'model' => $model,
  88.             ]);
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * Updates an existing Events model.
  94.      * If update is successful, the browser will be redirected to the 'view' page.
  95.      * @param integer $id
  96.      * @return mixed
  97.      */
  98.     public function actionUpdate($id)
  99.     {
  100.         $model = $this->findModel($id);
  101.  
  102.         if ($model->load(Yii::$app->request->post()) && $model->save()) {
  103.             return $this->redirect(['view', 'id' => $model->id]);
  104.         } else {
  105.             return $this->renderAjax('update', [
  106.                 'model' => $model,
  107.             ]);
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * Deletes an existing Events model.
  113.      * If deletion is successful, the browser will be redirected to the 'index' page.
  114.      * @param integer $id
  115.      * @return mixed
  116.      */
  117.     public function actionDelete($id)
  118.     {
  119.         $this->findModel($id)->delete();
  120.  
  121.         return $this->redirect(['index']);
  122.     }
  123.  
  124.     /**
  125.      * Finds the Events model based on its primary key value.
  126.      * If the model is not found, a 404 HTTP exception will be thrown.
  127.      * @param integer $id
  128.      * @return Events the loaded model
  129.      * @throws NotFoundHttpException if the model cannot be found
  130.      */
  131.     protected function findModel($id)
  132.     {
  133.         if (($model = Events::findOne($id)) !== null) {
  134.             return $model;
  135.         } else {
  136.             throw new NotFoundHttpException('The requested page does not exist.');
  137.         }
  138.     }
  139. }
Advertisement
RAW Paste Data Copied
Advertisement