Your IP : 216.73.216.124


Current Path : /var/www/html/chatbot/backend/models/
Upload File :
Current File : /var/www/html/chatbot/backend/models/ContentArticleSearch.php

<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\ContentArticle;

/**
 * ContentArticleSearch represents the model behind the search form of `backend\models\ContentArticle`.
 */
class ContentArticleSearch extends ContentArticle
{
    public $year;
    public $month;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['content_article_id', 'content_article_status'], 'integer'],
            [['content_article_title_my', 'content_article_title_en', 'content_article_category', 'content_article_subcategory', 'content_article_content_my', 'content_article_content_en', 'created_at', 'created_by', 'updated_at', 'updated_by'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ContentArticle::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'created_at' => SORT_DESC
                ]
            ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'content_article_id' => $this->content_article_id,
            'content_article_status' => $this->content_article_status,
            // 'created_at' => $this->created_at,
            // 'updated_at' => $this->updated_at,
        ]);
        
        $role  = Yii::$app->user->identity->staffroles[0];
        $rolecat = parent::retRolecat()[$role] ?? $role;

        if (!empty($this->year)) {
            $query->andFilterWhere(['YEAR(created_at)' => $this->year]);
        }

        if (!empty($this->month)) {
            $query->andFilterWhere(['MONTH(created_at)' => $this->month]);
        }

        if (!empty($this->content_article_status)) {
            $query->andFilterWhere(['content_article_status' => $this->content_article_status]);
        }
        
        if($role != 'ADM'){
            $query->andFilterWhere(['in', 'content_article_category', $rolecat]);
        }

        $query->andFilterWhere(['like', 'content_article_title_my', $this->content_article_title_my])
            ->andFilterWhere(['like', 'content_article_title_en', $this->content_article_title_en])
            ->andFilterWhere(['like', 'content_article_category', $this->content_article_category])
            ->andFilterWhere(['like', 'content_article_subcategory', $this->content_article_subcategory])
            ->andFilterWhere(['like', 'content_article_content_my', $this->content_article_content_my])
            ->andFilterWhere(['like', 'content_article_content_en', $this->content_article_content_en])
            ->andFilterWhere(['like', 'created_by', $this->created_by])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by]);

        return $dataProvider;
    }
}