<?php
/**
 * Created by PhpStorm.
 * User: PHOENIX
 * Date: 16.02.16
 * Time: 20:42
 */

namespace common\modules\messageTemplate\controllers;

use Yii;
use common\components\AdminController;
use common\modules\messageTemplate\models\MessageTemplate;
use yii\data\ActiveDataProvider;
use yii\swiftmailer\Message;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
use common\modules\messageTemplate\components\Templates;

class TemplateAdminController extends AdminController {

    /**
     * @return array|void
     */
    public static function actionsTitles(){
        return [
            'Manage' => 'Управление шаблонами',
            'Create' => 'Добавление шаблона',
            'Update' => 'Редактирование шаблона',
            'Delete' => 'Удаление шаблона',
            'Copy'   => 'Создание копии'
        ];
    }

    public function actionCopy($id){
        /** @var MessageTemplate $model */
        /** @var MessageTemplate $model_two */
        $model = new MessageTemplate();
        $model_two = $this->findModel($id);
        $model->attributes=$model_two->attributes;
        if ($model->save())
            $this->redirect(['update', 'id' => $model->getPrimaryKey()]);
        else
            throw new ServerErrorHttpException('Копирование не удалось', 500);
    }

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

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

    /**
     * @return string|\yii\web\Response
     */
    public function actionCreate(){
        $model = new MessageTemplate();

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save())
                return $this->redirect(
                    [
                        'manage'
                    ]
                );
        }
        $form = new \common\components\BaseForm('/common/modules/messageTemplate/forms/TemplateForm', $model);
        return $this->render(
            'create',
            [
                'model' => $model,
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     */
    public function actionUpdate($id){
        /** @var MessageTemplate $model */
        $model = MessageTemplate::findOne($id);

        if ($model->load(Yii::$app->request->post())) {
            if ($model->save())
                return $this->redirect(
                    [
                        'manage'
                    ]
                );
        }
        $form = new \common\components\BaseForm('/common/modules/messageTemplate/forms/TemplateForm', $model);
        return $this->render(
            'update',
            [
                'model' => $model,
                'form' => $form
            ]
        );
    }

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

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

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