<?php

namespace backend\controllers;

use Yii;
use common\models\Newsletter;
use backend\models\NewsletterSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Customer;
use backend\models\NewsletterTestForm;
use common\components\Helper;

/**
 * NewsletterController implements the CRUD actions for Newsletter model.
 */
class NewsletterController  extends \backend\controllers\BackendController
{
    
    public function behaviors()
    {
    	return [
    			'verbs' => [
    					'class' => VerbFilter::className(),
    					'actions' => [
    							'delete' => ['post'],
    					],
    			],
    			'access' => [
    					'class' => \yii\filters\AccessControl::className(),
    					'rules' => [
    							// allow authenticated users
    							[
    									'actions' => ['create','index','view','update','delete'],
    									'allow' => true,
    									'roles' => ['admin','employee','reception'],
    							],
    							// everything else is denied
    					],
    			],
    	];
    }
    

    /**
     * Lists all Newsletter models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new NewsletterSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single Newsletter model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
    	$model = $this->findModel($id);
    	$testModel = new NewsletterTestForm(
    		[
    			'newsletter' => $model
    		]
    			);
    	
    	
    	if ($testModel->load(Yii::$app->request->post()) && $testModel->validate()) {
    		$testModel->sendEmail();
    		Helper::flash("success", "Teszt e-mail elküldve");	
    		return $this->redirect(['view', 'id' => $model->id_newsletter]);
    	}
    	
        return $this->render('view', [
            'model' => $model,
        	'testModel' => $testModel
        ]);
    }

    /**
     * Creates a new Newsletter model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Newsletter();

        $model->status = Newsletter::$STATUS_ACTIVE;
        $model->sent = Newsletter::$SENT_NOT;
        
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id_newsletter]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Newsletter model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id_newsletter]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Newsletter model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        
        $model->status =  Newsletter::$STATUS_DELETED;
        
        $model->save(false);

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

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