ENHANCEMENT Supporting passing in objects in LeftAndMain->getRecord() (and subclasses) in order to reduce boilerplate code in controller actions

This commit is contained in:
Ingo Schommer 2011-03-16 16:39:49 +13:00
parent 934c3f2c1b
commit 824f7451a0
3 changed files with 26 additions and 10 deletions

View File

@ -369,7 +369,9 @@ JS;
public function getRecord($id) {
$treeClass = $this->stat('tree_class');
if($id && is_numeric($id)) {
if($id instanceof $treeClass) {
return $id;
} else if($id && is_numeric($id)) {
$version = isset($_REQUEST['Version']) ? $_REQUEST['Version'] : null;
if(is_numeric($version)) {
$record = Versioned::get_version($treeClass, $id, $version);
@ -417,11 +419,12 @@ JS;
// Include JavaScript to ensure HtmlEditorField works.
HtmlEditorField::include_js();
if(!$id) $id = $this->currentPageID();
$form = parent::getEditForm($id);
// TODO Duplicate record fetching (see parent implementation)
if(!$id) $id = $this->currentPageID();
$record = ($id && $id != "root") ? $this->getRecord($id) : null;
$record = $this->getRecord($id);
if($record && !$record->canView()) return Security::permissionFailure($this);
$fields = $form->Fields();
$actions = $form->Actions();

View File

@ -474,10 +474,14 @@ class LeftAndMain extends Controller {
return $this->renderWith($this->getTemplatesWithSuffix('_right'));
}
public function getRecord($id, $className = null) {
if($id && is_numeric($id)) {
if(!$className) $className = $this->stat('tree_class');
public function getRecord($id) {
$className = $this->stat('tree_class');
if($id instanceof $className) {
return $id;
} else if(is_numeric($id)) {
return DataObject::get_by_id($className, $id);
} else {
return false;
}
}
@ -743,9 +747,13 @@ class LeftAndMain extends Controller {
public function getEditForm($id = null) {
if(!$id) $id = $this->currentPageID();
$record = ($id && $id != "root") ? $this->getRecord($id) : null;
if($record && !$record->canView()) return Security::permissionFailure($this);
if(is_object($id)) {
$record = $id;
} else {
$record = ($id && $id != "root") ? $this->getRecord($id) : null;
if($record && !$record->canView()) return Security::permissionFailure($this);
}
if($record) {
$fields = $record->getCMSFields();

View File

@ -42,8 +42,13 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
}
function getEditForm($id = null) {
// TODO Duplicate record fetching (see parent implementation)
if(!$id) $id = $this->currentPageID();
$record = ($id && $id != "root") ? $this->getRecord($id) : null;
$form = parent::getEditForm($id);
// TODO Duplicate record fetching (see parent implementation)
$record = $this->getRecord($id);
if($record && !$record->canView()) return Security::permissionFailure($this);
if($id && is_numeric($id)) {
$form = parent::getEditForm($id);