<?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' => 'Добавление нового курса',
            'Update' => 'Редактирование курса'
        ];
    }

    /**
     * @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|\yii\web\Response
     */
    public function actionUpdate($id){
        /** @var AnalyticsSchoolCourse $model */
        $model = self::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.');
        }
    }
}