OrderSearch.php 3.95 KB
<?php
    
    namespace artweb\artbox\ecommerce\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    
    /**
     * OrderSearch represents the model behind the search form about `\artweb\artbox\ecommerce\models\Order`.
     */
    class OrderSearch extends Order
    {
        public $date_from;
        public $date_to;
        public $date_range;
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'id',
                        'user_id',
                        'delivery',
                        'payment',
                        'status',
                    ],
                    'integer',
                ],
                [
                    [
                        'name',
                        'email',
                        'phone',
                        'date_from',
                        'date_to',
                        'date_range',
                        'created_at'
                    ],
                    '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 = Order::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(
                [
                    'id'       => $this->id,
                    'user_id'  => $this->user_id,
                    'delivery' => $this->delivery,
                    'payment'  => $this->payment,
                    'status'   => $this->status,
                ]
            );
            
            $query->andFilterWhere(
                [
                    'like',
                    'name',
                    $this->name,
                ]
            )
                  ->andFilterWhere(
                      [
                          'like',
                          'email',
                          $this->email,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'phone',
                          $this->phone,
                      ]
                  );
            if (!empty($this->date_range)) {
                $this->date_from = strtotime(explode('to', $this->date_range)[ 0 ]);
                $this->date_to = strtotime(explode('to', $this->date_range)[ 1 ]);
    
                $query->andFilterWhere(
                    [
                        '>=',
                        'created_at',
                        $this->date_from,
                    ]
                );
                $query->andFilterWhere(
                    [
                        '<=',
                        'created_at',
                        $this->date_to,
                    ]
                );
            }
            
            return $dataProvider;
        }
    }