OSDN Git Service

e84d74a7cf80dfad79649d3c4a6b64d4ae66cbb4
[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         ]);
168
169         $dataProvider->setSort([
170             'defaultOrder' => [
171                 'id' => SORT_ASC,
172             ],
173         ]);
174
175         $this->load($params);
176
177         if (!$this->validate())
178         {
179             // uncomment the following line if you do not want to return any records when validation fails
180             // $query->where('0=1');
181             return $dataProvider;
182         }
183
184         // grid filtering conditions
185         $query->andFilterWhere([
186             'id'   => $this->id,
187         ])->andFilterWhere([
188             'ilike', Status::tableName() .    '.name', $this->statusName
189         ])->andFilterWhere([ 'OR', [
190             'ilike', Models::tableName() .    '.name', $this->modelName
191         ], [
192             'ilike', Items::tableName() .     '.name', $this->modelName
193         ]])->andFilterWhere([
194             'ilike', Types::tableName() .     '.name', $this->typeName
195         ])->andFilterWhere([ 'OR', [
196             'ilike', Locations::tableName() . '.name', $this->regionName
197         ], [
198             'ilike', Regions::tableName() .   '.name', $this->regionName
199         ]])->andFilterWhere([ 'OR', [
200             'ilike', Locations::tableName() . '.name', $this->locationName
201         ], [
202             'ilike', Regions::tableName() .   '.name', $this->locationName
203         ]]);
204
205         $query->andFilterWhere(['ilike', 'name',        $this->name])
206             ->andFilterWhere(  ['ilike', 'os',          $this->os])
207             ->andFilterWhere(  ['ilike', 'mac',         $this->mac])
208             ->andFilterWhere(  ['ilike', 'serial',      $this->serial])
209             ->andFilterWhere(  ['ilike', 'invent',      $this->invent])
210             ->andFilterWhere(  ['ilike', 'comment',     $this->comment]);
211
212         $dataProvider->sort->attributes['modelName'] = [
213             'asc'  => [Models::tableName() . '.name' => SORT_ASC],
214             'desc' => [Models::tableName() . '.name' => SORT_DESC],
215         ];
216         $dataProvider->sort->attributes['statusName'] = [
217             'asc'  => [Status::tableName() . '.name' => SORT_ASC],
218             'desc' => [Status::tableName() . '.name' => SORT_DESC],
219         ];
220         $dataProvider->sort->attributes['typeName'] = [
221             'asc'  => [Types::tableName() . '.name' => SORT_ASC],
222             'desc' => [Types::tableName() . '.name' => SORT_DESC],
223         ];
224         $dataProvider->sort->attributes['locationName'] = [
225             'asc'  => [Locations::tableName() . '.name' => SORT_ASC],
226             'desc' => [Locations::tableName() . '.name' => SORT_DESC],
227         ];
228         $dataProvider->sort->attributes['regionName'] = [
229             'asc'  => [Regions::tableName() . '.name' => SORT_ASC],
230             'desc' => [Regions::tableName() . '.name' => SORT_DESC],
231         ];
232
233         return $dataProvider;
234     }
235 }