lekoala /
silverstripe-admini
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\Admini; |
||
| 4 | |||
| 5 | use SilverStripe\Core\ClassInfo; |
||
| 6 | use SilverStripe\Core\Config\Config; |
||
| 7 | use SilverStripe\Core\Convert; |
||
| 8 | use SilverStripe\Core\Manifest\ClassLoader; |
||
| 9 | use SilverStripe\Control\Controller; |
||
| 10 | use SilverStripe\Dev\TestOnly; |
||
| 11 | use SilverStripe\i18n\i18nEntityProvider; |
||
| 12 | use SilverStripe\Security\Member; |
||
| 13 | use IteratorAggregate; |
||
| 14 | use ReflectionClass; |
||
| 15 | use ArrayIterator; |
||
| 16 | use SilverStripe\Security\Security; |
||
| 17 | use Traversable; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The object manages the main CMS menu. See {@link LeftAndMain::init()} for |
||
| 21 | * example usage. |
||
| 22 | * |
||
| 23 | * The menu will be automatically populated with menu items for subclasses of |
||
| 24 | * {@link LeftAndMain}. That is, for each class in the CMS that creates an |
||
| 25 | * administration panel, a CMS menu item will be created. The default |
||
| 26 | * configuration will also include a 'help' link to the SilverStripe user |
||
| 27 | * documentation. |
||
| 28 | * |
||
| 29 | * Additional CMSMenu items can be added through {@link LeftAndMainExtension::init()} |
||
| 30 | * extensions added to {@link LeftAndMain}. |
||
| 31 | */ |
||
| 32 | class CMSMenu implements IteratorAggregate, i18nEntityProvider |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Sort by menu priority, highest to lowest |
||
| 37 | */ |
||
| 38 | const MENU_PRIORITY = 'menu_priority'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Sort by url priority, highest to lowest |
||
| 42 | */ |
||
| 43 | const URL_PRIORITY = 'url_priority'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * An array of changes to be made to the menu items, in the order that the changes should be |
||
| 47 | * applied. Each item is a map in one of the two forms: |
||
| 48 | * - array('type' => 'add', 'item' => CMSMenuItem::create(...) ) |
||
| 49 | * - array('type' => 'remove', 'code' => 'codename' ) |
||
| 50 | */ |
||
| 51 | protected static $menu_item_changes = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set to true if clear_menu() is called, to indicate that the default menu shouldn't be |
||
| 55 | * included |
||
| 56 | */ |
||
| 57 | protected static $menu_is_cleared = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Generate CMS main menu items by collecting valid |
||
| 61 | * subclasses of {@link LeftAndMain} |
||
| 62 | */ |
||
| 63 | public static function populate_menu() |
||
| 64 | { |
||
| 65 | self::$menu_is_cleared = false; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Add a LeftAndMain controller to the CMS menu. |
||
| 70 | * |
||
| 71 | * @param string $controllerClass The class name of the controller |
||
| 72 | * @todo A director rule is added when a controller link is added, but it won't be removed |
||
| 73 | * when the item is removed. Functionality needed in {@link Director}. |
||
| 74 | */ |
||
| 75 | public static function add_controller($controllerClass) |
||
| 76 | { |
||
| 77 | if ($menuItem = self::menuitem_for_controller($controllerClass)) { |
||
| 78 | $code = static::get_menu_code($controllerClass); |
||
| 79 | self::add_menu_item_obj($code, $menuItem); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Return a CMSMenuItem to add the given controller to the CMSMenu |
||
| 85 | * |
||
| 86 | * @param string $controllerClass |
||
| 87 | * @return CMSMenuItem |
||
| 88 | */ |
||
| 89 | protected static function menuitem_for_controller($controllerClass) |
||
| 90 | { |
||
| 91 | $urlBase = AdminiRootController::admin_url(); |
||
| 92 | $urlSegment = Config::inst()->get($controllerClass, 'url_segment'); |
||
| 93 | $menuPriority = Config::inst()->get($controllerClass, 'menu_priority'); |
||
| 94 | $ignoreFromMenu = Config::inst()->get($controllerClass, 'ignore_menuitem'); |
||
| 95 | |||
| 96 | // Don't add menu items defined the old way, or for controllers that are set to be ignored |
||
| 97 | if (!$urlSegment || $ignoreFromMenu) { |
||
| 98 | return null; |
||
| 99 | } |
||
| 100 | |||
| 101 | $link = Controller::join_links($urlBase, $urlSegment) . '/'; |
||
| 102 | |||
| 103 | // doesn't work if called outside of a controller context (e.g. in _config.php) |
||
| 104 | // as the locale won't be detected properly. Use {@link LeftAndMain->MainMenu()} to update |
||
| 105 | // titles for existing menu entries |
||
| 106 | $menuTitle = LeftAndMain::menu_title($controllerClass); |
||
| 107 | |||
| 108 | return CMSMenuItem::create($menuTitle, $link, $controllerClass, $menuPriority); |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Add an arbitrary URL to the CMS menu. |
||
| 114 | * |
||
| 115 | * @param string $code A unique identifier (used to create a CSS ID and its key in {@link $menu_items}) |
||
| 116 | * @param string $menuTitle The link's title in the CMS menu |
||
| 117 | * @param string $url The url of the link |
||
| 118 | * @param integer $priority The menu priority (sorting order) of the menu item. Higher priorities will be further |
||
| 119 | * left. |
||
| 120 | * @param array $attributes an array of attributes to include on the link. |
||
| 121 | * @param string $iconClass |
||
| 122 | * |
||
| 123 | * @return boolean The result of the operation. |
||
| 124 | */ |
||
| 125 | public static function add_link($code, $menuTitle, $url, $priority = -1, $attributes = null, $iconClass = null) |
||
| 126 | { |
||
| 127 | return self::add_menu_item($code, $menuTitle, $url, null, $priority, $attributes, $iconClass); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Add a navigation item to the main administration menu showing in the top bar. |
||
| 132 | * |
||
| 133 | * uses {@link CMSMenu::$menu_items} |
||
| 134 | * |
||
| 135 | * @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and |
||
| 136 | * {@link remove_menu_item}. Also used as a CSS-class for icon customization. |
||
| 137 | * @param string $menuTitle Localized title showing in the menu bar |
||
| 138 | * @param string $url A relative URL that will be linked in the menu bar. |
||
| 139 | * @param string $controllerClass The controller class for this menu, used to check permisssions. |
||
| 140 | * If blank, it's assumed that this is public, and always shown to users who |
||
| 141 | * have the rights to access some other part of the admin area. |
||
| 142 | * @param int $priority |
||
| 143 | * @param array $attributes an array of attributes to include on the link. |
||
| 144 | * @param string $iconClass |
||
| 145 | * |
||
| 146 | * @return bool Success |
||
| 147 | */ |
||
| 148 | public static function add_menu_item( |
||
| 149 | $code, |
||
| 150 | $menuTitle, |
||
| 151 | $url, |
||
| 152 | $controllerClass = null, |
||
| 153 | $priority = -1, |
||
| 154 | $attributes = null, |
||
| 155 | $iconClass = null |
||
| 156 | ) { |
||
| 157 | // If a class is defined, then force the use of that as a code. This helps prevent menu item duplication |
||
| 158 | if ($controllerClass) { |
||
| 159 | $code = self::get_menu_code($controllerClass); |
||
| 160 | } |
||
| 161 | |||
| 162 | return self::replace_menu_item( |
||
| 163 | $code, |
||
| 164 | $menuTitle, |
||
| 165 | $url, |
||
| 166 | $controllerClass, |
||
| 167 | $priority, |
||
| 168 | $attributes, |
||
| 169 | $iconClass |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get a single menu item by its code value. |
||
| 175 | * |
||
| 176 | * @param string $code |
||
| 177 | * @return array|null |
||
| 178 | */ |
||
| 179 | public static function get_menu_item($code) |
||
| 180 | { |
||
| 181 | $menuItems = self::get_menu_items(); |
||
| 182 | return (isset($menuItems[$code])) ? $menuItems[$code] : null; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get menu code for class |
||
| 187 | * |
||
| 188 | * @param string $cmsClass Controller class name |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public static function get_menu_code($cmsClass) |
||
| 192 | { |
||
| 193 | return Convert::raw2htmlname(str_replace('\\', '-', $cmsClass)); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get all menu entries. |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public static function get_menu_items() |
||
| 202 | { |
||
| 203 | $menuItems = array(); |
||
| 204 | |||
| 205 | // Set up default menu items |
||
| 206 | if (!self::$menu_is_cleared) { |
||
| 207 | $cmsClasses = self::get_cms_classes(); |
||
| 208 | foreach ($cmsClasses as $cmsClass) { |
||
| 209 | $menuItem = self::menuitem_for_controller($cmsClass); |
||
| 210 | $menuCode = self::get_menu_code($cmsClass); |
||
| 211 | if ($menuItem) { |
||
| 212 | $menuItems[$menuCode] = $menuItem; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | // Apply changes |
||
| 218 | foreach (self::$menu_item_changes as $change) { |
||
| 219 | switch ($change['type']) { |
||
| 220 | case 'add': |
||
| 221 | $menuItems[$change['code']] = $change['item']; |
||
| 222 | break; |
||
| 223 | |||
| 224 | case 'remove': |
||
| 225 | unset($menuItems[$change['code']]); |
||
| 226 | break; |
||
| 227 | |||
| 228 | default: |
||
| 229 | user_error("Bad menu item change type {$change['type']}", E_USER_WARNING); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | // Sort menu items according to priority, then title asc |
||
| 234 | $menuPriority = array(); |
||
| 235 | $menuTitle = array(); |
||
| 236 | foreach ($menuItems as $key => $menuItem) { |
||
| 237 | $menuPriority[$key] = is_numeric($menuItem->priority) ? $menuItem->priority : 0; |
||
| 238 | $menuTitle[$key] = $menuItem->title; |
||
| 239 | } |
||
| 240 | array_multisort($menuPriority, \SORT_DESC, $menuTitle, \SORT_ASC, $menuItems); |
||
| 241 | |||
| 242 | return $menuItems; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get all menu items that the passed member can view. |
||
| 247 | * Defaults to {@link Security::getCurrentUser()}. |
||
| 248 | * |
||
| 249 | * @param Member $member |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public static function get_viewable_menu_items($member = null) |
||
| 253 | { |
||
| 254 | if (!$member && $member !== false) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 255 | $member = Security::getCurrentUser(); |
||
| 256 | } |
||
| 257 | |||
| 258 | $viewableMenuItems = array(); |
||
| 259 | $allMenuItems = self::get_menu_items(); |
||
| 260 | if ($allMenuItems) { |
||
| 261 | foreach ($allMenuItems as $code => $menuItem) { |
||
| 262 | // exclude all items which have a controller to perform permission checks on |
||
| 263 | if ($menuItem->controller) { |
||
| 264 | $controllerObj = singleton($menuItem->controller); |
||
| 265 | if (Controller::has_curr()) { |
||
| 266 | // Necessary for canView() to have request data available, |
||
| 267 | // e.g. to check permissions against LeftAndMain->currentPage() |
||
| 268 | $controllerObj->setRequest(Controller::curr()->getRequest()); |
||
| 269 | if (!$controllerObj->canView($member)) { |
||
| 270 | continue; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | $viewableMenuItems[$code] = $menuItem; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | return $viewableMenuItems; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Removes an existing item from the menu. |
||
| 284 | * |
||
| 285 | * @param string $code Unique identifier for this menu item |
||
| 286 | */ |
||
| 287 | public static function remove_menu_item($code) |
||
| 288 | { |
||
| 289 | self::$menu_item_changes[] = array('type' => 'remove', 'code' => $code); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Remove menu item by class name. |
||
| 294 | * |
||
| 295 | * @param string $className Name of class |
||
| 296 | */ |
||
| 297 | public static function remove_menu_class($className) |
||
| 298 | { |
||
| 299 | $code = self::get_menu_code($className); |
||
| 300 | self::remove_menu_item($code); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Clears the entire menu |
||
| 305 | */ |
||
| 306 | public static function clear_menu() |
||
| 307 | { |
||
| 308 | self::$menu_item_changes = array(); |
||
| 309 | self::$menu_is_cleared = true; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Replace a navigation item to the main administration menu showing in the top bar. |
||
| 314 | * |
||
| 315 | * @param string $code Unique identifier for this menu item (e.g. used by {@link replace_menu_item()} and |
||
| 316 | * {@link remove_menu_item}. Also used as a CSS-class for icon customization. |
||
| 317 | * @param string $menuTitle Localized title showing in the menu bar |
||
| 318 | * @param string $url A relative URL that will be linked in the menu bar. |
||
| 319 | * Make sure to add a matching route via {@link Director::$rules} to this url. |
||
| 320 | * @param string $controllerClass The controller class for this menu, used to check permisssions. |
||
| 321 | * If blank, it's assumed that this is public, and always shown to users who |
||
| 322 | * have the rights to access some other part of the admin area. |
||
| 323 | * @param int $priority |
||
| 324 | * @param array $attributes an array of attributes to include on the link. |
||
| 325 | * @param string $iconClass |
||
| 326 | * |
||
| 327 | * @return bool Success |
||
| 328 | */ |
||
| 329 | public static function replace_menu_item( |
||
| 330 | $code, |
||
| 331 | $menuTitle, |
||
| 332 | $url, |
||
| 333 | $controllerClass = null, |
||
| 334 | $priority = -1, |
||
| 335 | $attributes = null, |
||
| 336 | $iconClass = null |
||
| 337 | ) { |
||
| 338 | $item = CMSMenuItem::create($menuTitle, $url, $controllerClass, $priority, $iconClass); |
||
| 339 | |||
| 340 | if ($attributes) { |
||
| 341 | $item->setAttributes($attributes); |
||
| 342 | } |
||
| 343 | |||
| 344 | self::$menu_item_changes[] = array( |
||
| 345 | 'type' => 'add', |
||
| 346 | 'code' => $code, |
||
| 347 | 'item' => $item, |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Add a previously built menu item object to the menu |
||
| 353 | * |
||
| 354 | * @param string $code |
||
| 355 | * @param CMSMenuItem $cmsMenuItem |
||
| 356 | */ |
||
| 357 | protected static function add_menu_item_obj($code, $cmsMenuItem) |
||
| 358 | { |
||
| 359 | self::$menu_item_changes[] = array( |
||
| 360 | 'type' => 'add', |
||
| 361 | 'code' => $code, |
||
| 362 | 'item' => $cmsMenuItem, |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * A utility funciton to retrieve subclasses of a given class that |
||
| 368 | * are instantiable (ie, not abstract) and have a valid menu title. |
||
| 369 | * |
||
| 370 | * Sorted by url_priority config. |
||
| 371 | * |
||
| 372 | * @todo A variation of this function could probably be moved to {@link ClassInfo} |
||
| 373 | * @param string $root The root class to begin finding subclasses |
||
| 374 | * @param boolean $recursive Look for subclasses recursively? |
||
| 375 | * @param string $sort Name of config on which to sort. Can be 'menu_priority' or 'url_priority' |
||
| 376 | * @return array Valid, unique subclasses |
||
| 377 | */ |
||
| 378 | public static function get_cms_classes($root = null, $recursive = true, $sort = self::MENU_PRIORITY) |
||
| 379 | { |
||
| 380 | if (!$root) { |
||
| 381 | $root = LeftAndMain::class; |
||
| 382 | } |
||
| 383 | $abstractClasses = [LeftAndMain::class]; |
||
| 384 | $subClasses = array_values(ClassInfo::subclassesFor($root)); |
||
| 385 | foreach ($subClasses as $className) { |
||
| 386 | if ($recursive && $className != $root) { |
||
| 387 | $subClasses = array_merge($subClasses, array_values(ClassInfo::subclassesFor($className))); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | $subClasses = array_unique($subClasses); |
||
| 391 | foreach ($subClasses as $key => $className) { |
||
| 392 | // Remove test & abstract classes |
||
| 393 | if (in_array($className, $abstractClasses) || ClassInfo::classImplements($className, TestOnly::class)) { |
||
| 394 | unset($subClasses[$key]); |
||
| 395 | } else { |
||
| 396 | // Separate conditional to avoid autoloading the class |
||
| 397 | $classReflection = new ReflectionClass($className); |
||
| 398 | if (!$classReflection->isInstantiable()) { |
||
| 399 | unset($subClasses[$key]); |
||
| 400 | } |
||
| 401 | } |
||
| 402 | } |
||
| 403 | // Sort by specified sorting config |
||
| 404 | usort($subClasses, function ($a, $b) use ($sort) { |
||
| 405 | $priorityA = Config::inst()->get($a, $sort); |
||
| 406 | $priorityB = Config::inst()->get($b, $sort); |
||
| 407 | return $priorityB - $priorityA; |
||
| 408 | }); |
||
| 409 | |||
| 410 | return $subClasses; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * IteratorAggregate Interface Method. Iterates over the menu items. |
||
| 415 | */ |
||
| 416 | public function getIterator(): Traversable |
||
| 417 | { |
||
| 418 | return new ArrayIterator(self::get_menu_items()); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Provide menu titles to the i18n entity provider |
||
| 423 | */ |
||
| 424 | public function provideI18nEntities() |
||
| 425 | { |
||
| 426 | $cmsClasses = self::get_cms_classes(); |
||
| 427 | $entities = array(); |
||
| 428 | foreach ($cmsClasses as $cmsClass) { |
||
| 429 | $defaultTitle = LeftAndMain::menu_title($cmsClass, false); |
||
| 430 | $ownerModule = ClassLoader::inst()->getManifest()->getOwnerModule($cmsClass); |
||
| 431 | $entities["{$cmsClass}.MENUTITLE"] = [ |
||
| 432 | 'default' => $defaultTitle, |
||
| 433 | 'module' => $ownerModule->getShortName() |
||
| 434 | ]; |
||
| 435 | } |
||
| 436 | return $entities; |
||
| 437 | } |
||
| 438 | } |
||
| 439 |