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