From 45c4d670b614a7ee1363b2f27847d56f5b72b43c Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 19 Nov 2008 23:26:07 +0000 Subject: [PATCH] API CHANGE: Revamped CMSMenu system to not instantiate any objects, so that _config.php doesn't get fskd git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.3@66264 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/AssetAdmin.php | 4 --- code/CMSMain.php | 4 --- code/CMSMenu.php | 58 +++++++++++++++++++++++++--------------- code/CommentAdmin.php | 4 --- code/LeftAndMain.php | 24 ++++++++++------- code/ReportAdmin.php | 4 --- code/SecurityAdmin.php | 4 --- templates/LeftAndMain.ss | 2 +- tests/CMSMenuTest.php | 2 -- 9 files changed, 52 insertions(+), 54 deletions(-) diff --git a/code/AssetAdmin.php b/code/AssetAdmin.php index 9919f97b..24ef7156 100755 --- a/code/AssetAdmin.php +++ b/code/AssetAdmin.php @@ -44,10 +44,6 @@ class AssetAdmin extends LeftAndMain { 'UploadForm', 'deleteUnusedThumbnails' => 'ADMIN' ); - - public function getMenuTitle() { - return _t('LeftAndMain.FILESIMAGES', 'Files & Images', PR_HIGH, 'Menu title'); - } /** * Return fake-ID "root" if no ID is found (needed to upload files into the root-folder) diff --git a/code/CMSMain.php b/code/CMSMain.php index ff452571..fd6f5b03 100644 --- a/code/CMSMain.php +++ b/code/CMSMain.php @@ -108,10 +108,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr Requirements::javascript(CMS_DIR . '/javascript/CMSMain_right.js'); } - public function getMenuTitle() { - return _t('LeftAndMain.SITECONTENT', 'Site Content', PR_HIGH, 'Menu title'); - } - /** * If this is set to true, the "switchView" context in the * template is shown, with links to the staging and publish site. diff --git a/code/CMSMenu.php b/code/CMSMenu.php index 60c00419..27932942 100644 --- a/code/CMSMenu.php +++ b/code/CMSMenu.php @@ -5,7 +5,7 @@ * @package cms * @subpackage content */ -class CMSMenu extends Object implements Iterator +class CMSMenu extends Object implements Iterator, i18nEntityProvider { protected static $menu_items = array(); @@ -21,7 +21,6 @@ class CMSMenu extends Object implements Iterator } return true; } - /** * Add a LeftAndMain controller to the CMS menu. @@ -32,25 +31,35 @@ class CMSMenu extends Object implements Iterator * when the item is removed. Functionality needed in {@link Director}. */ public static function add_controller($controllerClass) { - $controller = singleton($controllerClass); + // Get static bits + $urlBase = eval("return $controllerClass::\$url_base;"); + $urlSegment = eval("return $controllerClass::\$url_segment;"); + $urlRule = eval("return $controllerClass::\$url_rule;"); + $urlPriority = eval("return $controllerClass::\$url_priority;"); + $menuPriority = eval("return $controllerClass::\$menu_priority;"); - $link = $controller->Link(); - if(substr($link,-1) == '/') $link = substr($link,0,-1); - $subRule = $controller->stat('url_rule', true); - if($subRule[0] == '/') $subRule = substr($subRule,1); - $rule = $link . '//' . $subRule; - - Director::addRules($controller->stat('url_priority', true), array( + // Don't add menu items defined the old way + if($urlSegment === null) return; + + $link = Controller::join_links($urlBase,$urlSegment) . '/'; + + // Make director rule + if($urlRule[0] == '/') $urlRule = substr($urlRule,1); + $rule = $link . '/' . $urlRule; // the / will combine with the / on the end of $link to make a // + Director::addRules($urlPriority, array( $rule => $controllerClass - )); + + // Add menu item + $defaultTitle = LeftAndMain::menu_title_for_class($controllerClass); + $menuTitle = _t('LeftAndMain.' . strtoupper($controllerClass) . '_MENU', $defaultTitle); return self::add_menu_item( $controllerClass, - $controller->getMenuTitle(), - $controller->Link(), + $menuTitle, + $link, $controllerClass, - $controller->stat('menu_priority') + $menuPriority ); } @@ -183,12 +192,7 @@ class CMSMenu extends Object implements Iterator $classReflection = new ReflectionClass($className); if(!$classReflection->isInstantiable() || 'LeftAndMain' == $className) { unset($subClasses[$key]); - } else { - if(singleton($className)->getMenuTitle() == '') { - unset($subClasses[$key]); - } - } - + } } return $subClasses; } @@ -213,6 +217,18 @@ class CMSMenu extends Object implements Iterator public function valid() { return (bool)self::current(); } - + + /** + * Provide menu titles to the i18n entity provider + */ + function provideI18nEntities() { + $cmsClasses = self::get_cms_classes(); + $entities = array(); + foreach($cmsClasses as $cmsClass) { + $defaultTitle = LeftAndMain::menu_title_for_class($cmsClass); + $entities['LeftAndMain.' . strtoupper($cmsClass) . '_MENU'] = array($defaultTitle, PR_HIGH, 'Menu title'); + } + return $entities; + } } ?> diff --git a/code/CommentAdmin.php b/code/CommentAdmin.php index 1242b28a..f226400d 100644 --- a/code/CommentAdmin.php +++ b/code/CommentAdmin.php @@ -29,10 +29,6 @@ class CommentAdmin extends LeftAndMain { Requirements::css(CMS_DIR . 'css/CommentAdmin.css'); } - public function getMenuTitle() { - return _t('LeftAndMain.COMMENTS', 'Comments', PR_HIGH, 'Menu title'); - } - public function showtable($params) { return $this->getLastFormIn($this->renderWith('CommentAdmin_right')); } diff --git a/code/LeftAndMain.php b/code/LeftAndMain.php index 2273d23d..dc13b0d3 100644 --- a/code/LeftAndMain.php +++ b/code/LeftAndMain.php @@ -18,7 +18,7 @@ class LeftAndMain extends Controller { * * @var string $url_base */ - protected static $url_base = "admin"; + static $url_base = "admin"; static $url_segment; @@ -212,13 +212,14 @@ class LeftAndMain extends Controller { } /** - * Override {@link getMenuTitle} in child classes to make the menu title translatable for that class. - * Uses {@link $menu_title} if present, otherwise falls back to the classname without the "Admin" suffix. - * - * @return string + * Returns the menu title for the given LeftAndMain subclass. + * Implemented static so that we can get this value without instantiating an object. + * Menu title is *not* internationalised. */ - public function getMenuTitle() { - return ($this->stat('menu_title')) ? $this->stat('menu_title') : preg_replace('/Admin$/', '', $this->class); + static function menu_title_for_class($class) { + $title = eval("return $class::\$menu_title;"); + if(!$title) $title = preg_replace('/Admin$/', '', $class); + return $title; } public function show($params) { @@ -305,10 +306,9 @@ class LeftAndMain extends Controller { // already set in CMSMenu::populate_menu(), but from a static pre-controller // context, so doesn't respect the current user locale in _t() calls - as a workaround, - // we simply call getMenuTitle() again if we're dealing with a controller + // we simply call LeftAndMain::menu_title_for_class() again if we're dealing with a controller if($menuItem->controller) { - $controllerObj = singleton($menuItem->controller); - $title = $controllerObj->getMenuTitle(); + $title = LeftAndMain::menu_title_for_class($menuItem->controller); } else { $title = $menuItem->title; } @@ -327,6 +327,10 @@ class LeftAndMain extends Controller { } + public function CMSTopMenu() { + return $this->renderWith(array('CMSTopMenu_alternative','CMSTopMenu')); + } + /** * Return a list of appropriate templates for this class, with the given suffix */ diff --git a/code/ReportAdmin.php b/code/ReportAdmin.php index 56f5eb78..bc6c6e74 100755 --- a/code/ReportAdmin.php +++ b/code/ReportAdmin.php @@ -41,10 +41,6 @@ class ReportAdmin extends LeftAndMain { } } - public function getMenuTitle() { - return _t('LeftAndMain.REPORTS', 'Reports', PR_HIGH, 'Menu title'); - } - /** * Return a DataObjectSet of SSReport subclasses * that are available for use. diff --git a/code/SecurityAdmin.php b/code/SecurityAdmin.php index d2a1c25b..8bf48369 100644 --- a/code/SecurityAdmin.php +++ b/code/SecurityAdmin.php @@ -258,10 +258,6 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider { return DataObject::get_by_id("Member", Session::get('currentMember')); } - public function getMenuTitle() { - return _t('LeftAndMain.SECURITY', 'Security', PR_HIGH, 'Menu title'); - } - function providePermissions() { return array( 'EDIT_PERMISSIONS' => _t('SecurityAdmin.EDITPERMISSIONS', 'Edit permissions and IP addresses on each group'), diff --git a/templates/LeftAndMain.ss b/templates/LeftAndMain.ss index efb7f022..8836c504 100644 --- a/templates/LeftAndMain.ss +++ b/templates/LeftAndMain.ss @@ -12,7 +12,7 @@
<% _t('LOADING','Loading...',PR_HIGH) %>
- <% include CMSTopMenu %> + $CMSTopMenu
$Left diff --git a/tests/CMSMenuTest.php b/tests/CMSMenuTest.php index fb85d4f4..ef3387e9 100644 --- a/tests/CMSMenuTest.php +++ b/tests/CMSMenuTest.php @@ -17,7 +17,6 @@ class CMSMenuTest extends SapphireTest implements TestOnly { $menuItems = CMSMenu::get_menu_items(); $menuItem = $menuItems['CMSMain']; $this->assertType('CMSMenuItem', $menuItem, 'Controller menu item is of class CMSMenuItem'); - $this->assertEquals($menuItem->title, singleton('CMSMain')->getMenuTitle(), 'Controller menu item has the correct title'); $this->assertEquals($menuItem->url, singleton('CMSMain')->Link(), 'Controller menu item has the correct link'); $this->assertEquals($menuItem->controller, 'CMSMain', 'Controller menu item has the correct controller class'); $this->assertEquals($menuItem->priority, singleton('CMSMain')->stat('menu_priority'), 'Controller menu item has the correct priority'); @@ -55,7 +54,6 @@ class CMSMenuTest extends SapphireTest implements TestOnly { CMSMenu::populate_menu(); $menuItem = CMSMenu::get_menu_item('CMSMain'); $this->assertType('CMSMenuItem', $menuItem, 'CMSMain menu item exists'); - $this->assertEquals($menuItem->title, singleton('CMSMain')->getMenuTitle(), 'Menu item has the correct title'); $this->assertEquals($menuItem->url, singleton('CMSMain')->Link(), 'Menu item has the correct link'); $this->assertEquals($menuItem->controller, 'CMSMain', 'Menu item has the correct controller class'); $this->assertEquals(