1<?php
2/**
3 * Provide navigation sidebar functionality to Dokuwiki Templates
4 *
5 * @author Christopher Smith <chris@jalakai.co.uk>
6 * @author Esther Brunner <wikidesign@gmail.com>
7 */
8
9/**
10 * Recursive function to establish best sidebar file to be used
11 */
12function getSidebarFN($ns, $file){
13
14	// check for wiki page = $ns:$file (or $file where no namespace)
15	$nsFile = ($ns) ? "$ns:$file" : $file;
16	if (@file_exists(wikiFN($nsFile)) && auth_quickaclcheck($nsFile)) return $nsFile;
17
18  // remove deepest namespace level and call function recursively
19
20	// no namespace left, exit with no file found
21	if (!$ns) return '';
22
23	$i = strrpos($ns, ":");
24	$ns = ($i) ? substr($ns, 0, $i) : false;
25	return getSidebarFN($ns, $file);
26}
27
28/**
29 * Display the sidebar
30 */
31function tpl_sidebar(){
32	global $ID, $REV, $conf;
33
34	// save globals
35	$saveID = $ID;
36	$saveREV = $REV;
37
38	// discover file to be displayed in navigation sidebar
39  $fileSidebar = getSidebarFN(getNS($ID), tpl_getConf('sidebar_page'));
40
41	// determine what to display
42	if ($fileSidebar){
43		$ID = $fileSidebar;
44		$REV = '';
45		print p_wiki_xhtml($ID,$REV,false);
46	} else {
47    global $IDX;
48    html_index($IDX);
49	}
50
51	// restore globals
52	$ID = $saveID;
53	$REV = $saveREV;
54}
55
56/**
57 * Return the correct ID for <div class="dokuwiki">
58 */
59function tpl_classID(){
60  echo 'minima__'.tpl_getConf('width').'_'.tpl_getConf('sidebar_position');
61}
62
63/**
64 * Checks if the color scheme has changed
65 */
66function tpl_checkColor(){
67  $color = tpl_getConf('color');
68  $file  = DOKU_TPLINC.'style.ini';
69  $file2 = DOKU_TPLINC.'style_'.$color.'.ini';
70  $ini   = parse_ini_file($file);
71
72  if ($ini['__theme__'] != '_'.$color){
73    if ((@file_exists($file2)) && (@unlink($file)) && (@copy($file2, $file))){
74      global $conf;
75      if ($conf['fperm']) chmod($file, $conf['fperm']);
76    } else {
77      msg('Could not set correct style.ini file for your chosen color scheme.', -1);
78    }
79  }
80}
81
82/**
83 * Display tabs for easy navigation
84 */
85function tpl_tabs(){
86  global $ID;
87
88  $out = '';
89  $ns = getNS($ID);
90  $tabsFile = wikiFN(($ns).':'.tpl_getConf('tabs_page'));
91  if ((@file_exists($tabsFile)) && (auth_quickaclcheck($tabs))){
92    $ins = p_cached_instructions($tabsFile);
93
94    foreach ($ins as $in){
95      if ($in[0] == 'internallink'){
96        list($id, $hash) = explode('#', $in[1][0], 2);
97        resolve_pageid(getNS($ID), $id, $exists);
98        if (getNS($id) != $ns) continue; // ignore links to other namespaces
99        if (!$exists) continue;          // ignore links to non-existent pages
100
101        // determine link title
102        $title = hsc($in[1][1]);
103        if (!$title) $title = hsc(p_get_first_heading($id));
104        if (!$title) $title = hsc(ucwords(noNS($id)));
105
106        // now construct the output link
107        if ($id == $ID) $out .= '<span class="activetab">'.$title.'</span> ';
108        else $out .= '<a href="'.wl($id).'" class="tab">'.$title.'</a> ';
109      }
110    }
111    echo '<div class="tabs">'.$out.'</div>';
112  }
113}
114