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
15use ComboStrap\TagAttribute\StyleAttribute;
16use ComboStrap\Web\UrlEndpoint;
17use dokuwiki\Menu\Item\AbstractItem;
18
19/**
20 * Class MenuItem
21 * @package ComboStrap
22 *
23 */
24class SlotManagerMenuItem extends AbstractItem
25{
26
27    const CANONICAL = "slot:manager";
28    const TAG = "slot-manager";
29
30    const EDIT_ACTION = "Edit";
31    const CREATE_ACTION = "Create";
32
33    private static function getClass(): string
34    {
35        return StyleAttribute::addComboStrapSuffix(self::TAG);
36    }
37
38
39    /**
40     *
41     * @return string
42     */
43    public function getLabel(): string
44    {
45        return "Manage the slots";
46    }
47
48    public function getLinkAttributes($classprefix = 'menuitem '): array
49    {
50
51        $snippetManager = PluginUtility::getSnippetManager()->addPopoverLibrary();
52        $snippetManager->attachJavascriptFromComponentId(self::TAG);
53
54        $linkAttributes = parent::getLinkAttributes($classprefix);
55        /**
56         * A class and not an id
57         * because a menu item can be found twice on
58         * a page (For instance if you want to display it in a layout at a
59         * breakpoint and at another in another breakpoint
60         */
61        $linkAttributes['class'] = self::getClass();
62        $linkAttributes['href'] = "#";
63        $dataAttributeNamespace = Bootstrap::getDataNamespace();
64        $linkAttributes["data{$dataAttributeNamespace}-toggle"] = "popover";
65        $linkAttributes["data{$dataAttributeNamespace}-html"] = "true";
66        $linkAttributes["data{$dataAttributeNamespace}-title"] = "Slot Manager";
67
68        /**
69         * TODO: right when rtl language
70         */
71        $linkAttributes["data{$dataAttributeNamespace}-placement"] = "left";
72
73        // encoding happens
74        $linkAttributes["data{$dataAttributeNamespace}-content"] = $this->createHtml();
75
76
77        // Dismiss on next click
78        // To debug, just comment this line
79        $linkAttributes["data{$dataAttributeNamespace}-trigger"] = "focus";
80
81        // See for the tabindex
82        // https://getbootstrap.com/docs/5.1/components/popovers/#dismiss-on-next-click
83        $linkAttributes['tabindex'] = "0";
84
85        return $linkAttributes;
86
87
88    }
89
90    public function getTitle(): string
91    {
92        return "Slot Manager";
93    }
94
95    public function getSvg(): string
96    {
97        /** @var string icon file */
98        return DirectoryLayout::getComboImagesDirectory()->resolve('entypo-text-document-inverted.svg')->toAbsoluteId();
99    }
100
101    public function createHtml(): string
102    {
103        $requestedPath = WikiPath::getContextPath();
104        $url = UrlEndpoint::createComboStrapUrl()->setPath("/" . self::TAG);
105        $html = "<p>Edit and/or create the <a href=\"{$url->toHtmlString()}\">slots</a> of the page</p>";
106        foreach (SlotSystem::getSlotNames() as $secondarySlot) {
107
108            $label = $secondarySlot;
109            switch ($secondarySlot) {
110                case SlotSystem::getSidebarName():
111                    $label = "Page Sidebar";
112                    break;
113                case SlotSystem::getMainHeaderSlotName():
114                    $label = "Content Header";
115                    break;
116                case SlotSystem::getMainFooterSlotName():
117                    $label = "Content Footer";
118                    break;
119                case SlotSystem::getPageFooterSlotName():
120                    $label = "Page Footer";
121                    break;
122                case SlotSystem::getPageHeaderSlotName():
123                    $label = "Page Header";
124                    break;
125            }
126            $html .= "<p class='mb-0 mt-1'><strong>$label</strong></p>";
127            $html .= "<table>";
128
129            $parentPath = $requestedPath;
130            while (true) {
131                try {
132                    $parentPath = $parentPath->getParent();
133                } catch (ExceptionNotFound $e) {
134                    break;
135                }
136                $secondaryPath = $parentPath->resolveId($secondarySlot);
137
138                $secondaryPage = MarkupPath::createPageFromAbsoluteId($secondaryPath->toAbsoluteId());
139                $class = StyleAttribute::addComboStrapSuffix(\syntax_plugin_combo_link::TAG);
140                if (FileSystems::exists($secondaryPath)) {
141                    $action = self::EDIT_ACTION;
142                    $style = '';
143                } else {
144                    $action = self::CREATE_ACTION;
145                    $style = ' style="color:rgba(0,0,0,0.65)"';
146                }
147                $url = UrlEndpoint::createDokuUrl()
148                    ->addQueryParameter(DokuwikiId::DOKUWIKI_ID_ATTRIBUTE, $secondaryPage->getWikiId())
149                    ->addQueryParameter("do", "edit");
150
151                $html .= "<tr><td class='pe-2'>$action</td><td><a href=\"{$url->toHtmlString()}\" class=\"$class\"$style>{$secondaryPath->toAbsoluteId()}</a></td></tr>";
152
153                if ($action === self::EDIT_ACTION) {
154                    break;
155                }
156
157            }
158            $html .= "</table>";
159
160        };
161
162
163        return $html;
164
165    }
166
167
168}
169