<?php

class ArticleController extends Controller
{

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/main';

    /**
     * Displays a particular model.
     * @param integer $slug the ID of the model to be displayed
     */
    public function actionView($slug)
    {

        $model = $this->loadModel($slug);
        $this->setSeoProperty('keywords', $model->meta);
        if (isset($model->image) && $model->image != "" && $model->image != null)
            $this->setSeoProperty('image_src', Yii::app()->getBaseUrl(true) . "/uploads/Article/" . $model->image);
        $this->setSeoProperty('title', $model->title);
        $lang = Yii::app()->getLanguage();
        if ($lang == 'en')
            $this->setSeoProperty('description', strip_tags($model->description_en));
        else
            $this->setSeoProperty('description', strip_tags($model->description_ar));
        // $model->hits = $model->hits + 1;
        // $model->save(false);
        $this->render('view', array(
            'model' => $model,
        ));
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {

        $c = new CDbCriteria(array('scopes' => array('published', 'filterLanguage'), 'order' => 'create_datetime DESC'));
        $config = array(
            'pagination' => array(
                'pageSize' => 20
            ),
            'criteria' => $c,
        );

        $dataProvider = new CActiveDataProvider('Article', $config);
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }


    public function actionCategory($slug, $data = null)
    {
        $category = $this->loadCategory($slug);

        $criteria = new CDbCriteria(array('scopes' => array('published'), 'order' => 't.id DESC'));
        $criteria->together = true;
        $criteria->with = array('categories' => array('select' => 'id'));
        $criteria->compare('categories.id', $category->id);




        $dataProvider = new CActiveDataProvider('Article', array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 6
            ),
        ));
        $this->render('category', array(
            'category' => $category,
            'dataProvider' => $dataProvider, 'data' => $data
        ));
    }

    public function getArticles(){
        $featuerd = 1;
        $Criteria = new CDbCriteria(array('scopes' => array('published', 'filterLanguage'), 'order' => 't.create_datetime'));
        $Criteria->condition = "featured =:featured";
        $Criteria->params = array(':featured' => $featuerd);
        return Article::model()->findAll($Criteria);
    }

    public function actionMenu()
    {
        $this->render('menu', array(
            'articles' => $this->getArticles(),
        ));
    }

    public function actionScriptMenu(){
        $articles = $this->getArticles();
        $articles_data = [];
        foreach($articles as $article){
            $articles_data[] = [
                'id'    => $article->id,
                'image' => $article->image
            ];
        }
        $this->renderPartial('script_menu', [
            'articles_data' => $articles_data
        ]);
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $slug the ID of the model to be loaded
     * @return Article the loaded model
     * @throws CHttpException
     */
    public function loadModel($slug)
    {
        $model = Article::model()->published()->findByAttributes(array('slug' => $slug));
        if ($model === null)
            throw new CHttpException(404, '');
        return $model;
    }

    public function loadCategory($slug)
    {
        $model = Category::model()->findByAttributes(array('slug' => $slug));
        if ($model === null)
            throw new CHttpException(404, '');


        return $model;
    }
}
