xref: /template/sprintdoc/Template.php (revision 06cdf1484d980754fc39934b12a7eda059d7f4dd)
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) . ' />';
120    }
121}
122