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']);
 }
 /**
  * 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]);
     }
 }
Exemple #3
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;
 }