AliasSearch.php 3.42 KB
<?php
    
    namespace artbox\core\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    
    /**
     * AliasSearch represents the model behind the search form about `artbox\core\models\Alias`.
     */
    class AliasSearch extends Alias
    {
        
        public $search;
        public $static;
        public $entity;
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'search',
                        'entity',
                    ],
                    'string',
                ],
                [
                    [ 'language_id' ],
                    'integer',
                ],
                [
                    [ 'static' ],
                    'boolean',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function scenarios()
        {
            // bypass scenarios() implementation in the parent class
            return Model::scenarios();
        }
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [];
        }
        
        /**
         * Creates data provider instance with search query applied
         *
         * @param array $params
         *
         * @return ActiveDataProvider
         */
        public function search($params)
        {
            $query = Alias::find();
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => $query,
                ]
            );
            
            $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(
                [
                    'entity' => $this->entity,
                ]
            );
    
            $query->andFilterWhere(
                [
                    'ilike',
                    'value',
                    $this->search,
                ]
            );
            
            // If language_id == 0, search for records with null language
            if ($this->language_id === '0') {
                $query->andWhere(
                    [
                        'language_id' => null,
                    ]
                );
            } else {
                $query->andFilterWhere(
                    [
                        'language_id' => $this->language_id,
                    ]
                );
            }
    
            // If language_id == 0, search for records without entities
            if ($this->static === '0') {
                $query->andWhere(
                    [
                        'entity' => null,
                    ]
                );
            } elseif ($this->static === '1') {
                $query->andWhere(
                    [
                        'not',
                        [
                            'entity' => null,
                        ],
                    ]
                );
            }
            
            return $dataProvider;
        }
    }