OSDN Git Service

Добавлена возможность изменения количества записей, выводимых на странице предметов...
[invent/invent.git] / models / ItemsSearch.php
1 <?php
2
3 namespace app\models;
4
5 use yii\base\Model;
6 use yii\data\ActiveDataProvider;
7 use app\models\Items;
8 use app\models\Status;
9 use app\models\Types;
10 use app\models\Locations;
11 use app\models\Regions;
12 use app\models\Moving;
13
14 /**
15  * ItemsSearch represents the model behind the search form of `app\models\Items`.
16  */
17 class ItemsSearch extends Items
18 {
19     /**
20      * {@inheritdoc}
21      */
22     public function rules()
23     {
24         return [
25             [['id', 'model_id'], 'integer'],
26             [['name', 'modelName', 'os', 'mac', 'serial', 'invent', 'date', 'comment', 'statusName', 'typeName', 'locationName', 'regionName'], 'safe'],
27         ];
28     }
29
30     /**
31      * {@inheritdoc}
32      */
33     public function scenarios()
34     {
35         // bypass scenarios() implementation in the parent class
36         return Model::scenarios();
37     }
38
39     public function noinvent($params)
40     {
41
42         $query = Moving::find()
43             ->select('MAX(' . Moving::tableName() . '.id) AS mid');
44         if (isset($params->region) && ($params->region != ''))
45         {
46             $query->joinWith([ 'locations' ])
47                 ->where([ 'region_id' => $params->region ]);
48         }
49         if (isset($params->location) && ($params->location != ''))
50         {
51             if (isset($params->region) && ($params->location != ''))
52             {
53                 $query->andWhere(['location_id' => $params->location]);
54             }
55             else
56             {
57                 $query->where(['location_id' => $params->location]);
58             }
59         }
60         $query->distinct('item_id')->groupBy('item_id');
61
62         $query = Items::find()
63             ->select(Items::tableName() . '.*, ' .
64                 Locations::tableName() .  '.name AS locationName, ' .
65                 Models::tableName() .     '.name AS modelName, ' .
66                 Types::tableName() .      '.name AS typeName, ' .
67                 Regions::tableName() .    '.name AS regionName, ' .
68                 Status::tableName() .     '.name AS statusName ')
69             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models', ])
70             ->where([ 'in', Moving::tableName() . '.id', $query ])
71             ->andWhere([ 'checked' => false ]);
72
73         $dataProvider = new ActiveDataProvider([
74             'query' => $query,
75         ]);
76         $dataProvider->setSort([
77             'defaultOrder' => [
78                 'id' => SORT_ASC,
79             ],
80         ]);
81
82         $this->load($params);
83         if (!$this->validate())
84         {
85             return $dataProvider;
86         }
87
88         // grid filtering conditions
89         $query->andFilterWhere([
90             'id'   => $this->id,
91         ])->andFilterWhere([
92             'ilike', Status::tableName() .    '.name', $this->statusName
93         ])->andFilterWhere([
94             'ilike', Models::tableName() .    '.name', $this->modelName
95         ])->andFilterWhere([
96             'ilike', Types::tableName() .     '.name', $this->typeName
97         ])->andFilterWhere([ 'OR', [
98             'ilike', Locations::tableName() . '.name', $this->regionName
99         ], [
100             'ilike', Regions::tableName() .   '.name', $this->regionName
101         ]])->andFilterWhere([ 'OR', [
102             'ilike', Locations::tableName() . '.name', $this->locationName
103         ], [
104             'ilike', Regions::tableName() .   '.name', $this->locationName
105         ]]);
106
107         $query->andFilterWhere(['ilike', 'name',        $this->name])
108             ->andFilterWhere(  ['ilike', 'os',          $this->os])
109             ->andFilterWhere(  ['ilike', 'mac',         $this->mac])
110             ->andFilterWhere(  ['ilike', 'serial',      $this->serial])
111             ->andFilterWhere(  ['ilike', 'invent',      $this->invent])
112             ->andFilterWhere(  ['ilike', 'comment',     $this->comment]);
113
114
115         $dataProvider->sort->attributes['modelName'] = [
116             'asc'  => [ Models::tableName() . '.name' => SORT_ASC ],
117             'desc' => [ Models::tableName() . '.name' => SORT_DESC ],
118         ];
119         $dataProvider->sort->attributes['statusName'] = [
120             'asc'  => [ Status::tableName() . '.name' => SORT_ASC ],
121             'desc' => [ Status::tableName() . '.name' => SORT_DESC ],
122         ];
123         $dataProvider->sort->attributes['typeName'] = [
124             'asc'  => [ Types::tableName() . '.name' => SORT_ASC ],
125             'desc' => [ Types::tableName() . '.name' => SORT_DESC ],
126         ];
127         $dataProvider->sort->attributes['locationName'] = [
128             'asc'  => [ Locations::tableName() . '.name' => SORT_ASC ],
129             'desc' => [ Locations::tableName() . '.name' => SORT_DESC ],
130         ];
131         $dataProvider->sort->attributes['regionName'] = [
132             'asc'  => [ Regions::tableName() . '.name' => SORT_ASC ],
133             'desc' => [ Regions::tableName() . '.name' => SORT_DESC ],
134         ];
135         return $dataProvider;
136     }
137
138     /**
139      * Creates data provider instance with search query applied
140      *
141      * @param array $params
142      *
143      * @return ActiveDataProvider
144      */
145     public function search($params)
146     {
147         // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
148         $subQuery = Moving::find()
149                 ->select('MAX(id) AS id')
150                 ->distinct('item_id')
151                 ->groupBy([ 'item_id' ]);
152
153         $query = Items::find()
154             ->select(Items::tableName() . '.*, ' .
155                 Locations::tableName() .  '.name AS locationName, ' .
156                 Models::tableName() .     '.name AS modelName, ' .
157                 Types::tableName() .      '.name AS typeName, ' .
158                 Regions::tableName() .    '.name AS regionName, ' .
159                 Status::tableName() .     '.name AS statusName ')
160             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models', ])
161             ->where([ 'in', Moving::tableName() . '.id', $subQuery ]);
162
163         // add conditions that should always apply here
164
165         $dataProvider = new ActiveDataProvider([
166             'query' => $query,
167             'pagination' => [
168                 'pageSize' => \Yii::$app->session['pageSize'] ?? 20,
169             ],
170         ]);
171
172         $dataProvider->setSort([
173             'defaultOrder' => [
174                 'id' => SORT_ASC,
175             ],
176         ]);
177
178         $this->load($params);
179
180         if (!$this->validate())
181         {
182             // uncomment the following line if you do not want to return any records when validation fails
183             // $query->where('0=1');
184             return $dataProvider;
185         }
186
187         // grid filtering conditions
188         $query->andFilterWhere([
189             'id'   => $this->id,
190         ])->andFilterWhere([
191             'ilike', Status::tableName() .    '.name', $this->statusName
192         ])->andFilterWhere([ 'OR', [
193             'ilike', Models::tableName() .    '.name', $this->modelName
194         ], [
195             'ilike', Items::tableName() .     '.name', $this->modelName
196         ]])->andFilterWhere([
197             'ilike', Types::tableName() .     '.name', $this->typeName
198         ])->andFilterWhere([ 'OR', [
199             'ilike', Locations::tableName() . '.name', $this->regionName
200         ], [
201             'ilike', Regions::tableName() .   '.name', $this->regionName
202         ]])->andFilterWhere([ 'OR', [
203             'ilike', Locations::tableName() . '.name', $this->locationName
204         ], [
205             'ilike', Regions::tableName() .   '.name', $this->locationName
206         ]]);
207
208         $query->andFilterWhere(['ilike', 'name',        $this->name])
209             ->andFilterWhere(  ['ilike', 'os',          $this->os])
210             ->andFilterWhere(  ['ilike', 'mac',         $this->mac])
211             ->andFilterWhere(  ['ilike', 'serial',      $this->serial])
212             ->andFilterWhere(  ['ilike', 'invent',      $this->invent])
213             ->andFilterWhere(  ['ilike', 'comment',     $this->comment]);
214
215         $dataProvider->sort->attributes['modelName'] = [
216             'asc'  => [Models::tableName() . '.name' => SORT_ASC],
217             'desc' => [Models::tableName() . '.name' => SORT_DESC],
218         ];
219         $dataProvider->sort->attributes['statusName'] = [
220             'asc'  => [Status::tableName() . '.name' => SORT_ASC],
221             'desc' => [Status::tableName() . '.name' => SORT_DESC],
222         ];
223         $dataProvider->sort->attributes['typeName'] = [
224             'asc'  => [Types::tableName() . '.name' => SORT_ASC],
225             'desc' => [Types::tableName() . '.name' => SORT_DESC],
226         ];
227         $dataProvider->sort->attributes['locationName'] = [
228             'asc'  => [Locations::tableName() . '.name' => SORT_ASC],
229             'desc' => [Locations::tableName() . '.name' => SORT_DESC],
230         ];
231         $dataProvider->sort->attributes['regionName'] = [
232             'asc'  => [Regions::tableName() . '.name' => SORT_ASC],
233             'desc' => [Regions::tableName() . '.name' => SORT_DESC],
234         ];
235
236         return $dataProvider;
237     }
238 }