xref: /dokuwiki/inc/Menu/AbstractMenu.php (revision cbb44eabe033d70affb048ec0daf4e579e09dd20)
193b8c351SAndreas Gohr<?php
293b8c351SAndreas Gohr
393b8c351SAndreas Gohrnamespace dokuwiki\Menu;
493b8c351SAndreas Gohr
5*cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
693b8c351SAndreas Gohruse dokuwiki\Menu\Item\AbstractItem;
793b8c351SAndreas Gohr
8368ce258SAndreas Gohr/**
9368ce258SAndreas Gohr * Class AbstractMenu
10368ce258SAndreas Gohr *
11368ce258SAndreas Gohr * Basic menu functionality. A menu defines a list of AbstractItem that shall be shown.
12368ce258SAndreas Gohr * It contains convenience functions to display the menu in HTML, but template authors can also
13368ce258SAndreas Gohr * just accesst the items via getItems() and create the HTML as however they see fit.
14368ce258SAndreas Gohr */
1531110e19SAndreas Gohrabstract class AbstractMenu implements MenuInterface {
1693b8c351SAndreas Gohr
1793b8c351SAndreas Gohr    /** @var string[] list of Item classes to load */
1893b8c351SAndreas Gohr    protected $types = array();
1993b8c351SAndreas Gohr
2093b8c351SAndreas Gohr    /** @var int the context this menu is used in */
2193b8c351SAndreas Gohr    protected $context = AbstractItem::CTX_DESKTOP;
2293b8c351SAndreas Gohr
2393b8c351SAndreas Gohr    /** @var string view identifier to be set in the event */
2493b8c351SAndreas Gohr    protected $view = '';
2593b8c351SAndreas Gohr
2693b8c351SAndreas Gohr    /**
2793b8c351SAndreas Gohr     * AbstractMenu constructor.
2893b8c351SAndreas Gohr     *
2993b8c351SAndreas Gohr     * @param int $context the context this menu is used in
3093b8c351SAndreas Gohr     */
3193b8c351SAndreas Gohr    public function __construct($context = AbstractItem::CTX_DESKTOP) {
3293b8c351SAndreas Gohr        $this->context = $context;
3393b8c351SAndreas Gohr    }
3493b8c351SAndreas Gohr
3593b8c351SAndreas Gohr    /**
3693b8c351SAndreas Gohr     * Get the list of action items in this menu
3793b8c351SAndreas Gohr     *
3893b8c351SAndreas Gohr     * @return AbstractItem[]
3993b8c351SAndreas Gohr     * @triggers MENU_ITEMS_ASSEMBLY
4093b8c351SAndreas Gohr     */
4193b8c351SAndreas Gohr    public function getItems() {
4293b8c351SAndreas Gohr        $data = array(
4393b8c351SAndreas Gohr            'view' => $this->view,
4493b8c351SAndreas Gohr            'items' => array(),
4593b8c351SAndreas Gohr        );
46*cbb44eabSAndreas Gohr        Event::createAndTrigger('MENU_ITEMS_ASSEMBLY', $data, array($this, 'loadItems'));
4793b8c351SAndreas Gohr        return $data['items'];
4893b8c351SAndreas Gohr    }
4993b8c351SAndreas Gohr
5093b8c351SAndreas Gohr    /**
5193b8c351SAndreas Gohr     * Default action for the MENU_ITEMS_ASSEMBLY event
5293b8c351SAndreas Gohr     *
5393b8c351SAndreas Gohr     * @see getItems()
5493b8c351SAndreas Gohr     * @param array $data The plugin data
5593b8c351SAndreas Gohr     */
5693b8c351SAndreas Gohr    public function loadItems(&$data) {
5793b8c351SAndreas Gohr        foreach($this->types as $class) {
5893b8c351SAndreas Gohr            try {
5993b8c351SAndreas Gohr                $class = "\\dokuwiki\\Menu\\Item\\$class";
6093b8c351SAndreas Gohr                /** @var AbstractItem $item */
6193b8c351SAndreas Gohr                $item = new $class();
6293b8c351SAndreas Gohr                if(!$item->visibleInContext($this->context)) continue;
63e6bd4c92SAndreas Gohr                $data['items'][] = $item;
6493b8c351SAndreas Gohr            } catch(\RuntimeException $ignored) {
6593b8c351SAndreas Gohr                // item not available
6693b8c351SAndreas Gohr            }
6793b8c351SAndreas Gohr        }
6893b8c351SAndreas Gohr    }
6993b8c351SAndreas Gohr
7093b8c351SAndreas Gohr    /**
7193b8c351SAndreas Gohr     * Generate HTML list items for this menu
7293b8c351SAndreas Gohr     *
7393b8c351SAndreas Gohr     * This is a convenience method for template authors. If you need more fine control over the
7493b8c351SAndreas Gohr     * output, use getItems() and build the HTML yourself
7593b8c351SAndreas Gohr     *
7693b8c351SAndreas Gohr     * @param string|false $classprefix create a class from type with this prefix, false for no class
772f7a5efdSAndreas Gohr     * @param bool $svg add the SVG link
78c2b9771aSAndreas Gohr     * @return string
7993b8c351SAndreas Gohr     */
802f7a5efdSAndreas Gohr    public function getListItems($classprefix = '', $svg = true) {
8193b8c351SAndreas Gohr        $html = '';
8293b8c351SAndreas Gohr        foreach($this->getItems() as $item) {
8393b8c351SAndreas Gohr            if($classprefix !== false) {
8493b8c351SAndreas Gohr                $class = ' class="' . $classprefix . $item->getType() . '"';
8593b8c351SAndreas Gohr            } else {
8693b8c351SAndreas Gohr                $class = '';
8793b8c351SAndreas Gohr            }
8893b8c351SAndreas Gohr
8993b8c351SAndreas Gohr            $html .= "<li$class>";
902f7a5efdSAndreas Gohr            $html .= $item->asHtmlLink(false, $svg);
9193b8c351SAndreas Gohr            $html .= '</li>';
9293b8c351SAndreas Gohr        }
93c2b9771aSAndreas Gohr        return $html;
9493b8c351SAndreas Gohr    }
9593b8c351SAndreas Gohr
9693b8c351SAndreas Gohr}
97