xref: /template/sprintdoc/Template.php (revision e302d67fb601311046a86f685e4d745f7d8a7e3b)
1<?php
2
3namespace dokuwiki\template\sprintdoc;
4
5/**
6 * Class Template
7 *
8 * provides additional logic for the sprintdoc template
9 *
10 * @package dokuwiki\template\sprintdoc
11 */
12class Template {
13
14    /**
15     * @var array loaded plugins
16     */
17    protected $plugins = array(
18        'sqlite' => null,
19        'tagging' => null,
20        'magicmatcher' => null,
21    );
22
23    /**
24     * Get the singleton instance
25     *
26     * @return Template
27     */
28    public static function getInstance() {
29        static $instance = null;
30        if($instance === null) $instance = new Template();
31        return $instance;
32    }
33
34    /**
35     * Template constructor.
36     */
37    protected function __construct() {
38        $this->initializePlugins();
39
40        /** @var \Doku_Event_Handler */
41        global $EVENT_HANDLER;
42        $EVENT_HANDLER->register_hook('PLUGIN_TPLINC_LOCATIONS_SET', 'BEFORE', $this, 'registerIncludes');
43    }
44
45    /**
46     * Makes include position info available to the tplinc plugin
47     *
48     * @param \Doku_Event $event
49     */
50    public function registerIncludes(\Doku_Event $event) {
51        $event->data['footer'] = 'Footer below the page content';
52        $event->data['sidebarfooter'] = 'Footer below the sidebar';
53        $event->data['sidebarheader'] = 'Header above the sidebar';
54    }
55
56    /**
57     * Get the content to include from the tplinc plugin
58     *
59     * prefix and postfix are only added when there actually is any content
60     *
61     * @param string $location
62     * @param string $pre prepend this before the content
63     * @param string $post append this to the content
64     * @return string
65     */
66    public function getInclude($location, $pre = '', $post = '') {
67        if(!$this->plugins['tplinc']) return '';
68        $content = $this->plugins['tplinc']->renderIncludes($location);
69        if($content === '') return '';
70        return $pre . $content . $post;
71    }
72
73    /**
74     * Load all the plugins we support directly
75     */
76    protected function initializePlugins() {
77        $this->plugins['sqlite'] = plugin_load('helper', 'sqlite');
78        if($this->plugins['sqlite']) {
79            $this->plugins['tagging'] = plugin_load('helper', 'tagging');
80            $this->plugins['magicmatcher'] = plugin_load('syntax', 'magicmatcher_issuelist');
81        }
82        $this->plugins['tplinc'] = plugin_load('helper', 'tplinc');
83    }
84
85    /**
86     * Get all the tabs to display
87     *
88     * @return array
89     */
90    public function getMetaBoxTabs() {
91        global $lang;
92        $tabs = array();
93
94        $toc = tpl_toc(true);
95        if($toc) {
96            $tabs[] = array(
97                'id' => 'spr__tab-toc',
98                'label' => $lang['toc'],
99                'tab' => $toc,
100                'count' => null,
101            );
102        }
103
104        if($this->plugins['tagging']) {
105            $tabs[] = array(
106                'id' => 'spr__tab-tags',
107                'label' => tpl_getLang('tab_tags'),
108                'tab' => $this->plugins['tagging']->tpl_tags(false),
109                'count' => null, // FIXME
110            );
111        }
112
113        if ($this->plugins['magicmatcher']) {
114            $tabs[] = array(
115                'id' => 'spr__tab-issues',
116                'label' => tpl_getLang('tab_issues'),
117                'tab' => $this->plugins['magicmatcher']->getIssueListHTML(),
118                'count' =>  $this->plugins['magicmatcher']->getCountIssues(),
119            );
120        }
121
122        return $tabs;
123    }
124
125    /**
126     * Creates an image tag and includes the first found image correctly resized
127     *
128     * @param string $tag
129     * @param array $attributes
130     * @param int $w
131     * @param int $h
132     * @return string
133     */
134    public static function getResizedImgTag($tag, $attributes, $w, $h) {
135        $attr = '';
136        $medias = array();
137
138        // the attribute having an array defines where the image goes
139        foreach($attributes as $attribute => $data) {
140            if(is_array($data)) {
141                $medias = $data;
142                $attr = $attribute;
143            }
144        }
145        // if the image attribute could not be found return
146        if(!$attr || !$medias) return '';
147
148        // try all medias until an existing one is found
149        $media = '';
150        foreach($medias as $media) {
151            if(file_exists(mediaFN($media))) break;
152            $media = '';
153        }
154        if($media === '') return '';
155
156        // replace the array
157        $media = ml($media, array('w' => $w, 'h' => $h, 'crop' => 1), true, '&');
158        $attributes[$attr] = $media;
159
160        // return the full tag
161        return '<' . $tag . ' ' . buildAttributes($attributes) . ' />' . "\n";
162    }
163
164    /**
165     * Embed the main logo
166     *
167     * Tries a few different locations
168     */
169    public function mainLogo() {
170        global $conf;
171
172        // homepage logo should not link to itself (BITV accessibility requirement)
173        $linkit = (strcmp(wl(), $_SERVER['REQUEST_URI']) !== 0);
174        if($linkit) {
175            $title = $conf['title'] . tpl_getLang('adjunct_linked_logo_text');
176        } else {
177            $title = tpl_getLang('adjunct_start_logo_text') . $conf['title'];
178        }
179
180        $desktop = self::getResizedImgTag(
181            'img',
182            array(
183                'class' => 'mobile-hide',
184                'src' => array('wiki:logo-wide.png', 'wiki:logo.png'),
185                'alt' => $title,
186            ),
187            0, 0
188        );
189        $mobile = self::getResizedImgTag(
190            'img',
191            array(
192                'class' => 'mobile-only',
193                'src' => array('wiki:logo-32x32.png', 'wiki:favicon.png', 'wiki:logo-square.png', 'wiki:logo.png'),
194                'alt' => $title,
195            ),
196            32, 32
197        );
198
199        // homepage logo should not link to itself (BITV accessibility requirement)
200        if($linkit) {
201            tpl_link(wl(), $desktop, 'accesskey="h" title="[H]"');
202            tpl_link(wl(), $mobile, 'accesskey="h" title="[H]"');
203        } else {
204            echo $desktop;
205            echo $mobile;
206        }
207    }
208}
209