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, $return=0) {
20    global $ID;
21    $output = '';
22
23    $discussPage    = str_replace('@ID@', $ID, $discussionPage);
24    $discussPageRaw = str_replace('@ID@', '', $discussionPage);
25    $isDiscussPage  = strpos($ID, $discussPageRaw) !== false;
26    $backID         = ':'.str_replace($discussPageRaw, '', $ID);
27
28    if ($wrapper) $output .= "<$wrapper>";
29
30    if ($isDiscussPage) {
31        if ($link) {
32            ob_start();
33            tpl_pagelink($backID, $backTitle);
34            $output .= ob_get_contents();
35            ob_end_clean();
36        } else {
37            $output .= html_btn('back2article', $backID, '', array(), 'get', 0, $backTitle);
38        }
39    } else {
40        if ($link) {
41            ob_start();
42            tpl_pagelink($discussPage, $title);
43            $output .= ob_get_contents();
44            ob_end_clean();
45        } else {
46            $output .= html_btn('discussion', $discussPage, '', array(), 'get', 0, $title);
47        }
48    }
49
50    if ($wrapper) $output .= "</$wrapper>";
51    if ($return) return $output;
52    echo $output;
53}
54
55/**
56 * Create link/button to user page
57 *
58 * @author Anika Henke <anika@selfthinker.org>
59 */
60function _tpl_userpage($userPage, $title, $link=0, $wrapper=0, $return=0) {
61    if (empty($_SERVER['REMOTE_USER'])) return;
62
63    global $conf;
64    $output = '';
65    $userPage = str_replace('@USER@', $_SERVER['REMOTE_USER'], $userPage);
66
67    if ($wrapper) $output .= "<$wrapper>";
68
69    if ($link) {
70        ob_start();
71        tpl_pagelink($userPage, $title);
72        $output .= ob_get_contents();
73        ob_end_clean();
74    } else {
75        $output .= html_btn('userpage', $userPage, '', array(), 'get', 0, $title);
76    }
77
78    if ($wrapper) $output .= "</$wrapper>";
79    if ($return) return $output;
80    echo $output;
81}
82
83/**
84 * Wrapper around custom template actions
85 *
86 * @author Anika Henke <anika@selfthinker.org>
87 */
88function _tpl_action($type, $link=0, $wrapper=0, $return=0) {
89    switch ($type) {
90        case 'discussion':
91            if (tpl_getConf('discussionPage')) {
92                $output = _tpl_discussion(tpl_getConf('discussionPage'), tpl_getLang('discussion'), tpl_getLang('back_to_article'), $link, $wrapper, 1);
93                if ($return) return $output;
94                echo $output;
95            }
96            break;
97        case 'userpage':
98            if (tpl_getConf('userPage')) {
99                $output = _tpl_userpage(tpl_getConf('userPage'), tpl_getLang('userpage'), $link, $wrapper, 1);
100                if ($return) return $output;
101                echo $output;
102            }
103            break;
104    }
105}
106
107/**
108 * copied to core (available since Detritus)
109 */
110if (!function_exists('tpl_toolsevent')) {
111    function tpl_toolsevent($toolsname, $items, $view='main') {
112        $data = array(
113            'view'  => $view,
114            'items' => $items
115        );
116
117        $hook = 'TEMPLATE_'.strtoupper($toolsname).'_DISPLAY';
118        $evt = new Doku_Event($hook, $data);
119        if($evt->advise_before()){
120            foreach($evt->data['items'] as $k => $html) echo $html;
121        }
122        $evt->advise_after();
123    }
124}
125
126/**
127 * copied from core (available since Binky)
128 */
129if (!function_exists('tpl_classes')) {
130    function tpl_classes() {
131        global $ACT, $conf, $ID, $INFO;
132        $classes = array(
133            'dokuwiki',
134            'mode_'.$ACT,
135            'tpl_'.$conf['template'],
136            !empty($_SERVER['REMOTE_USER']) ? 'loggedIn' : '',
137            $INFO['exists'] ? '' : 'notFound',
138            ($ID == $conf['start']) ? 'home' : '',
139        );
140        return join(' ', $classes);
141    }
142}
143