xref: /plugin/combo/ComboStrap/HistoricalBreadcrumbMenuItem.php (revision c3437056399326d621a01da73b649707fbb0ae69)
1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16use dokuwiki\Menu\Item\AbstractItem;
17
18/**
19 * Class MenuItem
20 * *
21 * @package ComboStrap
22 *
23 * To be able to debug, disable the trigger data attribute
24 * The popover will stay on the page
25 */
26class HistoricalBreadcrumbMenuItem extends AbstractItem
27{
28
29
30    const RECENT_PAGES_VISITED = "Recent Pages Visited";
31
32    /**
33     * This unique name should not be in the {@link \action_plugin_combo_historicalbreadcrumb}
34     * to avoid circular reference
35     */
36    const HISTORICAL_BREADCRUMB_NAME = "historical-breadcrumb";
37
38
39    /**
40     *
41     * @return string
42     */
43    public function getLabel()
44    {
45        return self::RECENT_PAGES_VISITED;
46    }
47
48    public function getLinkAttributes($classprefix = 'menuitem '): array
49    {
50
51        $linkAttributes = parent::getLinkAttributes($classprefix);
52        $linkAttributes['href'] = "#";
53        $dataAttributeNamespace = Bootstrap::getDataNamespace();
54        $linkAttributes["data{$dataAttributeNamespace}-toggle"] = "popover";
55        $linkAttributes["data{$dataAttributeNamespace}-html"] = "true";
56        global $lang;
57        $linkAttributes["data{$dataAttributeNamespace}-title"] = $lang['breadcrumb'];
58
59
60        $pages = breadcrumbs();
61        if (sizeof($pages) === 0) {
62            // happens when there is no history
63            return $linkAttributes;
64        }
65        $pages = array_reverse($pages);
66
67        /**
68         * All page should be shown,
69         * also the actual
70         * because when the user is going
71         * in admin mode, it's an easy way to get back
72         */
73        $actualPageId = array_keys($pages)[0];
74        $actualPageName = array_shift($pages);
75        $html = $this->createLink($actualPageId, $actualPageName, self::HISTORICAL_BREADCRUMB_NAME . "-home");
76
77        $html .= '<ol>' . PHP_EOL;
78        foreach ($pages as $id => $name) {
79
80            $html .= '<li>';
81            $html .= $this->createLink($id, $name);
82            $html .= '</li>' . PHP_EOL;
83
84        }
85        $html .= '</ol>' . PHP_EOL;
86        $html .= '</nav>' . PHP_EOL;
87
88
89        $linkAttributes["data{$dataAttributeNamespace}-content"] = $html;
90
91        // Dismiss on next click
92        // To debug, just comment this line
93        $linkAttributes["data{$dataAttributeNamespace}-trigger"] = "focus";
94
95        // See for the tabindex
96        // https://getbootstrap.com/docs/5.1/components/popovers/#dismiss-on-next-click
97        $linkAttributes['tabindex'] = "0";
98
99        $linkAttributes["data{$dataAttributeNamespace}custom-class"] = "historical-breadcrumb";
100        return $linkAttributes;
101    }
102
103    public function getTitle()
104    {
105        /**
106         * The title (unfortunately) is deleted from the anchor
107         * and is used as header in the popover
108         */
109        return self::RECENT_PAGES_VISITED;
110    }
111
112    public function getSvg()
113    {
114        /** @var string icon file */
115        return Resources::getImagesDirectory() . '/history.svg';
116    }
117
118    /**
119     * @param $id
120     * @param $name
121     * @param null $class
122     * @return string
123     */
124    public function createLink($id, $name, $class = null): string
125    {
126        $page = Page::createPageFromId($id);
127        if ($name == "start") {
128            $name = "Home Page";
129        } else {
130            $name = $page->getTitleOrDefault();
131        }
132        $tagAttributes = null;
133        if ($class != null) {
134            $tagAttributes = TagAttributes::createEmpty();
135            $tagAttributes->addClassName($class);
136        }
137        $link = LinkUtility::createFromPageId($id, $tagAttributes);
138
139        return $link->renderOpenTag() . $name . $link->renderClosingTag();
140    }
141
142
143}
144