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                 Types::tableName() .      '.name AS typeName, ' .
66                 Regions::tableName() .    '.name AS regionName, ' .
67                 Status::tableName() .     '.name AS statusName ')
68             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models' ])
69             ->where([ 'in', Moving::tableName() . '.id', $query ])
70             ->andWhere([ 'checked' => false ]);
71
72         $dataProvider = new ActiveDataProvider([
73             'query' => $query,
74         ]);
75         $dataProvider->setSort([
76             'defaultOrder' => [
77                 'id' => SORT_ASC,
78             ],
79         ]);
80         $dataProvider->sort->attributes['modelName'] = [
81             'asc'  => [ Models::tableName() . '.name' => SORT_ASC ],
82             'desc' => [ Models::tableName() . '.name' => SORT_DESC ],
83         ];
84         $dataProvider->sort->attributes['statusName'] = [
85             'asc'  => [ Status::tableName() . '.name' => SORT_ASC ],
86             'desc' => [ Status::tableName() . '.name' => SORT_DESC ],
87         ];
88         $dataProvider->sort->attributes['typeName'] = [
89             'asc'  => [ Types::tableName() . '.name' => SORT_ASC ],
90             'desc' => [ Types::tableName() . '.name' => SORT_DESC ],
91         ];
92         $dataProvider->sort->attributes['locationName'] = [
93             'asc'  => [ Locations::tableName() . '.name' => SORT_ASC ],
94             'desc' => [ Locations::tableName() . '.name' => SORT_DESC ],
95         ];
96         $dataProvider->sort->attributes['regionName'] = [
97             'asc'  => [ Regions::tableName() . '.name' => SORT_ASC ],
98             'desc' => [ Regions::tableName() . '.name' => SORT_DESC ],
99         ];
100         return $dataProvider;
101     }
102
103     /**
104      * Creates data provider instance with search query applied
105      *
106      * @param array $params
107      *
108      * @return ActiveDataProvider
109      */
110     public function search($params)
111     {
112         // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
113         $subQuery = Moving::find()
114                 ->select('MAX(id) AS id')
115                 ->distinct('item_id')
116                 ->groupBy([ 'item_id' ]);
117
118         $query = Items::find()
119             ->select(Items::tableName() . '.*, ' .
120                 Locations::tableName() .  '.name AS locationName, ' .
121                 Models::tableName() . '.name AS modelName, ' .
122                 Types::tableName() .      '.name AS typeName, ' .
123                 Regions::tableName() .    '.name AS regionName, ' .
124                 Status::tableName() .     '.name AS statusName ')
125             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models', ])
126             ->where([ 'in', Moving::tableName() . '.id', $subQuery ]);
127
128         // add conditions that should always apply here
129
130         $dataProvider = new ActiveDataProvider([
131             'query' => $query,
132         ]);
133
134         $dataProvider->setSort([
135             'defaultOrder' => [
136                 'id' => SORT_ASC,
137             ],
138         ]);
139
140         $this->load($params);
141
142         if (!$this->validate())
143         {
144             // uncomment the following line if you do not want to return any records when validation fails
145             // $query->where('0=1');
146             return $dataProvider;
147         }
148
149         // grid filtering conditions
150         $query->andFilterWhere([
151             'id'   => $this->id,
152         ])->andFilterWhere([
153             'ilike', Status::tableName() .    '.name', $this->statusName
154         ])->andFilterWhere([
155             'ilike', Models::tableName() .    '.name', $this->modelName
156         ])->andFilterWhere([
157             'ilike', Types::tableName() .     '.name', $this->typeName
158         ])->andFilterWhere([ 'OR', [
159             'ilike', Locations::tableName() . '.name', $this->regionName
160         ], [
161             'ilike', Regions::tableName() .   '.name', $this->regionName
162         ]])->andFilterWhere([ 'OR', [
163             'ilike', Locations::tableName() . '.name', $this->locationName
164         ], [
165             'ilike', Regions::tableName() .   '.name', $this->locationName
166         ]]);
167
168         $query->andFilterWhere(['ilike', 'name',        $this->name])
169             ->andFilterWhere(  ['ilike', 'os',          $this->os])
170             ->andFilterWhere(  ['ilike', 'mac',         $this->mac])
171             ->andFilterWhere(  ['ilike', 'serial',      $this->serial])
172             ->andFilterWhere(  ['ilike', 'invent',      $this->invent])
173             ->andFilterWhere(  ['ilike', 'comment',     $this->comment]);
174
175         $dataProvider->sort->attributes['modelName'] = [
176             'asc'  => [Models::tableName() . '.name' => SORT_ASC],
177             'desc' => [Models::tableName() . '.name' => SORT_DESC],
178         ];
179         $dataProvider->sort->attributes['statusName'] = [
180             'asc'  => [Status::tableName() . '.name' => SORT_ASC],
181             'desc' => [Status::tableName() . '.name' => SORT_DESC],
182         ];
183         $dataProvider->sort->attributes['typeName'] = [
184             'asc'  => [Types::tableName() . '.name' => SORT_ASC],
185             'desc' => [Types::tableName() . '.name' => SORT_DESC],
186         ];
187         $dataProvider->sort->attributes['locationName'] = [
188             'asc'  => [Locations::tableName() . '.name' => SORT_ASC],
189             'desc' => [Locations::tableName() . '.name' => SORT_DESC],
190         ];
191         $dataProvider->sort->attributes['regionName'] = [
192             'asc'  => [Regions::tableName() . '.name' => SORT_ASC],
193             'desc' => [Regions::tableName() . '.name' => SORT_DESC],
194         ];
195
196         return $dataProvider;
197     }
198 }