public function init() { parent::init(); $view = $this->getView(); $module = Yii::$app->getModule("versioning"); $user_id = Yii::$app->user->id; $groups = \amilna\versioning\components\Libs::userGroups($user_id); $groups = [1]; $bundle = NotificationAsset::register($view); $this->bundle = $bundle; $searchModel = new VersionSearch(); $dataProvider = $searchModel->search([]); $query = $dataProvider->query; $query->andWhere([Version::tableName() . ".status" => true])->andWhere(Record::tableName() . ".record_id is not null"); if (count($this->models) > 0) { $query->andWhere([Record::tableName() . ".model" => $this->models])->andWhere(Record::tableName() . ".filter_viewers = false"); } if ($user_id > 0) { $query->andWhere("concat(','," . Record::tableName() . ".viewers,',') not like '%," . $user_id . ",%'")->andWhere(Record::tableName() . ".filter_viewers = false OR (" . Record::tableName() . ".filter_viewers = true AND (" . Record::tableName() . ".owner_id = :uid OR " . Record::tableName() . ".group_id in (" . implode(",", $groups) . ")) )", [":uid" => $user_id]); } else { $query->limit(10); } $query->orderBy(Route::tableName() . ".time DESC," . Version::tableName() . ".id DESC"); $script = "\t\t\n\t\t" . PHP_EOL; $view->registerJs($script); echo $this->render($this->viewPath, ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'module' => $module, 'widget' => $this]); }
public static function mkView($app, $eventName, $event) { $module = $app->getModule("versioning"); $controller = $app->requestedAction->controller; $rotname = (isset($controller->module->module) ? $controller->module->id . "/" : "") . $controller->id; $user_id = $app->user->id; //$action_param = $controller->actionParams; $action_param = $app->request->queryParams; if ($user_id > 0) { $params = []; foreach ($action_param as $p) { if (!is_float($p) && is_numeric($p)) { array_push($params, $p); } } if (count($params) > 0 || isset($action_param["vrid"])) { $searchModel = new VersionSearch(); $dataProvider = $searchModel->search([]); $query = $dataProvider->query; $query->andWhere([Version::tableName() . ".status" => true]); if (isset($action_param["vrid"])) { $query->andWhere([Record::tableName() . ".id" => $action_param["vrid"]]); } else { //$query->andWhere(Route::tableName().".route like :route",[":route"=>$rotname."%"]); $query->andWhere([Record::tableName() . ".record_id" => $params]); } $groups = self::userGroups($user_id); try { $allowall = count($dataProvider->getModels()) > 0 ? false : true; foreach ($dataProvider->getModels() as $mod) { $m = $mod; $v = $mod->version; if ($v) { $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); } $inarr = false; foreach ($v->getPrimaryKey(true) as $k => $p) { if (isset($action_param[$k])) { if ($action_param[$k] == $p) { $inarr = true; } } } $users = $m->record->viewers == null ? [] : explode(",", $m->record->viewers); $group_id = $m->record->group_id; if ($inarr && !$allow) { if (in_array($group_id, $groups) || $m->record->owner_id == $user_id) { $allow = true; } if (!$allow && !$m->record->filter_viewers) { $views = $module->views; $mviews = [$rotname . "/" . $module->defaults["view"]]; foreach ($views as $mw => $vws) { if ($mw == $m->record->model) { foreach ($vws as $vw) { $mviews[] = $rotname . "/" . $vw; } } } $allow = in_array($app->requestedRoute, $mviews); } } else { $allow = true; } if ($allow) { array_push($users, $user_id); $m->record->viewers = implode(",", array_unique($users)); $m->record->save(); } } else { $allow = true; } $allowall = !$allowall ? $allow : true; } if (!$allowall) { return $controller->redirect(["//" . $rotname]); } } catch (yii\db\Exception $e) { } } } else { $param = $app->request->queryParams; if (isset($param['asusername'])) { $userClass = $module->userClass; $user = $userClass::findOne(["username" => $param['asusername']]); if ($user) { $user_id = $user->id; $app->session->set('asuserid', $user_id); $cookie = new \yii\web\Cookie(['name' => 'asuserid', 'value' => $user_id]); $cookie->expire = time() + 60 * 60 * 24 * 365; // (1 year) $app->response->cookies->add($cookie); } } } }
/** * @return \yii\db\ActiveQuery */ public function getRecords() { return $this->hasMany(Record::className(), ['group_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getRecord() { return $this->hasOne(Record::className(), ['id' => 'record_id']); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->find(); $query->joinWith(['record', 'route', 'route.user']); $dataProvider = new ActiveDataProvider(['query' => $query]); $userClass = Yii::$app->getModule('versioning')->userClass; /* uncomment to sort by relations table on respective column */ $dataProvider->sort->attributes['recordModel'] = ['asc' => ['concat(' . Record::tableName() . '.model,' . Record::tableName() . '.id)' => SORT_ASC], 'desc' => ['concat(' . Record::tableName() . '.model,' . Record::tableName() . '.id)' => SORT_DESC]]; $dataProvider->sort->attributes['time'] = ['asc' => ['' . Route::tableName() . '.time' => SORT_ASC], 'desc' => ['' . Route::tableName() . '.time' => SORT_DESC]]; $dataProvider->sort->attributes['routeUser'] = ['asc' => [$userClass::tableName() . '.username' => SORT_ASC], 'desc' => [$userClass::tableName() . '.username' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['status' => $this->status]); $params = self::queryNumber([['id', $this->tableName()], ['route_id'], ['record_id'], ['type'], ['isdel']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['record_attributes']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryTime([['time', Route::tableName()]]); foreach ($params as $p) { $query->andFilterWhere($p); } $query->andFilterWhere(["like", "lower(concat(" . Record::tableName() . ".model,' '," . Record::tableName() . ".record_id))", strtolower($this->recordModel)]); $query->andFilterWhere(['like', 'lower(' . $userClass::tableName() . '.username)', strtolower($this->routeUser)]); /* example to use search all in field1,field2,field3 or field4 */ //print_r(self::mkArrQuery([["OR","lower(field1) like '%".strtolower($this->recordModel)."%'"],["OR","lower(field2) like '%".strtolower($this->recordModel)."%'"]])); //die(); /* 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; }
/** * Finds the Record model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Record the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Record::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionReadall($models = false) { $user_id = Yii::$app->user->id; if ($user_id > 0) { $res = Yii::$app->db->createCommand("UPDATE \n\t\t\t\t\t" . Record::tableName() . "\n\t\t\t\t\tSET viewers = concat(viewers,','," . $user_id . ")\n\t\t\t\t\tWHERE concat(','," . Record::tableName() . ".viewers,',') not like '%," . $user_id . ",%'" . ($models ? " AND model = ANY (array['" . str_replace(",", "','", $models) . "'])" : ""))->execute(); } return $this->redirect($_SERVER["HTTP_REFERER"]); }