OSDN Git Service

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