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 (empty($_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 * Wrapper around custom template actions
67 *
68 * @author Anika Henke <anika@selfthinker.org>
69 */
70function _tpl_action($type, $link=0, $wrapper=0) {
71    switch ($type) {
72        case 'discussion':
73            if (tpl_getConf('discussionPage')) {
74                _tpl_discussion(tpl_getConf('discussionPage'), tpl_getLang('discussion'), tpl_getLang('back_to_article'), $link, $wrapper);
75            }
76            break;
77        case 'userpage':
78            if (tpl_getConf('userPage')) {
79                _tpl_userpage(tpl_getConf('userPage'), tpl_getLang('userpage'), $link, $wrapper);
80            }
81            break;
82    }
83}
84
85/**
86 * Create event for tools menues
87 *
88 * @author Anika Henke <anika@selfthinker.org>
89 */
90function _tpl_toolsevent($toolsname, $items, $view='main') {
91    $data = array(
92        'view'  => $view,
93        'items' => $items
94    );
95
96    $hook = 'TEMPLATE_'.strtoupper($toolsname).'_DISPLAY';
97    $evt = new Doku_Event($hook, $data);
98    if($evt->advise_before()){
99        foreach($evt->data['items'] as $k => $html) echo $html;
100    }
101    $evt->advise_after();
102}
103
104/**
105 * copied from core (available since Binky)
106 */
107if (!function_exists('tpl_classes')) {
108    function tpl_classes() {
109        global $ACT, $conf, $ID, $INFO;
110        $classes = array(
111            'dokuwiki',
112            'mode_'.$ACT,
113            'tpl_'.$conf['template'],
114            !empty($_SERVER['REMOTE_USER']) ? 'loggedIn' : '',
115            $INFO['exists'] ? '' : 'notFound',
116            ($ID == $conf['start']) ? 'home' : '',
117        );
118        return join(' ', $classes);
119    }
120}
121