CustomerSearch.php 7.35 KB
<?php

    namespace common\models;

    use Yii;
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use common\models\User;

    /**
     * CustomerSearch represents the model behind the search form about `common\models\UserInfo`.
     */
    class CustomerSearch extends User
    {

        public $type;

        public $rating;

        public $online;

        public $city;

        public $info;

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'type',
                        'online',
                    ],
                    'integer',
                ],
                [
                    [ 'rating' ],
                    'number',
                    'min' => 0,
                    'max' => 10,
                ],
                [
                    [ 'rating' ],
                    'default',
                    'value' => 0,
                ],
                [
                    [
                        'city',
                        'info',
                    ],
                    'safe',
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'type'   => Yii::t('app', 'type'),
                'rating' => Yii::t('app', 'rating'),
                'online' => Yii::t('app', 'online'),
                'city'   => Yii::t('app', 'city'),
                'info'   => Yii::t('app', 'info'),
            ];
        }

        /**
         * @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 = User::find()
                         ->joinWith('userInfo')
                         ->joinWith('companyInfo');

            $dataProvider = new ActiveDataProvider([
                'query' => $query,
            ]);

            $dataProvider->setSort([
                'defaultOrder' => [
                    'name' => SORT_ASC,
                ],
                'attributes'   => [
                    'name'  => [
                        'asc'     => [
                            'company_info.name' => SORT_ASC,
                            'firstname'         => SORT_ASC,
                            'lastname'          => SORT_ASC,
                        ],
                        'desc'    => [
                            'company_info.name' => SORT_DESC,
                            'firstname'         => SORT_DESC,
                            'lastname'          => SORT_DESC,
                        ],
                        'default' => SORT_ASC,
                        'label'   => 'Название',
                    ],
                    'staff' => [
                        'asc'     => [
                            'company_info.staff' => SORT_ASC,
                        ],
                        'desc'    => [
                            'company_info.staff' => SORT_DESC,
                        ],
                        'default' => SORT_DESC,
                        'label'   => 'Количество сотрудников',
                    ],
                    'visit' => [
                        'asc'     => [
                            'user_info.date_visit' => SORT_ASC,
                        ],
                        'desc'    => [
                            'user_info.date_visit' => SORT_DESC,
                        ],
                        'default' => SORT_DESC,
                        'label'   => 'Последний визит',
                    ],
                    'city'  => [
                        'asc'     => [
                            'user_info.city' => SORT_ASC,
                        ],
                        'desc'    => [
                            'user_info.city' => SORT_DESC,
                        ],
                        'default' => SORT_ASC,
                        'label'   => 'Город',
                    ],
                ],
            ]);

            $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;
            }

            $query->andWhere([
                'is_customer' => 1,
            ]);

            if($this->type == 2) {
                $query->andWhere([
                    'not',
                    [ 'company_info.company_info_id' => NULL ],
                ]);

                $query->andWhere([
                    'type' => 2,
                ]);
            } elseif($this->type == 1) {
                $query->andWhere([
                    'type' => 1,
                ]);
            }

            if($this->online == 1) {
                $query->andWhere([
                    '>=',
                    'user_info.date_visit',
                    date('Y-m-d H:i:s.u', time() - 1800),
                ]);
            }

            $query->andFilterWhere([
                '>=',
                'user_info.rating',
                $this->rating,
            ]);

            $query->andFilterWhere([
                'user_info.city' => $this->city,
            ])
                  ->andFilterWhere([
                      'or',
                      [
                          'like',
                          'LOWER(username)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(lastname)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(firstname)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(middlename)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(company_info.name)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(company_info.street)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(user_info.country)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(user_info.city)',
                          mb_strtolower($this->info),
                      ],
                      [
                          'like',
                          'LOWER(user_info.about)',
                          mb_strtolower($this->info),
                      ],
                  ]);

            return $dataProvider;
        }
    }