Skip to content

Commit

Permalink
Refactoring repeated blocks of code into a method.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 18, 2010
1 parent f6edbfa commit c1c8008
Showing 1 changed file with 23 additions and 37 deletions.
60 changes: 23 additions & 37 deletions cake/libs/controller/scaffold.php
Expand Up @@ -179,11 +179,8 @@ protected function _scaffoldView(CakeRequest $request) {
$message = __(sprintf("No id set for %s::view()", Inflector::humanize($this->modelKey)));
if (isset($request->params['pass'][0])) {
$this->ScaffoldModel->id = $request->params['pass'][0];
} elseif ($this->_validSession) {
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
return $this->controller->flash($message, '/' . Inflector::underscore($this->controller->viewPath));
return $this->_sendMessage($message);
}
$this->ScaffoldModel->recursive = 1;
$this->controller->request->data = $this->controller->data = $this->ScaffoldModel->read();
Expand Down Expand Up @@ -254,13 +251,7 @@ protected function _scaffoldSave(CakeRequest $request, $action = 'edit') {

if (!$this->ScaffoldModel->exists()) {
$message = __(sprintf("Invalid id for %s::edit()", 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();
}
return $this->_sendMessage($message);
}
}

Expand All @@ -274,13 +265,7 @@ protected function _scaffoldSave(CakeRequest $request, $action = 'edit') {
$message = __(
sprintf('The %1$s has been %2$s', 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();
}
return $this->_sendMessage($message);
} else {
return $this->controller->_afterScaffoldSaveError($action);
}
Expand Down Expand Up @@ -329,43 +314,44 @@ protected function _scaffoldDelete(CakeRequest $request) {
);
if (isset($request->params['pass'][0])) {
$id = $request->params['pass'][0];
} elseif ($this->_validSession) {
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
return $this->_output();
return $this->_sendMessage($message);
}

if ($this->ScaffoldModel->delete($id)) {
$message = __(
sprintf('The %1$s with id: %2$d has been deleted.', Inflector::humanize($this->modelClass), $id)
);
if ($this->_validSession) {
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
return $this->_output();
}
return $this->_sendMessage($message);
} else {
$message = __(sprintf(
'There was an error deleting the %1$s with id: %2$d',
Inflector::humanize($this->modelClass), $id
));
if ($this->_validSession) {
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
return $this->_output();
}
return $this->_sendMessage($message);
}
} elseif ($this->controller->_scaffoldError('delete') === false) {
return $this->_scaffoldError();
}
}

/**
* Sends a message to the user. Either uses Sessions or flash messages depending
* on the availability of a session
*
* @param string $message Message to display
* @return void
*/
protected function _sendMessage($message) {
if ($this->_validSession) {
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
$this->_output();
}
}

/**
* Show a scaffold error
*
Expand Down

0 comments on commit c1c8008

Please sign in to comment.