OptionGroupSearch.php 3.63 KB
<?php
    
    namespace artbox\catalog\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use yii\db\ActiveQuery;
    
    /**
     * OptionGroupSearch represents the abstract search model for Option Groups.
     */
    abstract class OptionGroupSearch extends OptionGroup
    {
        
        public $title;
        
        public $category;
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'id',
                        'sort',
                    ],
                    'integer',
                ],
                [
                    [
                        'is_filter',
                        'status',
                        'in_menu',
                    ],
                    'boolean',
                ],
                [
                    [
                        'title',
                        'category',
                    ],
                    'safe',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [];
        }
        
        /**
         * @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 = $this->createQuery()
                          ->joinWith(
                              [
                                  'lang',
                                  'categories.lang',
                              ]
                          );
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query'      => $query,
                    'pagination' => [
                        'pageSize' => 50,
                    ],
                ]
            );
            $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(
                [
                    'id'        => $this->id,
                    'is_filter' => $this->is_filter,
                    'sort'      => $this->sort,
                    'status'    => $this->status,
                    'in_menu'   => $this->in_menu,
                ]
            );
            
            $query->andFilterWhere(
                [
                    'ilike',
                    'category_lang.title',
                    $this->category,
                ]
            );
            
            return $dataProvider;
        }
        
        /**
         * @inheritdoc
         */
        public function getCategories()
        {
            return Category::find();
        }
        
        /**
         * @inheritdoc
         */
        public function getOptions()
        {
            
        }
        
        /**
         * Return ActiveQueyr for current model
         *
         * @return \yii\db\ActiveQuery
         */
        abstract protected function createQuery(): ActiveQuery;
    }