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
This commit is contained in:
Sam Minnee 2008-11-19 23:26:07 +00:00
parent e4b2ebdc1e
commit 45c4d670b6
9 changed files with 52 additions and 54 deletions

View File

@ -44,10 +44,6 @@ class AssetAdmin extends LeftAndMain {
'UploadForm', 'UploadForm',
'deleteUnusedThumbnails' => 'ADMIN' '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) * Return fake-ID "root" if no ID is found (needed to upload files into the root-folder)

View File

@ -108,10 +108,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
Requirements::javascript(CMS_DIR . '/javascript/CMSMain_right.js'); 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 * If this is set to true, the "switchView" context in the
* template is shown, with links to the staging and publish site. * template is shown, with links to the staging and publish site.

View File

@ -5,7 +5,7 @@
* @package cms * @package cms
* @subpackage content * @subpackage content
*/ */
class CMSMenu extends Object implements Iterator class CMSMenu extends Object implements Iterator, i18nEntityProvider
{ {
protected static $menu_items = array(); protected static $menu_items = array();
@ -21,7 +21,6 @@ class CMSMenu extends Object implements Iterator
} }
return true; return true;
} }
/** /**
* Add a LeftAndMain controller to the CMS menu. * 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}. * when the item is removed. Functionality needed in {@link Director}.
*/ */
public static function add_controller($controllerClass) { 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(); // Don't add menu items defined the old way
if(substr($link,-1) == '/') $link = substr($link,0,-1); if($urlSegment === null) return;
$subRule = $controller->stat('url_rule', true);
if($subRule[0] == '/') $subRule = substr($subRule,1); $link = Controller::join_links($urlBase,$urlSegment) . '/';
$rule = $link . '//' . $subRule;
// Make director rule
Director::addRules($controller->stat('url_priority', true), array( 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 $rule => $controllerClass
)); ));
// Add menu item
$defaultTitle = LeftAndMain::menu_title_for_class($controllerClass);
$menuTitle = _t('LeftAndMain.' . strtoupper($controllerClass) . '_MENU', $defaultTitle);
return self::add_menu_item( return self::add_menu_item(
$controllerClass, $controllerClass,
$controller->getMenuTitle(), $menuTitle,
$controller->Link(), $link,
$controllerClass, $controllerClass,
$controller->stat('menu_priority') $menuPriority
); );
} }
@ -183,12 +192,7 @@ class CMSMenu extends Object implements Iterator
$classReflection = new ReflectionClass($className); $classReflection = new ReflectionClass($className);
if(!$classReflection->isInstantiable() || 'LeftAndMain' == $className) { if(!$classReflection->isInstantiable() || 'LeftAndMain' == $className) {
unset($subClasses[$key]); unset($subClasses[$key]);
} else { }
if(singleton($className)->getMenuTitle() == '') {
unset($subClasses[$key]);
}
}
} }
return $subClasses; return $subClasses;
} }
@ -213,6 +217,18 @@ class CMSMenu extends Object implements Iterator
public function valid() { public function valid() {
return (bool)self::current(); 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;
}
} }
?> ?>

View File

@ -29,10 +29,6 @@ class CommentAdmin extends LeftAndMain {
Requirements::css(CMS_DIR . 'css/CommentAdmin.css'); Requirements::css(CMS_DIR . 'css/CommentAdmin.css');
} }
public function getMenuTitle() {
return _t('LeftAndMain.COMMENTS', 'Comments', PR_HIGH, 'Menu title');
}
public function showtable($params) { public function showtable($params) {
return $this->getLastFormIn($this->renderWith('CommentAdmin_right')); return $this->getLastFormIn($this->renderWith('CommentAdmin_right'));
} }

View File

@ -18,7 +18,7 @@ class LeftAndMain extends Controller {
* *
* @var string $url_base * @var string $url_base
*/ */
protected static $url_base = "admin"; static $url_base = "admin";
static $url_segment; 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. * Returns the menu title for the given LeftAndMain subclass.
* Uses {@link $menu_title} if present, otherwise falls back to the classname without the "Admin" suffix. * Implemented static so that we can get this value without instantiating an object.
* * Menu title is *not* internationalised.
* @return string
*/ */
public function getMenuTitle() { static function menu_title_for_class($class) {
return ($this->stat('menu_title')) ? $this->stat('menu_title') : preg_replace('/Admin$/', '', $this->class); $title = eval("return $class::\$menu_title;");
if(!$title) $title = preg_replace('/Admin$/', '', $class);
return $title;
} }
public function show($params) { public function show($params) {
@ -305,10 +306,9 @@ class LeftAndMain extends Controller {
// already set in CMSMenu::populate_menu(), but from a static pre-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, // 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) { if($menuItem->controller) {
$controllerObj = singleton($menuItem->controller); $title = LeftAndMain::menu_title_for_class($menuItem->controller);
$title = $controllerObj->getMenuTitle();
} else { } else {
$title = $menuItem->title; $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 * Return a list of appropriate templates for this class, with the given suffix
*/ */

View File

@ -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 * Return a DataObjectSet of SSReport subclasses
* that are available for use. * that are available for use.

View File

@ -258,10 +258,6 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
return DataObject::get_by_id("Member", Session::get('currentMember')); return DataObject::get_by_id("Member", Session::get('currentMember'));
} }
public function getMenuTitle() {
return _t('LeftAndMain.SECURITY', 'Security', PR_HIGH, 'Menu title');
}
function providePermissions() { function providePermissions() {
return array( return array(
'EDIT_PERMISSIONS' => _t('SecurityAdmin.EDITPERMISSIONS', 'Edit permissions and IP addresses on each group'), 'EDIT_PERMISSIONS' => _t('SecurityAdmin.EDITPERMISSIONS', 'Edit permissions and IP addresses on each group'),

View File

@ -12,7 +12,7 @@
<div id="Loading" style="background: #FFF url($LoadingImage) 50% 50% no-repeat; position: absolute;z-index: 100000;height: 100%;width: 100%;margin: 0;padding: 0;z-index: 100000;position: absolute;"><% _t('LOADING','Loading...',PR_HIGH) %><noscript><h1><% _t('REQUIREJS','The CMS requires that you have JavaScript enabled.',PR_HIGH) %></h1></noscript></div> <div id="Loading" style="background: #FFF url($LoadingImage) 50% 50% no-repeat; position: absolute;z-index: 100000;height: 100%;width: 100%;margin: 0;padding: 0;z-index: 100000;position: absolute;"><% _t('LOADING','Loading...',PR_HIGH) %><noscript><h1><% _t('REQUIREJS','The CMS requires that you have JavaScript enabled.',PR_HIGH) %></h1></noscript></div>
<div id="top"> <div id="top">
<% include CMSTopMenu %> $CMSTopMenu
</div><div id="left" style="float:left"> </div><div id="left" style="float:left">
$Left $Left

View File

@ -17,7 +17,6 @@ class CMSMenuTest extends SapphireTest implements TestOnly {
$menuItems = CMSMenu::get_menu_items(); $menuItems = CMSMenu::get_menu_items();
$menuItem = $menuItems['CMSMain']; $menuItem = $menuItems['CMSMain'];
$this->assertType('CMSMenuItem', $menuItem, 'Controller menu item is of class CMSMenuItem'); $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->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->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'); $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(); CMSMenu::populate_menu();
$menuItem = CMSMenu::get_menu_item('CMSMain'); $menuItem = CMSMenu::get_menu_item('CMSMain');
$this->assertType('CMSMenuItem', $menuItem, 'CMSMain menu item exists'); $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->url, singleton('CMSMain')->Link(), 'Menu item has the correct link');
$this->assertEquals($menuItem->controller, 'CMSMain', 'Menu item has the correct controller class'); $this->assertEquals($menuItem->controller, 'CMSMain', 'Menu item has the correct controller class');
$this->assertEquals( $this->assertEquals(