AbstractMenu.php 2.8 KB

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