fixing cms page history controller to use new page id param

This commit is contained in:
Tim Kung 2017-04-07 15:58:35 +12:00
parent 487235f991
commit 41eddfcc8e

View File

@ -12,56 +12,81 @@ class CMSPageHistoryController extends CMSMain {
private static $menu_title = 'History'; private static $menu_title = 'History';
private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
private static $session_namespace = 'CMSMain'; private static $session_namespace = 'CMSMain';
private static $allowed_actions = array( private static $allowed_actions = array(
'EditForm',
'VersionsForm', 'VersionsForm',
'CompareVersionsForm', 'CompareVersionsForm',
'show', 'show',
'compare' 'compare'
); );
private static $url_handlers = array( private static $url_handlers = array(
'$Action/$ID/$VersionID/$OtherVersionID' => 'handleAction' 'EditForm/$ID/$VersionID' => 'EditForm',
'$Action/$ID/$VersionID/$OtherVersionID' => 'handleAction',
); );
/**
* Current version ID for this request. Can be 0 for latest version
*
* @var int
*/
protected $versionID = null;
public function getResponseNegotiator() { public function getResponseNegotiator() {
$negotiator = parent::getResponseNegotiator(); $negotiator = parent::getResponseNegotiator();
$controller = $this; $controller = $this;
$negotiator->setCallback('CurrentForm', function() use(&$controller) { $negotiator->setCallback('CurrentForm', function() use(&$controller) {
$form = $controller->ShowVersionForm($controller->getRequest()->param('VersionID')); $form = $controller->getEditForm();
if($form) return $form->forTemplate(); if ($form) {
else return $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); return $form->forTemplate();
} else {
return $controller->renderWith($controller->getTemplatesWithSuffix('_Content'));
}
}); });
$negotiator->setCallback('default', function() use(&$controller) { $negotiator->setCallback('default', function() use(&$controller) {
return $controller->renderWith($controller->getViewer('show')); return $controller->renderWith($controller->getViewer('show'));
}); });
return $negotiator; return $negotiator;
} }
/** /**
* @param SS_HTTPRequest $request * @param SS_HTTPRequest $request
* @return array * @return SS_HTTPResponse
*/ */
public function show($request) { public function show($request) {
$form = $this->ShowVersionForm($request->param('VersionID')); // Record id and version for this request
$id = $request->param('ID');
$this->setCurrentPageID($id);
$versionID = $request->param('VersionID');
$this->setVersionID($versionID);
$form = $this->getEditForm();
$negotiator = $this->getResponseNegotiator(); $negotiator = $this->getResponseNegotiator();
$controller = $this; $controller = $this;
$negotiator->setCallback('CurrentForm', function() use(&$controller, &$form) { $negotiator->setCallback('CurrentForm', function() use(&$controller, &$form) {
return $form ? $form->forTemplate() : $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); return $form
? $form->forTemplate()
: $controller->renderWith($controller->getTemplatesWithSuffix('_Content'));
}); });
$negotiator->setCallback('default', function() use(&$controller, &$form) { $negotiator->setCallback('default', function() use(&$controller, &$form) {
return $controller->customise(array('EditForm' => $form))->renderWith($controller->getViewer('show')); return $controller
->customise(array('EditForm' => $form))
->renderWith($controller->getViewer('show'));
}); });
return $negotiator->respond($request); return $negotiator->respond($request);
} }
/** /**
* @param SS_HTTPRequest $request * @param SS_HTTPRequest $request
* @return array * @return SS_HTTPResponse
*/ */
public function compare($request) { public function compare($request) {
$id = $request->param('ID');
$this->setCurrentPageID($id);
$form = $this->CompareVersionsForm( $form = $this->CompareVersionsForm(
$request->param('VersionID'), $request->param('VersionID'),
$request->param('OtherVersionID') $request->param('OtherVersionID')
@ -70,10 +95,14 @@ class CMSPageHistoryController extends CMSMain {
$negotiator = $this->getResponseNegotiator(); $negotiator = $this->getResponseNegotiator();
$controller = $this; $controller = $this;
$negotiator->setCallback('CurrentForm', function() use(&$controller, &$form) { $negotiator->setCallback('CurrentForm', function() use(&$controller, &$form) {
return $form ? $form->forTemplate() : $controller->renderWith($controller->getTemplatesWithSuffix('_Content')); return $form
? $form->forTemplate()
: $controller->renderWith($controller->getTemplatesWithSuffix('_Content'));
}); });
$negotiator->setCallback('default', function() use(&$controller, &$form) { $negotiator->setCallback('default', function() use(&$controller, &$form) {
return $controller->customise(array('EditForm' => $form))->renderWith($controller->getViewer('show')); return $controller
->customise(array('EditForm' => $form))
->renderWith($controller->getViewer('show'));
}); });
return $negotiator->respond($request); return $negotiator->respond($request);
@ -88,7 +117,24 @@ class CMSPageHistoryController extends CMSMain {
return false; return false;
} }
} }
/**
* @param SS_HTTPRequest $request
* @return Form
*/
public function EditForm($request = null) {
if ($request) {
// Validate VersionID is present
$versionID = $request->param('VersionID');
if (!isset($versionID)) {
$this->httpError(400);
return null;
}
$this->setVersionID($versionID);
}
return parent::EditForm($request);
}
/** /**
* Returns the read only version of the edit form. Detaches all {@link FormAction} * Returns the read only version of the edit form. Detaches all {@link FormAction}
* instances attached since only action relates to revert. * instances attached since only action relates to revert.
@ -103,29 +149,48 @@ class CMSPageHistoryController extends CMSMain {
* @return Form * @return Form
*/ */
public function getEditForm($id = null, $fields = null, $versionID = null, $compareID = null) { public function getEditForm($id = null, $fields = null, $versionID = null, $compareID = null) {
if(!$id) $id = $this->currentPageID(); if (!$id) {
$id = $this->currentPageID();
}
if (!$versionID) {
$versionID = $this->getVersionID();
}
$record = $this->getRecord($id, $versionID); $record = $this->getRecord($id, $versionID);
$versionID = ($record) ? $record->Version : $versionID; if (!$record) {
return $this->EmptyForm();
$form = parent::getEditForm($record, ($record) ? $record->getCMSFields() : null); }
// Refresh version ID
$versionID = $record->Version;
$this->setVersionID($versionID);
// Get edit form
$form = parent::getEditForm($record, $record->getCMSFields());
// Respect permission failures from parent implementation // Respect permission failures from parent implementation
if(!($form instanceof Form)) return $form; if (!($form instanceof Form)) {
return $form;
}
// TODO: move to the SilverStripeNavigator structure so the new preview can pick it up. // TODO: move to the SilverStripeNavigator structure so the new preview can pick it up.
//$nav = new SilverStripeNavigatorItem_ArchiveLink($record); //$nav = new SilverStripeNavigatorItem_ArchiveLink($record);
$form->setActions(new FieldList( $actions = new FieldList(
$revert = FormAction::create('doRollback', _t('CMSPageHistoryController.REVERTTOTHISVERSION', 'Revert to this version'))->setUseButtonTag(true) $revert = FormAction::create(
)); 'doRollback',
_t('CMSPageHistoryController.REVERTTOTHISVERSION', 'Revert to this version')
)->setUseButtonTag(true)
);
$actions->setForm($form);
$form->setActions($actions);
$fields = $form->Fields(); $fields = $form->Fields();
$fields->removeByName("Status"); $fields->removeByName("Status");
$fields->push(new HiddenField("ID")); $fields->push(new HiddenField("ID"));
$fields->push(new HiddenField("Version")); $fields->push(new HiddenField("Version"));
$fields = $fields->makeReadonly(); $fields = $fields->makeReadonly();
if($compareID) { if($compareID) {
$link = Controller::join_links( $link = Controller::join_links(
$this->Link('show'), $this->Link('show'),
@ -133,7 +198,7 @@ class CMSPageHistoryController extends CMSMain {
); );
$view = _t('CMSPageHistoryController.VIEW',"view"); $view = _t('CMSPageHistoryController.VIEW',"view");
$message = _t( $message = _t(
'CMSPageHistoryController.COMPARINGVERSION', 'CMSPageHistoryController.COMPARINGVERSION',
"Comparing versions {version1} and {version2}.", "Comparing versions {version1} and {version2}.",
@ -142,7 +207,7 @@ class CMSPageHistoryController extends CMSMain {
'version2' => sprintf('%s (<a href="%s">%s</a>)', $compareID, Controller::join_links($link, $compareID), $view) 'version2' => sprintf('%s (<a href="%s">%s</a>)', $compareID, Controller::join_links($link, $compareID), $view)
) )
); );
$revert->setReadonly(true); $revert->setReadonly(true);
} else { } else {
if($record->isLatestVersion()) { if($record->isLatestVersion()) {
@ -155,7 +220,7 @@ class CMSPageHistoryController extends CMSMain {
); );
} }
} }
$fields->addFieldToTab('Root.Main', $fields->addFieldToTab('Root.Main',
new LiteralField('CurrentlyViewingMessage', $this->customise(array( new LiteralField('CurrentlyViewingMessage', $this->customise(array(
'Content' => $message, 'Content' => $message,
@ -169,17 +234,19 @@ class CMSPageHistoryController extends CMSMain {
"ID" => $id, "ID" => $id,
"Version" => $versionID, "Version" => $versionID,
)); ));
if(($record && $record->isLatestVersion())) { if ($record->isLatestVersion()) {
$revert->setReadonly(true); $revert->setReadonly(true);
} }
$form->removeExtraClass('cms-content'); $form->removeExtraClass('cms-content');
$form->setFormAction(Controller::join_links($form->FormAction(), $id, $versionID));
return $form; return $form;
} }
/** /**
* Version select form. Main interface between selecting versions to view * Version select form. Main interface between selecting versions to view
* and comparing multiple versions. * and comparing multiple versions.
@ -197,7 +264,7 @@ class CMSPageHistoryController extends CMSMain {
$action = $this->getRequest()->param('Action'); $action = $this->getRequest()->param('Action');
$versionID = $this->getRequest()->param('VersionID'); $versionID = $this->getRequest()->param('VersionID');
$otherVersionID = $this->getRequest()->param('OtherVersionID'); $otherVersionID = $this->getRequest()->param('OtherVersionID');
$showUnpublishedChecked = 0; $showUnpublishedChecked = 0;
$compareModeChecked = ($action == "compare"); $compareModeChecked = ($action == "compare");
@ -208,19 +275,19 @@ class CMSPageHistoryController extends CMSMain {
if($versions) { if($versions) {
foreach($versions as $k => $version) { foreach($versions as $k => $version) {
$active = false; $active = false;
if($version->Version == $versionID || $version->Version == $otherVersionID) { if($version->Version == $versionID || $version->Version == $otherVersionID) {
$active = true; $active = true;
if(!$version->WasPublished) $showUnpublishedChecked = 1; if(!$version->WasPublished) $showUnpublishedChecked = 1;
} }
$version->Active = ($active); $version->Active = ($active);
} }
} }
$vd = new ViewableData(); $vd = new ViewableData();
$versionsHtml = $vd->customise(array( $versionsHtml = $vd->customise(array(
'Versions' => $versions 'Versions' => $versions
))->renderWith('CMSPageHistoryController_versions'); ))->renderWith('CMSPageHistoryController_versions');
@ -241,120 +308,25 @@ class CMSPageHistoryController extends CMSMain {
$hiddenID = new HiddenField('ID', false, "") $hiddenID = new HiddenField('ID', false, "")
); );
$actions = new FieldList(
new FormAction(
'doCompare', _t('CMSPageHistoryController.COMPAREVERSIONS','Compare Versions')
),
new FormAction(
'doShowVersion', _t('CMSPageHistoryController.SHOWVERSION','Show Version')
)
);
// Use <button> to allow full jQuery UI styling
foreach($actions->dataFields() as $action) $action->setUseButtonTag(true);
$form = CMSForm::create( $form = CMSForm::create(
$this, $this,
'VersionsForm', 'VersionsForm',
$fields, $fields,
$actions new FieldList()
)->setHTMLID('Form_VersionsForm'); )->setHTMLID('Form_VersionsForm');
$form->setResponseNegotiator($this->getResponseNegotiator()); $form->setResponseNegotiator($this->getResponseNegotiator());
$form->loadDataFrom($this->getRequest()->requestVars()); $form->loadDataFrom($this->getRequest()->requestVars());
$hiddenID->setValue($id); $hiddenID->setValue($id);
$form->unsetValidator(); $form->unsetValidator();
$form $form
->addExtraClass('cms-versions-form') // placeholder, necessary for $.metadata() to work ->addExtraClass('cms-versions-form') // placeholder, necessary for $.metadata() to work
->setAttribute('data-link-tmpl-compare', Controller::join_links($this->Link('compare'), '%s', '%s', '%s')) ->setAttribute('data-link-tmpl-compare', Controller::join_links($this->Link('compare'), '%s', '%s', '%s'))
->setAttribute('data-link-tmpl-show', Controller::join_links($this->Link('show'), '%s', '%s')); ->setAttribute('data-link-tmpl-show', Controller::join_links($this->Link('show'), '%s', '%s'));
return $form;
}
/**
* Process the {@link VersionsForm} compare function between two pages.
*
* @param array
* @param Form
*
* @return html
*/
public function doCompare($data, $form) {
$versions = $data['Versions'];
if(count($versions) < 2) return null;
$id = $this->currentPageID();
$version1 = array_shift($versions);
$version2 = array_shift($versions);
$form = $this->CompareVersionsForm($version1, $version2);
// javascript solution, render into template
if($this->getRequest()->isAjax()) {
return $this->customise(array(
"EditForm" => $form
))->renderWith(array(
$this->class . '_EditForm',
'LeftAndMain_Content'
));
}
// non javascript, redirect the user to the page
$this->redirect(Controller::join_links(
$this->Link('compare'),
$version1,
$version2
));
}
/**
* Process the {@link VersionsForm} show version function. Only requires
* one page to be selected.
*
* @param array
* @param Form
*
* @return html
*/
public function doShowVersion($data, $form) {
$versionID = null;
if(isset($data['Versions']) && is_array($data['Versions'])) {
$versionID = array_shift($data['Versions']);
}
if(!$versionID) return;
if($request->isAjax()) {
return $this->customise(array(
"EditForm" => $this->ShowVersionForm($versionID)
))->renderWith(array(
$this->class . '_EditForm',
'LeftAndMain_Content'
));
}
// non javascript, redirect the user to the page
$this->redirect(Controller::join_links(
$this->Link('version'),
$versionID
));
}
/**
* @param int|null $versionID
* @return Form
*/
public function ShowVersionForm($versionID = null) {
if(!$versionID) return null;
$id = $this->currentPageID();
$form = $this->getEditForm($id, null, $versionID);
return $form; return $form;
} }
/** /**
* @param int $versionID * @param int $versionID
* @param int $otherVersionID * @param int $otherVersionID
@ -370,10 +342,10 @@ class CMSPageHistoryController extends CMSMain {
} }
if(!$toVersion || !$fromVersion) return false; if(!$toVersion || !$fromVersion) return false;
$id = $this->currentPageID(); $id = $this->currentPageID();
$page = DataObject::get_by_id("SiteTree", $id); $page = DataObject::get_by_id("SiteTree", $id);
if($page && $page->exists()) { if($page && $page->exists()) {
if(!$page->canView()) { if(!$page->canView()) {
return Security::permissionFailure($this); return Security::permissionFailure($this);
@ -384,11 +356,11 @@ class CMSPageHistoryController extends CMSMain {
$fromVersionRecord = Versioned::get_version('SiteTree', $id, $fromVersion); $fromVersionRecord = Versioned::get_version('SiteTree', $id, $fromVersion);
$toVersionRecord = Versioned::get_version('SiteTree', $id, $toVersion); $toVersionRecord = Versioned::get_version('SiteTree', $id, $toVersion);
if(!$fromVersionRecord) { if(!$fromVersionRecord) {
user_error("Can't find version $fromVersion of page $id", E_USER_ERROR); user_error("Can't find version $fromVersion of page $id", E_USER_ERROR);
} }
if(!$toVersionRecord) { if(!$toVersionRecord) {
user_error("Can't find version $toVersion of page $id", E_USER_ERROR); user_error("Can't find version $toVersion of page $id", E_USER_ERROR);
} }
@ -397,27 +369,27 @@ class CMSPageHistoryController extends CMSMain {
$form = $this->getEditForm($id, null, $fromVersion, $toVersion); $form = $this->getEditForm($id, null, $fromVersion, $toVersion);
$form->setActions(new FieldList()); $form->setActions(new FieldList());
$form->addExtraClass('compare'); $form->addExtraClass('compare');
// Comparison views shouldn't be editable. // Comparison views shouldn't be editable.
// Its important to convert fields *before* loading data, // Its important to convert fields *before* loading data,
// as the comparison output is HTML and not valid values for the various field types // as the comparison output is HTML and not valid values for the various field types
$readonlyFields = $form->Fields()->makeReadonly(); $readonlyFields = $form->Fields()->makeReadonly();
$form->setFields($readonlyFields); $form->setFields($readonlyFields);
$form->loadDataFrom($record); $form->loadDataFrom($record);
$form->loadDataFrom(array( $form->loadDataFrom(array(
"ID" => $id, "ID" => $id,
"Version" => $fromVersion, "Version" => $fromVersion,
)); ));
foreach($form->Fields()->dataFields() as $field) { foreach($form->Fields()->dataFields() as $field) {
$field->dontEscape = true; $field->dontEscape = true;
} }
return $form; return $form;
} }
return false; return false;
} }
public function Breadcrumbs($unlinked = false) { public function Breadcrumbs($unlinked = false) {
@ -426,4 +398,24 @@ class CMSPageHistoryController extends CMSMain {
return $crumbs; return $crumbs;
} }
/**
* Set current version ID
*
* @param int $versionID
* @return $this
*/
public function setVersionID($versionID) {
$this->versionID = $versionID;
return $this;
}
/**
* Get current version ID
*
* @return int
*/
public function getVersionID() {
return $this->versionID;
}
} }