1<?php
2
3namespace ComboStrap;
4
5class SlotSystem
6{
7
8    const CANONICAL = "slot-system";
9
10    /**
11     * A slot should never run in its own path as context path
12     * This function returns the context path in which see should run
13     * (ie the last visited path)
14     *
15     * This context path is given globally via the {@link ExecutionContext::getContextPath()}
16     * when the {@link MarkupPath::isSlot() page is a slot}
17     * @throws ExceptionNotFound
18     */
19    public static function getContextPath(): MarkupPath
20    {
21        $crumbs = $_SESSION[DOKU_COOKIE]['bc'] ?? array();
22        if (empty($crumbs)) {
23            throw new ExceptionNotFound("No historical crumbs");
24        }
25        $size = sizeof($crumbs);
26        $visitedIds = array_keys($crumbs);
27        for ($i = $size - 1; $i > 0; $i--) {
28            $id = $visitedIds[$i];
29            $markupPath = MarkupPath::createMarkupFromId($id);
30            if (!$markupPath->isSlot()) {
31                return $markupPath;
32            }
33        }
34        throw new ExceptionNotFound("No historical crumbs");
35    }
36
37    public static function sendContextPathMessage(MarkupPath $contextPath): void
38    {
39        try {
40            $anchorLink = LinkMarkup::createFromPageIdOrPath($contextPath->getWikiId())
41                    ->toAttributes()
42                    ->toHtmlEnterTag("a")
43                . $contextPath->getTitleOrDefault()
44                . "</a>";
45        } catch (ExceptionBadArgument|ExceptionNotFound $e) {
46            LogUtility::internalError("Should not happen");
47            $anchorLink = $contextPath->getTitleOrDefault();
48        }
49        $docLink = PluginUtility::getDocumentationHyperLink("slot", "slot");
50
51        $html = <<<EOF
52  <p>This page is a $docLink.</p>
53   <p>It returns information as if it was run within the last visited path: $anchorLink</p>
54EOF;
55        LogUtility::warning($html, self::CANONICAL);
56
57    }
58
59    public static function getSlotNames(): array
60    {
61
62
63        return [
64
65            self::getPageHeaderSlotName(),
66            self::getSidebarName(),
67            self::getPageFooterSlotName(),
68            self::getMainHeaderSlotName(),
69            self::getMainFooterSlotName()
70        ];
71
72
73    }
74
75    /**
76     *
77     */
78    public static function getMainHeaderSlotName(): ?string
79    {
80        return Site::SLOT_MAIN_HEADER_NAME;
81    }
82
83    /**
84     */
85    public static function getPageFooterSlotName()
86    {
87        return ExecutionContext::getActualOrCreateFromEnv()->getConfValue(TemplateSlot::CONF_PAGE_FOOTER_NAME, TemplateSlot::CONF_PAGE_FOOTER_NAME_DEFAULT);
88    }
89
90    /**
91     *
92     */
93    public static function getPageHeaderSlotName()
94    {
95        return ExecutionContext::getActualOrCreateFromEnv()->getConfig()->getPageHeaderSlotName();
96    }
97
98    /**
99     * @deprecated
100     */
101    public static function getMainSideSlotName()
102    {
103        return ExecutionContext::getActualOrCreateFromEnv()->getConfValue(TemplateSlot::CONF_PAGE_MAIN_SIDEKICK_NAME, TemplateSlot::CONF_PAGE_MAIN_SIDEKICK_NAME_DEFAULT);
104    }
105
106    /**
107     *
108     */
109    public static function getMainFooterSlotName(): string
110    {
111        return Site::SLOT_MAIN_FOOTER_NAME;
112    }
113
114    /**
115     * @return string - the name of the sidebar page
116     */
117    public static function getSidebarName(): string
118    {
119        global $conf;
120        return $conf["sidebar"];
121    }
122
123    public static function isMainHeaderSlot(Path $sourcePath): bool
124    {
125        try {
126            $nameWithoutExtension = $sourcePath->getLastNameWithoutExtension();
127        } catch (ExceptionNotFound $e) {
128            return false;
129        }
130        $mainHeaderSlotName = self::getMainHeaderSlotName();
131        return $nameWithoutExtension === $mainHeaderSlotName;
132    }
133}
134