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 * @author Andreas Gohr <andi@splitbrain.org>
11 * @author Anika Henke <anika@selfthinker.org>
12 * @author Klaus Vormweg <klaus.vormweg@gmx.de>
13 *
14 */
15
16// must be run from within DokuWiki
17if (!defined('DOKU_INC')) die();
18
19/**
20 * prints the menu
21 *
22 * @param void
23 * @return void
24*/
25function _tpl_mainmenu() {
26  require_once(DOKU_INC.'inc/search.php');
27  global $conf;
28
29  /* options for search() function */
30  $opts = array(
31   'depth' => 0,
32   'listfiles' => true,
33   'listdirs'  => true,
34   'pagesonly' => true,
35   'firsthead' => true,
36   'sneakyacl' => true
37  );
38
39  if(isset($conf['start'])) {
40    $start = $conf['start'];
41  } else {
42    $start = 'start';
43  }
44
45  $data = array();
46  search($data,$conf['datadir'],'search_universal',$opts);
47  $i = 0;
48  $data2 = array();
49  foreach($data as $item) {
50    if(strpos($item['id'],'playground') !== false) {
51      continue;
52    }
53    if(isset($conf['sidebar'])) {
54      if($conf['sidebar']) {
55        if(strpos($item['id'], $conf['sidebar']) !== false) {
56          continue;
57        }
58      }
59    }
60    ## unset()!!!
61    if($item['id'] == $start or preg_match('/:'.$start.'$/',$item['id'])
62       or preg_match('/(\w+):\1$/',$item['id'])) {
63      continue;
64    }
65    if(array_key_exists($item['id'], $data2)) {
66      $data2[$item['id']]['type'] = 'd';
67      $data2[$item['id']]['ns'] = $item['id'];
68      continue;
69    }
70    $data2[$item['id']] = $item;
71    $i++;
72  }
73  usort($data2,"_tpl_sort_index");
74  echo html_buildlist($data2,'idx','_tpl_list_index','_tpl_html_li_index');
75}
76
77/**
78 * Index item formatter
79 * Callback function for html_buildlist()
80 *
81 * @param array $item
82 * @return string html
83*/
84function _tpl_list_index($item) {
85  global $conf;
86  $ret = '';
87  if($item['type'] == 'd') {
88    if(@file_exists(wikiFN($item['id'].':'.$conf['start']))) {
89      $ret .= html_wikilink($item['id'].':'.$conf['start'], $item['title']);
90    } elseif(@file_exists(wikiFN($item['id'].':'.$item['id']))) {
91      $ret .= html_wikilink($item['id'].':'.$item['id'], $item['title']);
92    } else {
93      $ret .= html_wikilink($item['id'].':', $conf['start']);
94    }
95  } else {
96    $ret .= html_wikilink(':'.$item['id'], $item['title']);
97  }
98  return $ret;
99}
100
101/**
102 * Index List item
103 *
104 * Callback function for html_buildlist to build the
105 * <li> tags for namespaces when displaying the page index
106 *
107 * @param array $item
108 * @return string html
109 */
110function _tpl_html_li_index($item) {
111  global $INFO;
112
113  $class = '';
114  $id = '';
115
116  if($item['type'] == "f") {
117    return '<li class="level'.$item['level'].$class.'" '.$id.'>';
118  } else {
119    return '<li class="closed level'.$item['level'].$class.'">';
120  }
121}
122
123/**
124 * Returns <link> tag for various icon types (favicon|mobile)
125 *
126 * @param  array $types - list of icon types to display (favicon|mobile)
127 * @return string
128 */
129function _tpl_favicon($types = array('favicon','mobile')) {
130
131  $return = '';
132
133  $typearr = array();
134  foreach($types as $type) {
135    switch($type) {
136      case 'favicon':
137        $typearr['shortcut icon'] = 'favicon.ico';
138      case 'mobile':
139        $typearr['apple-touch-icon'] = 'apple-touch-icon.png';
140    }
141  }
142  foreach($typearr as $type => $fname) {
143    $look = array(':wiki:'.$fname, ':'.$fname, 'images/'.$fname);
144    $i = 0;
145    while($look[$i] and strpos($look[$i],'images') === FALSE) {
146      if(!auth_quickaclcheck($look[$i])) {
147        unset($look[$i]);
148      }
149      $i++;
150    }
151    $return .= '<link rel="'.$type.'" href="'.tpl_getMediaFile($look).'" />'.NL;
152  }
153  return $return;
154}
155
156
157/**
158 * Checks if a media file is readable by the current user
159 *
160 * @param string $id
161 * @return bool
162*/
163function _tpl_media_isreadable($id) {
164  $id = cleanID($id);
165  if(auth_quickaclcheck($id)) {
166    return true;
167  } else {
168    return false;
169  }
170}
171
172/**
173 * Callback function for usort
174*/
175function _tpl_sort_index($a, $b) {
176  return strcmp($a['id'],$b['id']);
177}
178