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.

274 lines
7.9 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 (!$_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. * Create link/button to register page
  55. * @deprecated DW versions > 2011-02-20 can use the core function tpl_action('register')
  56. *
  57. * @author Anika Henke <anika@selfthinker.org>
  58. */
  59. function _tpl_register($link=0, $wrapper=0) {
  60. global $conf;
  61. global $lang;
  62. global $ID;
  63. $lang_register = !empty($lang['btn_register']) ? $lang['btn_register'] : $lang['register'];
  64. if ($_SERVER['REMOTE_USER'] || !$conf['useacl'] || !actionOK('register')) return;
  65. if ($wrapper) echo "<$wrapper>";
  66. if ($link)
  67. tpl_link(wl($ID, 'do=register'), $lang_register, 'class="action register" rel="nofollow"');
  68. else
  69. echo html_btn('register', $ID, '', array('do'=>'register'), 'get', 0, $lang_register);
  70. if ($wrapper) echo "</$wrapper>";
  71. }
  72. /**
  73. * Wrapper around custom template actions
  74. *
  75. * @author Anika Henke <anika@selfthinker.org>
  76. */
  77. function _tpl_action($type, $link=0, $wrapper=0) {
  78. switch ($type) {
  79. case 'discussion':
  80. if (tpl_getConf('discussionPage')) {
  81. _tpl_discussion(tpl_getConf('discussionPage'), tpl_getLang('discussion'), tpl_getLang('back_to_article'), $link, $wrapper);
  82. }
  83. break;
  84. case 'userpage':
  85. if (tpl_getConf('userPage')) {
  86. _tpl_userpage(tpl_getConf('userPage'), tpl_getLang('userpage'), $link, $wrapper);
  87. }
  88. break;
  89. case 'register': // deprecated
  90. _tpl_register($link, $wrapper);
  91. break;
  92. }
  93. }
  94. /* fallbacks for things missing in older DokuWiki versions
  95. ********************************************************************/
  96. /* if newer settings exist in the core, use them, otherwise fall back to template settings */
  97. if (!isset($conf['tagline'])) {
  98. $conf['tagline'] = tpl_getConf('tagline');
  99. }
  100. if (!isset($conf['sidebar'])) {
  101. $conf['sidebar'] = tpl_getConf('sidebarID');
  102. }
  103. /* these $lang strings are now in the core */
  104. if (!isset($lang['user_tools'])) {
  105. $lang['user_tools'] = tpl_getLang('user_tools');
  106. }
  107. if (!isset($lang['site_tools'])) {
  108. $lang['site_tools'] = tpl_getLang('site_tools');
  109. }
  110. if (!isset($lang['page_tools'])) {
  111. $lang['page_tools'] = tpl_getLang('page_tools');
  112. }
  113. if (!isset($lang['skip_to_content'])) {
  114. $lang['skip_to_content'] = tpl_getLang('skip_to_content');
  115. }
  116. /**
  117. * copied from core (available since Adora Belle)
  118. */
  119. if (!function_exists('tpl_getMediaFile')) {
  120. function tpl_getMediaFile($search, $abs = false, &$imginfo = null) {
  121. $img = '';
  122. $file = '';
  123. $ismedia = false;
  124. // loop through candidates until a match was found:
  125. foreach($search as $img) {
  126. if(substr($img, 0, 1) == ':') {
  127. $file = mediaFN($img);
  128. $ismedia = true;
  129. } else {
  130. $file = tpl_incdir().$img;
  131. $ismedia = false;
  132. }
  133. if(file_exists($file)) break;
  134. }
  135. // fetch image data if requested
  136. if(!is_null($imginfo)) {
  137. $imginfo = getimagesize($file);
  138. }
  139. // build URL
  140. if($ismedia) {
  141. $url = ml($img, '', true, '', $abs);
  142. } else {
  143. $url = tpl_basedir().$img;
  144. if($abs) $url = DOKU_URL.substr($url, strlen(DOKU_REL));
  145. }
  146. return $url;
  147. }
  148. }
  149. /**
  150. * copied from core (available since Angua)
  151. */
  152. if (!function_exists('tpl_favicon')) {
  153. function tpl_favicon($types = array('favicon')) {
  154. $return = '';
  155. foreach($types as $type) {
  156. switch($type) {
  157. case 'favicon':
  158. $look = array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico');
  159. $return .= '<link rel="shortcut icon" href="'.tpl_getMediaFile($look).'" />'.NL;
  160. break;
  161. case 'mobile':
  162. $look = array(':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png');
  163. $return .= '<link rel="apple-touch-icon" href="'.tpl_getMediaFile($look).'" />'.NL;
  164. break;
  165. case 'generic':
  166. // ideal world solution, which doesn't work in any browser yet
  167. $look = array(':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg');
  168. $return .= '<link rel="icon" href="'.tpl_getMediaFile($look).'" type="image/svg+xml" />'.NL;
  169. break;
  170. }
  171. }
  172. return $return;
  173. }
  174. }
  175. /**
  176. * copied from core (available since Adora Belle)
  177. */
  178. if (!function_exists('tpl_includeFile')) {
  179. function tpl_includeFile($file) {
  180. global $config_cascade;
  181. foreach(array('protected', 'local', 'default') as $config_group) {
  182. if(empty($config_cascade['main'][$config_group])) continue;
  183. foreach($config_cascade['main'][$config_group] as $conf_file) {
  184. $dir = dirname($conf_file);
  185. if(file_exists("$dir/$file")) {
  186. include("$dir/$file");
  187. return;
  188. }
  189. }
  190. }
  191. // still here? try the template dir
  192. $file = tpl_incdir().$file;
  193. if(file_exists($file)) {
  194. include($file);
  195. }
  196. }
  197. }
  198. /**
  199. * copied from core (available since Adora Belle)
  200. */
  201. if (!function_exists('tpl_incdir')) {
  202. function tpl_incdir() {
  203. global $conf;
  204. return DOKU_INC.'lib/tpl/'.$conf['template'].'/';
  205. }
  206. }
  207. /**
  208. * does *not* emulate the core function, but returns only
  209. * if sidebar is set to make it roughly backwards compatible
  210. * (available since Adora Belle)
  211. */
  212. if (!function_exists('page_findnearest')) {
  213. function page_findnearest($sidebar){
  214. return $sidebar;
  215. }
  216. }
  217. /**
  218. * copied from core (will be available in autumn 2013 release)
  219. */
  220. if (!function_exists('tpl_classes')) {
  221. function tpl_classes() {
  222. global $ACT, $conf, $ID, $INFO;
  223. $classes = array(
  224. 'dokuwiki',
  225. 'mode_'.$ACT,
  226. 'tpl_'.$conf['template'],
  227. $_SERVER['REMOTE_USER'] ? 'loggedIn' : '',
  228. $INFO['exists'] ? '' : 'notFound',
  229. ($ID == $conf['start']) ? 'home' : '',
  230. );
  231. return join(' ', $classes);
  232. }
  233. }