You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

97 lines
2.8 KiB

  1. <?php
  2. namespace dokuwiki\Menu;
  3. use dokuwiki\Extension\Event;
  4. use dokuwiki\Menu\Item\AbstractItem;
  5. /**
  6. * Class AbstractMenu
  7. *
  8. * Basic menu functionality. A menu defines a list of AbstractItem that shall be shown.
  9. * It contains convenience functions to display the menu in HTML, but template authors can also
  10. * just accesst the items via getItems() and create the HTML as however they see fit.
  11. */
  12. abstract class AbstractMenu implements MenuInterface
  13. {
  14. /** @var string[] list of Item classes to load */
  15. protected $types = [];
  16. /** @var int the context this menu is used in */
  17. protected $context = AbstractItem::CTX_DESKTOP;
  18. /** @var string view identifier to be set in the event */
  19. protected $view = '';
  20. /**
  21. * AbstractMenu constructor.
  22. *
  23. * @param int $context the context this menu is used in
  24. */
  25. public function __construct($context = AbstractItem::CTX_DESKTOP)
  26. {
  27. $this->context = $context;
  28. }
  29. /**
  30. * Get the list of action items in this menu
  31. *
  32. * @return AbstractItem[]
  33. * @triggers MENU_ITEMS_ASSEMBLY
  34. */
  35. public function getItems()
  36. {
  37. $data = ['view' => $this->view, 'items' => []];
  38. Event::createAndTrigger('MENU_ITEMS_ASSEMBLY', $data, [$this, 'loadItems']);
  39. return $data['items'];
  40. }
  41. /**
  42. * Default action for the MENU_ITEMS_ASSEMBLY event
  43. *
  44. * @param array $data The plugin data
  45. * @see getItems()
  46. */
  47. public function loadItems(&$data)
  48. {
  49. foreach ($this->types as $class) {
  50. try {
  51. $class = "\\dokuwiki\\Menu\\Item\\$class";
  52. /** @var AbstractItem $item */
  53. $item = new $class();
  54. if (!$item->visibleInContext($this->context)) continue;
  55. $data['items'][] = $item;
  56. } catch (\RuntimeException $ignored) {
  57. // item not available
  58. }
  59. }
  60. }
  61. /**
  62. * Generate HTML list items for this menu
  63. *
  64. * This is a convenience method for template authors. If you need more fine control over the
  65. * output, use getItems() and build the HTML yourself
  66. *
  67. * @param string|false $classprefix create a class from type with this prefix, false for no class
  68. * @param bool $svg add the SVG link
  69. * @return string
  70. */
  71. public function getListItems($classprefix = '', $svg = true)
  72. {
  73. $html = '';
  74. foreach ($this->getItems() as $item) {
  75. if ($classprefix !== false) {
  76. $class = ' class="' . $classprefix . $item->getType() . '"';
  77. } else {
  78. $class = '';
  79. }
  80. $html .= "<li$class>";
  81. $html .= $item->asHtmlLink(false, $svg);
  82. $html .= '</li>';
  83. }
  84. return $html;
  85. }
  86. }