xref: /dokuwiki/inc/Menu/AbstractMenu.php (revision 33b91513e25639a6c7eb35668484d29098f7c9b4)
193b8c351SAndreas Gohr<?php
293b8c351SAndreas Gohr
393b8c351SAndreas Gohrnamespace dokuwiki\Menu;
493b8c351SAndreas Gohr
5cbb44eabSAndreas 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 */
15*33b91513SAndreas Gohrabstract class AbstractMenu implements MenuInterface
16*33b91513SAndreas Gohr{
1793b8c351SAndreas Gohr
1893b8c351SAndreas Gohr    /** @var string[] list of Item classes to load */
19*33b91513SAndreas Gohr    protected $types = [];
2093b8c351SAndreas Gohr
2193b8c351SAndreas Gohr    /** @var int the context this menu is used in */
2293b8c351SAndreas Gohr    protected $context = AbstractItem::CTX_DESKTOP;
2393b8c351SAndreas Gohr
2493b8c351SAndreas Gohr    /** @var string view identifier to be set in the event */
2593b8c351SAndreas Gohr    protected $view = '';
2693b8c351SAndreas Gohr
2793b8c351SAndreas Gohr    /**
2893b8c351SAndreas Gohr     * AbstractMenu constructor.
2993b8c351SAndreas Gohr     *
3093b8c351SAndreas Gohr     * @param int $context the context this menu is used in
3193b8c351SAndreas Gohr     */
32*33b91513SAndreas Gohr    public function __construct($context = AbstractItem::CTX_DESKTOP)
33*33b91513SAndreas Gohr    {
3493b8c351SAndreas Gohr        $this->context = $context;
3593b8c351SAndreas Gohr    }
3693b8c351SAndreas Gohr
3793b8c351SAndreas Gohr    /**
3893b8c351SAndreas Gohr     * Get the list of action items in this menu
3993b8c351SAndreas Gohr     *
4093b8c351SAndreas Gohr     * @return AbstractItem[]
4193b8c351SAndreas Gohr     * @triggers MENU_ITEMS_ASSEMBLY
4293b8c351SAndreas Gohr     */
43*33b91513SAndreas Gohr    public function getItems()
44*33b91513SAndreas Gohr    {
45*33b91513SAndreas Gohr        $data = ['view' => $this->view, 'items' => []];
46*33b91513SAndreas Gohr        Event::createAndTrigger('MENU_ITEMS_ASSEMBLY', $data, [$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     * @param array $data The plugin data
54*33b91513SAndreas Gohr     * @see getItems()
5593b8c351SAndreas Gohr     */
56*33b91513SAndreas Gohr    public function loadItems(&$data)
57*33b91513SAndreas Gohr    {
5893b8c351SAndreas Gohr        foreach ($this->types as $class) {
5993b8c351SAndreas Gohr            try {
6093b8c351SAndreas Gohr                $class = "\\dokuwiki\\Menu\\Item\\$class";
6193b8c351SAndreas Gohr                /** @var AbstractItem $item */
6293b8c351SAndreas Gohr                $item = new $class();
6393b8c351SAndreas Gohr                if (!$item->visibleInContext($this->context)) continue;
64e6bd4c92SAndreas Gohr                $data['items'][] = $item;
6593b8c351SAndreas Gohr            } catch (\RuntimeException $ignored) {
6693b8c351SAndreas Gohr                // item not available
6793b8c351SAndreas Gohr            }
6893b8c351SAndreas Gohr        }
6993b8c351SAndreas Gohr    }
7093b8c351SAndreas Gohr
7193b8c351SAndreas Gohr    /**
7293b8c351SAndreas Gohr     * Generate HTML list items for this menu
7393b8c351SAndreas Gohr     *
7493b8c351SAndreas Gohr     * This is a convenience method for template authors. If you need more fine control over the
7593b8c351SAndreas Gohr     * output, use getItems() and build the HTML yourself
7693b8c351SAndreas Gohr     *
7793b8c351SAndreas Gohr     * @param string|false $classprefix create a class from type with this prefix, false for no class
782f7a5efdSAndreas Gohr     * @param bool $svg add the SVG link
79c2b9771aSAndreas Gohr     * @return string
8093b8c351SAndreas Gohr     */
81*33b91513SAndreas Gohr    public function getListItems($classprefix = '', $svg = true)
82*33b91513SAndreas Gohr    {
8393b8c351SAndreas Gohr        $html = '';
8493b8c351SAndreas Gohr        foreach ($this->getItems() as $item) {
8593b8c351SAndreas Gohr            if ($classprefix !== false) {
8693b8c351SAndreas Gohr                $class = ' class="' . $classprefix . $item->getType() . '"';
8793b8c351SAndreas Gohr            } else {
8893b8c351SAndreas Gohr                $class = '';
8993b8c351SAndreas Gohr            }
9093b8c351SAndreas Gohr
9193b8c351SAndreas Gohr            $html .= "<li$class>";
922f7a5efdSAndreas Gohr            $html .= $item->asHtmlLink(false, $svg);
9393b8c351SAndreas Gohr            $html .= '</li>';
9493b8c351SAndreas Gohr        }
95c2b9771aSAndreas Gohr        return $html;
9693b8c351SAndreas Gohr    }
9793b8c351SAndreas Gohr
9893b8c351SAndreas Gohr}
99