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    const CANONICAL = "breadcrumb";
38
39
40    /**
41     *
42     * @return string
43     */
44    public function getLabel()
45    {
46        return self::RECENT_PAGES_VISITED;
47    }
48
49    public function getLinkAttributes($classprefix = 'menuitem '): array
50    {
51
52        $linkAttributes = parent::getLinkAttributes($classprefix);
53        $linkAttributes['href'] = "#";
54        $dataAttributeNamespace = Bootstrap::getDataNamespace();
55        $linkAttributes["data{$dataAttributeNamespace}-toggle"] = "popover";
56        $linkAttributes["data{$dataAttributeNamespace}-html"] = "true";
57        global $lang;
58        $linkAttributes["data{$dataAttributeNamespace}-title"] = $lang['breadcrumb'];
59
60
61        $pages = breadcrumbs();
62        if (sizeof($pages) === 0) {
63            // happens when there is no history
64            return $linkAttributes;
65        }
66        $pages = array_reverse($pages);
67
68        /**
69         * All page should be shown,
70         * also the actual
71         * because when the user is going
72         * in admin mode, it's an easy way to get back
73         */
74        $actualPageId = array_keys($pages)[0];
75        $actualPageName = array_shift($pages);
76        $html = $this->createLink($actualPageId, $actualPageName, self::HISTORICAL_BREADCRUMB_NAME . "-home");
77
78        $html .= '<ol>' . PHP_EOL;
79        foreach ($pages as $id => $name) {
80
81            $html .= '<li>';
82            $html .= $this->createLink($id, $name);
83            $html .= '</li>' . PHP_EOL;
84
85        }
86        $html .= '</ol>' . PHP_EOL;
87        $html .= '</nav>' . PHP_EOL;
88
89
90        $linkAttributes["data{$dataAttributeNamespace}-content"] = $html;
91
92        // Dismiss on next click
93        // To debug, just comment this line
94        $linkAttributes["data{$dataAttributeNamespace}-trigger"] = "focus";
95
96        // See for the tabindex
97        // https://getbootstrap.com/docs/5.1/components/popovers/#dismiss-on-next-click
98        $linkAttributes['tabindex'] = "0";
99
100        $linkAttributes["data{$dataAttributeNamespace}custom-class"] = "historical-breadcrumb";
101        return $linkAttributes;
102    }
103
104    public function getTitle()
105    {
106        /**
107         * The title (unfortunately) is deleted from the anchor
108         * and is used as header in the popover
109         */
110        return self::RECENT_PAGES_VISITED;
111    }
112
113    public function getSvg()
114    {
115        /** @var string icon file */
116        return Site::getComboImagesDirectory()->resolve('history.svg')->toString();
117    }
118
119    /**
120     * @param $id
121     * @param $name
122     * @param null $class
123     * @return string
124     */
125    public function createLink($id, $name, $class = null): string
126    {
127        $page = Page::createPageFromId($id);
128        if ($name == "start") {
129            $name = "Home Page";
130        } else {
131            $name = $page->getTitleOrDefault();
132        }
133
134        try {
135            $attributes = MarkupRef::createFromPageId($id)->toAttributes(self::CANONICAL);
136        } catch (ExceptionCombo $e) {
137            return LogUtility::wrapInRedForHtml("Error on breadcrumb markup ref. Message: {$e->getMessage()}");
138        }
139        if($class!==null){
140            $attributes->addClassName($class);
141        }
142
143        return $attributes->toHtmlEnterTag("a") . $name . "</a>";
144    }
145
146
147}
148