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

@ -45,10 +45,6 @@ class AssetAdmin extends LeftAndMain {
'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)
*/

View File

@ -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.

View File

@ -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();
@ -22,7 +22,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;
// Don't add menu items defined the old way
if($urlSegment === null) return;
Director::addRules($controller->stat('url_priority', true), array(
$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,13 +192,8 @@ 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;
}
@ -214,5 +218,17 @@ class CMSMenu extends Object implements Iterator
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');
}
public function getMenuTitle() {
return _t('LeftAndMain.COMMENTS', 'Comments', PR_HIGH, 'Menu title');
}
public function showtable($params) {
return $this->getLastFormIn($this->renderWith('CommentAdmin_right'));
}

View File

@ -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
*/

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
* 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'));
}
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'),

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="top">
<% include CMSTopMenu %>
$CMSTopMenu
</div><div id="left" style="float:left">
$Left

View File

@ -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(