You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
8.5 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  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. // must be run from within DokuWiki
  11. if (!defined('DOKU_INC')) die();
  12. /**
  13. * Create link/button to discussion page and back
  14. *
  15. * @author Anika Henke <anika@selfthinker.org>
  16. */
  17. function _tpl_discussion($discussionPage, $title, $backTitle, $link=0, $wrapper=0) {
  18. global $ID;
  19. $discussPage = str_replace('@ID@', $ID, $discussionPage);
  20. $discussPageRaw = str_replace('@ID@', '', $discussionPage);
  21. $isDiscussPage = strpos($ID, $discussPageRaw) !== false;
  22. $backID = str_replace($discussPageRaw, '', $ID);
  23. if ($wrapper) echo "<$wrapper>";
  24. if ($isDiscussPage) {
  25. if ($link)
  26. tpl_pagelink($backID, $backTitle);
  27. else
  28. echo html_btn('back2article', $backID, '', array(), 'get', 0, $backTitle);
  29. } else {
  30. if ($link)
  31. tpl_pagelink($discussPage, $title);
  32. else
  33. echo html_btn('discussion', $discussPage, '', array(), 'get', 0, $title);
  34. }
  35. if ($wrapper) echo "</$wrapper>";
  36. }
  37. /**
  38. * Create link/button to user page
  39. *
  40. * @author Anika Henke <anika@selfthinker.org>
  41. */
  42. function _tpl_userpage($userPage, $title, $link=0, $wrapper=0) {
  43. if (empty($_SERVER['REMOTE_USER'])) return;
  44. global $conf;
  45. $userPage = str_replace('@USER@', $_SERVER['REMOTE_USER'], $userPage);
  46. if ($wrapper) echo "<$wrapper>";
  47. if ($link)
  48. tpl_pagelink($userPage, $title);
  49. else
  50. echo html_btn('userpage', $userPage, '', array(), 'get', 0, $title);
  51. if ($wrapper) echo "</$wrapper>";
  52. }
  53. /**
  54. * Wrapper around custom template actions
  55. *
  56. * @author Anika Henke <anika@selfthinker.org>
  57. */
  58. function _tpl_action($type, $link=0, $wrapper=0) {
  59. switch ($type) {
  60. case 'discussion':
  61. if (tpl_getConf('discussionPage')) {
  62. _tpl_discussion(tpl_getConf('discussionPage'), tpl_getLang('discussion'), tpl_getLang('back_to_article'), $link, $wrapper);
  63. }
  64. break;
  65. case 'userpage':
  66. if (tpl_getConf('userPage')) {
  67. _tpl_userpage(tpl_getConf('userPage'), tpl_getLang('userpage'), $link, $wrapper);
  68. }
  69. break;
  70. case 'register': // deprecated
  71. _tpl_register($link, $wrapper);
  72. break;
  73. }
  74. }
  75. /**
  76. * Create event for tools menues
  77. *
  78. * @author Anika Henke <anika@selfthinker.org>
  79. */
  80. function _tpl_toolsevent($toolsname, $items, $view='main') {
  81. $data = array(
  82. 'view' => $view,
  83. 'items' => $items
  84. );
  85. $hook = 'TEMPLATE_'.strtoupper($toolsname).'_DISPLAY';
  86. $evt = new Doku_Event($hook, $data);
  87. if($evt->advise_before()){
  88. foreach($evt->data['items'] as $k => $html) echo $html;
  89. }
  90. $evt->advise_after();
  91. }
  92. /* fallbacks for things missing in older DokuWiki versions
  93. ********************************************************************/
  94. /* if newer settings exist in the core, use them, otherwise fall back to template settings */
  95. if (!isset($conf['tagline'])) {
  96. $conf['tagline'] = tpl_getConf('tagline');
  97. }
  98. if (!isset($conf['sidebar'])) {
  99. $conf['sidebar'] = tpl_getConf('sidebarID');
  100. }
  101. /* these $lang strings are now in the core */
  102. if (!isset($lang['user_tools'])) {
  103. $lang['user_tools'] = tpl_getLang('user_tools');
  104. }
  105. if (!isset($lang['site_tools'])) {
  106. $lang['site_tools'] = tpl_getLang('site_tools');
  107. }
  108. if (!isset($lang['page_tools'])) {
  109. $lang['page_tools'] = tpl_getLang('page_tools');
  110. }
  111. if (!isset($lang['skip_to_content'])) {
  112. $lang['skip_to_content'] = tpl_getLang('skip_to_content');
  113. }
  114. /**
  115. * copied from core (available since Adora Belle)
  116. */
  117. if (!function_exists('tpl_getMediaFile')) {
  118. function tpl_getMediaFile($search, $abs = false, &$imginfo = null) {
  119. $img = '';
  120. $file = '';
  121. $ismedia = false;
  122. // loop through candidates until a match was found:
  123. foreach($search as $img) {
  124. if(substr($img, 0, 1) == ':') {
  125. $file = mediaFN($img);
  126. $ismedia = true;
  127. } else {
  128. $file = tpl_incdir().$img;
  129. $ismedia = false;
  130. }
  131. if(file_exists($file)) break;
  132. }
  133. // fetch image data if requested
  134. if(!is_null($imginfo)) {
  135. $imginfo = getimagesize($file);
  136. }
  137. // build URL
  138. if($ismedia) {
  139. $url = ml($img, '', true, '', $abs);
  140. } else {
  141. $url = tpl_basedir().$img;
  142. if($abs) $url = DOKU_URL.substr($url, strlen(DOKU_REL));
  143. }
  144. return $url;
  145. }
  146. }
  147. /**
  148. * copied from core (available since Angua)
  149. */
  150. if (!function_exists('tpl_favicon')) {
  151. function tpl_favicon($types = array('favicon')) {
  152. $return = '';
  153. foreach($types as $type) {
  154. switch($type) {
  155. case 'favicon':
  156. $look = array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico');
  157. $return .= '<link rel="shortcut icon" href="'.tpl_getMediaFile($look).'" />'.NL;
  158. break;
  159. case 'mobile':
  160. $look = array(':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png');
  161. $return .= '<link rel="apple-touch-icon" href="'.tpl_getMediaFile($look).'" />'.NL;
  162. break;
  163. case 'generic':
  164. // ideal world solution, which doesn't work in any browser yet
  165. $look = array(':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg');
  166. $return .= '<link rel="icon" href="'.tpl_getMediaFile($look).'" type="image/svg+xml" />'.NL;
  167. break;
  168. }
  169. }
  170. return $return;
  171. }
  172. }
  173. /**
  174. * copied from core (available since Adora Belle)
  175. */
  176. if (!function_exists('tpl_includeFile')) {
  177. function tpl_includeFile($file) {
  178. global $config_cascade;
  179. foreach(array('protected', 'local', 'default') as $config_group) {
  180. if(empty($config_cascade['main'][$config_group])) continue;
  181. foreach($config_cascade['main'][$config_group] as $conf_file) {
  182. $dir = dirname($conf_file);
  183. if(file_exists("$dir/$file")) {
  184. include("$dir/$file");
  185. return;
  186. }
  187. }
  188. }
  189. // still here? try the template dir
  190. $file = tpl_incdir().$file;
  191. if(file_exists($file)) {
  192. include($file);
  193. }
  194. }
  195. }
  196. /**
  197. * copied from core (available since Adora Belle)
  198. */
  199. if (!function_exists('tpl_incdir')) {
  200. function tpl_incdir() {
  201. global $conf;
  202. return DOKU_INC.'lib/tpl/'.$conf['template'].'/';
  203. }
  204. }
  205. /**
  206. * copied from core (available since Adora Belle)
  207. */
  208. if (!function_exists('tpl_basedir')) {
  209. function tpl_basedir() {
  210. global $conf;
  211. return DOKU_BASE.'lib/tpl/'.$conf['template'].'/';
  212. }
  213. }
  214. /**
  215. * does *not* emulate the core function, but returns only
  216. * if sidebar is set to make it roughly backwards compatible
  217. * (available since Adora Belle)
  218. */
  219. if (!function_exists('page_findnearest')) {
  220. function page_findnearest($sidebar){
  221. return $sidebar;
  222. }
  223. }
  224. /**
  225. * copied from core (available since Binky)
  226. */
  227. if (!function_exists('tpl_classes')) {
  228. function tpl_classes() {
  229. global $ACT, $conf, $ID, $INFO;
  230. $classes = array(
  231. 'dokuwiki',
  232. 'mode_'.$ACT,
  233. 'tpl_'.$conf['template'],
  234. !empty($_SERVER['REMOTE_USER']) ? 'loggedIn' : '',
  235. $INFO['exists'] ? '' : 'notFound',
  236. ($ID == $conf['start']) ? 'home' : '',
  237. );
  238. return join(' ', $classes);
  239. }
  240. }
  241. /**
  242. * Create link/button to register page
  243. * @deprecated DW versions > 2011-02-20 can use the core function tpl_action('register')
  244. *
  245. * @author Anika Henke <anika@selfthinker.org>
  246. */
  247. function _tpl_register($link=0, $wrapper=0) {
  248. global $conf;
  249. global $lang;
  250. global $ID;
  251. $lang_register = !empty($lang['btn_register']) ? $lang['btn_register'] : $lang['register'];
  252. if (!empty($_SERVER['REMOTE_USER']) || !$conf['useacl'] || !actionOK('register')) return;
  253. if ($wrapper) echo "<$wrapper>";
  254. if ($link)
  255. tpl_link(wl($ID, 'do=register'), $lang_register, 'class="action register" rel="nofollow"');
  256. else
  257. echo html_btn('register', $ID, '', array('do'=>'register'), 'get', 0, $lang_register);
  258. if ($wrapper) echo "</$wrapper>";
  259. }