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
13 /**
14  * ItemsSearch represents the model behind the search form of `app\models\Items`.
15  */
16 class ItemsSearch extends Items
17 {
18     /**
19      * {@inheritdoc}
20      */
21     public function rules()
22     {
23         return [
24             [['id', 'state_id', 'type_id'], 'integer'],
25             [['name', 'model', 'os', 'mac', 'serial', 'product', 'modelnumber', 'invent', 'date', 'comment', 'statusName', 'typeName', 'locationName', 'regionName'], 'safe'],
26         ];
27     }
28
29     /**
30      * {@inheritdoc}
31      */
32     public function scenarios()
33     {
34         // bypass scenarios() implementation in the parent class
35         return Model::scenarios();
36     }
37
38     /**
39      * Creates data provider instance with search query applied
40      *
41      * @param array $params
42      *
43      * @return ActiveDataProvider
44      */
45     public function search($params)
46     {
47         $query = Items::find();
48         $query->joinWith(['status']);
49         $query->joinWith(['types']);
50 <<<<<<< HEAD
51 =======
52         $query->joinWith(['locations']);
53 //        $query->joinWith(['regions']);
54 >>>>>>> develop
55
56         // add conditions that should always apply here
57
58         $dataProvider = new ActiveDataProvider([
59             'query' => $query,
60         ]);
61
62         $dataProvider->setSort([
63             'defaultOrder' => [
64                 'id' => SORT_ASC,
65             ],
66         ]);
67
68         $this->load($params);
69
70         if (!$this->validate()) {
71             // uncomment the following line if you do not want to return any records when validation fails
72             // $query->where('0=1');
73             return $dataProvider;
74         }
75
76         // grid filtering conditions
77         $query->andFilterWhere([
78             'id' => $this->id,
79             'date' => $this->date,
80         ])->andFilterWhere([
81             'like', Status::tableName().'.name', $this->statusName
82         ])->andFilterWhere([
83             'like', Types::tableName().'.name', $this->typeName
84         ])->andFilterWhere([
85             'like', Locations::tableName().'.name', $this->locationName
86 //        ])->andFilterWhere([
87 //            'like', Regions::tableName().'.name', $this->regionName
88         ]);
89
90         $query->andFilterWhere(['ilike', 'name', $this->name])
91             ->andFilterWhere(['ilike', 'model', $this->model])
92             ->andFilterWhere(['ilike', 'os', $this->os])
93             ->andFilterWhere(['ilike', 'mac', $this->mac])
94             ->andFilterWhere(['ilike', 'serial', $this->serial])
95             ->andFilterWhere(['ilike', 'product', $this->product])
96             ->andFilterWhere(['ilike', 'modelnumber', $this->modelnumber])
97             ->andFilterWhere(['ilike', 'invent', $this->invent])
98             ->andFilterWhere(['ilike', 'comment', $this->comment]);
99
100         $dataProvider->sort->attributes['statusName'] = [
101             'asc' => [Status::tableName().'.name' => SORT_ASC],
102             'desc' => [Status::tableName().'.name' => SORT_DESC],
103         ];
104         $dataProvider->sort->attributes['typeName'] = [
105             'asc' => [Types::tableName().'.name' => SORT_ASC],
106             'desc' => [Types::tableName().'.name' => SORT_DESC],
107         ];
108         $dataProvider->sort->attributes['locationName'] = [
109             'asc' => [Locations::tableName().'.name' => SORT_ASC],
110             'desc' => [Locations::tableName().'.name' => SORT_DESC],
111         ];
112 //        $dataProvider->sort->attributes['regionName'] = [
113 //            'asc' => [Regions::tableName().'.name' => SORT_ASC],
114 //            'desc' => [Regions::tableName().'.name' => SORT_DESC],
115 //        ];
116         
117         return $dataProvider;
118     }
119 }