<?php

namespace common\modules\school\controllers;

use yii\filters\AccessControl;

use common\components\FrontendController;
use common\modules\school\models\Courses;
use common\modules\school\models\SearchCourses;
use common\modules\school\models\SearchLessons;
use common\modules\users\models\User;
use common\models\LoginForm;

class CourseController extends FrontendController
{
    public static function actionsTitles()
    {
        return [
            'Index'	 => 'Курсы',
            'View'	 => 'Курс',
        ];
    }

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'user' => 'support',
                'only' => ['view', 'index'],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['view', 'index'],
                        'roles' => ['?'],
                    ],
                ],
            ],
        ];
    }
    
    public function actionIndex()
    {        
        \Yii::$app->view->registerMetaTag([
            'property' => 'og:title',
            'content' => 'Школа аналитики'
        ]);
        $searchModel  = new SearchCourses();
        $search = \Yii::$app->request->queryParams;
        $dataProvider = $searchModel->search($search);		
        
        /*$modelUser = new User();
        $modelLogin = new LoginForm();
                
        $formPopup = new \common\components\BaseForm('/common/modules/users/forms/RegistrationUserPopupForm', $modelUser);
        //$form = new \common\components\BaseForm('/common/modules/users/forms/RegistrationUserForm', $modelUser);
        //$formLogin = new \common\components\BaseForm('/common/modules/users/forms/LoginForm', $modelLogin);
        $formLoginPopup = new \common\components\BaseForm('/common/modules/users/forms/LoginPopupForm', $modelLogin);*/
        return $this->render('index', ['dataProvider' => $dataProvider, 
            /*'formPopup' => $formPopup->out, 'formLoginPopup' => $formLoginPopup->out*/]);
    }
    
    public function actionView($id)
    {
        if($_GET["source"] == 'email' && !Yii::$app->response->cookies->get('source')){
            $options['name'] = 'source';
            $options['value'] = 'email'; 
            $options['expire'] = time()+86400*365; 
            $cookie = new \yii\web\Cookie($options);
            \Yii::$app->response->cookies->add($cookie); 
        }
        $model = Courses::findOne($id);
        $searchModel  = new SearchLessons();
        $search['course_id'] = $id;
        $dataProvider = $searchModel->search($search);
        return $this->render('view', ['dataProvider' => $dataProvider, 'model' => $model]);
    }
}
