From 649de6e1bce05a15c9fcf40db359eedb1a9deee6 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 25 Jan 2013 11:35:48 +0100 Subject: [PATCH] BUG Reinstated CMS "History" Preview (fixes ##8089) --- code/controllers/CMSPageHistoryController.php | 10 +++ code/controllers/SilverStripeNavigator.php | 74 +++++++++++-------- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/code/controllers/CMSPageHistoryController.php b/code/controllers/CMSPageHistoryController.php index f8055212..c5827f32 100644 --- a/code/controllers/CMSPageHistoryController.php +++ b/code/controllers/CMSPageHistoryController.php @@ -74,6 +74,16 @@ class CMSPageHistoryController extends CMSMain { return $negotiator->respond($request); } + + public function getSilverStripeNavigator() { + $record = $this->getRecord($this->currentPageID(), $this->request->param('VersionID')); + if($record) { + $navigator = new SilverStripeNavigator($record); + return $navigator->renderWith($this->getTemplatesWithSuffix('_SilverStripeNavigator')); + } else { + return false; + } + } /** * Returns the read only version of the edit form. Detaches all {@link FormAction} diff --git a/code/controllers/SilverStripeNavigator.php b/code/controllers/SilverStripeNavigator.php index 728bf86f..30199d89 100644 --- a/code/controllers/SilverStripeNavigator.php +++ b/code/controllers/SilverStripeNavigator.php @@ -182,6 +182,36 @@ class SilverStripeNavigatorItem extends ViewableData { public function canView($member = null) { return true; } + + /** + * Counts as "archived" if the current record is a different version from both live and draft. + * + * @return boolean + */ + public function isArchived() { + if(!$this->record->hasExtension('Versioned')) return false; + + if(!isset($this->record->_cached_isArchived)) { + $baseTable = ClassInfo::baseDataClass($this->record->class); + $currentDraft = Versioned::get_one_by_stage( + $baseTable, + 'Stage', + sprintf('"%s"."ID" = %d', $baseTable, $this->record->ID) + ); + $currentLive = Versioned::get_one_by_stage( + $baseTable, + 'Live', + sprintf('"%s"."ID" = %d', $baseTable, $this->record->ID) + ); + + $this->record->_cached_isArchived = ( + (!$currentDraft || ($currentDraft && $this->record->Version != $currentDraft->Version)) + && (!$currentLive || ($currentLive && $this->record->Version != $currentLive->Version)) + ); + } + + return $this->record->_cached_isArchived; + } } /** @@ -246,7 +276,12 @@ class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem { } public function getLink() { - return Controller::join_links($this->record->PreviewLink(), '?stage=Stage'); + $date = Versioned::current_archived_date(); + return Controller::join_links( + $this->record->PreviewLink(), + '?stage=Stage', + $date ? '?archiveDate=' . $date : null + ); } public function canView($member = null) { @@ -262,6 +297,7 @@ class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem { return ( Versioned::current_stage() == 'Stage' && !(ClassInfo::exists('SiteTreeFutureState') && SiteTreeFutureState::get_future_datetime()) + && !$this->isArchived() ); } @@ -312,7 +348,10 @@ class SilverStripeNavigatorItem_LiveLink extends SilverStripeNavigatorItem { } public function isActive() { - return (!Versioned::current_stage() || Versioned::current_stage() == 'Live'); + return ( + (!Versioned::current_stage() || Versioned::current_stage() == 'Live') + && !$this->isArchived() + ); } protected function getLivePage() { @@ -338,7 +377,7 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem { } public function getTitle() { - return _t('ContentController.VERSION', 'Version').': '.$this->record->LastEdited; + return _t('SilverStripeNavigator.ARCHIVED', 'Archived'); } public function getMessage() { @@ -350,7 +389,7 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem { } public function getLink() { - return $this->record->PreviewLink() . '?archiveDate=' . $this->record->LastEdited; + return $this->record->PreviewLink() . '?archiveDate=' . urlencode($this->record->LastEdited); } public function canView($member = null) { @@ -363,32 +402,7 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem { } public function isActive() { - return (Versioned::current_archived_date()); - } - - /** - * Counts as "archived" if the current record is a different version from both live and draft. - * - * @return boolean - */ - public function isArchived() { - if(!$this->record->hasExtension('Versioned')) return false; - - $baseTable = ClassInfo::baseDataClass($this->record->class); - $currentDraft = Versioned::get_one_by_stage( - $baseTable, - 'Stage', - sprintf('"%s"."ID" = %d', $baseTable, $this->record->ID) - ); - $currentLive = Versioned::get_one_by_stage( - $baseTable, - 'Live', - sprintf('"%s"."ID" = %d', $baseTable, $this->record->ID) - ); - return ( - (!$currentDraft || ($currentDraft && $this->record->Version != $currentDraft->Version)) - && (!$currentLive || ($currentLive && $this->record->Version != $currentLive->Version)) - ); + return $this->isArchived(); } }