Esempio n. 1
0
 public function doAfterSave($withRelation = true)
 {
     $pk = $this->tableSchema->primaryKey;
     if ($this->isNewRecord) {
         if (Setting::get('db.driver') == "mysql") {
             $this->{$pk} = $this->dbConnection->getLastInsertID();
             ## this is hack
             ## UPDATE AUDIT TRAIL 'CREATE' ID
             if (isset(Yii::app()->user) && !Yii::app()->user->isGuest) {
                 $a = $this->dbConnection->createCommand("\n            update p_audit_trail set model_id = :model_id\n            WHERE user_id = :user_id and\n            model_class = :model_class and\n            type = 'create' and\n            model_id is null")->execute(['model_class' => ActiveRecord::baseClass($this), 'model_id' => $this->{$pk}, 'user_id' => Yii::app()->user->id]);
             }
         }
     } else {
         $this->deleteResetedRelations();
     }
     ## handling untuk file upload
     if (method_exists($this, 'getFields')) {
         $currentClass = get_class($this);
         $attrs = $this->handleFileUpload($currentClass, $this);
         if (count($attrs) > 0) {
             if ($this->isNewRecord) {
                 $this->isNewRecord = false;
                 $this->updateByPk($this->{$pk}, $attrs);
                 $this->isNewRecord = true;
             } else {
                 $this->saveAttributes($attrs);
             }
         }
         ## handle listview
         $fb = FormBuilder::load($currentClass);
         $listView = $fb->findAllField(['type' => 'ListView']);
         foreach ($listView as $k => $lv) {
             ## if listview is valid
             if ($lv['fieldTemplate'] == "datasource" && @$lv['datasource'] != '' && @$lv['templateForm'] != '') {
                 $ds = $fb->findField(['name' => $lv['datasource']]);
                 ## if datasource is saved via relation and data is posted
                 if (@$ds['postData'] == 'Yes' && @$ds['relationTo'] != '' && isset($this->__relUpdate[$ds['relationTo']])) {
                     foreach ($this->__relUpdate[$ds['relationTo']] as $k => $rel) {
                         $this->handleFileUpload($lv['templateForm'], $this->__relUpdate[$ds['relationTo']][$k]);
                     }
                     foreach ($this->__relInsert[$ds['relationTo']] as $k => $rel) {
                         $this->handleFileUpload($lv['templateForm'], $this->__relInsert[$ds['relationTo']][$k]);
                     }
                 }
             }
         }
     }
     if ($withRelation) {
         $this->saveRelation();
     }
     return true;
 }
Esempio n. 2
0
?>
";
        $scope.pageInfo = <?php 
echo json_encode(AuditTrail::getPathInfo());
?>
;
        $scope.formClass = "<?php 
echo $modelClass;
?>
";
        $scope.formClassPath = "<?php 
echo $modelClassPath;
?>
";
        $scope.modelBaseClass = "<?php 
echo ActiveRecord::baseClass($this->model);
?>
";
        $scope.lastModified = "<?php 
echo Helper::getLastModified($modelClass);
?>
";
        $scope.date = date;
        $scope.strtotime = strtotime;
        $scope.angular = angular;
        
        if (window.plansys) {
            window.plansys.rootPath = "<?php 
echo Setting::getRootPath();
?>
";
Esempio n. 3
0
 public function doAfterSave($withRelation = true)
 {
     $pk = $this->tableSchema->primaryKey;
     if ($this->isNewRecord) {
         $this->{$pk} = $this->dbConnection->getLastInsertID();
         ## this is hack
         ## UPDATE AUDIT TRAIL 'CREATE' ID
         if (!!Yii::app()->user && !Yii::app()->user->isGuest) {
             $a = $this->dbConnection->createCommand("\n                update p_audit_trail set model_id = :model_id\n                WHERE user_id = :user_id and\n                model_class = :model_class and\n                type = 'create' and\n                model_id is null")->execute(['model_class' => ActiveRecord::baseClass($this), 'model_id' => $this->{$pk}, 'user_id' => Yii::app()->user->id]);
         }
     } else {
         $this->deleteResetedRelations();
     }
     if ($withRelation) {
         $this->saveRelation();
     }
     ## handling untuk file upload
     if (method_exists($this, 'getFields')) {
         $fb = FormBuilder::load(get_class($this));
         $uploadFields = $fb->findAllField(['type' => 'UploadFile']);
         $attrs = [];
         $model = $this;
         foreach ($uploadFields as $k => $f) {
             if (@$f['name'] == '' || @$f['uploadPath'] == '') {
                 continue;
             }
             ## create directory
             ## Jika disini gagal, berarti ada yang salah dengan format uploadPath di FormBuilder-nya
             $evalDir = '';
             eval('$evalDir = "' . $f['uploadPath'] . '";');
             $evalDir = str_replace(["\n", "\r"], "", $evalDir);
             $repopath = realpath(Yii::getPathOfAlias("repo"));
             $evalDirArr = explode("/", $evalDir);
             foreach ($evalDirArr as $i => $j) {
                 $evalDirArr[$i] = preg_replace('/[\\/\\?\\:\\*\\"\\<\\>\\|\\\\]*/', "", $j);
             }
             $evalDir = implode("/", $evalDirArr);
             $dir = $repopath . "/" . $evalDir . "/";
             $dir = str_replace(["\n", "\r"], "", $dir);
             if (!is_dir($dir)) {
                 mkdir($dir, 0777, true);
             }
             ## get oldname
             $old = $this->{$f['name']};
             $ext = pathinfo($old, PATHINFO_EXTENSION);
             $filename = pathinfo($old, PATHINFO_FILENAME);
             if (@$f['filePattern']) {
                 ## get newname
                 ## Jika disini gagal, berarti ada yang salah dengan format filePattern di FormBuilder-nya
                 eval('$newname = "' . $f['filePattern'] . '";');
             } else {
                 $newname = $filename . "." . $ext;
             }
             $new = $dir . preg_replace('/[\\/\\?\\:\\*\\"\\<\\>\\|\\\\]*/', "", $newname);
             $new = str_replace(["\n", "\r"], "", $new);
             ## delete file if already exist and allowed to overwrite
             if (is_file($new) && $f['allowOverwrite'] == 'Yes' && is_file($old)) {
                 unlink($new);
             }
             if (!is_file($new) && is_file($old)) {
                 rename($old, $new);
                 $this->{$f['name']} = trim($evalDir, "/") . "/" . $newname;
                 if ($this->hasAttribute($f['name'])) {
                     $attrs[] = $f['name'];
                 }
             }
         }
         if (count($attrs) > 0) {
             if ($this->isNewRecord) {
                 $this->isNewRecord = false;
                 $this->updateByPk($this->id, $this->getAttributes($attrs));
                 $this->isNewRecord = true;
             } else {
                 $this->update($attrs);
             }
         }
     }
     return true;
 }
Esempio n. 4
0
 public function doAfterSave($withRelation = true)
 {
     $pk = $this->tableSchema->primaryKey;
     if ($this->isNewRecord) {
         $this->{$pk} = $this->dbConnection->getLastInsertID();
         ## this is hack
         ## UPDATE AUDIT TRAIL 'CREATE' ID
         if (!!Yii::app()->user && !Yii::app()->user->isGuest) {
             $a = $this->dbConnection->createCommand("\n                update p_audit_trail set model_id = :model_id\n                WHERE user_id = :user_id and\n                model_class = :model_class and\n                type = 'create' and\n                model_id is null")->execute(['model_class' => ActiveRecord::baseClass($this), 'model_id' => $this->{$pk}, 'user_id' => Yii::app()->user->id]);
         }
     } else {
         $this->deleteResetedRelations();
     }
     if ($withRelation) {
         foreach ($this->__relations as $k => $new) {
             if ($k == 'currentModel') {
                 $rel = new CHasManyRelation('currentModel', get_class($this), 'id');
             } else {
                 $rel = $this->getMetaData()->relations[$k];
             }
             $relClass = $rel->className;
             if (!class_exists($relClass)) {
                 continue;
             }
             $relType = get_class($rel);
             $relForeignKey = $rel->foreignKey;
             $relTableModel = $relClass::model();
             $relTable = $relTableModel->tableName();
             $relPK = $relTableModel->metadata->tableSchema->primaryKey;
             switch ($relType) {
                 case 'CHasOneRelation':
                 case 'CBelongsToRelation':
                     if (!empty($new)) {
                         $relForeignKey = $rel->foreignKey;
                         if ($this->{$relForeignKey} == $new[$relPK]) {
                             $model = $relClass::model()->findByPk($this->{$relForeignKey});
                             if (is_null($model)) {
                                 $model = new $relClass();
                             }
                             if (array_diff($model->attributes, $new)) {
                                 $model->attributes = $new;
                                 if ($relType == 'CHasOneRelation') {
                                     $model->{$relForeignKey} = $this->{$pk};
                                 }
                                 $model->save();
                             }
                         } else {
                             $this->loadRelation($rel->name);
                         }
                     }
                     break;
                 case 'CManyManyRelation':
                 case 'CHasManyRelation':
                     ## if relation type is Many to Many, prepare required variable
                     $relMM = [];
                     if ($relType == 'CManyManyRelation') {
                         $parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative());
                         $stmts = $parser->parse('<?php ' . $relForeignKey . ';');
                         if (count($stmts) > 0) {
                             $relMM = ['tableName' => $stmts[0]->name->parts[0], 'from' => $stmts[0]->args[0]->value->name->parts[0], 'to' => $stmts[0]->args[1]->value->name->parts[0]];
                         }
                     }
                     ## Handle Insert
                     if (isset($this->__relInsert[$k])) {
                         if ($k != 'currentModel') {
                             if (is_string($relForeignKey)) {
                                 ## without through
                                 if ($relType != 'CManyManyRelation') {
                                     foreach ($this->__relInsert[$k] as $n => $m) {
                                         $this->__relInsert[$k][$n][$relForeignKey] = $this->{$pk};
                                     }
                                 }
                             } else {
                                 if (is_array($relForeignKey)) {
                                     ## with through
                                     foreach ($this->__relInsert[$k] as $n => $m) {
                                         foreach ($relForeignKey as $rk => $fk) {
                                             $this->__relInsert[$k][$n][$fk] = $this->__relations[$rel->through][$rk];
                                         }
                                     }
                                 }
                             }
                         }
                         if (count($this->__relInsert[$k]) > 0) {
                             if ($relType == "CHasManyRelation") {
                                 ActiveRecord::batchInsert($relClass, $this->__relInsert[$k]);
                             }
                             ## if current relation is many to many
                             if ($relType == 'CManyManyRelation' && !empty($relMM)) {
                                 $manyRel = [];
                                 foreach ($this->__relInsert[$k] as $item) {
                                     $manyRel[] = [$relMM['from'] => $this->{$pk}, $relMM['to'] => $item[$relPK]];
                                 }
                                 ## if relinsert is already exist, then do not insert it again
                                 foreach ($this->__relInsert[$k] as $insIdx => &$ins) {
                                     if (!!@$ins[$relPK]) {
                                         unset($this->__relInsert[$k]);
                                     }
                                 }
                                 ActiveRecord::batchInsert($relClass, $this->__relInsert[$k]);
                                 ## create transaction entry to link between
                                 ## related model and current model
                                 ActiveRecord::batchInsert($relMM['tableName'], $manyRel, false);
                             }
                         }
                         $this->__relInsert[$k] = [];
                     }
                     ## Handle Update
                     if (isset($this->__relUpdate[$k])) {
                         if ($k != 'currentModel') {
                             if (is_string($relForeignKey)) {
                                 ## without through
                                 if ($relType == 'CManyManyRelation') {
                                 } else {
                                     foreach ($this->__relUpdate[$k] as $n => $m) {
                                         $this->__relUpdate[$k][$n][$relForeignKey] = $this->{$pk};
                                     }
                                 }
                             } else {
                                 if (is_array($relForeignKey)) {
                                     ## with through
                                     foreach ($this->__relUpdate[$k] as $n => $m) {
                                         foreach ($relForeignKey as $rk => $fk) {
                                             $this->__relUpdate[$k][$n][$fk] = $this->__relations[$rel->through][$rk];
                                         }
                                     }
                                 }
                             }
                         }
                         if (count($this->__relUpdate[$k]) > 0) {
                             ActiveRecord::batchUpdate($relClass, $this->__relUpdate[$k]);
                         }
                         $this->__relUpdate[$k] = [];
                     }
                     ## Handle Delete
                     if (isset($this->__relDelete[$k])) {
                         if (count($this->__relDelete[$k]) > 0) {
                             if ($relType == 'CManyManyRelation') {
                                 if (!empty($relMM)) {
                                     //first remove entry in transaction table first
                                     ActiveRecord::batchDelete($relMM['tableName'], $this->__relDelete[$k], ['table' => $relMM['tableName'], 'pk' => $relPK, 'condition' => "{$relMM['from']} = {$this->{$pk}} AND {$relMM['to']} IN (:ids)", 'integrityError' => false]);
                                     //and then remove entry in actual table
                                     //ActiveRecord::batchDelete($relClass, $this->__relDelete[$k]);
                                 }
                             } else {
                                 ActiveRecord::batchDelete($relClass, $this->__relDelete[$k]);
                             }
                         }
                         $this->__relDelete[$k] = [];
                     }
                     break;
             }
         }
     }
     ## handling untuk file upload
     if (method_exists($this, 'getFields')) {
         $fb = FormBuilder::load(get_class($this));
         $uploadFields = $fb->findAllField(['type' => 'UploadFile']);
         $attrs = [];
         $model = $this;
         foreach ($uploadFields as $k => $f) {
             if (@$f['name'] == '' || @$f['uploadPath'] == '') {
                 continue;
             }
             ## create directory
             ## Jika disini gagal, berarti ada yang salah dengan format uploadPath di FormBuilder-nya
             $evalDir = '';
             eval('$evalDir = "' . $f['uploadPath'] . '";');
             $evalDir = str_replace(["\n", "\r"], "", $evalDir);
             $repopath = realpath(Yii::getPathOfAlias("repo"));
             $evalDirArr = explode("/", $evalDir);
             foreach ($evalDirArr as $i => $j) {
                 $evalDirArr[$i] = preg_replace('/[\\/\\?\\:\\*\\"\\<\\>\\|\\\\]*/', "", $j);
             }
             $evalDir = implode("/", $evalDirArr);
             $dir = $repopath . "/" . $evalDir . "/";
             $dir = str_replace(["\n", "\r"], "", $dir);
             if (!is_dir($dir)) {
                 mkdir($dir, 0777, true);
             }
             ## get oldname
             $old = $this->{$f['name']};
             $ext = pathinfo($old, PATHINFO_EXTENSION);
             $filename = pathinfo($old, PATHINFO_FILENAME);
             if (@$f['filePattern']) {
                 ## get newname
                 ## Jika disini gagal, berarti ada yang salah dengan format filePattern di FormBuilder-nya
                 eval('$newname = "' . $f['filePattern'] . '";');
             } else {
                 $newname = $filename . "." . $ext;
             }
             $new = $dir . preg_replace('/[\\/\\?\\:\\*\\"\\<\\>\\|\\\\]*/', "", $newname);
             $new = str_replace(["\n", "\r"], "", $new);
             ## delete file if already exist and allowed to overwrite
             if (is_file($new) && $f['allowOverwrite'] == 'Yes' && is_file($old)) {
                 unlink($new);
             }
             if (!is_file($new) && is_file($old)) {
                 rename($old, $new);
                 $this->{$f['name']} = trim($evalDir, "/") . "/" . $newname;
                 $attrs[] = $f['name'];
             }
         }
         if (count($attrs) > 0) {
             if ($this->isNewRecord) {
                 $this->isNewRecord = false;
                 $this->updateByPk($this->id, $this->getAttributes($attrs));
                 $this->isNewRecord = true;
             } else {
                 $this->update($attrs);
             }
         }
     }
     return true;
 }