Example #1
0
 /**
  * Saves or updates a model.
  *
  * @param array $params
  * @param string $type create or update
  * @return success on save/update, add/edit form if data is empty or error if save or update fails
  * @access private
  */
 function __scaffoldSave($params = array(), $type)
 {
     $thtml = 'edit';
     $form = 'Edit';
     $success = 'updated';
     $formError = 'edit';
     if ($this->controllerClass->_beforeScaffold($type)) {
         if (empty($this->controllerClass->params['data'])) {
             if ($type === 'create') {
                 $formError = 'add';
             }
             return $this->__scaffoldForm($params, $formError);
         }
         $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames());
         $this->controllerClass->cleanUpFields();
         if ($type == 'create') {
             $this->controllerClass->{$this->modelKey}->create();
             $thtml = 'add';
             $form = 'Add';
             $success = 'saved';
         }
         if ($this->controllerClass->{$this->modelKey}->save($this->controllerClass->params['data'])) {
             if ($this->controllerClass->_afterScaffoldSave($type)) {
                 if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
                     $this->controllerClass->Session->setFlash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.');
                     $this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));
                 } else {
                     return $this->controllerClass->flash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.', '/' . Inflector::underscore($this->controllerClass->viewPath));
                 }
             } else {
                 return $this->controllerClass->_afterScaffoldSaveError($type);
             }
         } else {
             if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {
                 $this->controllerClass->Session->setFlash('Please correct errors below.');
             }
             $this->controllerClass->set('data', $this->controllerClass->params['data']);
             $this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames($this->__rebuild($this->controllerClass->params['data'])));
             $this->controllerClass->validateErrors($this->controllerClass->{$this->modelKey});
             $this->controllerClass->set('type', $form);
             if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.' . $thtml . '.thtml')) {
                 return $this->controllerClass->render($this->actionView, '', APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.' . $thtml . '.thtml');
             } elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml')) {
                 return $this->controllerClass->render($this->actionView, '', APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml');
             } else {
                 return $this->controllerClass->render($this->actionView, '', LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'edit.thtml');
             }
         }
     } elseif ($this->controllerClass->_scaffoldError($type) === false) {
         return $this->__scaffoldError();
     }
 }