/**
  * Saves or updates the scaffolded model.
  *
  * @param CakeRequest $request Request Object for scaffolding
  * @param string $action add or edt
  * @return mixed Success on save/update, add/edit form if data is empty or error if save or update fails
  */
 protected function _scaffoldSave(CakeRequest $request, $action = 'edit')
 {
     $formAction = 'edit';
     $success = __('updated');
     if ($action === 'add') {
         $formAction = 'add';
         $success = __('saved');
     }
     if ($this->controller->_beforeScaffold($action)) {
         if ($action == 'edit') {
             if (isset($request->params['pass'][0])) {
                 $this->ScaffoldModel->id = $request['pass'][0];
             }
             if (!$this->ScaffoldModel->exists()) {
                 throw new NotFoundException(__('Invalid %s', Inflector::humanize($this->modelKey)));
             }
         }
         if (!empty($request->data)) {
             if ($action == 'create') {
                 $this->ScaffoldModel->create();
             }
             if ($this->ScaffoldModel->save($request->data)) {
                 if ($this->controller->_afterScaffoldSave($action)) {
                     $message = __('The %1$s has been %2$s', Inflector::humanize($this->modelKey), $success);
                     return $this->_sendMessage($message);
                 } else {
                     return $this->controller->_afterScaffoldSaveError($action);
                 }
             } else {
                 if ($this->_validSession) {
                     $this->controller->Session->setFlash(__('Please correct errors below.'));
                 }
             }
         }
         if (empty($request->data)) {
             if ($this->ScaffoldModel->id) {
                 $this->controller->data = $request->data = $this->ScaffoldModel->read();
             } else {
                 $this->controller->data = $request->data = $this->ScaffoldModel->create();
             }
         }
         foreach ($this->ScaffoldModel->belongsTo as $assocName => $assocData) {
             $varName = Inflector::variable(Inflector::pluralize(preg_replace('/(?:_id)$/', '', $assocData['foreignKey'])));
             $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
         }
         foreach ($this->ScaffoldModel->hasAndBelongsToMany as $assocName => $assocData) {
             $varName = Inflector::variable(Inflector::pluralize($assocName));
             $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
         }
         return $this->_scaffoldForm($formAction);
     } elseif ($this->controller->_scaffoldError($action) === false) {
         return $this->_scaffoldError();
     }
 }
Example #2
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();
     }
 }
Example #3
0
 /**
  * Saves or updates the scaffolded model.
  *
  * @param array $params Parameters for scaffolding
  * @param string $action add or edt
  * @return mixed Success on save/update, add/edit form if data is empty or error if save or update fails
  * @access private
  */
 function __scaffoldSave($params = array(), $action = 'edit')
 {
     $formAction = 'edit';
     $success = __('updated', true);
     if ($action === 'add') {
         $formAction = 'add';
         $success = __('saved', true);
     }
     if ($this->controller->_beforeScaffold($action)) {
         if ($action == 'edit') {
             if (isset($params['pass'][0])) {
                 $this->ScaffoldModel->id = $params['pass'][0];
             }
             if (!$this->ScaffoldModel->exists()) {
                 $message = sprintf(__("Invalid id for %s::edit()", true), Inflector::humanize($this->modelKey));
                 if ($this->_validSession) {
                     $this->controller->Session->setFlash($message);
                     $this->controller->redirect($this->redirect);
                 } else {
                     $this->controller->flash($message, $this->redirect);
                     $this->_output();
                 }
             }
         }
         if (!empty($this->controller->data)) {
             if ($action == 'create') {
                 $this->ScaffoldModel->create();
             }
             if ($this->ScaffoldModel->save($this->controller->data)) {
                 if ($this->controller->_afterScaffoldSave($action)) {
                     $message = sprintf(__('The %1$s has been %2$s', true), Inflector::humanize($this->modelKey), $success);
                     if ($this->_validSession) {
                         $this->controller->Session->setFlash($message);
                         $this->controller->redirect($this->redirect);
                     } else {
                         $this->controller->flash($message, $this->redirect);
                         return $this->_output();
                     }
                 } else {
                     return $this->controller->_afterScaffoldSaveError($action);
                 }
             } else {
                 if ($this->_validSession) {
                     $this->controller->Session->setFlash(__('Please correct errors below.', true));
                 }
             }
         }
         if (empty($this->controller->data)) {
             if ($this->ScaffoldModel->id) {
                 $this->controller->data = $this->ScaffoldModel->read();
             } else {
                 $this->controller->data = $this->ScaffoldModel->create();
             }
         }
         foreach ($this->ScaffoldModel->belongsTo as $assocName => $assocData) {
             $varName = Inflector::variable(Inflector::pluralize(preg_replace('/(?:_id)$/', '', $assocData['foreignKey'])));
             $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
         }
         foreach ($this->ScaffoldModel->hasAndBelongsToMany as $assocName => $assocData) {
             $varName = Inflector::variable(Inflector::pluralize($assocName));
             $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list'));
         }
         return $this->__scaffoldForm($formAction);
     } elseif ($this->controller->_scaffoldError($action) === false) {
         return $this->__scaffoldError();
     }
 }
Example #4
0
 /**
  * testToBeInheritedGuardmethods method
  *
  * @access public
  * @return void
  */
 function testToBeInheritedGuardmethods()
 {
     $Controller = new Controller();
     $this->assertTrue($Controller->_beforeScaffold(''));
     $this->assertTrue($Controller->_afterScaffoldSave(''));
     $this->assertTrue($Controller->_afterScaffoldSaveError(''));
     $this->assertFalse($Controller->_scaffoldError(''));
 }
Example #5
0
 /**
  * testToBeInheritedGuardmethods method
  *
  * @access public
  * @return void
  */
 function testToBeInheritedGuardmethods()
 {
     $request = new CakeRequest('controller_posts/index');
     $Controller = new Controller($request);
     $this->assertTrue($Controller->_beforeScaffold(''));
     $this->assertTrue($Controller->_afterScaffoldSave(''));
     $this->assertTrue($Controller->_afterScaffoldSaveError(''));
     $this->assertFalse($Controller->_scaffoldError(''));
 }