Refactoring repeated blocks of code into a method.

This commit is contained in:
mark_story 2010-05-17 23:51:41 -04:00
parent f6edbfa6e4
commit c1c800865c

View file

@ -179,11 +179,8 @@ class Scaffold {
$message = __(sprintf("No id set for %s::view()", Inflector::humanize($this->modelKey))); $message = __(sprintf("No id set for %s::view()", Inflector::humanize($this->modelKey)));
if (isset($request->params['pass'][0])) { if (isset($request->params['pass'][0])) {
$this->ScaffoldModel->id = $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 { } else {
return $this->controller->flash($message, '/' . Inflector::underscore($this->controller->viewPath)); return $this->_sendMessage($message);
} }
$this->ScaffoldModel->recursive = 1; $this->ScaffoldModel->recursive = 1;
$this->controller->request->data = $this->controller->data = $this->ScaffoldModel->read(); $this->controller->request->data = $this->controller->data = $this->ScaffoldModel->read();
@ -254,13 +251,7 @@ class Scaffold {
if (!$this->ScaffoldModel->exists()) { if (!$this->ScaffoldModel->exists()) {
$message = __(sprintf("Invalid id for %s::edit()", Inflector::humanize($this->modelKey))); $message = __(sprintf("Invalid id for %s::edit()", Inflector::humanize($this->modelKey)));
if ($this->_validSession) { return $this->_sendMessage($message);
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
$this->_output();
}
} }
} }
@ -274,13 +265,7 @@ class Scaffold {
$message = __( $message = __(
sprintf('The %1$s has been %2$s', Inflector::humanize($this->modelKey), $success) sprintf('The %1$s has been %2$s', Inflector::humanize($this->modelKey), $success)
); );
if ($this->_validSession) { return $this->_sendMessage($message);
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
return $this->_output();
}
} else { } else {
return $this->controller->_afterScaffoldSaveError($action); return $this->controller->_afterScaffoldSaveError($action);
} }
@ -329,43 +314,44 @@ class Scaffold {
); );
if (isset($request->params['pass'][0])) { if (isset($request->params['pass'][0])) {
$id = $request->params['pass'][0]; $id = $request->params['pass'][0];
} elseif ($this->_validSession) {
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else { } else {
$this->controller->flash($message, $this->redirect); return $this->_sendMessage($message);
return $this->_output();
} }
if ($this->ScaffoldModel->delete($id)) { if ($this->ScaffoldModel->delete($id)) {
$message = __( $message = __(
sprintf('The %1$s with id: %2$d has been deleted.', Inflector::humanize($this->modelClass), $id) sprintf('The %1$s with id: %2$d has been deleted.', Inflector::humanize($this->modelClass), $id)
); );
if ($this->_validSession) { return $this->_sendMessage($message);
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
return $this->_output();
}
} else { } else {
$message = __(sprintf( $message = __(sprintf(
'There was an error deleting the %1$s with id: %2$d', 'There was an error deleting the %1$s with id: %2$d',
Inflector::humanize($this->modelClass), $id Inflector::humanize($this->modelClass), $id
)); ));
if ($this->_validSession) { return $this->_sendMessage($message);
$this->controller->Session->setFlash($message);
$this->controller->redirect($this->redirect);
} else {
$this->controller->flash($message, $this->redirect);
return $this->_output();
}
} }
} elseif ($this->controller->_scaffoldError('delete') === false) { } elseif ($this->controller->_scaffoldError('delete') === false) {
return $this->_scaffoldError(); 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 * Show a scaffold error
* *