<?php
/**
 * Created by PhpStorm.
 * User: PHOENIX
 * Date: 10.04.16
 * Time: 17:10
 */

namespace common\modules\analyticsSchool\controllers;


use common\components\AdminController;
use common\modules\analyticsSchool\models\AnalyticsSchoolLesson;
use common\modules\analyticsSchool\models\AnalyticsSchoolLessonFile;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

class LessonAdminController extends AdminController {
    /**
     * @return array|void
     */
    public static function actionsTitles(){
        return [
            'Manage' => 'Управление уроками',
            'Create' => 'Добавление нового урока',
            'View'   => 'Просмотр урока',
            'Update' => 'Редактирование урока',
            'Delete' => 'Удаление урока',
            'Deletefile' => 'Удаление файла вложения'
        ];
    }

    /**
     * @return string
     */
    public function actionManage(){
        $dataProvider = new ActiveDataProvider([
            'query' => AnalyticsSchoolLesson::find(),
        ]);

        return $this->render(
            'manage',
            [
                'dataProvider' => $dataProvider,
            ]
        );
    }

    /**
     * @return string|\yii\web\Response
     */
    public function actionCreate(){
        $model = new AnalyticsSchoolLesson();
        if ($model->load(\Yii::$app->request->post())) {
            if ($model->validate() && $model->save())
                return $this->redirect([
                    'manage'
                ]);
        }

        $form = new \common\components\BaseForm('/common/modules/analyticsSchool/forms/LessonForm', $model);

        return $this->render(
            'create',
            [
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return string
     */
    public function actionView($id){
        $files = new ActiveDataProvider([
            'query' => AnalyticsSchoolLessonFile::find()->where([
                    'lesson_id' => $id
                ])
        ]);
        return $this->render(
            'view', [
                'model' => $this->findModel($id),
                'files' => $files
            ]
        );
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     */
    public function actionUpdate($id){
        /** @var AnalyticsSchoolLesson $model */
        $model = $this->findModel($id);
        if ($model->load(\Yii::$app->request->post())) {
            if ($model->validate() && $model->save())
                return $this->redirect([
                    'manage'
                ]);
        }
        $form = new \common\components\BaseForm('/common/modules/analyticsSchool/forms/LessonForm', $model);
        return $this->render(
            'update',
            [
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return \yii\web\Response
     */
    public function actionDelete($id){
        $this->findModel($id)->delete();

        return $this->redirect(['manage']);
    }

    /**
     * @param $id
     * @return \yii\web\Response
     */
    public function actionDeletefile($id){
        /** @var AnalyticsSchoolLessonFile $model */
        $model = AnalyticsSchoolLessonFile::findOne($id);
        $model->delete();

        return $this->redirect(['update', 'id' => $model->lesson_id]);
    }

    /**
     * @param $id
     * @return AnalyticsSchoolLesson|null
     * @throws NotFoundHttpException
     */
    public function findModel($id)
    {
        if (($model = AnalyticsSchoolLesson::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
} 