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.
 
 
 
 
 

96 lines
2.8 KiB

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