Exemple #1
0
 /**
  * Either creates or updates a model depending on whether an ID is passed in.
  */
 public function action_save($table_name, $id = null, $method = null)
 {
     // Find class name and metadata etc
     $class_name = \Admin::getClassForTable($table_name);
     if ($class_name === false) {
         return $this->customPageOr404(array($table_name, $method), "type");
     }
     $can_edit = \CMF\Auth::can('edit', $class_name);
     if (!$can_edit) {
         return $this->show403('action_singular', array('action' => \Lang::get('admin.verbs.edit'), 'resource' => strtolower($class_name::singular())));
     }
     $metadata = $class_name::metadata();
     $plural = $class_name::plural();
     $singular = $class_name::singular();
     $list_page_segment = $metadata->table['name'];
     if ($metadata->name != $metadata->rootEntityName) {
         $rootClass = $metadata->rootEntityName;
         $rootMeta = $rootClass::metadata();
         $list_page_segment = $rootMeta->table['name'];
     }
     if (\Input::param('alias', false) !== false) {
         $plural = 'Links';
         $singular = 'Link';
     }
     \Admin::setCurrentClass($class_name);
     $message = \Lang::get('admin.messages.item_save_success', array('resource' => $singular));
     // Find the model, or create a new one if there's no ID
     if ($exists = isset($id) && !empty($id)) {
         $model = $class_name::find($id);
         if (is_null($model)) {
             return $this->show404(null, "type");
         }
     } else {
         $model = new $class_name();
         $message = \Lang::get('admin.messages.item_create_success', array('resource' => $singular));
     }
     $create_new = \Input::post('create_new', false) !== false;
     $save_and_close = \Input::post('saveAndClose', false) !== false;
     $old_url = $model->url;
     // Populate the model with posted data
     $model->populate(\Input::post());
     // Validate the model
     if ($model->validate(null, null, array('id', 'pos'))) {
         $em = \D::manager();
         $em->persist($model);
         $em->flush();
         if (!$exists && $model->_has_processing_errors) {
             // Try populating the model again if we've had errors - they could be to do with unit of work
             $model->populate(\Input::post());
             $em->persist($model);
             $em->flush();
         }
         // Sync file fields to DB
         try {
             \CMF\Storage::syncFileFieldsFor($model);
         } catch (\Exception $e) {
         }
         // Do something depending on what mode we're in...
         switch (\Input::param('_mode', 'default')) {
             // Renders a page with some JS that will close the fancybox popup
             case 'inline':
                 $options = $class_name::options();
                 $ids = array_keys($options);
                 $pos = array_search(strval($model->id), $ids);
                 if ($pos === false) {
                     $pos = 0;
                 }
                 $this->options = $options;
                 $this->pos = $pos;
                 $this->model = $model;
                 $this->className = $metadata->name;
                 $this->template = 'admin/item/saved-inline.twig';
                 return;
                 break;
             default:
                 $qs = \Uri::build_query_string(\Input::get());
                 if (strlen($qs) > 0) {
                     $qs = '?' . $qs;
                 }
                 \Session::set_flash('main_alert', array('attributes' => array('class' => 'alert-success'), 'msg' => $message));
                 if ($create_new) {
                     \Response::redirect("/admin/{$table_name}/create{$qs}", 'location');
                 }
                 if ($save_and_close) {
                     \Response::redirect("/admin/{$list_page_segment}" . $qs, 'location');
                 }
                 \Response::redirect("/admin/{$table_name}/" . $model->get('id') . "/edit{$qs}", 'location');
                 break;
         }
     }
     // If it's come this far, we have a problem. Render out the form with the errors...
     $this->actions = $class_name::actions();
     $this->form = new ModelForm($metadata, $model);
     $this->table_name = $metadata->table['name'];
     $this->model = $model;
     $this->qs = \Uri::build_query_string(\Input::get());
     $this->template = 'admin/item/edit.twig';
     // Permissions
     $this->can_edit = $can_edit;
     $this->can_create = \CMF\Auth::can('create', $class_name);
     $this->can_delete = \CMF\Auth::can('delete', $class_name);
     if ($exists) {
         $this->js['model'] = $class_name;
         $this->js['item_id'] = $model->id;
         $this->js['table_name'] = $table_name;
     }
     \Session::set_flash('main_alert', array('attributes' => array('class' => 'alert-danger'), 'msg' => \Lang::get('admin.errors.actions.save', array('resource' => strtolower($class_name::singular())))));
 }