Exemplo n.º 1
0
 public static function generate($model, $overwrite = false)
 {
     $file = APPLICATION_PATH . DS . 'models' . DS . 'Crud' . DS . ucfirst(Inflector::camelize($model)) . '.php';
     if (!File::exists($file) || $overwrite) {
         $db = model($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);
     }
 }
Exemplo n.º 2
0
 function updateAction()
 {
     $table = request()->getTable();
     $id = request()->getId();
     if (!is_null($table) && !is_null($id)) {
         $permission = $table . '_update';
         $auth = auth()->can($permission);
         if (true === $auth || true === $this->isAdmin) {
             $crud = new c(model($table));
             if (true === $this->isPost()) {
                 $pk = model($table)->pk();
                 $_POST[$pk] = $id;
                 $status = $crud->form();
                 if (true === $status) {
                     $this->forward('list');
                 }
             }
             $infos = $crud->config();
             $singular = isAke($infos, 'singular', 'item');
             $this->view->title = 'Mettre à jour - ' . $singular;
             $this->view->singular = $singular;
             $this->view->fields = $crud->fields();
             $this->view->fieldsInfos = isAke($infos, 'fields');
             $this->view->pk = $crud->pk();
             $this->view->row = model($table)->find($id);
             if (empty($this->view->row)) {
                 $this->forward('home');
             }
         } else {
             $this->forward('forbidden');
         }
     } else {
         $this->forward('home');
     }
 }