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/**
16 * Create link/button to user page
17 *
18 * @author Anika Henke <anika@selfthinker.org>
19 */
20function _tpl_userpage($userPage, $title, $link=0, $wrapper=0) {
21    if (!$_SERVER['REMOTE_USER']) return;
22
23    global $conf;
24    $userPage = str_replace('@USER@', $_SERVER['REMOTE_USER'], $userPage);
25
26    if ($wrapper) echo "<$wrapper>";
27
28    if ($link)
29        tpl_pagelink($userPage, $title);
30    else
31        echo html_btn('userpage', $userPage, '', array(), 'get', 0, $title);
32
33    if ($wrapper) echo "</$wrapper>";
34}
35
36
37/**
38 * Wrapper around custom template actions
39 *
40 * @author Anika Henke <anika@selfthinker.org>
41 */
42function _tpl_action($type, $link=0, $wrapper=0) {
43    switch ($type) {
44        case 'userpage':
45            if (tpl_getConf('userPage')) {
46                _tpl_userpage(tpl_getConf('userPage'), tpl_getLang('userpage'), $link, $wrapper);
47            }
48            break;
49    }
50}
51
52
53/* dokubootstrap-yeti related functions
54********************************************************************/
55
56function _tpl_toc_to_twitter_bootstrap_event_hander_dump_level($data, $firstlevel=false) {
57
58
59    if (count($data) == 0) {
60        return '';
61    }
62
63	//dw($data);
64    $out = '<div class="bs-sidebar" role="navigation">';
65    //$out .= '<ul class="nav nav-pills nav-stacked affix">';
66    $out .= '<ul class="nav">';
67
68    $li_open = false;
69	$level = $data[0]['level'];
70
71    //Only supports top level links for now.
72    foreach($data as $heading) {
73
74		// link or reference?
75		isset($heading['hid']) ? $href = '#'.$heading['hid'] : $href = $heading['link'];
76
77		if ($heading['level'] == $level) {
78
79			// Close previous open li.
80			if($li_open) {
81				 $out .= '</li>';
82				 $li_open = false;
83			}else{
84				 $out .= '';
85			}
86
87        	$out .= '<li><a href="' . $href . '">'. $heading['title'] . '</a>';
88        	$li_open = true;
89
90		}else if($heading['level'] > $level) {
91        	$out .= '<ul class="nav">';
92			$out .= '<li><a href="' . $href . '">'. $heading['title'] . '</a>';
93			$li_open = true;
94
95		}else if($heading['level'] < $level) {
96
97			// Close previous open li.
98			if($li_open) {
99				for ($i=0; $i < $level - $heading['level'] - 1; ++$i) {
100					$out .= '</li>';
101					$out .= '</ul>';
102				}
103				$out .= '</li>';
104				$li_open = false;
105
106			}else{
107				 $out .= '';
108			}
109
110			$out .= '</ul>';
111			$out .= '<li><a href="' . $href . '">'. $heading['title'] . '</a>';
112		}
113
114		$level = $heading['level'];
115    }
116
117	// Close previous open li.
118    if($li_open) {
119    	$out .= '</li>';
120    }
121
122    $out .= '</ul>';
123    $out .= '</div>';
124
125    return $out;
126}
127
128function _tpl_toc_to_twitter_bootstrap_event_hander(&$event, $param)
129{
130    global $conf;
131    //This is tied to the specific format of the DokuWiki TOC.
132    echo _tpl_toc_to_twitter_bootstrap_event_hander_dump_level($event->data, true);
133}
134
135function _tpl_toc_to_twitter_bootstrap()
136{
137    //Force generation of TOC, request that the TOC is returned as HTML, but then ignore the returned string. The hook will instead dump out the TOC.
138    global $EVENT_HANDLER;
139	$EVENT_HANDLER->register_hook('TPL_TOC_RENDER', 'AFTER', NULL, '_tpl_toc_to_twitter_bootstrap_event_hander');
140
141	tpl_toc(true);
142}
143
144
145function _tpl_output_page_tools($showTools = true, $element = 'li'){
146    global $lang;
147
148    if ($showTools) {
149
150			echo '<ul class="dropdown-menu">';
151
152            tpl_action('edit', 1, $element);
153            tpl_action('revisions', 1, $element);
154            tpl_action('backlink', 1, $element);
155            tpl_action('subscribe', 1, $element);
156            tpl_action('revert', $textonly, $element);
157
158			echo '<li class="divider"></li>';
159        	tpl_action('recent', 1, 'li');
160        	tpl_action('media', 1, 'li');
161        	tpl_action('index', 1, 'li');
162
163			echo '</ul>';
164    }
165}
166
167function _tpl_output_search_bar()
168{
169    //Modified from the official tpl_searchform function.
170    global $lang;
171    global $ACT;
172    global $QUERY;
173
174    // don't print the search form if search action has been disabled
175    if(!actionOk('search')) return false;
176
177    print '<form action="'.wl().'" accept-charset="utf-8" class="search" id="dw__search" method="get"><div class="no">';
178    print '<input type="hidden" name="do" value="search" />';
179    print '<input class="" type="text" placeholder="'.$lang['btn_search'].'" ';
180    if($ACT == 'search') print 'value="'.htmlspecialchars($QUERY).'" ';
181    if(!$autocomplete) print 'autocomplete="off" ';
182    print 'id="qsearch__in" accesskey="f" name="id" class="edit" title="[F]" />';
183
184    print '<button type="submit" value="" class="btn btn-default" title="'.$lang['btn_search'].'">';
185    print '<i class="glyphicon glyphicon-search"></i></button>';
186
187    if($ajax) print '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
188    print '</div></form>';
189    return true;
190
191}
192
193/**
194 * Define how the user related content is shown.
195 * When not logged in, login / register is shown
196 * When logged in the user's name is printed with a dropdown of user related options
197 *
198 * @author Paul in 't Hout <badeendjuh@email.com>
199 **/
200
201function _tpl_userinfo($element='li') {
202	global $INFO;
203
204 	if(isset($_SERVER['REMOTE_USER'])) {
205      	echo '<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">'.hsc($INFO['userinfo']['name']).'<b class="caret"></b></a>';
206 		echo '<ul class="dropdown-menu">';
207		tpl_action('admin', 1, $element);
208 		tpl_action('profile', 1, $element);
209		tpl_action('login', 1, $element);
210		echo '</ul>';
211 		echo '</li>';
212	}else{
213		tpl_action('login', 1, $element, 0, '', '', 'Login / Register');
214	}
215}
216
217// debug web
218function dw($message) {
219        print "<pre>";
220        if (is_array($message)) {
221                print_r($message);
222        }else{
223                print $message;
224        }
225        print "<pre>";
226}
227
228
229
230