Example #1
0
 private function facade()
 {
     $crud = new Crud($this);
     $config = $crud->config();
     $facade = isAke($config, 'facade', false);
     $facade2 = false;
     if (false === $facade) {
         $facade = ucfirst($this->db) . ucfirst($this->table);
         if ($this->db == SITE_NAME) {
             $facade2 = ucfirst($this->table);
         }
     }
     $class = '\\Dbjson\\' . $facade;
     if (!class_exists($class)) {
         $code = 'namespace Dbjson; class ' . $facade . ' extends Facade { public static $database = "' . $this->db . '"; public static $table = "' . $this->table . '"; }';
         eval($code);
         Alias::facade('Db' . $facade, $facade, 'Dbjson');
     }
     if (false !== $facade2) {
         $class2 = '\\Dbjson\\' . $facade2;
         if (!class_exists($class2)) {
             $code2 = 'namespace Dbjson; class ' . $facade2 . ' extends Facade { public static $database = "' . $this->db . '"; public static $table = "' . $this->table . '"; }';
             eval($code2);
             Alias::facade('Db' . $facade2, $facade2, 'Dbjson');
         }
     }
 }
Example #2
0
 function deleteAction()
 {
     $table = request()->getTable();
     $id = request()->getId();
     if (!is_null($table) && !is_null($id)) {
         $permission = $table . '_json_delete';
         $auth = auth()->can($permission);
         if (true === $auth || true === $this->isAdmin) {
             $row = jmodel($table)->find($id);
             if (empty($row)) {
                 $this->forward('home', 'static');
             }
             $infos = isAke($this->view->config['tables'], $table);
             $closure = isAke($infos, 'before_delete', false);
             if (false !== $closure && is_callable($closure)) {
                 $closure();
             }
             $crud = new c(jmodel($table));
             $status = $crud->delete($id);
             $closure = isAke($infos, 'after_delete', false);
             if (false !== $closure && is_callable($closure)) {
                 $closure();
             }
             if (true === $status) {
                 $this->forward('list');
             } else {
                 $this->forward('error', 'static');
             }
         } else {
             $this->forward('forbidden', 'static');
         }
     } else {
         $this->forward('home', 'static');
     }
 }
Example #3
0
 public static function generate($database, $model, $fields = [], $overwrite = false)
 {
     $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($database)) . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!is_dir(APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($database)))) {
         File::mkdir(APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($database)));
     }
     if (!File::exists($file) || $overwrite) {
         $db = jdb($database, $model);
         $crud = Crud::instance($db);
         File::delete($file);
         $tplModel = File::read(__DIR__ . DS . 'Model.tpl');
         $tplField = File::read(__DIR__ . DS . 'Field.tpl');
         $fields = empty($fields) ? $crud->fields() : $fields;
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(['##singular##', '##plural##', '##default_order##', '##foreigns##', '##uniques##', '##soft_delete##', '##before_create##', '##after_create##', '##before_update##', '##after_update##', '##before_read##', '##after_read##', '##before_delete##', '##after_delete##', '##before_list##', '##after_list##'], [$singular, $plural, $default_order, '[]', '[]', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false', 'false'], $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(['##field##', '##form_type##', '##helper##', '##required##', '##form_plus##', '##length##', '##is_listable##', '##is_exportable##', '##is_searchable##', '##is_sortable##', '##is_readable##', '##is_creatable##', '##is_updatable##', '##is_deletable##', '##content_view##', '##content_list##', '##content_search##', '##content_create##', '##label##'], [$field, 'text', 'false', 'true', 'false', 'false', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 'false', 'false', 'false', 'false', $label], $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }
Example #4
0
 public static function generate($model, $overwrite = false)
 {
     $file = APPLICATION_PATH . DS . 'models' . DS . 'CrudJson' . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!File::exists($file) || $overwrite) {
         $db = jmodel($model);
         $crud = new Crud($db);
         File::delete($file);
         $tplModel = fgc(__DIR__ . DS . 'Model.tpl');
         $tplField = fgc(__DIR__ . DS . 'Field.tpl');
         $fields = $crud->fields();
         $singular = ucfirst($model);
         $plural = $singular . 's';
         $default_order = $crud->pk();
         $tplModel = str_replace(array('##singular##', '##plural##', '##default_order##'), array($singular, $plural, $default_order), $tplModel);
         $fieldsSection = '';
         foreach ($fields as $field) {
             if ($field != $crud->pk()) {
                 $label = substr($field, -3) == '_id' ? ucfirst(str_replace('_id', '', $field)) : ucfirst(Inflector::camelize($field));
                 $fieldsSection .= str_replace(array('##field##', '##label##'), array($field, $label), $tplField);
             }
         }
         $tplModel = str_replace('##fields##', $fieldsSection, $tplModel);
         File::put($file, $tplModel);
     }
 }