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