diff --git a/code/controllers/CMSMain.php b/code/controllers/CMSMain.php index 2a92e07a..6d8a481e 100644 --- a/code/controllers/CMSMain.php +++ b/code/controllers/CMSMain.php @@ -809,6 +809,57 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr return $this->getResponseNegotiator()->respond($this->request); } + /** + * @return array + */ + function rollback() { + return $this->doRollback(array( + 'ID' => $this->currentPageID(), + 'Version' => $this->request->param('VersionID') + ), null); + } + + /** + * Rolls a site back to a given version ID + * + * @param array + * @param Form + * + * @return html + */ + function doRollback($data, $form) { + $this->extend('onBeforeRollback', $data['ID']); + + $id = (isset($data['ID'])) ? (int) $data['ID'] : null; + $version = (isset($data['Version'])) ? (int) $data['Version'] : null; + + $record = DataObject::get_by_id($this->stat('tree_class'), $id); + if($record && !$record->canEdit()) return Security::permissionFailure($this); + + if($version) { + $record->doRollbackTo($version); + $message = sprintf( + _t('CMSMain.ROLLEDBACKVERSION',"Rolled back to version #%d. New version number is #%d"), + $data['Version'], + $record->Version + ); + } else { + $record->doRollbackTo('Live'); + $message = sprintf( + _t('CMSMain.ROLLEDBACKPUB',"Rolled back to published version. New version number is #%d"), + $record->Version + ); + } + + $this->response->addHeader('X-Status', $message); + + // Can be used in different contexts: In normal page edit view, in which case the redirect won't have any effect. + // Or in history view, in which case a revert causes the CMS to re-load the edit view. + $url = Controller::join_links(singleton('CMSPageEditController')->Link('show'), $record->ID); + $this->response->addHeader('X-ControllerURL', $url); + return $this->getResponseNegotiator()->respond($this->request); + } + /** * Batch Actions Handler */ diff --git a/code/controllers/CMSPageHistoryController.php b/code/controllers/CMSPageHistoryController.php index 25ad3e9f..aaaafe1c 100644 --- a/code/controllers/CMSPageHistoryController.php +++ b/code/controllers/CMSPageHistoryController.php @@ -52,16 +52,6 @@ class CMSPageHistoryController extends CMSMain { return $content; } - /** - * @return array - */ - function rollback() { - return $this->doRollback(array( - 'ID' => $this->currentPageID(), - 'Version' => $this->request->param('VersionID') - ), null); - } - /** * Returns the read only version of the edit form. Detaches all {@link FormAction} * instances attached since only action relates to revert. @@ -308,54 +298,6 @@ class CMSPageHistoryController extends CMSMain { $versionID )); } - - /** - * Rolls a site back to a given version ID - * - * @param array - * @param Form - * - * @return html - */ - function doRollback($data, $form) { - $this->extend('onBeforeRollback', $data['ID']); - - $id = (isset($data['ID'])) ? (int) $data['ID'] : null; - $version = (isset($data['Version'])) ? (int) $data['Version'] : null; - - if(isset($data['Version']) && (bool)$data['Version']) { - $record = $this->performRollback($data['ID'], $data['Version']); - $message = sprintf( - _t('CMSMain.ROLLEDBACKVERSION',"Rolled back to version #%d. New version number is #%d"), - $data['Version'], - $record->Version - ); - } else { - $record = $this->performRollback($data['ID'], "Live"); - $message = sprintf( - _t('CMSMain.ROLLEDBACKPUB',"Rolled back to published version. New version number is #%d"), - $record->Version - ); - } - - return $this->redirect(Controller::join_links(singleton('CMSPageEditController')->Link('show'), $record->ID)); - } - - /** - * Performs a rollback of the a given - * - * @param int $id record ID - * @param int $version version ID to rollback to - */ - function performRollback($id, $version) { - $record = DataObject::get_by_id($this->stat('tree_class'), $id); - - if($record && !$record->canEdit()) return Security::permissionFailure($this); - - $record->doRollbackTo($version); - - return $record; - } /** * @return Form