Exemple #1
0
 public function getSubFormClass()
 {
     if ($this->_subformClass == "") {
         $this->_subformClass = Helper::explodeLast(".", $this->subForm);
     }
     return $this->_subformClass;
 }
Exemple #2
0
 public function validateSubForm()
 {
     $class = get_class($this);
     $fb = FormBuilder::load($class);
     $listView = $fb->findAllField(['type' => 'ListView']);
     foreach ($listView as $k => $lv) {
         ## if listview is valid
         if ((@$lv['fieldTemplate'] == "datasource" || @$lv["fieldTemplate"] == "form") && @$lv['templateForm'] != '') {
             if (isset($this->attributes[$lv['name']])) {
                 $items = $this->attributes[$lv['name']];
                 foreach ($items as $k => $item) {
                     Yii::import($lv['templateForm']);
                     $newClass = Helper::explodeLast(".", $lv['templateForm']);
                     $new = new $newClass();
                     $new->attributes = $item;
                     $new->validate();
                     if ($new->hasErrors()) {
                         foreach ($new->errors as $name => $errors) {
                             $this->addError($name, implode("<br> &bull; ", $errors));
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #3
0
 public function renderSubForm()
 {
     $class = Helper::explodeLast(".", $this->subForm);
     if ($class == get_class($this)) {
         return '<center><i class="fa fa-warning"></i> Error Rendering SubForm: Subform can not be the same as its parent</center>';
     } else {
         ## render
         Yii::import($this->subForm);
         $ctrl = Yii::app()->controller;
         $ctrl->renderForm($class, null, [], ['layout' => '//layouts/blank']);
     }
 }
Exemple #4
0
 public function actionListField()
 {
     if (!@$_GET['class']) {
         echo json_encode([]);
         die;
     }
     $class = $_GET['class'];
     Yii::import($class);
     $class = Helper::explodeLast(".", $class);
     $model = new $class();
     $data = [];
     if (is_subclass_of($model, 'ActiveRecord')) {
         $formType = "ActiveRecord";
         $data = $class::model()->attributesList;
         unset($data['Relations']);
         unset($data['Properties']);
     } else {
         if (is_subclass_of($model, 'FormField')) {
             $formType = "FormField";
             $mf = new $class();
             $data = $mf->attributes;
             unset($data['type']);
         } else {
             if (is_subclass_of($model, 'Form')) {
                 $formType = "Form";
                 $mf = new $class();
                 $data = $mf->attributes;
                 unset($data['type']);
             }
         }
     }
     echo json_encode($data);
 }
Exemple #5
0
 public static function listCmdForMenuTree()
 {
     $list = ['' => '-- Choose Command ---'];
     $devMode = Setting::get('app.mode') === "plansys";
     /*
      ** Fetching all command files inside app.commands
      **/
     $cmdsDir = Yii::getPathOfAlias("app.commands") . DIRECTORY_SEPARATOR;
     $cmds = self::listCmdInDir('app', $cmdsDir);
     foreach ($cmds as $cmd) {
         $shortUrl = Helper::explodeLast(".", $cmd['url']);
         $list['App'][$cmd['class']] = $shortUrl;
     }
     /*
      ** Fetching all command files inside app.modules
      **/
     $dir = Yii::getPathOfAlias("app.modules") . DIRECTORY_SEPARATOR;
     $items = glob($dir . "*", GLOB_ONLYDIR);
     foreach ($items as $k => $f) {
         $label = str_replace($dir, "", $f);
         $classPath = $f . DIRECTORY_SEPARATOR . 'commands' . DIRECTORY_SEPARATOR . ucfirst($label) . 'Command.php';
         if (is_file($classPath)) {
             $cmdsDir = $f . DIRECTORY_SEPARATOR . "commands" . DIRECTORY_SEPARATOR;
             $cmds = self::listCmdInDir('app.' . $label, $cmdsDir);
             foreach ($cmds as $cmd) {
                 $parts = explode(".", $cmd['url']);
                 $shortUrl = end($parts);
                 $list['App - ' . $parts[2]][$cmd['class']] = $shortUrl;
             }
         }
     }
     if ($devMode) {
         /*
          ** Fetching all command files inside plansys.commands
          **/
         $cmdsDir = Yii::getPathOfAlias("application.commands") . DIRECTORY_SEPARATOR;
         $cmds = self::listCmdInDir('plansys', $cmdsDir);
         foreach ($cmds as $cmd) {
             $shortUrl = Helper::explodeLast(".", $cmd['url']);
             $list['Plansys'][$cmd['class']] = $shortUrl;
         }
         /*
          ** Fetching all command files inside plansys.modules
          **/
         $dir = Yii::getPathOfAlias("application.modules") . DIRECTORY_SEPARATOR;
         $items = glob($dir . "*", GLOB_ONLYDIR);
         foreach ($items as $k => $f) {
             $label = str_replace($dir, "", $f);
             $classPath = $f . DIRECTORY_SEPARATOR . 'commands' . DIRECTORY_SEPARATOR . ucfirst($label) . 'Command.php';
             if (is_file($classPath)) {
                 $cmdsDir = $f . DIRECTORY_SEPARATOR . "commands" . DIRECTORY_SEPARATOR;
                 $cmds = self::listCmdInDir('plansys.' . $label, $cmdsDir);
                 foreach ($cmds as $cmd) {
                     $parts = explode(".", $cmd['url']);
                     $shortUrl = end($parts);
                     $list['Plansys - ' . $parts[2]][$cmd['class']] = $shortUrl;
                 }
             }
         }
     }
     return $list;
 }
Exemple #6
0
 /**
  * load
  * Fungsi ini digunakan untuk me-load FormBuilder
  * @param array $class
  * @param array $findByAttributes
  * @return mixed me-return null jika class tidak ada, jika ada maka me-return array $model
  */
 public static function load($class, $findByAttributes = [])
 {
     if (!is_string($class)) {
         return null;
     }
     $originalClass = $class;
     if (strpos($class, ".") !== false) {
         $classFile = FormBuilder::classPath($class);
         $class = Helper::explodeLast(".", $classFile);
         try {
             Yii::import($classFile);
         } catch (Exception $e) {
             if (isset(Yii::app()->controller) && isset(Yii::app()->controller->module)) {
                 $basePath = Yii::app()->controller->module->basePath;
                 $classFile = str_replace(".", DIRECTORY_SEPARATOR, $classFile) . ".php";
                 $classFile = $basePath . DIRECTORY_SEPARATOR . 'forms' . DIRECTORY_SEPARATOR . $classFile;
                 require_once $classFile;
             }
         }
         if (!class_exists($class)) {
             throw new CException("Class \"{$class}\" does not exists");
         }
     }
     $model = new FormBuilder();
     if (!empty($findByAttributes) && method_exists($class, 'model')) {
         if (is_subclass_of($class, 'ActiveRecord')) {
             $model->model = $class::model()->findByAttributes($findByAttributes);
             if (is_null($model->model)) {
                 $model->model = new $class();
             }
         }
     } else {
         $model->model = new $class();
     }
     $model->originalClass = $originalClass;
     if (!is_null($findByAttributes)) {
         $model->model->attributes = $findByAttributes;
     }
     ## get method line and length
     if (isset(Yii::app()->session)) {
         if (is_null(Yii::app()->session['FormBuilder_' . $originalClass])) {
             $reflector = new ReflectionClass($class);
             $model->sourceFile = $reflector->getFileName();
             $model->file = file($model->sourceFile, FILE_IGNORE_NEW_LINES);
             $methods = $reflector->getMethods();
             foreach ($methods as $m) {
                 if ($m->class == $class) {
                     $line = $m->getStartLine() - 1;
                     $length = $m->getEndLine() - $line;
                     $model->methods[$m->name] = ['line' => $line, 'length' => $length];
                 }
             }
             Yii::app()->session['FormBuilder_' . $originalClass] = ['sourceFile' => $model->sourceFile, 'file' => $model->file, 'methods' => $model->methods];
         } else {
             $s = Yii::app()->session['FormBuilder_' . $originalClass];
             $model->sourceFile = $s['sourceFile'];
             $model->file = $s['file'];
             $model->methods = $s['methods'];
             if (isset($s['timestamp'])) {
                 $model->timestamp = $s['timestamp'];
             }
         }
     }
     return $model;
 }
Exemple #7
0
 public static function getPlansysDirName()
 {
     return Helper::explodeLast(DIRECTORY_SEPARATOR, Yii::getPathOfAlias('application'));
 }
Exemple #8
0
 public function actionPreviewSQL()
 {
     $postdata = file_get_contents("php://input");
     $post = json_decode($postdata, true);
     $criteria = @$post['criteria'] ? $post['criteria'] : [];
     $params = @$post['params'] ? $post['params'] : [];
     $baseClass = $post['baseclass'];
     switch ($baseClass) {
         case "DataGrid":
         case "DataFilter":
         case "RelationField":
         case "TextField":
             $rel = 'currentModel';
             $name = $post['rfname'];
             $classPath = $post['rfclass'];
             $modelClassPath = $post['rfmodel'];
             $modelClass = Helper::explodeLast(".", $modelClassPath);
             Yii::import($modelClassPath);
             $class = Helper::explodeLast(".", $classPath);
             Yii::import($classPath);
             $model = new $modelClass();
             $builder = $model->commandBuilder;
             $fb = FormBuilder::load($classPath);
             $field = $fb->findField(['name' => $name]);
             $rf = new RelationField();
             $rf->builder = $fb;
             $rf->attributes = $field;
             $rf->relationCriteria = $criteria;
             $rf->params = $post['params'];
             $criteria = $rf->generateCriteria('', []);
             $criteria = new CDbCriteria($criteria);
             break;
         case "DataSource":
             $rel = $post['rel'];
             $name = $post['dsname'];
             $classPath = $post['dsclass'];
             $class = Helper::explodeLast(".", $classPath);
             Yii::import($classPath);
             $model = new $class();
             $builder = $model->commandBuilder;
             $fb = FormBuilder::load($classPath);
             $fb->model = new $model();
             $field = $fb->findField(['name' => $name]);
             $ds = new DataSource();
             $ds->attributes = $field;
             $criteria = DataSource::generateCriteria($params, $criteria, $ds);
             $criteria = SqlCriteria::convertPagingCriteria($criteria);
             $criteria = new CDbCriteria($criteria);
             break;
     }
     if (!isset($rel)) {
         echo json_encode(["sql" => '', "error" => '']);
         return false;
     }
     $isRelated = false;
     if ($rel == 'currentModel') {
         $tableSchema = $model->tableSchema;
     } else {
         $parent = $model::model()->find();
         $relMeta = $model->getMetadata()->relations[$rel];
         $relClass = $relMeta->className;
         if (!is_subclass_of($relClass, 'ActiveRecord')) {
             throw new CException("Class {$relClass} harus merupakan subclass dari ActiveRecord");
         }
         $tableSchema = $relClass::model()->tableSchema;
         if (!is_null($parent)) {
             $parentPrimaryKey = $parent->metadata->tableSchema->primaryKey;
             switch (get_class($relMeta)) {
                 case 'CHasOneRelation':
                 case 'CBelongsToRelation':
                     if (is_string($relMeta->foreignKey)) {
                         $criteria->addColumnCondition([$relMeta->foreignKey => $parent->{$parentPrimaryKey}]);
                         $isRelated = true;
                     }
                     break;
                 case 'CManyManyRelation':
                     $parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative());
                     $stmts = $parser->parse('<?php ' . $relMeta->foreignKey . ';');
                     $bridgeTable = $stmts[0]->name->parts[0];
                     $arg0 = $stmts[0]->args[0]->value->name->parts[0];
                     $arg1 = $stmts[0]->args[1]->value->name->parts[0];
                     $criteria->join .= " " . $relMeta->joinType . " {$bridgeTable} ON t.{$tableSchema->primaryKey} = {$bridgeTable}.{$arg1} ";
                     break;
                 case 'CHasManyRelation':
                     //without through
                     if (is_string($relMeta->foreignKey)) {
                         $criteria->addColumnCondition([$relMeta->foreignKey => $parent->{$parentPrimaryKey}]);
                         $isRelated = true;
                     }
                     //with through
                     //todo..
                     break;
             }
         }
     }
     $command = $builder->createFindCommand($tableSchema, $criteria);
     $commandText = $command->text;
     if ($isRelated) {
         $commandText = str_replace(":ycp0", "\n" . '"{$model->' . $relMeta->foreignKey . '}"', $commandText);
     }
     $commandText = SqlFormatter::highlight($commandText);
     $errMsg = '';
     try {
         $command->queryScalar();
     } catch (Exception $e) {
         $errMsg = $e->getMessage();
         $errMsg = str_replace("CDbCommand gagal menjalankan statement", "", $errMsg);
     }
     echo json_encode(["sql" => $commandText, "error" => $errMsg]);
 }
Exemple #9
0
 public function renderAllToolbar($formType)
 {
     FormField::$inEditor = false;
     $toolbarData = Yii::app()->cache->get('toolbarData');
     if (!$toolbarData) {
         $toolbarData = FormField::allSorted();
         Yii::app()->cache->set('toolbarData', $toolbarData, 0);
     }
     foreach ($toolbarData as $k => $f) {
         $ff = new $f['type']();
         $scripts = array_merge($ff->renderScript(), $ff->renderEditorScript());
         foreach ($scripts as $script) {
             $ext = Helper::explodeLast(".", $script);
             if ($ext == "js") {
                 Yii::app()->clientScript->registerScriptFile($script, CClientScript::POS_END);
             } else {
                 if ($ext == "css") {
                     Yii::app()->clientScript->registerCSSFile($script);
                 }
             }
         }
     }
     FormField::$inEditor = true;
     return array('data' => $toolbarData);
 }
Exemple #10
0
 public function prepareFormName($class, $module = null)
 {
     if (isset($module)) {
         if (is_string($module)) {
             $moduleList = Setting::getModules();
             if (isset($moduleList[$module])) {
                 $moduleAlias = $moduleList[$module]['class'];
                 $moduleClass = Helper::explodeLast(".", $moduleAlias);
                 Yii::import($moduleAlias);
                 if (@class_exists($moduleClass)) {
                     $module = new $moduleClass($module, null);
                 }
             }
         }
         if (!is_object($module)) {
             $module = null;
         }
     } else {
         if (!isset($module)) {
             if (isset($this->module)) {
                 $module = $this->module;
             }
         }
     }
     if (strpos($class, '.') > 0) {
         $className = Helper::explodeLast(".", $class);
         if (!class_exists($className, false)) {
             try {
                 Yii::import($class);
             } catch (CException $e) {
                 if ($module) {
                     $moduleAlias = Helper::getAlias($module->basePath);
                     Yii::import($moduleAlias . ".forms." . $class);
                 } else {
                     $reflection = new ReflectionClass($this);
                     $path = $reflection->getFileName();
                     if (strpos($path, Yii::getPathOfAlias('app')) === 0) {
                         Yii::import('app.forms.' . $class);
                     } else {
                         if (strpos($path, Yii::getPathOfAlias('application')) === 0) {
                             Yii::import('application.forms.' . $class);
                         }
                     }
                 }
             }
         }
         $class = $className;
     } else {
         if (isset($module)) {
             $module = $module->id;
             if (stripos($class, $module) !== 0) {
                 if (!@class_exists($class)) {
                     $class = ucfirst($module) . ucfirst($class);
                 }
             }
         }
     }
     return $class;
 }
Exemple #11
0
 /**
  * render
  * Fungsi ini untuk me-render field dan atributnya
  * @return mixed me-return sebuah field dan atribut checkboxlist dari hasil render
  */
 public function render()
 {
     $this->addClass('form-group form-group-sm flat', 'options');
     $this->addClass($this->layoutClass, 'options');
     $this->addClass($this->errorClass, 'options');
     $this->fieldOptions['ui-tree-node'] = '';
     $this->fieldOptions['ng-repeat'] = 'item in value';
     $this->fieldOptions['ng-init'] = 'initItem(value, $index)';
     $this->addClass('list-view-item', 'fieldOptions');
     Yii::import(FormBuilder::classPath($this->templateForm));
     $class = Helper::explodeLast(".", $this->templateForm);
     if (($this->fieldTemplate == 'form' || $this->fieldTemplate == 'datasource') && class_exists($class)) {
         $fb = FormBuilder::load($class);
         $model = new $class();
         if ($this->value == "") {
             $this->value = [];
         }
         $this->templateAttributes = $model->attributes;
         $fb->model = $model;
         $this->renderTemplateForm = $fb->render($model, ['wrapForm' => false]);
     } else {
         if ($this->fieldTemplate == 'default') {
             $field = new $this->singleView();
             $field->attributes = $this->singleViewOption;
             $field->renderID = $this->name . rand(0, 10000);
             $field->builder = $this->builder;
             $field->formProperties = $this->formProperties;
             $this->templateAttributes = ['val' => ''];
             $this->renderTemplateForm = $field->render();
         }
     }
     $this->setDefaultOption('ng-model', "model.{$this->originalName}", $this->options);
     $jspath = explode(".", FormBuilder::classPath($this->templateForm));
     array_pop($jspath);
     $jspath = implode(".", $jspath);
     $inlineJS = str_replace("/", DIRECTORY_SEPARATOR, trim($this->inlineJS, "/"));
     $inlineJS = Yii::getPathOfAlias($jspath) . DIRECTORY_SEPARATOR . $inlineJS;
     if (is_file($inlineJS)) {
         $inlineJS = file_get_contents($inlineJS);
     } else {
         $inlineJS = '';
     }
     return $this->renderInternal('template_render.php', ['inlineJS' => $inlineJS]);
 }
Exemple #12
0
 public function actionQuery()
 {
     $postdata = file_get_contents("php://input");
     $post = CJSON::decode($postdata);
     $class = Helper::explodeLast(".", $post['class']);
     Yii::import($post['class']);
     $this->lastCount = @$post['lc'] > 0 ? @$post['lc'] : 0;
     if (class_exists($class)) {
         $fb = FormBuilder::load($class);
         $field = $fb->findField(['name' => $post['name']]);
         if ($field['fieldType'] != "php" && method_exists($class, 'model')) {
             if (!is_null(@$post['model_id'])) {
                 $fb->model = $class::model()->findByPk(@$post['model_id']);
             }
             if (is_null($fb->model)) {
                 $fb->model = new $class();
             }
         }
         $this->attributes = $field;
         $this->builder = $fb;
         $this->queryParams = is_array(@$post['params']) ? @$post['params'] : [];
         if (is_object($this->model) && isset($post['modelParams'])) {
             $this->model->attributes = $post['modelParams'];
         }
         $isGenerate = isset($post['generate']);
         if (is_string($this->params)) {
             $this->params = [];
         }
         if ($this->postData == 'No' || $this->relationTo == '' || $this->relationTo == '-- NONE --') {
             ## without relatedTo
             switch ($this->fieldType) {
                 case "sql":
                     $data = $this->query($this->params);
                     break;
                 case "phpsql":
                     $this->sql = $this->execute($this->params);
                     $data = $this->query($this->params);
                     break;
                 case "php":
                     $data = $this->execute($this->params);
                     break;
             }
         } else {
             ## with relatedTo
             $data = $this->getRelated($this->params, $isGenerate);
         }
         if (empty($data)) {
             echo "{}";
             die;
         }
         echo json_encode(['data' => $data['data'], 'count' => $data['debug']['count'], 'params' => $data['debug']['params'], 'debug' => $this->debugSql == 'Yes' ? $data['debug'] : []]);
     }
 }
 public function actionUpdate($id = null)
 {
     $content = '';
     $name = '';
     $command = '';
     $commandFull = '';
     $commandPrefix = '';
     $file = '';
     $period = '';
     $periodType = '';
     $cmd = [];
     if (isset($id)) {
         $cmd = Setting::get('process.' . $id);
         if (count($cmd) > 0) {
             $id = $id;
             $name = $cmd['name'];
             $commandFull = $cmd['command'];
             $tmp = explode(" ", $cmd['command']);
             $commandPrefix = array_shift($tmp);
             $command = implode(" ", $tmp);
             $period = $cmd['period'];
             $periodType = $cmd['periodType'];
             $file = $cmd['file'];
             $prefix = Helper::explodeFirst("-", Helper::camelToSnake(Helper::explodeLast(".", $file)));
             //$filePath = Yii::getPathOfAlias((count($path)>2 ? "application.modules.". $path[1] . ".commands." . $path[2]  : "application.commands.". $path[1])) . ".php";
             $filePath = Yii::getPathOfAlias($file) . ".php";
             $content = file_get_contents($filePath);
         } else {
             $this->redirect(['/dev/processManager/']);
         }
     } else {
         $this->redirect(['/dev/processManager/']);
     }
     if (isset($_POST['DevSettingsProcessManagerForm'])) {
         $cmd = $_POST['DevSettingsProcessManagerForm'];
         $id = $_POST['processSettingsId'];
         $prefix = $_POST['processCommandPrefix'];
         Setting::set("process." . $id . ".name", $cmd['processName']);
         Setting::set("process." . $id . ".command", $prefix . ' ' . $cmd['processCommand']);
         Setting::set("process." . $id . ".period", $cmd['processPeriod']);
         Setting::set("process." . $id . ".periodType", $cmd['processPeriodType']);
         Setting::set("process." . $id . ".periodCount", ProcessHelper::periodConverter($cmd['processPeriod'], $cmd['processPeriodType']));
         $this->redirect(['/dev/processManager/']);
     }
     Asset::registerJS('application.static.js.lib.ace');
     $this->renderForm('settings.DevSettingsProcessManagerForm', ['content' => $content, 'name' => $file, 'prefix' => $name, 'processName' => $name, 'processCommand' => $command, 'processCommandPrefix' => $commandPrefix, 'processCommandFull' => $commandFull, 'processSettingsId' => $id, 'periodType' => $periodType, 'period' => $period]);
 }
Exemple #14
0
 public static function load($classpath, $options = null)
 {
     $mt = new MenuTree();
     $mt->title = @$options['title'];
     $mt->icon = @$options['icon'];
     $mt->options = @$options['options'];
     $mt->inlineJS = @$options['inlineJS'];
     $mt->sections = !is_array(@$options['sections']) ? [] : @$options['sections'];
     $mt->classpath = $classpath;
     $mt->class = Helper::explodeLast(".", $classpath);
     $mt->list = (include Yii::getPathOfAlias($classpath) . ".php");
     MenuTree::fillMenuItems($mt->list);
     return $mt;
 }