about summary refs log tree commit diff stats
path: root/wiki/inc/Menu/AbstractMenu.php
diff options
context:
space:
mode:
authorahriman <ahriman@falte.red>2018-12-03 19:22:25 -0500
committerahriman <ahriman@falte.red>2018-12-03 19:22:25 -0500
commit0ae8cbf5c0b1a198b963490985b7738392ebcb97 (patch)
treeb2c77ae72c6b717e2b97492065196ac5ffb2d9e2 /wiki/inc/Menu/AbstractMenu.php
parentf57f6cc5a2d159f90168d292437dc4bd8cd7f934 (diff)
downloadsite-0ae8cbf5c0b1a198b963490985b7738392ebcb97.tar.gz
installed dokuwiki, added to navbar, updated news
Diffstat (limited to 'wiki/inc/Menu/AbstractMenu.php')
-rw-r--r--wiki/inc/Menu/AbstractMenu.php95
1 files changed, 95 insertions, 0 deletions
diff --git a/wiki/inc/Menu/AbstractMenu.php b/wiki/inc/Menu/AbstractMenu.php
new file mode 100644
index 0000000..ce021ab
--- /dev/null
+++ b/wiki/inc/Menu/AbstractMenu.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace dokuwiki\Menu;
+
+use dokuwiki\Menu\Item\AbstractItem;
+
+/**
+ * Class AbstractMenu
+ *
+ * Basic menu functionality. A menu defines a list of AbstractItem that shall be shown.
+ * It contains convenience functions to display the menu in HTML, but template authors can also
+ * just accesst the items via getItems() and create the HTML as however they see fit.
+ */
+abstract class AbstractMenu implements MenuInterface {
+
+    /** @var string[] list of Item classes to load */
+    protected $types = array();
+
+    /** @var int the context this menu is used in */
+    protected $context = AbstractItem::CTX_DESKTOP;
+
+    /** @var string view identifier to be set in the event */
+    protected $view = '';
+
+    /**
+     * AbstractMenu constructor.
+     *
+     * @param int $context the context this menu is used in
+     */
+    public function __construct($context = AbstractItem::CTX_DESKTOP) {
+        $this->context = $context;
+    }
+
+    /**
+     * Get the list of action items in this menu
+     *
+     * @return AbstractItem[]
+     * @triggers MENU_ITEMS_ASSEMBLY
+     */
+    public function getItems() {
+        $data = array(
+            'view' => $this->view,
+            'items' => array(),
+        );
+        trigger_event('MENU_ITEMS_ASSEMBLY', $data, array($this, 'loadItems'));
+        return $data['items'];
+    }
+
+    /**
+     * Default action for the MENU_ITEMS_ASSEMBLY event
+     *
+     * @see getItems()
+     * @param array $data The plugin data
+     */
+    public function loadItems(&$data) {
+        foreach($this->types as $class) {
+            try {
+                $class = "\\dokuwiki\\Menu\\Item\\$class";
+                /** @var AbstractItem $item */
+                $item = new $class();
+                if(!$item->visibleInContext($this->context)) continue;
+                $data['items'][] = $item;
+            } catch(\RuntimeException $ignored) {
+                // item not available
+            }
+        }
+    }
+
+    /**
+     * Generate HTML list items for this menu
+     *
+     * This is a convenience method for template authors. If you need more fine control over the
+     * output, use getItems() and build the HTML yourself
+     *
+     * @param string|false $classprefix create a class from type with this prefix, false for no class
+     * @param bool $svg add the SVG link
+     * @return string
+     */
+    public function getListItems($classprefix = '', $svg = true) {
+        $html = '';
+        foreach($this->getItems() as $item) {
+            if($classprefix !== false) {
+                $class = ' class="' . $classprefix . $item->getType() . '"';
+            } else {
+                $class = '';
+            }
+
+            $html .= "<li$class>";
+            $html .= $item->asHtmlLink(false, $svg);
+            $html .= '</li>';
+        }
+        return $html;
+    }
+
+}