<?php
namespace common\modules\analyticsSchool\controllers;

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

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

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

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

    /**
     * @return string|\yii\web\Response
     */
    public function actionCreate(){
        $model = new AnalyticsSchoolCourse();
        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/CourseForm', $model);
        return $this->render(
            'create',
            [
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return string
     */
    public function actionView($id){
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     */
    public function actionUpdate($id){
        /** @var AnalyticsSchoolCourse $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/CourseForm', $model);
        return $this->render(
            'update',
            [
                'form' => $form
            ]
        );
    }

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

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

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