Beispiel #1
0
 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]);
 }
Beispiel #2
0
 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);
             }
         }
     }
 }
 /**
  * Lists all Record models.
  * @params string $format, array $arraymap, string $term
  * @return mixed
  */
 public function actionIndex($format = false, $arraymap = false, $term = false)
 {
     $searchModel = new RecordSearch();
     $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([Record::tableName() . '.owner_id' => Yii::$app->user->id]);
     }
     $dataProvider->pagination = ["pageSize" => 20];
     if (Yii::$app->request->post('hasEditable')) {
         $Id = Yii::$app->request->post('editableKey');
         $model = Record::findOne($Id);
         $out = json_encode(['id' => $Id, 'output' => '', 'message' => '', 'data' => 'null']);
         $post = [];
         $posted = current($_POST['RecordSearch']);
         $post['Record'] = $posted;
         if ($model->owner_id == Yii::$app->user->id || $allow) {
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 if ($model->load($post)) {
                     $model->attributes;
                     $model->owner_id = isset($posted['owner_id']) ? $posted['owner_id'] : $model->owner_id;
                     $model->group_id = isset($posted['groupTitle']) ? $posted['groupTitle'] : $model->group_id;
                     $model->save();
                     $output = '';
                     if (isset($posted['groupTitle'])) {
                         $output = $model->itemAlias('groups', $model->group_id);
                         // new value for edited td
                         $data = [];
                         $version = VersionSearch::find()->where(['record_id' => $model->id, 'status' => true])->one();
                         $versions = $version->route->versions;
                         foreach ($versions as $v) {
                             $v->record->group_id = $model->group_id;
                             $v->record->save();
                         }
                     }
                     if (isset($posted['owner_id'])) {
                         $output = $model->itemAlias('owner', $model->owner_id);
                         // new value for edited td
                         $data = [];
                         $version = VersionSearch::find()->where(['record_id' => $model->id, 'status' => true])->one();
                         $versions = $version->route->versions;
                         foreach ($versions as $v) {
                             $v->record->owner_id = $model->owner_id;
                             $v->record->save();
                         }
                     }
                     if (isset($posted['filter_viewers'])) {
                         $output = $model->itemAlias('filter_viewers', $model->filter_viewers ? 1 : 0);
                         // 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]);
     }
 }
 public function actionApply($id)
 {
     $model = $this->findModel($id);
     if ($model) {
         if (!$model->status) {
             $route_id = $model->route_id;
             $versions = VersionSearch::find()->andWhere("route_id = :route_id OR concat(',',substring(route_ids from 2 for (length(route_ids)-2)),',') LIKE :lk", [":route_id" => $route_id, ":lk" => '%,' . $route_id . ',%'])->all();
             $res = true;
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 foreach ($versions as $model) {
                     $record_id = $model->record->record_id;
                     $modelClass = $model->record->model;
                     if ($record_id != null) {
                         $origin = Version::findOne(["record_id" => $model->record_id, "status" => true]);
                         $origin->status = $origin->id != $model->id ? false : true;
                         $origin->save();
                     } else {
                         $done = Version::findOne(["record_id" => $model->record_id, "route_id" => $route_id]);
                         if (!$done) {
                             $recs = Version::findAll(["record_id" => $model->record_id]);
                             foreach ($recs as $r) {
                                 $sql = "";
                                 $key = [];
                                 foreach (json_decode($r->record_attributes) as $a => $v) {
                                     $sql .= ($sql == "" ? "" : " AND ") . $a . ($v === null ? " is null" : " = :" . $a);
                                     if ($v !== null) {
                                         $key[":" . $a] = $v;
                                     }
                                 }
                                 $rmod = $modelClass::find()->where($sql, $key)->one();
                                 if ($rmod && !in_array($route_id, json_decode($r->route_ids))) {
                                     $modelClass::deleteAll($sql, $key);
                                 }
                                 $r->route_id = $route_id;
                                 $r->save();
                             }
                         }
                     }
                     $version = $model->version;
                     $model->status = true;
                     if ($record_id == null && $version->isNewRecord || $record_id != null) {
                         foreach ($version->behaviors as $k => $b) {
                             if ($k === "tree") {
                                 $lid = $b->leftAttribute;
                                 $rid = $b->rightAttribute;
                                 $did = $b->depthAttribute;
                                 $parent = $version->find()->andWhere($lid . " < " . $version->{$lid} . " AND " . $rid . " > " . $version->{$rid} . " AND (" . $did . "+1) = " . $version->{$did})->one();
                                 if ($parent) {
                                     $version->prependTo($parent);
                                 } else {
                                     if (!empty($b->treeAttribute)) {
                                         $version->makeRoot();
                                     } else {
                                         $parent = $version->find()->andWhere($did . " = 1 ")->one();
                                         if ($parent) {
                                             $version->prependTo($parent);
                                         } else {
                                             $version->makeRoot();
                                         }
                                     }
                                 }
                             }
                         }
                         $res = !$version->save() ? false : $res;
                         if ($record_id != null) {
                             $pk = $version->getPrimaryKey(true);
                             if (is_array($pk)) {
                                 foreach ($pk as $pk_key => $pk_val) {
                                     $model->record->record_id = $version->{$pk_key};
                                     $res = !$model->record->save() ? false : $res;
                                 }
                             } else {
                                 $res = false;
                             }
                         }
                     }
                     $res = !$model->save() ? false : $res;
                 }
                 if ($res) {
                     $transaction->commit();
                 } else {
                     $transaction->rollBack();
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->redirect(['index']);
 }
Beispiel #5
0
 public function getVersion()
 {
     $modelClass = $this->model;
     $record_id = $this->record_id;
     $model = null;
     if (class_exists($modelClass)) {
         $version = VersionSearch::find()->where(['record_id' => $this->id, 'status' => true])->one();
         if ($record_id == null) {
             $sql = "";
             $key = [];
             foreach (json_decode($version->record_attributes) as $a => $v) {
                 $sql .= ($sql == "" ? "" : " AND ") . $a . ($v === null ? " is null" : " = :" . $a);
                 if ($v !== null) {
                     $key[":" . $a] = $v;
                 }
             }
             $model = $modelClass::find()->where($sql, $key)->one();
         } else {
             $model = $modelClass::findOne($record_id);
         }
         if (!$model) {
             $model = new $modelClass();
         }
         $parents = $version->parents()->orderBy("depth")->all();
         $attributes = [];
         foreach ($parents as $p) {
             $attr = json_decode($p->record_attributes);
             if (is_array($attr) || is_object($attr)) {
                 foreach ($attr as $a => $v) {
                     $attributes[$a] = $v;
                 }
             }
         }
         $attr = json_decode($version->record_attributes);
         if (is_array($attr) || is_object($attr)) {
             foreach ($attr as $a => $v) {
                 $attributes[$a] = $v;
             }
         }
         if (count($attributes) > 0) {
             $model->attributes = $attributes;
         }
         if ($model->isNewRecord && isset($model->id)) {
             $model->id = null;
         }
     }
     return $model;
 }