CustomerSearch.php 4.71 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',
                        'rating',
                        'online',
                    ],
                    'integer',
                ],
                [
                    [
                        'city',
                        'info',
                    ],
                    'safe',
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'type'         => Yii::t('app', 'Тип заказчика'),
                'rating'         => Yii::t('app', 'Рейтинг'),
                'online'         => Yii::t('app', 'Статус'),
                'city'         => Yii::t('app', 'Город'),
                'info'         => Yii::t('app', 'Любая информация о заказчике'),
            ];
        }

        /**
         * @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,
            ]);

            $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([
                'like',
                '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;
        }
    }