125caf3deSAndreas Gohr<?php 2d6d97f60SAnna Dabrowska 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\types; 425caf3deSAndreas Gohr 5ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ValidationException; 625caf3deSAndreas Gohr 7d6d97f60SAnna Dabrowskaclass Media extends AbstractBaseType 8d6d97f60SAnna Dabrowska{ 97fe2cdf2SAndreas Gohr protected $config = [ 107fe2cdf2SAndreas Gohr 'mime' => 'image/', 117fe2cdf2SAndreas Gohr 'width' => 90, 127fe2cdf2SAndreas Gohr 'height' => 90, 137fe2cdf2SAndreas Gohr 'agg_width' => '', 147fe2cdf2SAndreas Gohr 'agg_height' => '' 157fe2cdf2SAndreas Gohr ]; 1625caf3deSAndreas Gohr 1725caf3deSAndreas Gohr /** 1825caf3deSAndreas Gohr * Checks against the allowed mime types 1925caf3deSAndreas Gohr * 2023169abeSAndreas Gohr * @param string $rawvalue 21806eec82SAndreas Gohr * @return int|string 2225caf3deSAndreas Gohr */ 23d6d97f60SAnna Dabrowska public function validate($rawvalue) 24d6d97f60SAnna Dabrowska { 2523169abeSAndreas Gohr $rawvalue = parent::validate($rawvalue); 26806eec82SAndreas Gohr 2723169abeSAndreas Gohr if (!trim($this->config['mime'])) return $rawvalue; 2825caf3deSAndreas Gohr $allows = explode(',', $this->config['mime']); 2925caf3deSAndreas Gohr $allows = array_map('trim', $allows); 3025caf3deSAndreas Gohr $allows = array_filter($allows); 3125caf3deSAndreas Gohr 327234bfb1Ssplitbrain [, $mime, ] = mimetype($rawvalue, false); 3325caf3deSAndreas Gohr foreach ($allows as $allow) { 34*ba662a60SAndreas Gohr if (str_starts_with($mime, $allow)) return $rawvalue; 3525caf3deSAndreas Gohr } 3625caf3deSAndreas Gohr 3751629881SAndreas Gohr throw new ValidationException('Media mime type', $mime, $this->config['mime']); 3825caf3deSAndreas Gohr } 3925caf3deSAndreas Gohr 4025caf3deSAndreas Gohr /** 4125caf3deSAndreas Gohr * Output the stored data 4225caf3deSAndreas Gohr * 4325caf3deSAndreas Gohr * If outputted in an aggregation we collect the images into a gallery. 4425caf3deSAndreas Gohr * 4525caf3deSAndreas Gohr * @param string|int $value the value stored in the database 4625caf3deSAndreas Gohr * @param \Doku_Renderer $R the renderer currently used to render the data 4725caf3deSAndreas Gohr * @param string $mode The mode the output is rendered in (eg. XHTML) 4825caf3deSAndreas Gohr * @return bool true if $mode could be satisfied 4925caf3deSAndreas Gohr */ 50d6d97f60SAnna Dabrowska public function renderValue($value, \Doku_Renderer $R, $mode) 51d6d97f60SAnna Dabrowska { 5225caf3deSAndreas Gohr // get width and height from config 5325caf3deSAndreas Gohr $width = null; 5425caf3deSAndreas Gohr $height = null; 5525caf3deSAndreas Gohr if ($this->config['width']) $width = $this->config['width']; 5625caf3deSAndreas Gohr if ($this->config['height']) $height = $this->config['height']; 5725caf3deSAndreas Gohr if (!empty($R->info['struct_table_hash'])) { 5825caf3deSAndreas Gohr // this is an aggregation, check for special values 5925caf3deSAndreas Gohr if ($this->config['agg_width']) $width = $this->config['agg_width']; 6025caf3deSAndreas Gohr if ($this->config['agg_height']) $height = $this->config['agg_height']; 6125caf3deSAndreas Gohr } 6225caf3deSAndreas Gohr $html = ''; 6325caf3deSAndreas Gohr if (!media_isexternal($value)) { 6425caf3deSAndreas Gohr if (is_a($R, '\Doku_Renderer_xhtml')) { 6525caf3deSAndreas Gohr /** @var \Doku_Renderer_xhtml $R */ 6625caf3deSAndreas Gohr $html = $R->internalmedia($value, null, null, $width, $height, null, 'direct', true); 6725caf3deSAndreas Gohr } else { 6825caf3deSAndreas Gohr $R->internalmedia($value, null, null, $width, $height, null, 'direct'); 6925caf3deSAndreas Gohr } 707234bfb1Ssplitbrain } elseif (is_a($R, '\Doku_Renderer_xhtml')) { 7125caf3deSAndreas Gohr /** @var \Doku_Renderer_xhtml $R */ 7225caf3deSAndreas Gohr $html = $R->externalmedia($value, null, null, $width, $height, null, 'direct', true); 7325caf3deSAndreas Gohr } else { 7425caf3deSAndreas Gohr $R->externalmedia($value, null, null, $width, $height, null, 'direct'); 7525caf3deSAndreas Gohr } 7625caf3deSAndreas Gohr 7725caf3deSAndreas Gohr // add gallery meta data in XHTML 7825caf3deSAndreas Gohr if ($mode == 'xhtml') { 797234bfb1Ssplitbrain [, $mime, ] = mimetype($value, false); 80*ba662a60SAndreas Gohr if (str_starts_with($mime, 'image/')) { 817234bfb1Ssplitbrain $hash = empty($R->info['struct_table_hash']) ? '' : "[gal-" . $R->info['struct_table_hash'] . "]"; 8225caf3deSAndreas Gohr $html = str_replace('href', "rel=\"lightbox$hash\" href", $html); 8325caf3deSAndreas Gohr } 843b5909e1SAndreas Gohr $R->doc .= $html; 8525caf3deSAndreas Gohr } 8625caf3deSAndreas Gohr 8725caf3deSAndreas Gohr return true; 8825caf3deSAndreas Gohr } 8925caf3deSAndreas Gohr 9025caf3deSAndreas Gohr /** 9125caf3deSAndreas Gohr * Return the editor to edit a single value 9225caf3deSAndreas Gohr * 9325caf3deSAndreas Gohr * @param string $name the form name where this has to be stored 94c0230d2cSAndreas Gohr * @param string $rawvalue the current value 9579d22ad4SMichael Grosse * 96ee983135SMichael Große * @param string $htmlID 97ee983135SMichael Große * 9825caf3deSAndreas Gohr * @return string html 9925caf3deSAndreas Gohr */ 100d6d97f60SAnna Dabrowska public function valueEditor($name, $rawvalue, $htmlID) 101d6d97f60SAnna Dabrowska { 102194029b4SAndreas Gohr static $count = 0; 103194029b4SAndreas Gohr $count++; 10425caf3deSAndreas Gohr 105ee983135SMichael Große $id = $htmlID ?: 'struct__' . md5($name . $count); 10625caf3deSAndreas Gohr 1077fe2cdf2SAndreas Gohr $params = [ 1087fe2cdf2SAndreas Gohr 'name' => $name, 1097fe2cdf2SAndreas Gohr 'value' => $rawvalue, 1107fe2cdf2SAndreas Gohr 'class' => 'struct_media', 1117fe2cdf2SAndreas Gohr 'id' => $id 1127fe2cdf2SAndreas Gohr ]; 1133e7a5b3cSMichael Große $attributes = buildAttributes($params, true); 1143e7a5b3cSMichael Große $html = "<input $attributes />"; 115c266d282SAndreas Gohr $html .= "<button type=\"button\" class=\"struct_media\">"; 11625caf3deSAndreas Gohr $html .= "<img src=\"" . DOKU_BASE . "lib/images/toolbar/image.png\" height=\"16\" width=\"16\">"; 11725caf3deSAndreas Gohr $html .= "</button>"; 11825caf3deSAndreas Gohr return $html; 11925caf3deSAndreas Gohr } 120262c0fc6SMichael Grosse 121262c0fc6SMichael Grosse /** 122262c0fc6SMichael Grosse * @inheritDoc 123262c0fc6SMichael Grosse */ 1247a256bc0SAnna Dabrowska public function renderTagCloudLink($value, \Doku_Renderer $R, $mode, $page, $filter, $weight, $showCount = null) 125d6d97f60SAnna Dabrowska { 126262c0fc6SMichael Grosse $media = $this->displayValue($value); 127262c0fc6SMichael Grosse if ($mode == 'xhtml' && $this->getConfig()['mime'] == 'image/') { 128262c0fc6SMichael Grosse $url = wl($page, $filter); 129262c0fc6SMichael Grosse $image = ml($media, ['h' => $weight, 'w' => $weight]); 13023b146acSMichael Grosse $media_escaped = hsc($media); 131262c0fc6SMichael Grosse $R->doc .= "<div style=\"height:{$weight}px; width:{$weight}px\">"; 13217a3a578SAndreas Gohr $R->doc .= "<a href='$url' class='struct_image' style='background-image:url(\"$image\")' 13317a3a578SAndreas Gohr title='$media_escaped'>"; 13423b146acSMichael Grosse $R->doc .= "<span class='a11y'>$media_escaped</span>"; 135262c0fc6SMichael Grosse $R->doc .= "</a>"; 136262c0fc6SMichael Grosse $R->doc .= "</div>"; 137262c0fc6SMichael Grosse return; 138262c0fc6SMichael Grosse } 139262c0fc6SMichael Grosse $R->internallink("$page?$filter", $media); 140262c0fc6SMichael Grosse } 14125caf3deSAndreas Gohr} 142