2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
|
|
|
use SilverStripe\Security\Member;
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2016-01-06 00:34:58 +01:00
|
|
|
* The object manages the main CMS menu. See {@link LeftAndMain::init()} for
|
2013-10-02 08:06:54 +02:00
|
|
|
* example usage.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
|
|
|
* The menu will be automatically populated with menu items for subclasses of
|
|
|
|
* {@link LeftAndMain}. That is, for each class in the CMS that creates an
|
|
|
|
* administration panel, a CMS menu item will be created. The default
|
|
|
|
* configuration will also include a 'help' link to the SilverStripe user
|
2013-10-02 08:06:54 +02:00
|
|
|
* documentation.
|
|
|
|
*
|
|
|
|
* Additional CMSMenu items can be added through {@link LeftAndMainExtension::init()}
|
|
|
|
* extensions added to {@link LeftAndMain}.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-10-02 08:06:54 +02:00
|
|
|
* @package framework
|
|
|
|
* @subpackage admin
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2013-10-02 08:06:54 +02:00
|
|
|
class CMSMenu extends Object implements IteratorAggregate, i18nEntityProvider {
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-03-30 03:50:22 +02:00
|
|
|
/**
|
|
|
|
* Sort by menu priority, highest to lowest
|
|
|
|
*/
|
|
|
|
const MENU_PRIORITY = 'menu_priority';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort by url priority, highest to lowest
|
|
|
|
*/
|
|
|
|
const URL_PRIORITY = 'url_priority';
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* An array of changes to be made to the menu items, in the order that the changes should be
|
|
|
|
* applied. Each item is a map in one of the two forms:
|
|
|
|
* - array('type' => 'add', 'item' => new CMSMenuItem(...) )
|
|
|
|
* - array('type' => 'remove', 'code' => 'codename' )
|
|
|
|
*/
|
|
|
|
protected static $menu_item_changes = array();
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Set to true if clear_menu() is called, to indicate that the default menu shouldn't be
|
|
|
|
* included
|
|
|
|
*/
|
|
|
|
protected static $menu_is_cleared = false;
|
|
|
|
|
|
|
|
/**
|
2016-01-06 00:34:58 +01:00
|
|
|
* Generate CMS main menu items by collecting valid
|
2011-03-23 10:51:00 +01:00
|
|
|
* subclasses of {@link LeftAndMain}
|
|
|
|
*/
|
|
|
|
public static function populate_menu() {
|
|
|
|
self::$menu_is_cleared = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a LeftAndMain controller to the CMS menu.
|
|
|
|
*
|
|
|
|
* @param string $controllerClass The class name of the controller
|
|
|
|
* @todo A director rule is added when a controller link is added, but it won't be removed
|
|
|
|
* when the item is removed. Functionality needed in {@link Director}.
|
2016-01-06 00:34:58 +01:00
|
|
|
*/
|
2011-03-23 10:51:00 +01:00
|
|
|
public static function add_controller($controllerClass) {
|
|
|
|
if($menuItem = self::menuitem_for_controller($controllerClass)) {
|
|
|
|
self::add_menu_item_obj($controllerClass, $menuItem);
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Return a CMSMenuItem to add the given controller to the CMSMenu
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param string $controllerClass
|
|
|
|
* @return CMSMenuItem
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
protected static function menuitem_for_controller($controllerClass) {
|
2012-04-18 13:10:57 +02:00
|
|
|
$urlBase = Config::inst()->get($controllerClass, 'url_base', Config::FIRST_SET);
|
|
|
|
$urlSegment = Config::inst()->get($controllerClass, 'url_segment', Config::FIRST_SET);
|
|
|
|
$menuPriority = Config::inst()->get($controllerClass, 'menu_priority', Config::FIRST_SET);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Don't add menu items defined the old way
|
|
|
|
if($urlSegment === null && $controllerClass != "CMSMain") return;
|
|
|
|
|
|
|
|
$link = Controller::join_links($urlBase, $urlSegment) . '/';
|
|
|
|
|
|
|
|
// doesn't work if called outside of a controller context (e.g. in _config.php)
|
|
|
|
// as the locale won't be detected properly. Use {@link LeftAndMain->MainMenu()} to update
|
|
|
|
// titles for existing menu entries
|
2016-05-02 10:57:43 +02:00
|
|
|
$menuTitle = LeftAndMain::menu_title($controllerClass);
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
return new CMSMenuItem($menuTitle, $link, $controllerClass, $menuPriority);
|
|
|
|
}
|
2012-04-18 13:10:57 +02:00
|
|
|
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Add an arbitrary URL to the CMS menu.
|
|
|
|
*
|
2012-03-09 04:53:58 +01:00
|
|
|
* @param string $code A unique identifier (used to create a CSS ID and its key in {@link $menu_items})
|
2011-03-23 10:51:00 +01:00
|
|
|
* @param string $menuTitle The link's title in the CMS menu
|
|
|
|
* @param string $url The url of the link
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param integer $priority The menu priority (sorting order) of the menu item. Higher priorities will be further
|
|
|
|
* left.
|
2013-10-02 08:06:54 +02:00
|
|
|
* @param array $attributes an array of attributes to include on the link.
|
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* @return boolean The result of the operation.
|
|
|
|
*/
|
2013-10-02 08:06:54 +02:00
|
|
|
public static function add_link($code, $menuTitle, $url, $priority = -1, $attributes = null) {
|
|
|
|
return self::add_menu_item($code, $menuTitle, $url, null, $priority, $attributes);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Add a navigation item to the main administration menu showing in the top bar.
|
|
|
|
*
|
|
|
|
* uses {@link CMSMenu::$menu_items}
|
|
|
|
*
|
|
|
|
* @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and
|
2016-06-23 01:37:22 +02:00
|
|
|
* {@link remove_menu_item}. Also used as a CSS-class for icon customization.
|
2016-01-06 00:34:58 +01:00
|
|
|
* @param string $menuTitle Localized title showing in the menu bar
|
2011-03-23 10:51:00 +01:00
|
|
|
* @param string $url A relative URL that will be linked in the menu bar.
|
2016-01-06 00:34:58 +01:00
|
|
|
* @param string $controllerClass The controller class for this menu, used to check permisssions.
|
2016-06-23 01:37:22 +02:00
|
|
|
* If blank, it's assumed that this is public, and always shown to users who
|
|
|
|
* have the rights to access some other part of the admin area.
|
|
|
|
* @param int $priority
|
2013-10-02 08:06:54 +02:00
|
|
|
* @param array $attributes an array of attributes to include on the link.
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return bool Success
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2014-03-30 08:37:54 +02:00
|
|
|
public static function add_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1,
|
|
|
|
$attributes = null) {
|
2011-03-23 10:51:00 +01:00
|
|
|
// If a class is defined, then force the use of that as a code. This helps prevent menu item duplication
|
2013-10-02 08:06:54 +02:00
|
|
|
if($controllerClass) {
|
|
|
|
$code = $controllerClass;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-10-02 08:06:54 +02:00
|
|
|
return self::replace_menu_item($code, $menuTitle, $url, $controllerClass, $priority, $attributes);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Get a single menu item by its code value.
|
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_menu_item($code) {
|
|
|
|
$menuItems = self::get_menu_items();
|
2016-01-06 00:34:58 +01:00
|
|
|
return (isset($menuItems[$code])) ? $menuItems[$code] : false;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Get all menu entries.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_menu_items() {
|
|
|
|
$menuItems = array();
|
|
|
|
|
|
|
|
// Set up default menu items
|
|
|
|
if(!self::$menu_is_cleared) {
|
|
|
|
$cmsClasses = self::get_cms_classes();
|
|
|
|
foreach($cmsClasses as $cmsClass) {
|
|
|
|
$menuItem = self::menuitem_for_controller($cmsClass);
|
2015-07-15 16:33:00 +02:00
|
|
|
if($menuItem) $menuItems[Convert::raw2htmlname(str_replace('\\', '-', $cmsClass))] = $menuItem;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Apply changes
|
|
|
|
foreach(self::$menu_item_changes as $change) {
|
|
|
|
switch($change['type']) {
|
|
|
|
case 'add':
|
|
|
|
$menuItems[$change['code']] = $change['item'];
|
|
|
|
break;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
case 'remove':
|
|
|
|
unset($menuItems[$change['code']]);
|
|
|
|
break;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
default:
|
|
|
|
user_error("Bad menu item change type {$change[type]}", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-01-29 16:30:35 +01:00
|
|
|
// Sort menu items according to priority, then title asc
|
2011-03-23 10:51:00 +01:00
|
|
|
$menuPriority = array();
|
2013-01-29 16:30:35 +01:00
|
|
|
$menuTitle = array();
|
2011-03-23 10:51:00 +01:00
|
|
|
foreach($menuItems as $key => $menuItem) {
|
2013-04-09 12:19:33 +02:00
|
|
|
$menuPriority[$key] = is_numeric($menuItem->priority) ? $menuItem->priority : 0;
|
2013-01-29 16:30:35 +01:00
|
|
|
$menuTitle[$key] = $menuItem->title;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2013-01-29 16:30:35 +01:00
|
|
|
array_multisort($menuPriority, SORT_DESC, $menuTitle, SORT_ASC, $menuItems);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $menuItems;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Get all menu items that the passed member can view.
|
|
|
|
* Defaults to {@link Member::currentUser()}.
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* @param Member $member
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_viewable_menu_items($member = null) {
|
|
|
|
if(!$member && $member !== FALSE) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$viewableMenuItems = array();
|
|
|
|
$allMenuItems = self::get_menu_items();
|
|
|
|
if($allMenuItems) foreach($allMenuItems as $code => $menuItem) {
|
|
|
|
// exclude all items which have a controller to perform permission
|
|
|
|
// checks on
|
|
|
|
if($menuItem->controller) {
|
|
|
|
$controllerObj = singleton($menuItem->controller);
|
2011-06-09 05:47:59 +02:00
|
|
|
if(Controller::has_curr()) {
|
|
|
|
// Necessary for canView() to have request data available,
|
|
|
|
// e.g. to check permissions against LeftAndMain->currentPage()
|
|
|
|
$controllerObj->setRequest(Controller::curr()->getRequest());
|
|
|
|
if(!$controllerObj->canView($member)) continue;
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$viewableMenuItems[$code] = $menuItem;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $viewableMenuItems;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Removes an existing item from the menu.
|
|
|
|
*
|
|
|
|
* @param string $code Unique identifier for this menu item
|
|
|
|
*/
|
|
|
|
public static function remove_menu_item($code) {
|
|
|
|
self::$menu_item_changes[] = array('type' => 'remove', 'code' => $code);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Clears the entire menu
|
|
|
|
*/
|
|
|
|
public static function clear_menu() {
|
|
|
|
self::$menu_item_changes = array();
|
|
|
|
self::$menu_is_cleared = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace a navigation item to the main administration menu showing in the top bar.
|
|
|
|
*
|
|
|
|
* @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and
|
2016-06-23 01:37:22 +02:00
|
|
|
* {@link remove_menu_item}. Also used as a CSS-class for icon customization.
|
2016-01-06 00:34:58 +01:00
|
|
|
* @param string $menuTitle Localized title showing in the menu bar
|
2011-03-23 10:51:00 +01:00
|
|
|
* @param string $url A relative URL that will be linked in the menu bar.
|
2016-06-23 01:37:22 +02:00
|
|
|
* Make sure to add a matching route via {@link Director::$rules} to this url.
|
2016-01-06 00:34:58 +01:00
|
|
|
* @param string $controllerClass The controller class for this menu, used to check permisssions.
|
2016-06-23 01:37:22 +02:00
|
|
|
* If blank, it's assumed that this is public, and always shown to users who
|
|
|
|
* have the rights to access some other part of the admin area.
|
|
|
|
* @param int $priority
|
2013-10-02 08:06:54 +02:00
|
|
|
* @param array $attributes an array of attributes to include on the link.
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return bool Success
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2014-03-30 08:37:54 +02:00
|
|
|
public static function replace_menu_item($code, $menuTitle, $url, $controllerClass = null, $priority = -1,
|
|
|
|
$attributes = null) {
|
2013-10-02 08:06:54 +02:00
|
|
|
$item = new CMSMenuItem($menuTitle, $url, $controllerClass, $priority);
|
|
|
|
|
|
|
|
if($attributes) {
|
|
|
|
$item->setAttributes($attributes);
|
|
|
|
}
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
self::$menu_item_changes[] = array(
|
|
|
|
'type' => 'add',
|
|
|
|
'code' => $code,
|
2013-10-02 08:06:54 +02:00
|
|
|
'item' => $item,
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
2013-10-02 08:06:54 +02:00
|
|
|
* Add a previously built menu item object to the menu
|
2016-06-23 01:37:22 +02:00
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @param CMSMenuItem $cmsMenuItem
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
|
|
|
protected static function add_menu_item_obj($code, $cmsMenuItem) {
|
|
|
|
self::$menu_item_changes[] = array(
|
|
|
|
'type' => 'add',
|
|
|
|
'code' => $code,
|
|
|
|
'item' => $cmsMenuItem,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility funciton to retrieve subclasses of a given class that
|
|
|
|
* are instantiable (ie, not abstract) and have a valid menu title.
|
|
|
|
*
|
2016-03-30 03:50:22 +02:00
|
|
|
* Sorted by url_priority config.
|
|
|
|
*
|
2011-03-23 10:51:00 +01:00
|
|
|
* @todo A variation of this function could probably be moved to {@link ClassInfo}
|
|
|
|
* @param string $root The root class to begin finding subclasses
|
|
|
|
* @param boolean $recursive Look for subclasses recursively?
|
2016-03-30 03:50:22 +02:00
|
|
|
* @param string $sort Name of config on which to sort. Can be 'menu_priority' or 'url_priority'
|
2011-03-23 10:51:00 +01:00
|
|
|
* @return array Valid, unique subclasses
|
|
|
|
*/
|
2016-03-30 03:50:22 +02:00
|
|
|
public static function get_cms_classes($root = null, $recursive = true, $sort = self::MENU_PRIORITY) {
|
|
|
|
if(!$root) {
|
|
|
|
$root = 'LeftAndMain';
|
|
|
|
}
|
2016-07-14 07:51:01 +02:00
|
|
|
$abstractClasses = ['LeftAndMain', 'CMSMain'];
|
2011-03-23 10:51:00 +01:00
|
|
|
$subClasses = array_values(ClassInfo::subclassesFor($root));
|
|
|
|
foreach($subClasses as $className) {
|
|
|
|
if($recursive && $className != $root) {
|
|
|
|
$subClasses = array_merge($subClasses, array_values(ClassInfo::subclassesFor($className)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$subClasses = array_unique($subClasses);
|
|
|
|
foreach($subClasses as $key => $className) {
|
|
|
|
// Remove abstract classes and LeftAndMain
|
2016-07-14 07:51:01 +02:00
|
|
|
if(in_array($className, $abstractClasses) || ClassInfo::classImplements($className, 'TestOnly')) {
|
2011-03-23 10:51:00 +01:00
|
|
|
unset($subClasses[$key]);
|
2011-12-23 10:30:49 +01:00
|
|
|
} else {
|
|
|
|
// Separate conditional to avoid autoloading the class
|
|
|
|
$classReflection = new ReflectionClass($className);
|
2016-03-30 03:50:22 +02:00
|
|
|
if(!$classReflection->isInstantiable()) {
|
|
|
|
unset($subClasses[$key]);
|
|
|
|
}
|
2011-12-23 10:30:49 +01:00
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-03-30 03:50:22 +02:00
|
|
|
// Sort by specified sorting config
|
|
|
|
usort($subClasses, function ($a, $b) use ($sort) {
|
|
|
|
$priorityA = Config::inst()->get($a, $sort);
|
|
|
|
$priorityB = Config::inst()->get($b, $sort);
|
|
|
|
return $priorityB - $priorityA;
|
|
|
|
});
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
return $subClasses;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* IteratorAggregate Interface Method. Iterates over the menu items.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getIterator() {
|
2011-03-23 10:51:00 +01:00
|
|
|
return new ArrayIterator(self::get_menu_items());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide menu titles to the i18n entity provider
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function provideI18nEntities() {
|
2011-03-23 10:51:00 +01:00
|
|
|
$cmsClasses = self::get_cms_classes();
|
|
|
|
$entities = array();
|
|
|
|
foreach($cmsClasses as $cmsClass) {
|
2016-05-02 10:57:43 +02:00
|
|
|
$defaultTitle = LeftAndMain::menu_title($cmsClass, false);
|
2011-03-23 10:51:00 +01:00
|
|
|
$ownerModule = i18n::get_owner_module($cmsClass);
|
2012-04-14 00:16:22 +02:00
|
|
|
$entities["{$cmsClass}.MENUTITLE"] = array($defaultTitle, 'Menu title', $ownerModule);
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
}
|