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', 'type_id'], 'integer'],
26 [['name', 'model', 'os', 'mac', 'serial', 'product', 'modelnumber', '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 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 ]);
72 $dataProvider = new ActiveDataProvider([
75 $dataProvider->setSort([
80 $dataProvider->sort->attributes['statusName'] = [
81 'asc' => [ Status::tableName() . '.name' => SORT_ASC ],
82 'desc' => [ Status::tableName() . '.name' => SORT_DESC ],
84 $dataProvider->sort->attributes['typeName'] = [
85 'asc' => [ Types::tableName() . '.name' => SORT_ASC ],
86 'desc' => [ Types::tableName() . '.name' => SORT_DESC ],
88 $dataProvider->sort->attributes['locationName'] = [
89 'asc' => [ Locations::tableName() . '.name' => SORT_ASC ],
90 'desc' => [ Locations::tableName() . '.name' => SORT_DESC ],
92 $dataProvider->sort->attributes['regionName'] = [
93 'asc' => [ Regions::tableName() . '.name' => SORT_ASC ],
94 'desc' => [ Regions::tableName() . '.name' => SORT_DESC ],
100 * Creates data provider instance with search query applied
102 * @param array $params
104 * @return ActiveDataProvider
106 public function search($params)
108 // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
109 $subQuery = Moving::find()
110 ->select('MAX(id) AS id')
111 ->distinct('item_id')
112 ->groupBy([ 'item_id' ]);
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 ]);
124 // add conditions that should always apply here
126 $dataProvider = new ActiveDataProvider([
130 $dataProvider->setSort([
136 $this->load($params);
138 if (!$this->validate())
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;
145 // grid filtering conditions
146 $query->andFilterWhere([
149 'ilike', Status::tableName() . '.name', $this->statusName
150 // ])->andFilterWhere([
151 // 'ilike', Models::tableName() . '.name', $this->modelName
153 'ilike', Types::tableName() . '.name', $this->typeName
154 ])->andFilterWhere([ 'OR', [
155 'ilike', Locations::tableName() . '.name', $this->regionName
157 'ilike', Regions::tableName() . '.name', $this->regionName
158 ]])->andFilterWhere([ 'OR', [
159 'ilike', Locations::tableName() . '.name', $this->locationName
161 'ilike', Regions::tableName() . '.name', $this->locationName
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]);
174 // $dataProvider->sort->attributes['modelName'] = [
175 // 'asc' => [Models::tableName() . '.name' => SORT_ASC],
176 // 'desc' => [Models::tableName() . '.name' => SORT_DESC],
178 $dataProvider->sort->attributes['statusName'] = [
179 'asc' => [Status::tableName() . '.name' => SORT_ASC],
180 'desc' => [Status::tableName() . '.name' => SORT_DESC],
182 $dataProvider->sort->attributes['typeName'] = [
183 'asc' => [Types::tableName() . '.name' => SORT_ASC],
184 'desc' => [Types::tableName() . '.name' => SORT_DESC],
186 $dataProvider->sort->attributes['locationName'] = [
187 'asc' => [Locations::tableName() . '.name' => SORT_ASC],
188 'desc' => [Locations::tableName() . '.name' => SORT_DESC],
190 $dataProvider->sort->attributes['regionName'] = [
191 'asc' => [Regions::tableName() . '.name' => SORT_ASC],
192 'desc' => [Regions::tableName() . '.name' => SORT_DESC],
195 return $dataProvider;