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