/** * Lists all Contact models. * @return mixed */ public function actionIndex() { $categorie = Contact_category::getContact_category(); $searchModel = new ContactSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'categorie' => $categorie]); }
/** * Creates data provider instance with search query applied * and join with category and group tables * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $dataProvider = parent::search($params); $query = $dataProvider->query; $expandCategory = false; $expandGroup = false; // Join per poter filtrare anche attraverso categoria e gruppo if (isset($params['expand'])) { $expand = array_map('trim', explode(",", $params['expand'])); foreach ($expand as $value) { if ($value == 'contact_category') { $query->joinWith('contact_category'); $expandCategory = true; } elseif ($value == 'contact_groups') { $query->joinWith('contact_groups'); $expandGroup = true; } } } /*$dataProvider = new ActiveDataProvider([ 'query' => $query, ]);*/ if (!($this->load($params) && $this->validate())) { return $dataProvider; } // Filtro e ordinamento per categoria e gruppo solo se settato anche il rispettivo valore del parametro expand $dataProvider->sort->enableMultiSort = true; if ($expandCategory) { $query->andFilterWhere(['like', 'contact_category.name', $this->category]); $dataProvider->sort->attributes['category'] = ['asc' => ['contact_category.name' => SORT_ASC], 'desc' => ['contact_category.name' => SORT_DESC]]; } if ($expandGroup) { $query->andFilterWhere(['like', 'contact_group.name', $this->group]); $dataProvider->sort->attributes['group'] = ['asc' => ['contact_group.name' => SORT_ASC], 'desc' => ['contact_group.name' => SORT_DESC]]; } // Seconda soluzione di ordinamento con parametro in formato json e controllo sintassi. // I parametri non validi vengono ignorati. /*if(isset($params['sort']) && $params['sort']!=null){ $sort=$params['sort']; $i=0; $order=''; foreach ($sort as $attribute => $orderType) { if(($attribute=='categoria' && $expandCategoria==true) || ($attribute=='gruppo' && $expandGruppo==true)){ $order=$order . $attribute . '.nome ' . $orderType; } elseif(in_array($attribute, $this->activeAttributes(), true)==true) $order=$order . $attribute . ' ' . $orderType; $i=$i+1; if($i<count($sort)) $order=$order . ', '; } //$order="agaga" . implode(', ', Newrubrica::fields()); $query->orderBy($order); }*/ /*$s=Yii::$app->session; $s->open(); $s->set('tenant','1'); $t=$s->get('tenant'); $order="agaga" . implode(', ', $expand); $query->orderBy($order); return $dataProvider; */ return $dataProvider; }
/** * Lists all Contact models. * @return mixed */ public function actionIndex() { $searchModel = new ContactSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }