xref: /plugin/struct/types/Media.php (revision 25caf3de36ab784baa62789d6ec4e0b331e210b9)
1<?php
2namespace plugin\struct\types;
3
4use plugin\struct\meta\ValidationException;
5
6class Media extends AbstractBaseType {
7
8    protected $config = array(
9        'mime' => 'image/',
10        'width' => 150,
11        'height' => '',
12        'agg_width' => '',
13        'agg_height' => ''
14    );
15
16    /**
17     * Checks against the allowed mime types
18     *
19     * @param string $value
20     */
21    public function validate($value) {
22        if(!trim($this->config['mime'])) return;
23        $allows = explode(',', $this->config['mime']);
24        $allows = array_map('trim', $allows);
25        $allows = array_filter($allows);
26
27        list(, $mime,) = mimetype($value, false);
28        foreach($allows as $allow) {
29            if(strpos($mime, $allow) === 0) return;
30        }
31
32        throw new ValidationException('Media mime type', $this->config['mime']);
33    }
34
35    /**
36     * Output the stored data
37     *
38     * If outputted in an aggregation we collect the images into a gallery.
39     *
40     * @param string|int $value the value stored in the database
41     * @param \Doku_Renderer $R the renderer currently used to render the data
42     * @param string $mode The mode the output is rendered in (eg. XHTML)
43     * @return bool true if $mode could be satisfied
44     */
45    public function renderValue($value, \Doku_Renderer $R, $mode) {
46        if(empty($value)) {
47            return false;
48        }
49
50        // get width and height from config
51        $width = null;
52        $height = null;
53        if($this->config['width']) $width = $this->config['width'];
54        if($this->config['height']) $height = $this->config['height'];
55        if(!empty($R->info['struct_table_hash'])) {
56            // this is an aggregation, check for special values
57            if($this->config['agg_width']) $width = $this->config['agg_width'];
58            if($this->config['agg_height']) $height = $this->config['agg_height'];
59        }
60
61        // depending on renderer type directly output or get value from it
62        $returnLink = null;
63        $html = '';
64        if(!media_isexternal($value)) {
65            if(is_a($R, '\Doku_Renderer_xhtml')) {
66                /** @var \Doku_Renderer_xhtml $R */
67                $html = $R->internalmedia($value, null, null, $width, $height, null, 'direct', true);
68            } else {
69                $R->internalmedia($value, null, null, $width, $height, null, 'direct');
70            }
71        } else {
72            if(is_a($R, '\Doku_Renderer_xhtml')) {
73                /** @var \Doku_Renderer_xhtml $R */
74                $html = $R->externalmedia($value, null, null, $width, $height, null, 'direct', true);
75            } else {
76                $R->externalmedia($value, null, null, $width, $height, null, 'direct');
77            }
78        }
79
80        // add gallery meta data in XHTML
81        if($mode == 'xhtml') {
82            list(, $mime,) = mimetype($value, false);
83            if(substr($mime, 0, 6) == 'image/') {
84                $hash = !empty($R->info['struct_table_hash']) ? "[gal-" . $R->info['struct_table_hash'] . "]" : '';
85                $html = str_replace('href', "rel=\"lightbox$hash\" href", $html);
86                $R->doc .= $html;
87            }
88        }
89
90        return true;
91    }
92
93    /**
94     * Return the editor to edit a single value
95     *
96     * @param string $name the form name where this has to be stored
97     * @param string $value the current value
98     * @return string html
99     */
100    public function valueEditor($name, $value) {
101        $name = hsc($name);
102        $value = hsc($value);
103
104        $id = 'struct__' . md5($name);
105
106        $html = "<input id=\"$id\" class=\"struct_img\"  name=\"$name\" value=\"$value\" />";
107        $html .= "<button type=\"button\" class=\"struct_img\">";
108        $html .= "<img src=\"" . DOKU_BASE . "lib/images/toolbar/image.png\" height=\"16\" width=\"16\">";
109        $html .= "</button>";
110        return $html;
111    }
112}
113