| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace modules\users\models\search; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use yii\base\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use yii\data\ActiveDataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\db\ActiveQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use yii\db\QueryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use modules\users\models\query\UserQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use modules\users\models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class UserSearch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package modules\users\models\backend | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property string $userRoleName User Role Name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property string $date_from Date From | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property string $date_to Date To | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property-read UserQuery $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @property integer $pageSize Page Size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class UserSearch extends User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public $userRoleName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public $date_from; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public $date_to; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function rules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             [['id', 'status'], 'integer'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             [['username', 'email', 'role', 'userRoleName', 'date_from', 'date_to'], 'safe'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function scenarios() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         // bypass scenarios() implementation in the parent class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return Model::scenarios(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return UserQuery | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     protected function getQuery() | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $query = User::find(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $query->innerJoinWith('profile', ['profile.user_id' => 'id']); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%user}}.id'); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         return $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param ActiveQuery $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return ActiveDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function getDataProvider($query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return new ActiveDataProvider([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'query' => $query, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'pagination' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 'defaultPageSize' => 25 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'sort' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 'defaultOrder' => ['id' => SORT_ASC], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 'attributes' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     'id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     'username', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     'email', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     'status', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     'userRoleName' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                         'asc' => ['item_name' => SORT_ASC], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                         'desc' => ['item_name' => SORT_DESC], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                         'default' => SORT_ASC, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                         'label' => 'Role Name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     'profile.last_visit' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                         'asc' => ['item_name' => SORT_ASC], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                         'desc' => ['item_name' => SORT_DESC], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                         'default' => SORT_ASC, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                         'label' => 'Last Visit', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return mixed|ActiveDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function search($params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $query = $this->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $dataProvider = $this->getDataProvider($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->load($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (!$this->validate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             // uncomment the following line if you do not want to return any records when validation fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $query->where('0=1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             return $dataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->processFilter($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $dataProvider = $this->setTotalCount($query, $dataProvider); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $dataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @param $query QueryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     protected function processFilter($query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // grid filtering conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $query->andFilterWhere([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             'id' => $this->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             'status' => $this->status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'item_name' => $this->userRoleName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $query->andFilterWhere(['like', 'username', $this->username]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             ->andFilterWhere(['like', 'email', $this->email]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             ->andFilterWhere(['<=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 23:59:59') : null]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param ActiveQuery $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param ActiveDataProvider $dataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     protected function setTotalCount($query, $dataProvider) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         if (is_int($query->count())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $dataProvider->pagination->totalCount = $query->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return $dataProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |  |