1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5use dokuwiki\plugin\struct\meta\ValidationException;
6
7class Media extends AbstractBaseType
8{
9    protected $config = array(
10        'mime' => 'image/',
11        'width' => 90,
12        'height' => 90,
13        'agg_width' => '',
14        'agg_height' => ''
15    );
16
17    /**
18     * Checks against the allowed mime types
19     *
20     * @param string $rawvalue
21     * @return int|string
22     */
23    public function validate($rawvalue)
24    {
25        $rawvalue = parent::validate($rawvalue);
26
27        if (!trim($this->config['mime'])) return $rawvalue;
28        $allows = explode(',', $this->config['mime']);
29        $allows = array_map('trim', $allows);
30        $allows = array_filter($allows);
31
32        list(, $mime,) = mimetype($rawvalue, false);
33        foreach ($allows as $allow) {
34            if (strpos($mime, $allow) === 0) return $rawvalue;
35        }
36
37        throw new ValidationException('Media mime type', $mime, $this->config['mime']);
38    }
39
40    /**
41     * Output the stored data
42     *
43     * If outputted in an aggregation we collect the images into a gallery.
44     *
45     * @param string|int $value the value stored in the database
46     * @param \Doku_Renderer $R the renderer currently used to render the data
47     * @param string $mode The mode the output is rendered in (eg. XHTML)
48     * @return bool true if $mode could be satisfied
49     */
50    public function renderValue($value, \Doku_Renderer $R, $mode)
51    {
52        // get width and height from config
53        $width = null;
54        $height = null;
55        if ($this->config['width']) $width = $this->config['width'];
56        if ($this->config['height']) $height = $this->config['height'];
57        if (!empty($R->info['struct_table_hash'])) {
58            // this is an aggregation, check for special values
59            if ($this->config['agg_width']) $width = $this->config['agg_width'];
60            if ($this->config['agg_height']) $height = $this->config['agg_height'];
61        }
62
63        // depending on renderer type directly output or get value from it
64        $returnLink = null;
65        $html = '';
66        if (!media_isexternal($value)) {
67            if (is_a($R, '\Doku_Renderer_xhtml')) {
68                /** @var \Doku_Renderer_xhtml $R */
69                $html = $R->internalmedia($value, null, null, $width, $height, null, 'direct', true);
70            } else {
71                $R->internalmedia($value, null, null, $width, $height, null, 'direct');
72            }
73        } else {
74            if (is_a($R, '\Doku_Renderer_xhtml')) {
75                /** @var \Doku_Renderer_xhtml $R */
76                $html = $R->externalmedia($value, null, null, $width, $height, null, 'direct', true);
77            } else {
78                $R->externalmedia($value, null, null, $width, $height, null, 'direct');
79            }
80        }
81
82        // add gallery meta data in XHTML
83        if ($mode == 'xhtml') {
84            list(, $mime,) = mimetype($value, false);
85            if (substr($mime, 0, 6) == 'image/') {
86                $hash = !empty($R->info['struct_table_hash']) ? "[gal-" . $R->info['struct_table_hash'] . "]" : '';
87                $html = str_replace('href', "rel=\"lightbox$hash\" href", $html);
88            }
89            $R->doc .= $html;
90        }
91
92        return true;
93    }
94
95    /**
96     * Return the editor to edit a single value
97     *
98     * @param string $name the form name where this has to be stored
99     * @param string $rawvalue the current value
100     *
101     * @param string $htmlID
102     *
103     * @return string html
104     */
105    public function valueEditor($name, $rawvalue, $htmlID)
106    {
107        static $count = 0;
108        $count++;
109
110        $id = $htmlID ?: 'struct__' . md5($name . $count);
111
112        $params = array(
113            'name' => $name,
114            'value' => $rawvalue,
115            'class' => 'struct_media',
116            'id' => $id
117        );
118        $attributes = buildAttributes($params, true);
119        $html = "<input $attributes />";
120        $html .= "<button type=\"button\" class=\"struct_media\">";
121        $html .= "<img src=\"" . DOKU_BASE . "lib/images/toolbar/image.png\" height=\"16\" width=\"16\">";
122        $html .= "</button>";
123        return $html;
124    }
125
126    /**
127     * @inheritDoc
128     */
129    public function renderTagCloudLink($value, \Doku_Renderer $R, $mode, $page, $filter, $weight)
130    {
131        $media = $this->displayValue($value);
132        if ($mode == 'xhtml' && $this->getConfig()['mime'] == 'image/') {
133            $url = wl($page, $filter);
134            $image = ml($media, ['h' => $weight, 'w' => $weight]);
135            $media_escaped = hsc($media);
136            $R->doc .= "<div style=\"height:{$weight}px; width:{$weight}px\">";
137            $R->doc .= "<a href='$url' class='struct_image' style='background-image:url(\"$image\")'
138                        title='$media_escaped'>";
139            $R->doc .= "<span class='a11y'>$media_escaped</span>";
140            $R->doc .= "</a>";
141            $R->doc .= "</div>";
142            return;
143        }
144        $R->internallink("$page?$filter", $media);
145    }
146}
147