<?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;
use app\models\Contact;
use yii\config\db;
use yii\config\thdb;
use yii\config\j4db;
use \yii\db\Query;

class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            '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');
    }

    /**
     * Login action.
     *
     * @return Response|string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }

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

    /**
     * Logout action.
     *
     * @return Response
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return Response|string
     */
    /*public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }*/
    public function actionContact()
    {
        
        $formelemek = new ContactForm();
        $contactmodel = new Contact();
        //$contactmodel->getOtpConfig(Yii::$app->user->identity->id);
        /*if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }*/
        $validate = Yii::$app->getRequest()->getQueryParam('validate');

        if(isset($validate) && $validate == 1)
        {
           
            $validated = $contactmodel->TOTPValidate();
            if($validated === TRUE)
            {
                Yii::$app->session->setFlash('message', 'Sikeres validáció!');
                Yii::$app->getResponse()->redirect('index.php?r=site%2Fcontact');
                echo 'Sikeres validáció!';
            }
            else
            {
                Yii::$app->session->setFlash('message', 'Sikertelen validáció!');
                Yii::$app->getResponse()->redirect('index.php?r=site%2Fcontact');
                 echo 'Sikertelen validáció!';
            }

        }
        
        return $this->render('contact', [
            'formelemek' => $formelemek,
        ]);
        
        
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }

    public function actionMigrator()
    {
        /*$query = new Query();
        $query->select(['*']);
        $query->from(['gigkt_content']);
        $query->where('id > 0');
        $j3content = $query->all();

        $i = 1;
        foreach ($j3content as $key => $value) 
        {
            $query = new Query();
            $query->select(['id']);
            $query->from(['gigkt_assets']);
            $query->where('name = "com_content.category.'.$value['catid'].'"');
            $speckoid = $query->one();

            /*Régi assets*/
           /*Yii::$app->db->createCommand('INSERT INTO uok1r_assets (parent_id, lft, rgt, level, name, title, rules) VALUES ('.$speckoid['id'].', 19, 20, 3, "com_content.article.'.$value['id'].'", "'.$value['title'].'", "")')->execute();
            
            /*Vissza a régi assetsből*/
            /*$query = new Query();
            $query->select(['max(id) lastid']);
            $query->from(['uok1r_assets']);
            $lastid = $query->one();

            Yii::$app->db->createCommand('UPDATE gigkt_content SET asset_id = '.$lastid['lastid'].' WHERE id = '.$value['id'])->execute();

            
        }

        die('ende');*/
    }

    public function actionThmigrator()
    {
        

        
        
    }
    public function actionKkimigrator()
    {
        $content = Yii::$app->kkidb->createCommand('SELECT id,title,alias FROM oe4p1_content WHERE catid = 8')->queryAll();
        $month = array('01' => 'januar', '02' => 'februar','03' => 'marcius','04' => 'aprilis','05' => 'majus','06' => 'junius','07' => 'julius','08' => 'augusztus','09' => 'szeptember', '10' => 'oktober', '11' => 'november', '12' => 'december');
        /*echo '<pre>';
        print_r($month);
        echo '</pre>';*/
         /*echo '<pre>';
        print_r($content);
        echo '</pre>';
        die();*/
        $back = '';
        foreach ($content as $ckey => $cvalue) 
        { 
            $datearray = explode('-', $cvalue['alias']);
            /*echo '<pre>';
        print_r($datearray);
        echo '</pre>';
            
            echo var_export(strpos($datearray[1], 'februar'));
            die();*/
            $string = (string)(isset($datearray[1]) ? $datearray[1] : '');
            if(strpos($string, 'januar') !== FALSE)
            {
                $newdate = str_replace('januar', '01', $string);
            }
            else if(strpos($string, 'februar') !== FALSE)
            {
                $newdate = str_replace('februar', '02', $string);
            }
            else if(strpos($string, 'marcius') !== FALSE)
            {
                $newdate = str_replace('marcius', '03', $string);
            }
            else if(strpos($string, 'aprilis')!== FALSE)
            {
                $newdate = str_replace('aprilis', '04', $string);
            }
            else if(strpos($string, 'majus')!== FALSE)
            {
                $newdate = str_replace('majus', '05', $string);
            }
            else if(strpos($string, 'junius')!== FALSE)
            {
                $newdate = str_replace('junius', '06', $string);
            }
            else if(strpos($string, 'julius')!== FALSE)
            {
                $newdate = str_replace('julius', '07', $string);
            }
            else if(strpos($string, 'augusztus')!== FALSE)
            {
                $newdate = str_replace('augusztus', '08', $string);
            }
            else if(strpos($string, 'szeptember')!== FALSE)
            {
                $newdate = str_replace('szeptember', '09', $string);
            }
            else if(strpos($string, 'oktober')!== FALSE)
            {
                $newdate = str_replace('oktober', '10', $string);
            }
            else if(strpos($string, 'november')!== FALSE)
            {
                $newdate = str_replace('november', '11', $string);
            }
            else if(strpos($string, 'december')!== FALSE)
            {
                $newdate = str_replace('december', '12', $string);
            }
            else
            {
                $newdate = 'hiba';
            }

           
           //echo $datearray[0].'-'.$newdate.'-'.$datearray[2].' 00:00:00'.'<br>';
           //$back.= $cvalue['alias'].'<br>';
           $back.= 'UPDATE oe4p1_content SET publish_up = "'.$datearray[0].'-'.$newdate.'-'.(isset($datearray[2]) ? ($datearray[2] < 10 ? '0'.$datearray[2] : $datearray[2]) : '').' 00:00:00" WHERE id ='.$cvalue['id'].';<br>';

        }
        echo $back;
    }

    public function stripAccents($str) 
    {
        return strtr(utf8_decode($str), utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
    }

    public function actionCorrector()
    {
        /*$articles_for_assets_array = Yii::$app->kkidb->createCommand('SELECT * FROM oe4p1_content')->queryAll();

        foreach ($articles_for_assets_array as $key => $value) 
        {
           $x = str_replace('.','',$value['title']);
           $x = str_replace('   ',' ',$x);
           $x = str_replace(' ','-',$x);
           $x = str_replace('---','-',$x);
           $x = $this->stripAccents($x);

           echo $x.'<br>';

           Yii::$app->kkidb->createCommand('UPDATE oe4p1_content SET alias = "'.$x.'" WHERE id = '.$value['id'])->execute();
        }*/

    }
}


