VariantSearch.php 4.68 KB
<?php
    
    namespace artbox\catalog\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    
    /**
     * VariantSearch represents the model behind the search form about `artbox\catalog\models\Variant`.
     */
    class VariantSearch extends Variant
    {
    
        public $title;
        public $product_title;
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [];
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'id',
                    ],
                    'integer',
                ],
                [
                    [
                        'sku',
                        'title',
                        'product_title',
                    ],
                    'safe',
                ],
                [
                    [
                        'price',
                        'price_old',
                    ],
                    'number',
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
            ];
        }
        
        /**
         * @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 = Variant::find()
                            ->joinWith('lang')
                            ->joinWith('product.lang');
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => $query,
                    'sort'  => [
                        'attributes' => [
                            'id',
                            'title'         => [
                                'asc'  => [
                                    'variant_lang.title' => SORT_ASC,
                                ],
                                'desc' => [
                                    'variant_lang.title' => SORT_DESC,
                                ],
                            ],
                            'sku',
                            'product_title' => [
                                'asc'  => [
                                    'product_lang.title' => SORT_ASC,
                                ],
                                'desc' => [
                                    'product_lang.title' => SORT_DESC,
                                ],
                            ],
                            'price',
                            'stock',
                            'created_at',
                            'status',
                            'sort',
                        ],
                    ],
                ]
            );
            
            $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,
                    'variant.product_id' => $this->product_id,
                    'price'              => $this->price,
                    'price_old'          => $this->price_old,
                    'stock'              => $this->stock,
                    'status'             => $this->status,
                    'sort'               => $this->sort,
                    'created_at'         => $this->created_at,
                    'updated_at'         => $this->updated_at,
                    'sku'                => $this->sku,
                ]
            )
                  ->andFilterWhere(
                      [
                          'like',
                          'variant_lang.title',
                          $this->title,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'product_lang.title',
                          $this->product_title,
                      ]
                  );
            
            return $dataProvider;
        }
    }