xref: /plugin/combo/action/snippets.php (revision d2ffcff9d34a4eb25389de2cadc56faca90aa841)
1<?php
2
3use ComboStrap\LogUtility;
4use ComboStrap\PluginUtility;
5use ComboStrap\SnippetManager;
6
7if (!defined('DOKU_INC')) die();
8
9/**
10 *
11 *
12 * Add the snippet needed by the components
13 *
14 */
15class action_plugin_combo_snippets extends DokuWiki_Action_Plugin
16{
17
18
19    /**
20     * @var bool - to trace if the header output was called
21     */
22    private $headerOutputWasCalled = false;
23
24    function __construct()
25    {
26        // enable direct access to language strings
27        // ie $this->lang
28        $this->setupLocale();
29    }
30
31    public function register(Doku_Event_Handler $controller)
32    {
33        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'componentSnippetHead', array());
34        $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'componentSnippetContent', array());
35    }
36
37    /**
38     * Dokuwiki has already a canonical methodology
39     * https://www.dokuwiki.org/canonical
40     *
41     * @param $event
42     */
43    function componentSnippetHead($event)
44    {
45
46
47        global $ID;
48        if (empty($ID)) {
49            return;
50        }
51
52        /**
53         * Advertise that the header output was called
54         * If the user is using another template
55         * than strap that does not put the component snippet
56         * in the head
57         * Used in
58         */
59        $this->headerOutputWasCalled = true;
60
61        $snippetManager = PluginUtility::getSnippetManager();
62
63        /**
64         * Because the cache is at the bar level,
65         * a rendering for a page may run without the others
66         * Therefore we saved the data in between
67         * (The storage is done at the page level)
68         * Adapted from {@link p_cached_output()}
69         */
70        $cache = new \dokuwiki\Cache\Cache($ID, "snippet");
71        // note: $cache->useCache()) permits to add dependencies and get a cache results
72        // in case we want to add dependencies on the bar
73        $data = $cache->retrieveCache();
74        global $conf;
75        if ($conf['allowdebug']) {
76            LogUtility::log2file("Snippet cache file {$cache->cache} used", LogUtility::LVL_MSG_DEBUG);
77        }
78        if (!empty($data)) {
79            $storedArray = unserialize($data);
80        } else {
81            $storedArray = array();
82        }
83        $snippetManager->mergeWithPreviousRun($storedArray);
84
85        $cache->storeCache(serialize($snippetManager->getData()));
86
87
88        /**
89         * tags
90         */
91        foreach ($snippetManager->getTags() as $component => $tags) {
92            foreach ($tags as $tagType => $tagRows) {
93                foreach ($tagRows as $tagRow) {
94                    $tagRow["class"] = SnippetManager::getClassFromTag($component);;
95                    $event->data[$tagType][] = $tagRow;
96                }
97            }
98        }
99
100        /**
101         * Css
102         */
103        foreach ($snippetManager->getCss() as $component => $snippet) {
104            $event->data['style'][] = array(
105                "class" => SnippetManager::getClassFromTag($component),
106                "_data" => $snippet
107            );
108        }
109
110        /**
111         * Javascript
112         */
113        foreach ($snippetManager->getJavascript() as $component => $snippet) {
114            $event->data['script'][] = array(
115                "class" => SnippetManager::getClassFromTag($component),
116                "type" => "text/javascript",
117                "_data" => $snippet
118            );
119        }
120
121
122        $snippetManager->close();
123
124    }
125
126    /**
127     * Used if the template does not run the content
128     * before the calling of the header as strap does.
129     *
130     * In this case, the {@link \ComboStrap\SnippetManager::close()} has
131     * not run, and the snippets are still in memory.
132     *
133     * We store them in the HTML and they
134     * follows then the HTML cache of DokuWiki
135     * @param $event
136     */
137    function componentSnippetContent($event)
138    {
139
140
141        /**
142         * Run only if the header output was already called
143         */
144        if ($this->headerOutputWasCalled) {
145
146            $snippetManager = PluginUtility::getSnippetManager();
147
148            /**
149             * tags
150             */
151            foreach ($snippetManager->getTags() as $component => $tags) {
152                foreach ($tags as $tagType => $tagRows) {
153                    foreach ($tagRows as $tagRow) {
154                        $class = SnippetManager::getClassFromTag($component);
155                        $event->data .= "<$tagType class=\"$class\"";
156                        foreach ($tagRow as $attributeName => $attributeValue) {
157                            if ($attributeName != "_data") {
158                                $event->data .= " $attributeName=\"$attributeValue\"";
159                            } else {
160                                $content = $attributeValue;
161                            }
162                        }
163                        $event->data .= ">";
164                        if (!empty($content)) {
165                            $event->data .= $content;
166                        }
167                        $event->data .= "</$tagType>";
168                    }
169                }
170            }
171
172            /**
173             * Css
174             */
175            foreach ($snippetManager->getCss() as $component => $snippet) {
176
177                $class = SnippetManager::getClassFromTag($component);
178                $event->data .= "<style class=\"$class\">$snippet</style>" . DOKU_LF;
179
180            }
181
182            /**
183             * Javascript
184             */
185            foreach ($snippetManager->getJavascript() as $component => $snippet) {
186                $class = SnippetManager::getClassFromTag($component);
187                $event->data .= "<script class=\"$class\" type=\"text/javascript\">$snippet</script>" . DOKU_LF;
188            }
189
190            $snippetManager->close();
191
192            /**
193             * Set the value back
194             */
195            $this->headerOutputWasCalled = false;
196
197        }
198
199    }
200
201    /**
202     * Adapted from {@link p_cached_output()}
203     */
204    function getCachedSnippet()
205    {
206
207        global $ID;
208        $file = wikiFN($ID);
209        $format = "combo_head";
210        global $conf;
211
212
213    }
214
215//    function storeSnippetArray()
216//    {
217//        global $conf;
218//
219//        $cache = new CacheRenderer($ID, $file, $format);
220//        if (!empty($headHtml)) {
221//            if ($cache->storeCache($headHtml)) {              // storeCache() attempts to save cachefile
222//                if ($conf['allowdebug']) {
223//
224//                    LogUtility::log2file("No cache file used, but created {$cache->cache} ");
225//                }
226//            } else {
227//                $cache->removeCache();                     //try to delete cachefile
228//                if ($conf['allowdebug']) {
229//                    LogUtility::log2file("no cachefile used, caching forbidden");
230//                }
231//            }
232//        }
233//    }
234
235
236}
237