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 * Create link/button to discussion page and back
16 *
17 * @author Anika Henke <anika@selfthinker.org>
18 */
19function _tpl_discussion($discussionPage,$title,$backTitle,$link=0,$wrapper=0) {
20    global $ID;
21
22    $discussPage    = str_replace('@ID@',$ID,$discussionPage);
23    $discussPageRaw = str_replace('@ID@','',$discussionPage);
24    $isDiscussPage  = strpos($ID,$discussPageRaw)!==false;
25    $backID         = str_replace($discussPageRaw,'',$ID);
26
27    if ($wrapper) echo "<$wrapper>";
28
29    if ($isDiscussPage) {
30        if ($link)
31            tpl_pagelink($backID,$backTitle);
32        else
33            echo html_btn('back2article',$backID,'',array(),'get',0,$backTitle);
34    } else {
35        if ($link)
36            tpl_pagelink($discussPage,$title);
37        else
38            echo html_btn('discussion',$discussPage,'',array(),'get',0,$title);
39    }
40
41    if ($wrapper) echo "</$wrapper>";
42}
43
44/**
45 * Create link/button to user page
46 *
47 * @author Anika Henke <anika@selfthinker.org>
48 */
49function _tpl_userpage($userPage,$title,$link=0,$wrapper=0) {
50    if (!$_SERVER['REMOTE_USER']) return;
51
52    global $conf;
53    $userPage = str_replace('@USER@',$_SERVER['REMOTE_USER'],$userPage);
54
55    if ($wrapper) echo "<$wrapper>";
56
57    if ($link)
58        tpl_pagelink($userPage,$title);
59    else
60        echo html_btn('userpage',$userPage,'',array(),'get',0,$title);
61
62    if ($wrapper) echo "</$wrapper>";
63}
64
65/**
66 * Create link/button to register page
67 * DW versions > 2011-02-20 can use the core function tpl_action('register')
68 *
69 * @author Anika Henke <anika@selfthinker.org>
70 */
71function _tpl_register($link=0,$wrapper=0) {
72    global $conf;
73    global $lang;
74    global $ID;
75    $lang_register = !empty($lang['btn_register']) ? $lang['btn_register'] : $lang['register'];
76
77    if ($_SERVER['REMOTE_USER'] || !$conf['useacl'] || !actionOK('register')) return;
78
79    if ($wrapper) echo "<$wrapper>";
80
81    if ($link)
82        tpl_link(wl($ID,'do=register'),$lang_register,'class="action register" rel="nofollow"');
83    else
84        echo html_btn('register',$ID,'',array('do'=>'register'),'get',0,$lang_register);
85
86    if ($wrapper) echo "</$wrapper>";
87}
88
89/**
90 * Wrapper around custom template actions
91 *
92 * @author Anika Henke <anika@selfthinker.org>
93 */
94function _tpl_action($type,$link=0,$wrapper=0) {
95    switch ($type) {
96        case 'discussion':
97            if (tpl_getConf('discussionPage')) {
98                _tpl_discussion(tpl_getConf('discussionPage'),tpl_getLang('discussion'),tpl_getLang('back_to_article'),$link,$wrapper);
99            }
100            break;
101        case 'userpage':
102            if (tpl_getConf('userPage')) {
103                _tpl_userpage(tpl_getConf('userPage'),tpl_getLang('userpage'),$link,$wrapper);
104            }
105            break;
106        case 'register':
107            _tpl_register($link,$wrapper);
108            break;
109    }
110}
111
112/**
113 * Returns icon from data/media root directory if it exists, otherwise
114 * the one in the template's image directory.
115 *
116 * @param  bool $abs        - if to use absolute URL
117 * @param  string $fileName - file name of icon
118 * @author Anika Henke <anika@selfthinker.org>
119 */
120function _tpl_getFavicon($abs=false, $fileName='favicon.ico') {
121    if (file_exists(mediaFN($fileName))) {
122        return ml($fileName, '', true, '', $abs);
123    }
124
125    if($abs) {
126        return DOKU_URL.substr(DOKU_TPL.'images/'.$fileName, strlen(DOKU_REL));
127    }
128    return DOKU_TPL.'images/'.$fileName;
129}
130
131/* use core function if available, otherwise the custom one */
132if (!function_exists('tpl_getFavicon')) {
133    function tpl_getFavicon($abs=false, $fileName='favicon.ico'){
134        _tpl_getFavicon($abs, $fileName);
135    }
136}
137
138
139/**
140 * Returns <link> tag for various icon types (favicon|mobile|generic)
141 *
142 * @param  array $types - list of icon types to display (favicon|mobile|generic)
143 * @author Anika Henke <anika@selfthinker.org>
144 */
145function _tpl_favicon($types=array('favicon')) {
146
147    $return = '';
148
149    foreach ($types as $type) {
150        switch($type) {
151            case 'favicon':
152                $return .= '<link rel="shortcut icon" href="'.tpl_getFavicon().'" />'.NL;
153                break;
154            case 'mobile':
155                $return .= '<link rel="apple-touch-icon" href="'.tpl_getFavicon(false, 'apple-touch-icon.png').'" />'.NL;
156                break;
157            case 'generic':
158                // ideal world solution, which doesn't work in any browser yet
159                $return .= '<link rel="icon" href="'.tpl_getFavicon(false, 'icon.svg').'" type="image/svg+xml" />'.NL;
160                break;
161        }
162    }
163
164    return $return;
165}
166
167/* use core function if available, otherwise the custom one */
168if (!function_exists('tpl_favicon')) {
169    function tpl_favicon($types=array('favicon')){
170        _tpl_favicon($types);
171    }
172}
173
174
175/**
176 * Include additional html file from conf directory if it exists, otherwise use
177 * file in the template's root directory.
178 *
179 * @author Anika Henke <anika@selfthinker.org>
180 */
181function _tpl_include($fn) {
182    $confFile = DOKU_CONF.$fn;
183    $tplFile  = dirname(__FILE__).'/'.$fn;
184
185    if (file_exists($confFile))
186        include($confFile);
187    else if (file_exists($tplFile))
188        include($tplFile);
189}
190