xref: /plugin/combo/ComboStrap/FetcherAppPages.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1<?php
2
3namespace ComboStrap;
4
5
6use ComboStrap\Meta\Field\PageTemplateName;
7use ComboStrap\Web\Url;
8use ComboStrap\Web\UrlEndpoint;
9use dokuwiki\Action\Exception\FatalException;
10use dokuwiki\ActionRouter;
11
12/**
13 * No Cache for the idenity forms
14 * as if there is a cache problems,
15 * We can't login anymore for instance
16 */
17class FetcherAppPages extends IFetcherAbs implements IFetcherString
18{
19
20    const NAME = "page-app";
21    const CANONICAL = "page-app";
22
23    use FetcherTraitWikiPath;
24
25    private string $requestedLayout;
26    private bool $build = false;
27
28
29    private TemplateForWebPage $pageTemplate;
30
31
32    /**
33     * @param Url|null $url
34     * @return Url
35     *
36     * Note: The fetch url is the {@link FetcherCache keyCache}
37     */
38    function getFetchUrl(Url $url = null): Url
39    {
40        /**
41         * Overwrite default fetcher endpoint
42         * that is {@link UrlEndpoint::createFetchUrl()}
43         */
44        $url = UrlEndpoint::createDokuUrl();
45        try {
46            $url->addQueryParameter(PageTemplateName::PROPERTY_NAME, $this->getRequestedLayout());
47        } catch (ExceptionNotFound $e) {
48            // no requested layout
49        }
50        return parent::getFetchUrl($url)
51            ->addQueryParameter("do", self::NAME);
52    }
53
54
55    /**
56     * @return string
57     */
58    public function getFetchString(): string
59    {
60
61        $contextPath = $this->getSourcePath();
62        if($contextPath->hasRevision()) {
63            /**
64             * In the diff {@link ExecutionContext::DIFF_ACTION},
65             * the `rev` property is passed in the URL and we get a bad context
66             */
67            $contextPath = WikiPath::createMarkupPathFromId($contextPath->getWikiId());
68        }
69
70
71        if (!$this->build) {
72
73            $this->build = true;
74            $pageLang = Site::getLangObject();
75            $title = $this->getLabel();
76
77            $this->pageTemplate = TemplateForWebPage::create()
78                ->setRequestedTemplateName($this->getRequestedTemplateOrDefault())
79                ->setRequestedLang($pageLang)
80                ->setRequestedEnableTaskRunner(false) // no page id
81                ->setRequestedTitle($title)
82                ->setRequestedContextPath($contextPath);
83
84        }
85
86
87        /**
88         * The content
89         *
90         * Adapted from {@link tpl_content()}
91         *
92         * As this is only for identifier forms,
93         * the buffer should not be a problem
94         *
95         * Because all admin action are using the php buffer
96         * We can then have an overflow
97         */
98        global $ACT;
99        ob_start();
100        $actionName = FetcherAppPages::class . "::tpl_content_core";
101        \dokuwiki\Extension\Event::createAndTrigger('TPL_ACT_RENDER', $ACT, $actionName);
102        $mainHtml = ob_get_clean();
103
104
105        /**
106         * Add css
107         */
108        switch ($ACT) {
109            case ExecutionContext::PREVIEW_ACTION:
110            case ExecutionContext::EDIT_ACTION:
111                $markupPath = MarkupPath::createPageFromPathObject($contextPath);
112                if ($ACT === ExecutionContext::PREVIEW_ACTION && $markupPath->isSlot()) {
113                    try {
114                        SlotSystem::sendContextPathMessage(SlotSystem::getContextPath());
115                    } catch (ExceptionNotFound $e) {
116                        // no history (ie no cookie or new installation)
117                    }
118                }
119        }
120
121
122        /**
123         * Generate the whole html page via the layout
124         */
125        return $this->pageTemplate
126            ->setMainContent($mainHtml)
127            ->render();
128
129    }
130
131
132    /**
133     *
134     */
135    function getBuster(): string
136    {
137        return "";
138    }
139
140    public function getMime(): Mime
141    {
142        return Mime::create(Mime::HTML);
143    }
144
145    public function getFetcherName(): string
146    {
147        return self::NAME;
148    }
149
150    /**
151     * We take over the {@link tpl_content_core()} of Dokuwiki
152     * because the instance of the router is not reinit.
153     * We get then problem on test because of the private global static {@link ActionRouter::$instance) variable
154     * @return bool
155     * @noinspection PhpUnused - is a callback to the event TPL_ACT_RENDER called in this class
156     */
157    static public function tpl_content_core(): bool
158    {
159
160        /**
161         * Was false, is true
162         */
163        $router = ActionRouter::getInstance(true);
164        try {
165            $router->getAction()->tplContent();
166        } catch (FatalException $e) {
167            // there was no content for the action
168            msg(hsc($e->getMessage()), -1);
169            return false;
170        }
171        return true;
172    }
173
174    /**
175     * @throws ExceptionNotFound
176     */
177    private function getRequestedLayout(): string
178    {
179        if (!isset($this->requestedLayout)) {
180            throw new ExceptionNotFound("No requested layout");
181        }
182        return $this->requestedLayout;
183    }
184
185
186    /**
187     *
188     */
189    public function close(): FetcherAppPages
190    {
191        // nothing to do
192        return $this;
193    }
194
195
196    private function getRequestedTemplateOrDefault(): string
197    {
198        try {
199            return $this->getRequestedLayout();
200        } catch (ExceptionNotFound $e) {
201            global $ACT;
202            switch ($ACT) {
203                case ExecutionContext::EDIT_ACTION:
204                case ExecutionContext::PREVIEW_ACTION:
205                    return PageTemplateName::APP_EDIT;
206                case ExecutionContext::LOGIN_ACTION:
207                    return PageTemplateName::APP_LOGIN;
208                case ExecutionContext::REGISTER_ACTION:
209                    return PageTemplateName::APP_REGISTER;
210                case ExecutionContext::RESEND_PWD_ACTION:
211                    return PageTemplateName::APP_RESEND_PWD;
212                case ExecutionContext::REVISIONS_ACTION:
213                    return PageTemplateName::APP_REVISIONS;
214                case ExecutionContext::DIFF_ACTION:
215                    return PageTemplateName::APP_DIFF;
216                case ExecutionContext::INDEX_ACTION:
217                    return PageTemplateName::APP_INDEX;
218                case ExecutionContext::PROFILE_ACTION:
219                    return PageTemplateName::APP_PROFILE;
220                default:
221                case ExecutionContext::SEARCH_ACTION:
222                    return PageTemplateName::APP_SEARCH;
223            }
224
225        }
226    }
227
228
229    public function getLabel(): string
230    {
231        global $ACT;
232        $label = "App Pages";
233        switch ($ACT) {
234            case ExecutionContext::RESEND_PWD_ACTION:
235                $label = "Resend Password";
236                break;
237            case ExecutionContext::LOGIN_ACTION:
238                $label = "Login";
239                break;
240            case ExecutionContext::REGISTER_ACTION:
241                $label = "Register";
242                break;
243            case ExecutionContext::EDIT_ACTION:
244            case ExecutionContext::PREVIEW_ACTION:
245                $label = "Editor";
246                break;
247            case ExecutionContext::DIFF_ACTION:
248                $label = "Diff";
249                break;
250            case ExecutionContext::REVISIONS_ACTION:
251                $label = "Log";
252                break;
253        }
254        return $label;
255    }
256
257
258    /**
259     * @throws ExceptionBadArgument
260     * @throws ExceptionBadSyntax
261     * @throws ExceptionNotExists
262     * @throws ExceptionNotFound
263     */
264    public function buildFromTagAttributes(TagAttributes $tagAttributes): IFetcher
265    {
266        parent::buildFromTagAttributes($tagAttributes);
267        $this->buildOriginalPathFromTagAttributes($tagAttributes);
268        return $this;
269    }
270}
271