xref: /dokuwiki/inc/Menu/AbstractMenu.php (revision c2b9771a2c4f3934d170bc387b7fd0137cdb2c93)
193b8c351SAndreas Gohr<?php
293b8c351SAndreas Gohr
393b8c351SAndreas Gohrnamespace dokuwiki\Menu;
493b8c351SAndreas Gohr
593b8c351SAndreas Gohruse dokuwiki\Menu\Item\AbstractItem;
693b8c351SAndreas Gohr
793b8c351SAndreas Gohrabstract class AbstractMenu {
893b8c351SAndreas Gohr
993b8c351SAndreas Gohr    /** @var string[] list of Item classes to load */
1093b8c351SAndreas Gohr    protected $types = array();
1193b8c351SAndreas Gohr
1293b8c351SAndreas Gohr    /** @var int the context this menu is used in */
1393b8c351SAndreas Gohr    protected $context = AbstractItem::CTX_DESKTOP;
1493b8c351SAndreas Gohr
1593b8c351SAndreas Gohr    /** @var string view identifier to be set in the event */
1693b8c351SAndreas Gohr    protected $view = '';
1793b8c351SAndreas Gohr
1893b8c351SAndreas Gohr    /**
1993b8c351SAndreas Gohr     * AbstractMenu constructor.
2093b8c351SAndreas Gohr     *
2193b8c351SAndreas Gohr     * @param int $context the context this menu is used in
2293b8c351SAndreas Gohr     */
2393b8c351SAndreas Gohr    public function __construct($context = AbstractItem::CTX_DESKTOP) {
2493b8c351SAndreas Gohr        $this->context = $context;
2593b8c351SAndreas Gohr    }
2693b8c351SAndreas Gohr
2793b8c351SAndreas Gohr    /**
2893b8c351SAndreas Gohr     * Get the list of action items in this menu
2993b8c351SAndreas Gohr     *
3093b8c351SAndreas Gohr     * @return AbstractItem[]
3193b8c351SAndreas Gohr     * @triggers MENU_ITEMS_ASSEMBLY
3293b8c351SAndreas Gohr     */
3393b8c351SAndreas Gohr    public function getItems() {
3493b8c351SAndreas Gohr        $data = array(
3593b8c351SAndreas Gohr            'view' => $this->view,
3693b8c351SAndreas Gohr            'items' => array(),
3793b8c351SAndreas Gohr        );
3893b8c351SAndreas Gohr        trigger_event('MENU_ITEMS_ASSEMBLY', $data, array($this, 'loadItems'));
3993b8c351SAndreas Gohr        return $data['items'];
4093b8c351SAndreas Gohr    }
4193b8c351SAndreas Gohr
4293b8c351SAndreas Gohr    /**
4393b8c351SAndreas Gohr     * Default action for the MENU_ITEMS_ASSEMBLY event
4493b8c351SAndreas Gohr     *
4593b8c351SAndreas Gohr     * @see getItems()
4693b8c351SAndreas Gohr     * @param array $data The plugin data
4793b8c351SAndreas Gohr     */
4893b8c351SAndreas Gohr    public function loadItems(&$data) {
4993b8c351SAndreas Gohr        foreach($this->types as $class) {
5093b8c351SAndreas Gohr            try {
5193b8c351SAndreas Gohr                $class = "\\dokuwiki\\Menu\\Item\\$class";
5293b8c351SAndreas Gohr                /** @var AbstractItem $item */
5393b8c351SAndreas Gohr                $item = new $class();
5493b8c351SAndreas Gohr                if(!$item->visibleInContext($this->context)) continue;
5593b8c351SAndreas Gohr                $data['items'][$item->getType()] = $item;
5693b8c351SAndreas Gohr            } catch(\RuntimeException $ignored) {
5793b8c351SAndreas Gohr                // item not available
5893b8c351SAndreas Gohr            }
5993b8c351SAndreas Gohr        }
6093b8c351SAndreas Gohr    }
6193b8c351SAndreas Gohr
6293b8c351SAndreas Gohr    /**
6393b8c351SAndreas Gohr     * Generate HTML list items for this menu
6493b8c351SAndreas Gohr     *
6593b8c351SAndreas Gohr     * This is a convenience method for template authors. If you need more fine control over the
6693b8c351SAndreas Gohr     * output, use getItems() and build the HTML yourself
6793b8c351SAndreas Gohr     *
6893b8c351SAndreas Gohr     * @param string|false $classprefix create a class from type with this prefix, false for no class
69*c2b9771aSAndreas Gohr     * @return string
7093b8c351SAndreas Gohr     */
7193b8c351SAndreas Gohr    public function getListItems($classprefix = '') {
7293b8c351SAndreas Gohr        $html = '';
7393b8c351SAndreas Gohr        foreach($this->getItems() as $item) {
7493b8c351SAndreas Gohr            if($classprefix !== false) {
7593b8c351SAndreas Gohr                $class = ' class="' . $classprefix . $item->getType() . '"';
7693b8c351SAndreas Gohr            } else {
7793b8c351SAndreas Gohr                $class = '';
7893b8c351SAndreas Gohr            }
7993b8c351SAndreas Gohr
8093b8c351SAndreas Gohr            $html .= "<li$class>";
8193b8c351SAndreas Gohr            $html .= $item->asHtmlLink(false);
8293b8c351SAndreas Gohr            $html .= '</li>';
8393b8c351SAndreas Gohr        }
84*c2b9771aSAndreas Gohr        return $html;
8593b8c351SAndreas Gohr    }
8693b8c351SAndreas Gohr
8793b8c351SAndreas Gohr}
88