ENHANCEMENT Make page urls bookmarkable

This commit is contained in:
Damian Mooyman 2017-04-12 13:00:21 +12:00 committed by GitHub
commit 1a6518803b
2 changed files with 215 additions and 77 deletions

View File

@ -15,6 +15,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
private static $url_rule = '/$Action/$ID/$OtherID'; private static $url_rule = '/$Action/$ID/$OtherID';
private static $url_handlers = array(
'EditForm/$ID' => 'EditForm',
);
// Maintain a lower priority than other administration sections // Maintain a lower priority than other administration sections
// so that Director does not think they are actions of CMSMain // so that Director does not think they are actions of CMSMain
private static $url_priority = 39; private static $url_priority = 39;
@ -66,6 +70,13 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
*/ */
private static $enabled_legacy_actions = array(); private static $enabled_legacy_actions = array();
/**
* The page id for this request
*
* @var int|null
*/
protected $pageID = null;
public function init() { public function init() {
// set reading lang // set reading lang
if(SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) { if(SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) {
@ -592,6 +603,29 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
} }
} }
/**
* Ensuring we set the current page id from the $ID url parameter.
*
* @param SS_HTTPRequest $request
*
* @return Form
*/
public function EditForm($request = null) {
// set page ID from request
if ($request) {
// validate id is present
$id = $request->param('ID');
if (!isset($id)) {
return $this->httpError(400);
}
$this->setCurrentPageID($id);
}
return $this->getEditForm();
}
/** /**
* @param int $id * @param int $id
* @param FieldList $fields * @param FieldList $fields
@ -690,6 +724,13 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$form->setFields($readonlyFields); $form->setFields($readonlyFields);
} }
// update form action to include $pageID
$form->setFormAction(Controller::join_links(
$this->Link(),
$form->getName(),
$id
));
$this->extend('updateEditForm', $form); $this->extend('updateEditForm', $form);
return $form; return $form;
} else if($id) { } else if($id) {
@ -866,8 +907,25 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
return $listview; return $listview;
} }
/**
* Set the page id into $pageID rather than into {@link Session}.
*
* @param string|int $id
*
* @return void
*/
public function setCurrentPageID($id) {
$id = (int)$id;
$this->pageID = $id;
}
/**
* Get the page id from this request
*
* @return int
*/
public function currentPageID() { public function currentPageID() {
$id = parent::currentPageID(); $id = $this->pageID;
$this->extend('updateCurrentPageID', $id); $this->extend('updateCurrentPageID', $id);

View File

@ -14,6 +14,7 @@ class CMSPageHistoryController extends 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',
@ -21,16 +22,27 @@ class CMSPageHistoryController extends CMSMain {
); );
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'));
@ -40,18 +52,28 @@ class CMSPageHistoryController extends CMSMain {
/** /**
* @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);
@ -59,9 +81,12 @@ class CMSPageHistoryController extends CMSMain {
/** /**
* @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);
@ -89,6 +118,23 @@ class CMSPageHistoryController extends CMSMain {
} }
} }
/**
* @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,21 +149,40 @@ 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");
@ -170,12 +235,19 @@ class CMSPageHistoryController extends CMSMain {
"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(
$this->Link(),
$form->getName(),
$id,
$versionID
));
return $form; return $form;
} }
@ -241,23 +313,11 @@ 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());
@ -417,7 +477,7 @@ class CMSPageHistoryController extends CMSMain {
return $form; return $form;
} }
return false; return false;
} }
public function Breadcrumbs($unlinked = false) { public function Breadcrumbs($unlinked = false) {
@ -426,4 +486,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;
}
} }