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' => 90, 11 'height' => 90, 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', $mime, $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 // get width and height from config 47 $width = null; 48 $height = null; 49 if($this->config['width']) $width = $this->config['width']; 50 if($this->config['height']) $height = $this->config['height']; 51 if(!empty($R->info['struct_table_hash'])) { 52 // this is an aggregation, check for special values 53 if($this->config['agg_width']) $width = $this->config['agg_width']; 54 if($this->config['agg_height']) $height = $this->config['agg_height']; 55 } 56 57 // depending on renderer type directly output or get value from it 58 $returnLink = null; 59 $html = ''; 60 if(!media_isexternal($value)) { 61 if(is_a($R, '\Doku_Renderer_xhtml')) { 62 /** @var \Doku_Renderer_xhtml $R */ 63 $html = $R->internalmedia($value, null, null, $width, $height, null, 'direct', true); 64 } else { 65 $R->internalmedia($value, null, null, $width, $height, null, 'direct'); 66 } 67 } else { 68 if(is_a($R, '\Doku_Renderer_xhtml')) { 69 /** @var \Doku_Renderer_xhtml $R */ 70 $html = $R->externalmedia($value, null, null, $width, $height, null, 'direct', true); 71 } else { 72 $R->externalmedia($value, null, null, $width, $height, null, 'direct'); 73 } 74 } 75 76 // add gallery meta data in XHTML 77 if($mode == 'xhtml') { 78 list(, $mime,) = mimetype($value, false); 79 if(substr($mime, 0, 6) == 'image/') { 80 $hash = !empty($R->info['struct_table_hash']) ? "[gal-" . $R->info['struct_table_hash'] . "]" : ''; 81 $html = str_replace('href', "rel=\"lightbox$hash\" href", $html); 82 } 83 $R->doc .= $html; 84 } 85 86 return true; 87 } 88 89 /** 90 * Return the editor to edit a single value 91 * 92 * @param string $name the form name where this has to be stored 93 * @param string $value the current value 94 * @return string html 95 */ 96 public function valueEditor($name, $value) { 97 $name = hsc($name); 98 $value = hsc($value); 99 100 $id = 'struct__' . md5($name); 101 102 $html = "<input id=\"$id\" class=\"struct_media\" name=\"$name\" value=\"$value\" />"; 103 $html .= "<button type=\"button\" class=\"struct_media\">"; 104 $html .= "<img src=\"" . DOKU_BASE . "lib/images/toolbar/image.png\" height=\"16\" width=\"16\">"; 105 $html .= "</button>"; 106 return $html; 107 } 108} 109