xref: /template/sprintdoc/Template.php (revision a21c6f8785ac4967f21ffe25bb28652e3383f951)
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    }
53
54    /**
55     * Get the content to include from the tplinc plugin
56     *
57     * prefix and postfix are only added when there actually is any content
58     *
59     * @param string $location
60     * @param string $pre prepend this before the content
61     * @param string $post append this to the content
62     * @return string
63     */
64    public function getInclude($location, $pre = '', $post = '') {
65        if(!$this->plugins['tplinc']) return '';
66        $content = $this->plugins['tplinc']->renderIncludes($location);
67        if($content === '') return '';
68        return $pre . $content . $post;
69    }
70
71    /**
72     * Load all the plugins we support directly
73     */
74    protected function initializePlugins() {
75        $this->plugins['sqlite'] = plugin_load('helper', 'sqlite');
76        if($this->plugins['sqlite']) {
77            $this->plugins['tagging'] = plugin_load('helper', 'tagging');
78            $this->plugins['magicmatcher'] = plugin_load('syntax', 'magicmatcher_issuelist');
79        }
80        $this->plugins['tplinc'] = plugin_load('helper', 'tplinc');
81    }
82
83    /**
84     * Get all the tabs to display
85     *
86     * @return array
87     */
88    public function getMetaBoxTabs() {
89        global $lang;
90        $tabs = array();
91
92        $toc = tpl_toc(true);
93        if($toc) {
94            $tabs[] = array(
95                'id' => 'spr__tab-toc',
96                'label' => $lang['toc'],
97                'tab' => $toc,
98                'count' => null,
99            );
100        }
101
102        if($this->plugins['tagging']) {
103            $tabs[] = array(
104                'id' => 'spr__tab-tags',
105                'label' => tpl_getLang('tab_tags'),
106                'tab' => $this->plugins['tagging']->tpl_tags(false),
107                'count' => null, // FIXME
108            );
109        }
110
111        if ($this->plugins['magicmatcher']) {
112            $tabs[] = array(
113                'id' => 'spr__tab-issues',
114                'label' => tpl_getLang('tab_issues'),
115                'tab' => $this->plugins['magicmatcher']->getIssueListHTML(),
116                'count' =>  $this->plugins['magicmatcher']->getCountIssues(),
117            );
118        }
119
120        return $tabs;
121    }
122
123    /**
124     * Creates an image tag and includes the first found image correctly resized
125     *
126     * @param string $tag
127     * @param array $attributes
128     * @param int $w
129     * @param int $h
130     * @return string
131     */
132    public static function getResizedImgTag($tag, $attributes, $w, $h) {
133        $attr = '';
134        $medias = array();
135
136        // the attribute having an array defines where the image goes
137        foreach($attributes as $attribute => $data) {
138            if(is_array($data)) {
139                $medias = $data;
140                $attr = $attribute;
141            }
142        }
143        // if the image attribute could not be found return
144        if(!$attr || !$medias) return '';
145
146        // try all medias until an existing one is found
147        $media = '';
148        foreach($medias as $media) {
149            if(file_exists(mediaFN($media))) break;
150            $media = '';
151        }
152        if($media === '') return '';
153
154        // replace the array
155        $media = ml($media, array('w' => $w, 'h' => $h, 'crop' => 1), true, '&');
156        $attributes[$attr] = $media;
157
158        // return the full tag
159        return '<' . $tag . ' ' . buildAttributes($attributes) . ' />' . "\n";
160    }
161
162    /**
163     * Embed the main logo
164     *
165     * Tries a few different locations
166     */
167    public function mainLogo() {
168        global $conf;
169
170        // homepage logo should not link to itself (BITV accessibility requirement)
171        $linkit = (strcmp(wl(), $_SERVER['REQUEST_URI']) !== 0);
172        if($linkit) {
173            $title = $conf['title'] . tpl_getLang('adjunct_linked_logo_text');
174        } else {
175            $title = tpl_getLang('adjunct_start_logo_text') . $conf['title'];
176        }
177
178        $desktop = self::getResizedImgTag(
179            'img',
180            array(
181                'class' => 'mobile-hide',
182                'src' => array(tpl_getConf('logo'), 'wiki:logo-wide.png', 'wiki:logo.png'),
183                'alt' => $title,
184            ),
185            0, 0
186        );
187        $mobile = self::getResizedImgTag(
188            'img',
189            array(
190                'class' => 'mobile-only',
191                'src' => array('wiki:logo-32x32.png', 'wiki:favicon.png', 'wiki:logo-square.png', 'wiki:logo.png', tpl_getConf('logo')),
192                'alt' => $title,
193            ),
194            32, 32
195        );
196
197        // homepage logo should not link to itself (BITV accessibility requirement)
198        if($linkit) {
199            tpl_link(wl(), $desktop, 'accesskey="h" title="[H]"');
200            tpl_link(wl(), $mobile, 'accesskey="h" title="[H]"');
201        } else {
202            echo $desktop;
203            echo $mobile;
204        }
205    }
206}
207