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    public function __construct()
40    {
41        /**
42         * Making popover active
43         */
44        $snippetManager = PluginUtility::getSnippetManager();
45        $snippetManager
46            ->addPopoverLibrary()
47            ->attachJavascriptFromComponentId(HistoricalBreadcrumbMenuItem::HISTORICAL_BREADCRUMB_NAME);
48
49        /**
50         * Css
51         */
52        $snippetManager->attachCssInternalStylesheet(HistoricalBreadcrumbMenuItem::HISTORICAL_BREADCRUMB_NAME);
53
54        parent::__construct();
55
56    }
57
58
59    /**
60     *
61     * @return string
62     */
63    public function getLabel(): string
64    {
65        return self::RECENT_PAGES_VISITED;
66    }
67
68    public function getLinkAttributes($classprefix = 'menuitem '): array
69    {
70
71        $linkAttributes = parent::getLinkAttributes($classprefix);
72        $linkAttributes['href'] = "#";
73        $dataAttributeNamespace = Bootstrap::getDataNamespace();
74        $linkAttributes["data{$dataAttributeNamespace}-toggle"] = "popover";
75        $linkAttributes["data{$dataAttributeNamespace}-placement"] = "left";
76        $linkAttributes["data{$dataAttributeNamespace}-html"] = "true";
77        global $lang;
78        $linkAttributes["data{$dataAttributeNamespace}-title"] = $lang['breadcrumb'];
79
80
81        $pages = breadcrumbs();
82        if (sizeof($pages) === 0) {
83            // happens when there is no history
84            return $linkAttributes;
85        }
86        $pages = array_reverse($pages);
87
88        /**
89         * All page should be shown,
90         * also the actual
91         * because when the user is going
92         * in admin mode, it's an easy way to get back
93         */
94        $actualPageId = array_keys($pages)[0];
95        $actualPageName = array_shift($pages);
96        $html = $this->createLink($actualPageId, $actualPageName, self::HISTORICAL_BREADCRUMB_NAME . "-home");
97
98        $html .= '<ol>' . PHP_EOL;
99        foreach ($pages as $id => $name) {
100
101            $html .= '<li>';
102            $html .= $this->createLink($id, $name);
103            $html .= '</li>' . PHP_EOL;
104
105        }
106        $html .= '</ol>' . PHP_EOL;
107        $html .= '</nav>' . PHP_EOL;
108
109
110        $linkAttributes["data{$dataAttributeNamespace}-content"] = $html;
111
112        // https://github.com/ComboStrap/combo/issues/109
113        // Don't use the dismiss, happens before a link navigation
114        // preventing links to work
115        // $linkAttributes["data{$dataAttributeNamespace}-trigger"] = "focus";
116
117        // See for the tabindex
118        // https://getbootstrap.com/docs/5.1/components/popovers/#dismiss-on-next-click
119        $linkAttributes['tabindex'] = "0";
120
121        $linkAttributes["data{$dataAttributeNamespace}-custom-class"] = "historical-breadcrumb";
122        return $linkAttributes;
123
124    }
125
126    public function getTitle(): string
127    {
128        /**
129         * The title (unfortunately) is deleted from the anchor
130         * and is used as header in the popover
131         */
132        return self::RECENT_PAGES_VISITED;
133    }
134
135    public function getSvg(): string
136    {
137        /** @var string icon file */
138        return DirectoryLayout::getComboImagesDirectory()->resolve('history.svg')->toAbsoluteId();
139    }
140
141    /**
142     * @param $id
143     * @param $name
144     * @param null $class
145     * @return string
146     */
147    public function createLink($id, $name, $class = null): string
148    {
149        $page = MarkupPath::createMarkupFromId($id);
150        if ($name == "start") {
151            $name = "Home Page";
152        } else {
153            $name = $page->getTitleOrDefault();
154        }
155
156        try {
157            $attributes = LinkMarkup::createFromPageIdOrPath($id)->toAttributes(self::CANONICAL);
158        } catch (ExceptionCompile $e) {
159            return LogUtility::wrapInRedForHtml("Error on breadcrumb markup ref. Message: {$e->getMessage()}");
160        }
161        if ($class !== null) {
162            $attributes->addClassName($class);
163        }
164
165        return $attributes->toHtmlEnterTag("a") . $name . "</a>";
166    }
167
168
169}
170