<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout','index'],
                'rules' => [
                    [
                        'actions' => ['logout','index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
                'denyCallback'=>function ($rule,$action){return $this->redirect(['admins/login']); },
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionMenuLanguageArabic($lang, $current_url){
    
        \Yii::$app->language = $lang;
    
        $languageCookie = new \yii\web\Cookie([
            'name' => 'language',
            'value' => $lang,
        ]);
        Yii::$app->getResponse()->getCookies()->add($languageCookie);
            
        Yii::$app->session->set('language', $lang);
            
        return $this->redirect($current_url);
    }

    public function actionMenuLanguageEnglish($lang, $current_url){
    
        \Yii::$app->language = $lang;
    
        $languageCookie = new \yii\web\Cookie([
            'name' => 'language',
            'value' => $lang,
        ]);
        Yii::$app->getResponse()->getCookies()->add($languageCookie);
            
        Yii::$app->session->set('language', $lang);
            
        return $this->redirect($current_url);
    }

    public function actionChangeLanguage($lang, $current_url){
    
        \Yii::$app->language = $lang;
    
        $languageCookie = new \yii\web\Cookie([
            'name' => 'language',
            'value' => $lang,
        ]);
        Yii::$app->getResponse()->getCookies()->add($languageCookie);
            
        Yii::$app->session->set('language', $lang);
            
        return $this->redirect($current_url);
    }
}
