1<?php
2/**
3 * Template Functions
4 *
5 * This file provides template specific custom functions that are
6 * not provided by the DokuWiki core.
7 * It is common practice to start each function with an underscore
8 * to make sure it won't interfere with future core functions.
9 */
10
11// must be run from within DokuWiki
12if (!defined('DOKU_INC')) die();
13
14use dokuwiki\Extension\Event;
15
16/**
17 * Modified version of tpl_breadcrumbs() that adds Bulma classes
18 */
19function _tpl_breadcrumbs() {
20    $out='';
21    $out .= '<nav class="breadcrumb has-bullet-separator is-small" aria-label="breadcrumbs">';
22    $out .= '<ul>';
23    $out .= '<li class="is-active"><a>Trace</a></li>';
24    $crumbs = breadcrumbs(); //setup crumb trace
25
26    //render crumbs, highlight the last one
27    $last = count($crumbs);
28    $i    = 0;
29    foreach($crumbs as $id => $name) {
30        $i++;
31        if($i == $last) {
32            $out .= '<li class="is-active">';
33        } else {
34            $out .= '<li>';
35        }
36        $out .= tpl_link(wl($id), hsc($name), 'aria-current="page" title="'.$id.'"', true);
37        $out .= '</li>';
38    }
39    $out .= '</ul>';
40    $out .= '</nav>';
41    echo $out;
42}
43
44/**
45 * Modified version of tpl_content() that adds Bulma classes to basic
46 * elements
47 */
48function _tpl_content($prependTOC = false) {
49    global $ACT;
50    global $INFO;
51    $INFO['prependTOC'] = $prependTOC;
52
53    ob_start();
54    Event::createAndTrigger('TPL_ACT_RENDER', $ACT, 'tpl_content_core');
55    $html_output = ob_get_clean();
56    $libxml_errors = libxml_use_internal_errors(true);
57    $dom = new DOMDocument();
58    //$dom->loadHTML($html_output);
59    $dom->loadHTML(mb_convert_encoding($html_output, 'HTML-ENTITIES', 'UTF-8'));
60    libxml_clear_errors();
61    libxml_use_internal_errors($libxml_errors);
62    // add button class to buttons, reset, and submit
63    foreach ($dom->getElementsByTagName('button') as $ele) {
64        $ele->setAttribute('class', 'button is-small');
65    }
66    foreach ($dom->getElementsByTagName('reset') as $ele) {
67        $ele->setAttribute('class', 'button is-small');
68    }
69    foreach ($dom->getElementsByTagName('submit') as $ele) {
70        $ele->setAttribute('class', 'button is-small');
71    }
72    // add select class to selects
73    foreach ($dom->getElementsByTagName('select') as $ele) {
74        // skip a select on the acl page because it looks weird
75        $name = $ele->getAttribute('name');
76        if ($name == 'acl_t') continue;
77        $ele->parentNode->setAttribute('class', 'select is-small');
78    }
79    // add checkbox, radio, and input classes
80    foreach ($dom->getElementsByTagName('input') as $ele) {
81        $type = $ele->getAttribute('type');
82        switch ($type) {
83            case 'checkbox':
84                $label = $ele->parentNode;
85                $label->setAttribute('class', $label->getAttribute('class') . ' checkbox');
86                break;
87            case 'radio':
88                $label = $ele->parentNode;
89                $label->setAttribute('class', $label->getAttribute('class') . ' radio');
90                break;
91            default:
92                $ele->setAttribute('class', $ele->getAttribute('class') . ' input is-small');
93                break;
94        }
95        //if ($type != 'checkbox') {
96        //    $ele->setAttribute('class', $ele->getAttribute('class') . ' input is-small');
97        //} else {
98        //    $label = $ele->parentNode;
99        //    $label->setAttribute('class', $label->getAttribute('class') . ' checkbox');
100        //}
101    }
102    // add proper classes to odered lists
103    foreach ($dom->getElementsByTagName('ol') as $ele) {
104        foreach ($ele->childNodes as $i) {
105            // skip non DOMNode elements
106            if ($i->nodeType != 1) continue;
107            // skip non li elements
108            if ($i->nodeName != 'li') continue;
109            // apply proper class to ordered list based on what level# the
110            // first li element is
111            switch (substr($i->getAttribute('class'), 5, 1)) {
112                case '1':
113                    break;
114                case '2':
115                    $ele->setAttribute('class', 'is-lower-alpha');
116                    break;
117                case '3':
118                    $ele->setAttribute('class', 'is-upper-roman');
119                    break;
120                case '4':
121                    $ele->setAttribute('class', 'is-upper-alpha');
122                    break;
123                default:
124                    $ele->setAttribute('class', 'is-lower-roman');
125                    break;
126            }
127            // break because we only need to evaluate the very first one
128            // to get the level
129            break;
130        }
131    }
132    // add table class to tables
133    foreach ($dom->getElementsByTagName('table') as $ele) {
134        $ele->setAttribute('class', 'table inline is-bordered is-striped is-narrow is-hoverable');
135        // get parent div and add table-container class
136        $div = $ele->parentNode;
137        $classes = $div->getAttribute('class');
138        $div->setAttribute('class', $classes . ' table-container');
139    }
140    // make table headers nicer
141    foreach ($dom->getElementsByTagName('th') as $ele) {
142        $ele->setAttribute('class', 'has-background-white-ter has-text-weight-bold');
143    }
144    foreach ($dom->getElementsByTagName('thead') as $ele) {
145        $ele->setAttribute('class', 'has-background-white-ter has-text-weight-bold');
146    }
147    // add textarea class to textareas
148    foreach ($dom->getElementsByTagName('textarea') as $ele) {
149        $ele->setAttribute('class', $ele->getAttribute('class') .' textarea');
150    }
151    $html_output = $dom->saveHTML($dom->documentElement);
152    Event::createAndTrigger('TPL_CONTENT_DISPLAY', $html_output, 'ptln');
153
154    return !empty($html_output);
155}
156
157/**
158 * Modified version of tpl_page_tools() that adds Bulma classes and custom formatting
159 */
160function _tpl_page_tools() {
161    $items = (new \dokuwiki\Menu\PageMenu())->getItems();
162    $out = '';
163    $out .= '<ul class="page-tools">';
164    foreach ($items as $item) {
165        $out .= '<li> ';
166        $out .= '<a ';
167        $out .= buildAttributes($item->getLinkAttributes('button is-light is-small '));
168        $out .= '>';
169        //$out .= inlineSVG($item->getSvg());
170        $out .= $item->getLabel();
171        $out .= '</a>';
172        $out .= '</li>';
173    }
174    $out .= '</ul>';
175    echo $out;
176}
177
178/**
179 * Custom search form that excludes the button.
180 */
181function _tpl_searchform($ajax = true, $autocomplete = true) {
182    global $lang;
183    global $ACT;
184    global $QUERY;
185    global $ID;
186
187    // don't print the search form if search action has been disabled
188    if(!actionOK('search')) return false;
189
190    $searchForm = new dokuwiki\Form\Form([
191        'action' => wl(),
192        'method' => 'get',
193        'role' => 'search',
194        'class' => 'field has-addons is-horizontal pb-2 search',
195        'id' => 'dokuwiki__search',
196    ], true);
197    $searchForm->setHiddenField('do', 'search');
198    $searchForm->setHiddenField('id', $ID);
199    $searchForm->addTextInput('q')
200        ->addClass('edit input is-small')
201        ->attrs([
202            'title' => '[F]',
203            'accesskey' => 'f',
204            'placeholder' => $lang['btn_search'],
205            'autocomplete' => $autocomplete ? 'on' : 'off',
206        ])
207        ->id('qsearch__in')
208        ->val($ACT === 'search' ? $QUERY : '')
209        ->useInput(false)
210    ;
211    if ($ajax) {
212        $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
213        $searchForm->addTagClose('div');
214    }
215    Event::createAndTrigger('FORM_QUICKSEARCH_OUTPUT', $searchForm);
216
217    echo $searchForm->toHTML();
218
219    return true;
220}
221
222/**
223 * Modified version of tpl_sidebar() that adds Bulma classes and custom formatting
224 */
225function _tpl_sidebar() {
226    $sb = strval(tpl_include_page($conf['sidebar'], false, true));
227    $dom = new DOMDocument();
228    //$dom->loadHTML($sb);
229    $dom->loadHTML(mb_convert_encoding($sb, 'HTML-ENTITIES', 'UTF-8'));
230    $uls = $dom->getElementsByTagName('ul');
231    foreach ($uls as $ul) {
232        $ul->setAttribute('class', 'menu-list');
233    }
234    echo $dom->saveHTML($dom->documentElement);
235}
236
237/**
238 * Modified version of tpl_toc() that adds Bulma classes and custom formatting
239 */
240function _tpl_toc() {
241    $t = strval(tpl_toc(true));
242    $dom = new DOMDocument();
243    //$dom->loadHTML($t);
244    $dom->loadHTML(mb_convert_encoding($t, 'HTML-ENTITIES', 'UTF-8'));
245    $dw__toc = $dom->getElementById('dw__toc');
246    $dw__toc->setAttribute('id', 'bulma-toc');
247    $dw__toc->setAttribute('class', 'is-size-7');
248    $h3s = $dom->getElementsByTagName('h3');
249    foreach ($h3s as $h3) {
250        $h3->setAttribute('class', 'has-text-grey-light');
251        $h3->nodeValue = 'On this page';
252    }
253    $lis = $dom->getElementsByTagName('li');
254    foreach ($lis as $li) {
255        if ($li->getAttribute('class') == 'level2') {
256            $li->setAttribute('class', 'level2 pl-2');
257        }
258        if ($li->getAttribute('class') == 'level3') {
259            $li->setAttribute('class', 'level2 pl-4');
260        }
261    }
262    $as = $dom->getElementsByTagName('a');
263    foreach ($as as $a) {
264        $a->setAttribute('class', 'button is-light is-small');
265    }
266    echo $dom->saveHTML($dom->documentElement);
267}
268
269/**
270 * Modified version of tpl_tools_menu() that adds Bulma classes and custom formatting
271 */
272function _tpl_tools_menu() {
273    $out = '';
274    if (!empty($_SERVER['REMOTE_USER'])) {
275        $out .= '<div class="navbar-item has-dropdown is-hoverable">';
276        $out .= '<a class="navbar-link">Tools</a>';
277        $out .= '<div class="navbar-dropdown">';
278        try{
279            $items = (new \dokuwiki\Menu\SiteMenu())->getItems();
280            foreach ($items as $item) {
281                $out .= $item->asHtmlLink('navbar-item ', false);
282            }
283        } catch(\RuntimeException $ignored) {
284            // item not available
285        }
286        $out .= '</div>';
287        $out .= '</div>';
288    }
289    echo $out;
290}
291
292/**
293 * Modified version of tpl_user_menu() that adds Bulma classes and custom formatting
294 */
295function _tpl_user_menu() {
296    $out = '';
297    if (!empty($_SERVER['REMOTE_USER'])) {
298        $out .= '<div class="navbar-item has-dropdown is-hoverable">';
299        $out .= '<a class="navbar-link">Account</a>';
300        $out .= '<div class="navbar-dropdown">';
301        try{
302            $out .= (new \dokuwiki\Menu\Item\Admin())->asHtmlLink('navbar-item ', false);
303        } catch(\RuntimeException $ignored) {
304            // item not available
305        }
306        try{
307            $out .= (new \dokuwiki\Menu\Item\Register())->asHtmlLink('navbar-item ', false);
308        } catch(\RuntimeException $ignored) {
309            // item not available
310        }
311        $out .= '<hr class="navbar-divider">';
312        $out .= (new \dokuwiki\Menu\Item\Login())->asHtmlLink('navbar-item ', false);
313        $out .= '</div>';
314        $out .= '</div>';
315    }
316    else {
317        $out .= (new \dokuwiki\Menu\Item\Login())->asHtmlLink('navbar-item ', false);
318    }
319    echo $out;
320}
321