public static function find() { return parent::find()->where([Group::tableName() . '.isdel' => 0]); }
/** * Lists all Group models. * @params string $format, array $arraymap, string $term * @return mixed */ public function actionIndex($format = false, $arraymap = false, $term = false) { $searchModel = new GroupSearch(); $req = Yii::$app->request->queryParams; if ($term) { $req[basename(str_replace("\\", "/", get_class($searchModel)))]["term"] = $term; } $dataProvider = $searchModel->search($req); $query = $dataProvider->query; $module = Yii::$app->getModule("versioning"); $allow = false; if (isset(Yii::$app->user->identity->isAdmin)) { $allow = Yii::$app->user->identity->isAdmin; } else { $allow = in_array(Yii::$app->user->identity->username, $module->admins); } if (!$allow) { $query->andWhere([Group::tableName() . '.owner_id' => Yii::$app->user->id]); } if (Yii::$app->request->post('hasEditable')) { $Id = Yii::$app->request->post('editableKey'); $model = Group::findOne($Id); $out = json_encode(['id' => $Id, 'output' => '', 'message' => '', 'data' => 'null']); $post = []; $posted = current($_POST['GroupSearch']); $post['Group'] = $posted; if ($model->owner_id == Yii::$app->user->id) { $transaction = Yii::$app->db->beginTransaction(); try { if ($model->load($post)) { $model->attributes; $model->owner_id = isset($posted['ownerUsername']) ? $posted['ownerUsername'] : $model->owner_id; $model->save(); $output = ''; if (isset($posted['ownerUsername'])) { $output = $model->itemAlias('owner', $model->owner_id); // new value for edited td $data = []; $record = RecordSearch::find()->where(['record_id' => $model->id, 'model' => get_class($model)])->one(); $record->owner_id = $model->owner_id; $record->save(); } if (isset($posted['status'])) { $output = $model->itemAlias('status', $model->status); // new value for edited td $data = []; } $out = json_encode(['id' => $model->id, 'output' => $output, "data" => $data, 'message' => '']); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); } } echo $out; return; } if ($format == 'json') { $model = []; foreach ($dataProvider->getModels() as $d) { $obj = $d->attributes; if ($arraymap) { $map = explode(",", $arraymap); if (count($map) == 1) { $obj = isset($d[$arraymap]) ? $d[$arraymap] : null; } else { $obj = []; foreach ($map as $a) { $k = explode(":", $a); $v = count($k) > 1 ? $k[1] : $k[0]; $obj[$k[0]] = $v == "Obj" ? json_encode($d->attributes) : (isset($d->{$v}) ? $d->{$v} : null); } } } if ($term) { if (!in_array($obj, $model)) { array_push($model, $obj); } } else { array_push($model, $obj); } } return \yii\helpers\Json::encode($model); } else { return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->find(); $query->joinWith(['owner', 'group']); $dataProvider = new ActiveDataProvider(['query' => $query]); $userClass = Yii::$app->getModule('versioning')->userClass; /* uncomment to sort by relations table on respective column */ $dataProvider->sort->attributes['ownerUsername'] = ['asc' => [$userClass::tableName() . '.username' => SORT_ASC], 'desc' => [$userClass::tableName() . '.username' => SORT_DESC]]; $dataProvider->sort->attributes['groupTitle'] = ['asc' => ['' . Group::tableName() . '.title' => SORT_ASC], 'desc' => ['' . Group::tableName() . '.title' => SORT_DESC]]; $dataProvider->sort->attributes['recordModel'] = ['asc' => ['concat(' . Record::tableName() . '.model,' . Record::tableName() . '.id)' => SORT_ASC], 'desc' => ['concat(' . Record::tableName() . '.model,' . Record::tableName() . '.id)' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['filter_viewers' => $this->filter_viewers]); $params = self::queryNumber([['id', $this->tableName()], ['record_id'], ['owner_id', $this->tableName()], ['group_id'], ['isdel']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['model'], ['viewers']]); foreach ($params as $p) { $query->andFilterWhere($p); } $query->andFilterWhere(['like', 'lower(' . $userClass::tableName() . '.username)', strtolower($this->ownerUsername)]); $query->andFilterWhere(['like', 'lower(' . Group::tableName() . '.title)', strtolower($this->groupTitle)]); $query->andFilterWhere(["like", "lower(concat(" . Record::tableName() . ".model,' '," . Record::tableName() . ".record_id))", strtolower($this->recordModel)]); /* example to use search all in field1,field2,field3 or field4 if ($this->term) { $query->andFilterWhere(["OR","lower(field1) like '%".strtolower($this->term)."%'", ["OR","lower(field2) like '%".strtolower($this->term)."%'", ["OR","lower(field3) like '%".strtolower($this->term)."%'", "lower(field4) like '%".strtolower($this->term)."%'" ] ] ]); } */ return $dataProvider; }