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
14/**
15 * Create link/button to discussion page and back
16 *
17 * @author Anika Henke <anika@selfthinker.org>
18 */
19function _tpl_discussion($discussionPage, $title, $backTitle, $link=0, $wrapper=0) {
20    global $ID;
21
22    $discussPage    = str_replace('@ID@', $ID, $discussionPage);
23    $discussPageRaw = str_replace('@ID@', '', $discussionPage);
24    $isDiscussPage  = strpos($ID, $discussPageRaw) !== false;
25    $backID         = ':'.str_replace($discussPageRaw, '', $ID);
26
27    if ($wrapper) echo "<$wrapper>";
28
29    if ($isDiscussPage) {
30        if ($link)
31            tpl_pagelink($backID, $backTitle);
32        else
33            echo html_btn('back2article', $backID, '', array(), 'get', 0, $backTitle);
34    } else {
35        if ($link)
36            tpl_pagelink($discussPage, $title);
37        else
38            echo html_btn('discussion', $discussPage, '', array(), 'get', 0, $title);
39    }
40
41    if ($wrapper) echo "</$wrapper>";
42}
43
44/**
45 * Create link/button to user page
46 *
47 * @author Anika Henke <anika@selfthinker.org>
48 */
49function _tpl_userpage($userPage, $title, $link=0, $wrapper=0) {
50    if (empty($_SERVER['REMOTE_USER'])) return;
51
52    global $conf;
53    $userPage = str_replace('@USER@', $_SERVER['REMOTE_USER'], $userPage);
54
55    if ($wrapper) echo "<$wrapper>";
56
57    if ($link)
58        tpl_pagelink($userPage, $title);
59    else
60        echo html_btn('userpage', $userPage, '', array(), 'get', 0, $title);
61
62    if ($wrapper) echo "</$wrapper>";
63}
64
65/**
66 * Wrapper around custom template actions
67 *
68 * @author Anika Henke <anika@selfthinker.org>
69 */
70function _tpl_action($type, $link=0, $wrapper=0) {
71    switch ($type) {
72        case 'discussion':
73            if (tpl_getConf('discussionPage')) {
74                _tpl_discussion(tpl_getConf('discussionPage'), tpl_getLang('discussion'), tpl_getLang('back_to_article'), $link, $wrapper);
75            }
76            break;
77        case 'userpage':
78            if (tpl_getConf('userPage')) {
79                _tpl_userpage(tpl_getConf('userPage'), tpl_getLang('userpage'), $link, $wrapper);
80            }
81            break;
82    }
83}
84
85/**
86 * Create event for tools menues
87 *
88 * @author Anika Henke <anika@selfthinker.org>
89 */
90function _tpl_toolsevent($toolsname, $items, $view='main') {
91    $data = array(
92        'view'  => $view,
93        'items' => $items
94    );
95
96    $hook = 'TEMPLATE_'.strtoupper($toolsname).'_DISPLAY';
97    $evt = new Doku_Event($hook, $data);
98    if($evt->advise_before()){
99        foreach($evt->data['items'] as $k => $html) echo $html;
100    }
101    $evt->advise_after();
102}
103
104/**
105 * copied from core (available since Binky)
106 */
107if (!function_exists('tpl_classes')) {
108    function tpl_classes() {
109        global $ACT, $conf, $ID, $INFO;
110        $classes = array(
111            'dokuwiki',
112            'mode_'.$ACT,
113            'tpl_'.$conf['template'],
114            !empty($_SERVER['REMOTE_USER']) ? 'loggedIn' : '',
115            $INFO['exists'] ? '' : 'notFound',
116            ($ID == $conf['start']) ? 'home' : '',
117        );
118        return join(' ', $classes);
119    }
120}
121
122/**
123 * Special Functions for this template
124 *
125 * @author Florian Wehner <florian@whnr.de>
126 *
127 */
128
129// If output exists, put the content of $before and $after around it
130function _tpl_check_if_output_and_amend($before="", $tpl_function="", $after="") {
131    ob_start();
132    eval($tpl_function.";");
133    $function_output = ob_get_clean();
134
135    if(strlen($function_output)==0) return false;
136
137    print $before;
138    print $function_output;
139    print $after;
140    return true;
141}
142
143// Copied and adjusted from core for navbar search form
144function _tpl_draw_searchform($ajax = true, $autocomplete = true) {
145    global $lang;
146    global $ACT;
147    global $QUERY;
148
149    // don't print the search form if search action has been disabled
150    if(!actionOK('search')) return false;
151
152    print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search" method="get" role="search"><div class="field">';
153    print '<input type="hidden" name="do" value="search" />';
154    print '<input type="search" ';
155    if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" ';
156    if(!$autocomplete) print 'autocomplete="off" ';
157    print 'id="qsearch__in" accesskey="f" name="id" class="search input" title="[F]" placeholder="'.$lang['btn_search'].'"/>';
158    if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
159    print '</div></form>';
160    return true;
161}
162