6 use yii\data\ActiveDataProvider;
10 use app\models\Locations;
11 use app\models\Regions;
12 use app\models\Moving;
15 * ItemsSearch represents the model behind the search form of `app\models\Items`.
17 class ItemsSearch extends Items
22 public function rules()
25 [['id', 'model_id'], 'integer'],
26 [['name', 'modelName', 'os', 'mac', 'serial', 'invent', 'date', 'comment', 'statusName', 'typeName', 'locationName', 'regionName'], 'safe'],
33 public function scenarios()
35 // bypass scenarios() implementation in the parent class
36 return Model::scenarios();
39 public function noinvent($params)
42 $query = Moving::find()
43 ->select('MAX(' . Moving::tableName() . '.id) AS mid');
44 if (isset($params->region) && ($params->region != ''))
46 $query->joinWith([ 'locations' ])
47 ->where([ 'region_id' => $params->region ]);
49 if (isset($params->location) && ($params->location != ''))
51 if (isset($params->region) && ($params->location != ''))
53 $query->andWhere(['location_id' => $params->location]);
57 $query->where(['location_id' => $params->location]);
60 $query->distinct('item_id')->groupBy('item_id');
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 ]);
73 $dataProvider = new ActiveDataProvider([
76 $dataProvider->setSort([
83 if (!$this->validate())
88 // grid filtering conditions
89 $query->andFilterWhere([
92 'ilike', Status::tableName() . '.name', $this->statusName
94 'ilike', Models::tableName() . '.name', $this->modelName
96 'ilike', Types::tableName() . '.name', $this->typeName
97 ])->andFilterWhere([ 'OR', [
98 'ilike', Locations::tableName() . '.name', $this->regionName
100 'ilike', Regions::tableName() . '.name', $this->regionName
101 ]])->andFilterWhere([ 'OR', [
102 'ilike', Locations::tableName() . '.name', $this->locationName
104 'ilike', Regions::tableName() . '.name', $this->locationName
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]);
115 $dataProvider->sort->attributes['modelName'] = [
116 'asc' => [ Models::tableName() . '.name' => SORT_ASC ],
117 'desc' => [ Models::tableName() . '.name' => SORT_DESC ],
119 $dataProvider->sort->attributes['statusName'] = [
120 'asc' => [ Status::tableName() . '.name' => SORT_ASC ],
121 'desc' => [ Status::tableName() . '.name' => SORT_DESC ],
123 $dataProvider->sort->attributes['typeName'] = [
124 'asc' => [ Types::tableName() . '.name' => SORT_ASC ],
125 'desc' => [ Types::tableName() . '.name' => SORT_DESC ],
127 $dataProvider->sort->attributes['locationName'] = [
128 'asc' => [ Locations::tableName() . '.name' => SORT_ASC ],
129 'desc' => [ Locations::tableName() . '.name' => SORT_DESC ],
131 $dataProvider->sort->attributes['regionName'] = [
132 'asc' => [ Regions::tableName() . '.name' => SORT_ASC ],
133 'desc' => [ Regions::tableName() . '.name' => SORT_DESC ],
135 return $dataProvider;
139 * Creates data provider instance with search query applied
141 * @param array $params
143 * @return ActiveDataProvider
145 public function search($params)
147 // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
148 $subQuery = Moving::find()
149 ->select('MAX(id) AS id')
150 ->distinct('item_id')
151 ->groupBy([ 'item_id' ]);
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 ]);
163 // add conditions that should always apply here
165 $dataProvider = new ActiveDataProvider([
169 $dataProvider->setSort([
175 $this->load($params);
177 if (!$this->validate())
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;
184 // grid filtering conditions
185 $query->andFilterWhere([
188 'ilike', Status::tableName() . '.name', $this->statusName
189 ])->andFilterWhere([ 'OR', [
190 'ilike', Models::tableName() . '.name', $this->modelName
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
198 'ilike', Regions::tableName() . '.name', $this->regionName
199 ]])->andFilterWhere([ 'OR', [
200 'ilike', Locations::tableName() . '.name', $this->locationName
202 'ilike', Regions::tableName() . '.name', $this->locationName
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]);
212 $dataProvider->sort->attributes['modelName'] = [
213 'asc' => [Models::tableName() . '.name' => SORT_ASC],
214 'desc' => [Models::tableName() . '.name' => SORT_DESC],
216 $dataProvider->sort->attributes['statusName'] = [
217 'asc' => [Status::tableName() . '.name' => SORT_ASC],
218 'desc' => [Status::tableName() . '.name' => SORT_DESC],
220 $dataProvider->sort->attributes['typeName'] = [
221 'asc' => [Types::tableName() . '.name' => SORT_ASC],
222 'desc' => [Types::tableName() . '.name' => SORT_DESC],
224 $dataProvider->sort->attributes['locationName'] = [
225 'asc' => [Locations::tableName() . '.name' => SORT_ASC],
226 'desc' => [Locations::tableName() . '.name' => SORT_DESC],
228 $dataProvider->sort->attributes['regionName'] = [
229 'asc' => [Regions::tableName() . '.name' => SORT_ASC],
230 'desc' => [Regions::tableName() . '.name' => SORT_DESC],
233 return $dataProvider;