<?php
/**
 * Created by PhpStorm.
 * User: PHOENIX
 * Date: 27.04.16
 * Time: 20:32
 */

namespace common\modules\cases\controllers;

use Yii;
use yii\filters\VerbFilter;
use yii\web\NotFoundHttpException;
use common\components\AdminController;
use common\modules\content\models\CoContent;
use common\modules\content\models\SearchCoContent;

class CasesAdminController extends AdminController {
    public static function actionsTitles(){
        return [
            'Manage'          => 'Управление кейсами',
            'Createcase'      => 'Добавление кейса',
            'Createproject'   => 'Добавление проекта',
            'Update'          => 'Редактирование кейса',
            'View'            => 'Просмотр кейса',
            'Delete'          => 'Удаление кейса',
        ];
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @return string
     */
    public function actionManage(){
        $casesSearchModel = new SearchCoContent();
        $casesDataProvider = $casesSearchModel->searchByType(4, Yii::$app->request->queryParams);
        $projectsSearchModel = new SearchCoContent();
        $projectsDataProvider = $projectsSearchModel->searchByType(5, Yii::$app->request->queryParams);

        return $this->render(
            'manage',
            [
                'casesSearchModel' => $casesSearchModel,
                'casesDataProvider' => $casesDataProvider,
                'projectsSearchModel' => $projectsSearchModel,
                'projectsDataProvider' => $projectsDataProvider
            ]
        );
    }

    /**
     * @return string|\yii\web\Response
     * @throws \Exception
     */
    public function actionCreatecase()
    {
        $model = new CoContent();

        Yii::$app->controller->page_title = 'Добавить кейс';

        Yii::$app->controller->breadcrumbs = [
            [
                'Управление кейсами' => \yii\helpers\Url::toRoute('manage')
            ],
            'Добавить кейс',
        ];

        if (Yii::$app->request->isPost)
        {
            $transaction = Yii::$app->db->beginTransaction();

            try
            {
                $model->attributes = Yii::$app->request->post('CoContent');

                if($model->save())
                {
                    $transaction->commit();
                    return $this->redirect(['manage']);
                }
            }
            catch (\Exception $e)
            {
                $transaction->rollBack();
                throw $e;
            }
        }

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

    /**
     * @return string|\yii\web\Response
     * @throws \Exception
     */
    public function actionCreateproject()
    {
        $model = new CoContent();

        Yii::$app->controller->page_title = 'Добавить проект';

        Yii::$app->controller->breadcrumbs = [
            [
                'Управление кейсами' => \yii\helpers\Url::toRoute('manage')
            ],
            'Добавить проект',
        ];

        if (Yii::$app->request->isPost)
        {
            $transaction = Yii::$app->db->beginTransaction();

            try
            {
                $model->attributes = Yii::$app->request->post('CoContent');

                if($model->save())
                {
                    $transaction->commit();
                    return $this->redirect(['manage']);
                }
            }
            catch (\Exception $e)
            {
                $transaction->rollBack();
                throw $e;
            }
        }

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

    /**
     * @param $id
     * @return string|\yii\web\Response
     * @throws \Exception
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        Yii::$app->controller->page_title = 'Редактировать страницу';

        Yii::$app->controller->breadcrumbs = [
            ['Управление контентом' => \yii\helpers\Url::toRoute('manage')],
            $model->url,
        ];

        if (Yii::$app->request->isPost)
        {
            $transaction = Yii::$app->db->beginTransaction();

            try
            {
                $model->attributes = Yii::$app->request->post('CoContent');

                if($model->save())
                {
                    $transaction->commit();
                    return $this->redirect(['manage']);
                }
            }
            catch (\Exception $e)
            {
                $transaction->rollBack();
                throw $e;
            }
        }

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

    /**
     * @param $id
     * @return string|\yii\web\Response
     * @throws \Exception
     */
    public function actionCopy($id)
    {
        $model = $this->findModel($id);

        $model->url = null;

        Yii::$app->controller->page_title = 'Копирование страницы';

        Yii::$app->controller->breadcrumbs = [
            ['Управление контентом' => \yii\helpers\Url::toRoute('manage')],
            $model->url,
        ];

        if (Yii::$app->request->isPost)
        {
            $transaction = Yii::$app->db->beginTransaction();

            try
            {
                $model = new CoContent;

                $model->attributes = Yii::$app->request->post('CoContent');

                if($model->save())
                {
                    $transaction->commit();
                    return $this->redirect(['manage']);
                }
            }
            catch (\Exception $e)
            {
                $transaction->rollBack();
                throw $e;
            }
        }

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

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

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

    /**
     * Finds the CoContent model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CoContent the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CoContent::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
} 