xref: /template/writr/tpl_functions.php (revision d9a0be89ec3c62463bd2ef1acfcd2e72d7712b39)
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 * Get the logo of the wiki
16 *
17 * @return string
18 */
19if (!function_exists('tpl_getLogo')) {
20    function tpl_getLogo()
21    {
22        global $ID,$conf;
23
24        $return = '';
25        $logoSize = array();
26        $logoImages = array();
27        if(tpl_getConf('doLogoChangesByNamespace')){
28            $namespace = "";
29            $namespaces = array();
30            foreach(explode(':',getNS($ID)) as $ns){
31                $namespace .= "$ns:";
32                $namespaces[] = $namespace;
33            }
34            foreach(array_reverse($namespaces)  as $namespace){
35                $logoImages[] = ":".trim($namespace,":").":logo.png";
36            }
37        }
38        $logoImages[] = ':logo.png';
39        $logoImages[] = ':wiki:logo.png';
40        $logoImages[] = 'images/logo.png';
41        $logo = tpl_getMediaFile($logoImages, false, $logoSize);
42
43        $return .= '<a class="site-logo"  href="'.wl().'" title="'.$conf['title'].'" rel="home" accesskey="h" title="[H]">';
44        $return .= '<img src="'.$logo.'" '.$logoSize[3].' alt="" class="no-grav header-image" />';
45        $return .= '</a>';
46
47        return $return;
48    }
49}
50
51/**
52 * Generate the gravatar URL for a given email
53 *
54 * @return string
55 */
56if (!function_exists('tpl_getGravatarURL')) {
57    function tpl_getGravatarURL($email, $size = 96)
58    {
59        return 'https://www.gravatar.com/avatar/'.md5(strtolower(trim($email))).'?s='.$size;
60    }
61}
62
63
64/**
65 * Generate the HTML for a menu
66 *
67 * @return string
68 */
69if (!function_exists('tpl_getMenu')) {
70    function tpl_getMenu($menu)
71    {
72        switch($menu){
73            case 'usermenu':
74                return tpl_getUserMenu();
75                break;
76            case 'sidebarmenu':
77                return tpl_getSidebarMenu();
78                break;
79        }
80    }
81}
82
83/**
84 * Generate the HTML for the user menu
85 *
86 * @return string
87 */
88if (!function_exists('tpl_getUserMenu')) {
89    function tpl_getUserMenu()
90    {
91        global $lang,$ID,$conf,$INFO;
92
93        $return = '';
94
95        $items = (new \dokuwiki\Menu\UserMenu())->getItems();
96
97        if(isset($INFO['userinfo'])){
98            $return .= '<div class="dropdown user-tools">';
99                $return .= '<a href="'.wl($ID).'" class="dropdown-toggle" title="'.$lang['user_tools'].'" data-target="#" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">';
100                    $return .= '<i class="bi bi-person"></i>'.$INFO['userinfo']['name'];
101                $return .= '</a>';
102                $return .= '<ul class="dropdown-menu" role="menu">';
103                    $return .= '<li>';
104                        $return .= '<p class="avatar">';
105                            $return .= '<img alt="'.$INFO['userinfo']['name'].'" src="'.tpl_getGravatarURL($INFO['userinfo']['mail']).'" />';
106                        $return .= '</p>';
107                    $return .= '</li>';
108                    foreach($items as $item) {
109                        $return .= '<li>'
110                            .'<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
111                            .'<i></i> '
112                            .$item->getLabel()
113                            .'</a></li>';
114                    }
115                $return .= '</ul>';
116            $return .= '</div>';
117        } else {
118            $return .= '<div class="inline user-tools">';
119            foreach($items as $item) {
120                $return .= '<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
121                    .'<i></i> '
122                    .$item->getLabel()
123                    .'</a>';
124            }
125            $return .= '</div>';
126        }
127
128        return $return;
129    }
130}
131
132/**
133 * Generate the HTML for the sidebar menus
134 *
135 * @return string
136 */
137if (!function_exists('tpl_getSidebarMenu')) {
138    function tpl_getSidebarMenu()
139    {
140        global $lang,$ID,$conf,$INFO;
141
142        $userItems = (new \dokuwiki\Menu\UserMenu())->getItems();
143        $siteItems = (new \dokuwiki\Menu\SiteMenu())->getItems();
144
145        $return = '';
146
147        $return .= '<div id="writr__sidebar__tools">';
148
149        if(isset($INFO['userinfo'])){
150            $return .= '<div class="user-tools">';
151                $return .= '<a href="'.wl($ID).'" class="" title="'.$lang['user_tools'].'" data-target="#user-tools-menu" data-toggle="collapse" role="button" aria-haspopup="true" aria-expanded="false">';
152                    $return .= '<img alt="'.$INFO['userinfo']['name'].'" src="'.tpl_getGravatarURL($INFO['userinfo']['mail'], 32).'" />';
153                    $return .= '<span>'.$INFO['userinfo']['name'].'</span>';
154                $return .= '</a>';
155                $return .= '<div id="user-tools-menu" class="collapsed">';
156                    $return .= '<ul class="menu" role="menu">';
157                        foreach($userItems as $item) {
158                            $return .= '<li>'
159                                .'<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
160                                .'<i></i> '
161                                .$item->getLabel()
162                                .'</a></li>';
163                        }
164                    $return .= '</ul>';
165                $return .= '</div>';
166            $return .= '</div>';
167        } else {
168            $return .= '<div class="user-tools">';
169            foreach($userItems as $item) {
170                $return .= '<a href="'.$item->getLink().'" class="action '.strtolower($item->getType()).'" rel="nofollow" title="'.$item->getTitle().'">'
171                    .'<i></i> '
172                    .$item->getLabel()
173                    .'</a>';
174            }
175            $return .= '</div>';
176        }
177
178        $return .= '</div>';
179
180        return $return;
181    }
182}
183