From 5f2d8cae0d3da9f4bef13d7240aed9e07cae1099 Mon Sep 17 00:00:00 2001 From: micmania1 Date: Thu, 9 Jun 2016 15:13:09 +1200 Subject: [PATCH] Merge pull request #1522 from micmania1/fix-sitetree-rading-stage --- code/controllers/CMSMain.php | 212 ++++++++++++++++++----------------- 1 file changed, 108 insertions(+), 104 deletions(-) diff --git a/code/controllers/CMSMain.php b/code/controllers/CMSMain.php index c3b1444d..0d2b529d 100644 --- a/code/controllers/CMSMain.php +++ b/code/controllers/CMSMain.php @@ -4,29 +4,29 @@ * * This class creates a 2-frame layout - left-tree and right-form - to sit beneath the main * admin menu. - * + * * @package cms * @subpackage controller * @todo Create some base classes to contain the generic functionality that will be replicated. */ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionProvider { - + private static $url_segment = 'pages'; - + private static $url_rule = '/$Action/$ID/$OtherID'; - + // Maintain a lower priority than other administration sections // so that Director does not think they are actions of CMSMain private static $url_priority = 39; - + private static $menu_title = 'Edit Page'; - + private static $menu_priority = 10; - + private static $tree_class = "SiteTree"; - + private static $subitem_class = "Member"; - + /** * Amount of results showing on a single page. * @@ -34,7 +34,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr * @var int */ private static $page_length = 15; - + private static $allowed_actions = array( 'archive', 'buildbrokenlinks', @@ -71,14 +71,14 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if(SiteTree::has_extension('Translatable') && !$this->getRequest()->isAjax()) { Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages('SiteTree'))); } - + parent::init(); Versioned::reading_stage("Stage"); - + Requirements::css(CMS_DIR . '/css/screen.css'); Requirements::customCSS($this->generatePageIconsCss()); - + Requirements::combine_files( 'cmsmain.js', array_merge( @@ -144,7 +144,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr public function ShowSwitchView() { return true; } - + /** * Overloads the LeftAndMain::ShowView. Allows to pass a page as a parameter, so we are able * to switch view also for archived versions. @@ -153,7 +153,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if(!$page) { $page = $this->currentPage(); } - + if($page) { $nav = SilverStripeNavigator::get_for_record($page); return $nav['items']; @@ -305,10 +305,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $this->extend('updateExtraTreeTools', $html); return $html; } - + /** * Returns a Form for page searching for use in templates. - * + * * Can be modified from a decorator by a 'updateSearchForm' method * * @return Form @@ -318,7 +318,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $content = new TextField('q[Term]', _t('CMSSearch.FILTERLABELTEXT', 'Content')); $dateHeader = new HeaderField('q[Date]', _t('CMSSearch.FILTERDATEHEADING', 'Date'), 4); $dateFrom = new DateField( - 'q[LastEditedFrom]', + 'q[LastEditedFrom]', _t('CMSSearch.FILTERDATEFROM', 'From') ); $dateFrom->setConfig('showcalendar', true); @@ -328,17 +328,17 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr ); $dateTo->setConfig('showcalendar', true); $pageFilter = new DropdownField( - 'q[FilterClass]', - _t('CMSMain.PAGES', 'Pages'), + 'q[FilterClass]', + _t('CMSMain.PAGES', 'Pages'), CMSSiteTreeFilter::get_all_filters() ); $pageClasses = new DropdownField( - 'q[ClassName]', - _t('CMSMain.PAGETYPEOPT', 'Page Type', 'Dropdown for limiting search to a page type'), + 'q[ClassName]', + _t('CMSMain.PAGETYPEOPT', 'Page Type', 'Dropdown for limiting search to a page type'), $this->getPageTypes() ); $pageClasses->setEmptyString(_t('CMSMain.PAGETYPEANYOPT','Any')); - + // Group the Datefields $dateGroup = new FieldGroup( $dateHeader, @@ -346,7 +346,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $dateTo ); $dateGroup->setFieldHolderTemplate('FieldGroup_DefaultFieldHolder')->addExtraClass('stacked'); - + // Create the Field list $fields = new FieldList( $content, @@ -354,7 +354,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $pageFilter, $pageClasses ); - + // Create the Search and Reset action $actions = new FieldList( FormAction::create('doSearch', _t('CMSMain_left_ss.APPLY_FILTER', 'Apply Filter')) @@ -366,7 +366,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr foreach($actions->dataFields() as $action) { $action->setUseButtonTag(true); } - + // Create the form $form = Form::create($this, 'SearchForm', $fields, $actions) ->addExtraClass('cms-search-form') @@ -374,19 +374,19 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr ->setFormAction($this->Link()) ->disableSecurityToken() ->unsetValidator(); - + // Load the form with previously sent search data $form->loadDataFrom($this->getRequest()->getVars()); // Allow decorators to modify the form $this->extend('updateSearchForm', $form); - + return $form; } - + /** * Returns a sorted array suitable for a dropdown with pagetypes and their translated name - * + * * @return array */ protected function getPageTypes() { @@ -397,7 +397,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr asort($pageTypes); return $pageTypes; } - + public function doSearch($data, $form) { return $this->getsubtree($this->getRequest()); } @@ -506,18 +506,18 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue; $addAction = $instance->i18n_singular_name(); - + // Get description (convert 'Page' to 'SiteTree' for correct localization lookups) $description = _t((($class == 'Page') ? 'SiteTree' : $class) . '.DESCRIPTION'); if(!$description) { $description = $instance->uninherited('description'); } - + if($class == 'Page' && !$description) { $description = singleton('SiteTree')->uninherited('description'); } - + $result->push(new ArrayData(array( 'ClassName' => $class, 'AddAction' => $addAction, @@ -527,9 +527,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr 'Title' => singleton($class)->i18n_singular_name(), ))); } - + $result = $result->sort('AddAction'); - + return $result; } @@ -545,12 +545,14 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if($id instanceof $treeClass) { return $id; - } + } else if($id && is_numeric($id)) { + $currentStage = Versioned::get_reading_mode(); + if($this->getRequest()->getVar('Version')) { $versionID = (int) $this->getRequest()->getVar('Version'); } - + if($versionID) { $record = Versioned::get_version($treeClass, $id, $versionID); } else { @@ -564,9 +566,8 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr singleton($treeClass)->flushCache(); $record = DataObject::get_by_id($treeClass, $id); - if($record) Versioned::set_reading_mode(''); } - + // Then, try getting a deleted record if(!$record) { $record = Versioned::get_latest_version($treeClass, $id); @@ -577,19 +578,22 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr * we should not check their locale matches the Translatable::get_current_locale, * here as long as we all the HTTPRequest is init with right locale. * This bit breaks the all FileIFrameField functions if the field is used in CMS - * and its relevent ajax calles, like loading the tree dropdown for TreeSelectorField. + * and its relevent ajax calles, like loading the tree dropdown for TreeSelectorField. */ /* if($record && SiteTree::has_extension('Translatable') && $record->Locale && $record->Locale != Translatable::get_current_locale()) { $record = null; }*/ + // Set the reading mode back to what it was. + Versioned::set_reading_mode($currentStage); + return $record; } else if(substr($id,0,3) == 'new') { return $this->getNewItem($id); } } - + /** * @param int $id * @param FieldList $fields @@ -599,7 +603,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if(!$id) $id = $this->currentPageID(); $form = parent::getEditForm($id); - + // TODO Duplicate record fetching (see parent implementation) $record = $this->getRecord($id); if($record && !$record->canView()) return Security::permissionFailure($this); @@ -626,14 +630,14 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr if($stageLink) $stageLinkField->setValue($stageLink); } } - + // Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load if(in_array('CMSPreviewable', class_implements($record)) && !$fields->fieldByName('SilverStripeNavigator')) { $navField = new LiteralField('SilverStripeNavigator', $this->getSilverStripeNavigator()); $navField->setAllowHTML(true); $fields->push($navField); } - + // getAllCMSActions can be used to completely redefine the action list if($record->hasMethod('getAllCMSActions')) { $actions = $record->getAllCMSActions(); @@ -656,14 +660,14 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // Use