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 // Dismiss on next click 113 // To debug, just comment this line 114 $linkAttributes["data{$dataAttributeNamespace}-trigger"] = "focus"; 115 116 // See for the tabindex 117 // https://getbootstrap.com/docs/5.1/components/popovers/#dismiss-on-next-click 118 $linkAttributes['tabindex'] = "0"; 119 120 $linkAttributes["data{$dataAttributeNamespace}-custom-class"] = "historical-breadcrumb"; 121 return $linkAttributes; 122 123 } 124 125 public function getTitle(): string 126 { 127 /** 128 * The title (unfortunately) is deleted from the anchor 129 * and is used as header in the popover 130 */ 131 return self::RECENT_PAGES_VISITED; 132 } 133 134 public function getSvg(): string 135 { 136 /** @var string icon file */ 137 return DirectoryLayout::getComboImagesDirectory()->resolve('history.svg')->toAbsoluteId(); 138 } 139 140 /** 141 * @param $id 142 * @param $name 143 * @param null $class 144 * @return string 145 */ 146 public function createLink($id, $name, $class = null): string 147 { 148 $page = MarkupPath::createMarkupFromId($id); 149 if ($name == "start") { 150 $name = "Home Page"; 151 } else { 152 $name = $page->getTitleOrDefault(); 153 } 154 155 try { 156 $attributes = LinkMarkup::createFromPageIdOrPath($id)->toAttributes(self::CANONICAL); 157 } catch (ExceptionCompile $e) { 158 return LogUtility::wrapInRedForHtml("Error on breadcrumb markup ref. Message: {$e->getMessage()}"); 159 } 160 if ($class !== null) { 161 $attributes->addClassName($class); 162 } 163 164 return $attributes->toHtmlEnterTag("a") . $name . "</a>"; 165 } 166 167 168} 169