1<?php
2/**
3 * Copyright (c) 2020. 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
16/**
17 * Class TemplateUtility
18 * @package ComboStrap
19 * See also {@link Template template engine} - not finished
20 */
21class TemplateUtility
22{
23
24    const VARIABLE_PREFIX = "$";
25
26    static function renderStringTemplateForPageId($pageTemplate, $pageId)
27    {
28
29
30        $page = Page::createPageFromId($pageId);
31
32
33        return self::renderStringTemplateForDataPage($pageTemplate, $page);
34
35    }
36
37    /**
38     * @param $pageId
39     * @return array|mixed|null
40     * @deprecated 2021-07-02 see {@link Page::getH1()}
41     */
42    public static function getPageH1($pageId)
43    {
44        $h1 = p_get_metadata(cleanID($pageId), Analytics::H1, METADATA_DONT_RENDER);
45        if (empty($h1)) {
46            return self::getPageTitle($pageId);
47        } else {
48            return $h1;
49        }
50    }
51
52    /**
53     * This function is used on a lot of place
54     *
55     * Due to error with the title rendering, we have refactored
56     *
57     * @param $pageId
58     * @return array|mixed|null
59     * @deprecated 2021-07-02 see {@link Page::getTitle()}
60     */
61    public static function getPageTitle($pageId)
62    {
63        $name = $pageId;
64        // The title of the page
65        if (useHeading('navigation')) {
66
67            // $title = $page['title'] can not be used to retrieve the title
68            // because it didn't encode the HTML tag
69            // for instance if <math></math> is used, the output must have &lgt ...
70            // otherwise browser may add quote and the math plugin will not work
71            // May be a solution was just to encode the output
72
73            /**
74             * Bug:
75             * PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 98570240 bytes)
76             * in inc/Cache/CacheInstructions.php on line 44
77             * It was caused by a recursion in the rendering, it seems in {@link p_get_metadata()}
78             * The parameter METADATA_DONT_RENDER stops the recursion
79             *
80             * Don't use the below procedures, they all call the same function and render the meta
81             *   * $name = tpl_pagetitle($pageId, true);
82             *   * $title = p_get_first_heading($page['id']);
83             *
84             * In the <a href="https://www.dokuwiki.org/devel:metadata">documentation</a>,
85             * the rendering mode that they advertised for the title is `METADATA_RENDER_SIMPLE_CACHE`
86             *
87             * We have chosen METADATA_DONT_RENDER (0)
88             * because when we asks for the title of the directory,
89             * it will render the whole tree (the directory of the directory)
90             *
91             *
92             */
93            $name = p_get_metadata(cleanID($pageId), 'title', METADATA_DONT_RENDER);
94
95        }
96        return $name;
97    }
98
99    /**
100     * Replace placeholder
101     * @param Call[] $namespaceTemplateInstructions
102     * @param $pagePath
103     * @return array
104     */
105    public static function renderInstructionsTemplateFromDataPage(array $namespaceTemplateInstructions, $pagePath)
106    {
107        $page = Page::createPageFromQualifiedPath($pagePath);
108        $instructions = [];
109        foreach ($namespaceTemplateInstructions as $call) {
110            $newCall = clone $call;
111            $instructions[] = $newCall->render($page)->toCallArray();
112        }
113        return $instructions;
114
115    }
116
117    /**
118     * @param Call[] $namespaceTemplateInstructions
119     * @param array $array - the data array
120     * @return array - native call stack instructions
121     */
122    public static function renderInstructionsTemplateFromDataArray(array $namespaceTemplateInstructions, array $array): array
123    {
124
125        $instructions = [];
126        foreach ($namespaceTemplateInstructions as $call) {
127            $newCall = clone $call;
128            $instructions[] = $newCall->renderFromData($array)->toCallArray();
129        }
130        return $instructions;
131
132    }
133
134    public static function renderStringTemplateForDataPage($stringTemplate, Page $page)
135    {
136
137
138
139        return TemplateUtility::renderStringTemplateFromDataArray($stringTemplate, TemplateUtility::getMetadataDataFromPage($page));
140
141    }
142
143    /**
144     * Render a template string from a data array
145     * @param $pageTemplate
146     * @param array $array
147     * @return string
148     */
149    public static function renderStringTemplateFromDataArray($pageTemplate, array $array): string
150    {
151
152        $template = Template::create($pageTemplate);
153
154        foreach ($array as $key => $val) {
155            /**
156             * Hack: Replace every " by a ' to be able to detect/parse the title/h1 on a pipeline
157             * @see {@link \syntax_plugin_combo_pipeline}
158             */
159            $val = str_replace('"', "'", $val);
160            $template->set($key, $val);
161        }
162
163        return $template->render();
164
165    }
166
167    public static function getMetadataDataFromPage(Page $page)
168    {
169
170        $array = $page->getMetadataForRendering();
171        /**
172         * @deprecated for path
173         */
174        $array["id"] = $page->getId();
175
176        /**
177         * Override / add the user variable
178         */
179        foreach ($page->getPersistentMetadatas() as $metaKey => $metaValue) {
180
181            $array[$metaKey] = $metaValue;
182
183        }
184        return $array;
185
186    }
187
188
189
190
191}
192