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