Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / theme / cdm_dataportal.media.theme @ 806baeb2

History | View | Annotate | Download (16.9 KB)

1
<?php
2
// $Id$
3

    
4
/**
5
* Copyright (C) 2007 EDIT
6
* European Distributed Institute of Taxonomy 
7
* http://www.e-taxonomy.eu
8
* 
9
* The contents of this file are subject to the Mozilla Public License Version 1.1
10
* See http://www.mozilla.org/MPL/MPL-1.1.html for the full license terms.
11
*/
12

    
13
function media_content_type_dir($media_representation, $default = false){
14

    
15
	if($media_representation->mimeType){
16
		return substr($media_representation->mimeType, 0, stripos($media_representation->mimeType, '/'));
17
	} else {
18
		return $default;
19
	}
20
}
21

    
22
function getimagesize_remote($image_url) {
23

    
24
	$contents = cdm_http_request($image_url);
25
	if(!$contents){
26
		return false;
27
	}
28

    
29
	$im = ImageCreateFromString($contents);
30
	if (!$im) {
31
		return false;
32
	}
33
	$gis[0] = ImageSX($im);
34
	$gis[1] = ImageSY($im);
35
	// array member 3 is used below to keep with current getimagesize standards
36
	$gis[3] = "width={$gis[0]} height={$gis[1]}";
37
	ImageDestroy($im);
38
	return $gis;
39
}
40

    
41

    
42

    
43
function theme_cdm_media($descriptionElement, $mimeTypePreference){
44
	$out = "";
45

    
46
	_add_js_thickbox();
47

    
48
	$uuid = $descriptionElement->uuid;
49
	$feature = $descriptionElement->feature;
50
	$medias = $descriptionElement->media;
51

    
52
	foreach($medias as $media){
53
		$prefRepresentations = cdm_preferred_media_representations($media, $mimeTypePreference, 300, 400);
54
		$mediaRepresentation = array_shift($prefRepresentations);
55
		if($mediaRepresentation) {
56

    
57
			$contentTypeDirectory = media_content_type_dir($mediaRepresentation);
58

    
59
			$out .= theme('cdm_media_mime_' . $contentTypeDirectory,  $mediaRepresentation, $feature);
60

    
61
			//			$attributes = array('class'=>'thickbox', 'rel'=>'descriptionElement-'.$uuid, 'title'=>$feature->term);
62
			//		    for($i = 0; $part = $mediaRepresentation->representationParts[$i]; $i++){
63
			//		    	if($i == 0){
64
			//		    	    $image_url = drupal_get_path('module', 'cdm_dataportal').'/images/'.$feature->term.'-media.png';
65
			//		    	    $media = '<img src="'.$image_url.'" height="14px" alt="'.$feature->term.'" />';
66
			//		    	    $out .= l($media, $part->uri, $attributes, NULL, NULL, TRUE, TRUE);
67
			//		    	} else {
68
			//		    		$out .= l('', $part->uri, $attributes, NULL, NULL, TRUE);
69
			//		    	}
70
			//		  	}
71
		} else {
72
			// no media available, so display just the type term
73
			$out .=  $feature->representation_L10n;
74
		}
75
	}
76
	return $out;
77

    
78
}
79

    
80
function theme_cdm_mediaTypeTerm($feature){
81
	$icon_url = drupal_get_path('module', 'cdm_dataportal').'/images/'.$feature->representation_L10n.'-media.png';
82
	return '<img src="'.$icon_url.'" height="14px" alt="'.$feature->representation_L10n.'" />';
83
}
84

    
85
function theme_cdm_media_mime_application($mediaRepresentation, $feature){
86

    
87
	foreach($mediaRepresentation->parts as $part){
88
		$attributes = array('title'=> theme('cdm_feature_name', $feature->representation_L10n), 'target'=>'_blank');
89
		//$attributes = array('title'=>$feature->representation_L10n, 'target'=>'_blank');
90
		//$attributes = array('title'=>'original publication', 'target'=>'_blank');
91
		$out .= l(theme('cdm_mediaTypeTerm', $feature), $part->uri, $attributes, NULL, NULL, TRUE, TRUE);
92
	}
93
	return $out;
94
}
95

    
96
function theme_cdm_media_mime_image($mediaRepresentation, $feature){
97
	$out = '';
98
	//TODO thickbox is not used anymore -> delete ?
99
	$attributes = array('class'=>'thickbox', 'rel'=>'representation-'.$representation->uuid, 'title'=>$feature->representation_L10n);
100
	for($i = 0; $part = $representation->representationParts[$i]; $i++){
101
		if($i == 0){
102

    
103
			$out .= l(theme('cdm_mediaTypeTerm', $feature), $part->uri, $attributes, NULL, NULL, TRUE, TRUE);
104
		} else {
105
			$out .= l('', $part->uri, $attributes, NULL, NULL, TRUE);
106
		}
107
	}
108
	return $out;
109
}
110

    
111
function theme_cdm_media_mime_text($representation, $feature){
112

    
113
	foreach($representation->parts as $part){
114
		$attributes = array('title'=> theme('cdm_feature_name', $feature->representation_L10n), 'target'=>'_blank');
115
		//$attributes = array('title'=>t('original publication'), 'target'=>'_blank');
116
		$out .= l(theme('cdm_mediaTypeTerm', $feature), $part->uri, $attributes, NULL, NULL, TRUE, TRUE);
117
	}
118
	return $out;
119
}
120

    
121

    
122
function theme_cdm_media_caption($media, $elements = array('title', 'description', 'artist', 'location', 'rights'), $fileUri = null){
123

    
124
	$media_metadata = cdm_read_media_metadata($media);
125

    
126
	$out = '<dl class="media-caption">';
127
	//title
128
	if($media_metadata['title'] && (!$elements || array_search('title', $elements)!== false)){
129
		$out .= '<dt class = "title">' . t('Title') . '</dt> <dd class = "title">' . $media_metadata['title'] . '</dd>';
130
		//unset($media_metadata['title']);
131
	}
132
	//description
133
	if($media_metadata['description'] && (!$elements || array_search('description', $elements)!== false)){
134
		$out .= '<dt class = "description">' . t('Description') . '</dt> <dd class = "description">' . $media_metadata['description'] . '</dd>';
135
		//unset($media_metadata['description']);
136
	}
137
	//artist
138
	if($media_metadata['artist'] && (!$elements || array_search('artist', $elements)!== false)){
139
		//$out .= '<span class = "artist">' . ($media_metadata['artist'] ? 'Artist: ' . $media_metadata['artist'] . '</span>' . '<br>' : '');
140
		$out .= '<dt class = "artist">' . t('Artist') . '</dt> <dd class = "astist">' . $media_metadata['artist'] . '</dd>';
141
	}
142
	//location
143
	if(!$elements || array_search('location', $elements)!== false){
144
		$location = '';
145
		$location .= $media_metadata['location']['sublocation'];
146
		if ($location && $media_metadata['location']['city']){
147
			$location .= ', ';
148
		}
149
		$location .= $media_metadata['location']['city'];
150
		if ($location && $media_metadata['location']['province']){
151
			$location .= ', ';
152
		}
153
		$location .= $media_metadata['location']['province'];
154
		if ($location && $media_metadata['location']['country']){
155
			$location .= ' (' . $media_metadata['location']['country'] . ')';
156
		} else {
157
			$location .= $media_metadata['location']['country'];
158
		}
159
		if ($location){
160
			$out .= '<dt class = "location">' . t('Location') . '</dt> <dd class = "location">' . $location  . '</dd>';
161
		}
162
	}
163
	//rights
164
	if(!$elements || array_search('rights', $elements)!== false){
165
		$rights = '';
166
		//copyrights
167
		$cnt = count($media_metadata['rights']['copyright']['agentNames']);
168
		if($cnt > 0){
169
			$rights .= '<dt class="rights">&copy;</dt> <dd class="rights"> ';
170
			for($i = 0; $i < $cnt; $i++){
171
				$rights .= $media_metadata['rights']['copyright']['agentNames'][$i];
172
				if($i+1 < $cnt){
173
					$rights .= ' / ';
174
				}
175
			}
176
			$rights .= '</dd>';
177
		}
178
		//license
179
		$cnt = count($media_metadata['rights']['license']['agentNames']);
180
		if($cnt > 0){
181
			$rights .= '<dt class ="license">' . t('License') . '</dt> <dd class = "license">';
182
			for($i = 0; $i < $cnt; $i++){
183
				$rights .= $media_metadata['rights']['license']['agentNames'][$i];
184
				if ($i+1 < $cnt){
185
					$rights .= ' / ';
186
				}
187
			}
188
			$rights .= '</dd>';
189
		}
190
		if($rights){
191
			$out .=  $rights . '</dt>';
192
		}
193
	}
194
	//TODO add all other metadata elemenst generically
195
	$out .= '</dl>';
196
	//return value
197
	return $out;
198
}
199

    
200

    
201
/**
202
 * @param $mediaList an array of Media entities
203
 * @param $maxExtend
204
 * @param $cols
205
 * @param $maxRows
206
 * @param $captionElements an array possible values are like in the following example: array('title', 'description', 'file', 'filename'),
207
 *         to add a link to the caption: array('titlecache', '#uri'=>t('open Image'));
208
 * @param $mediaLinkType valid values:
209
 *      "NONE": do not link the images,
210
 *      "LIGHTBOX": open the link in a light box,
211
 *      "NORMAL": link to the image page or to the $alternativeMediaUri if it is defined
212
 * @param $alternativeMediaUri an array of alternative URIs to link the images wich will overwrite the URIs of the media parts.
213
 *     The order of URI in this array must correspond with the order of images in $mediaList
214
 * @param $galleryLinkUri an URI to link the the hint on more images to; if null no link is created
215
 * @return unknown_type
216
 */
217
function theme_cdm_media_gallerie($mediaList, $galleryName, $maxExtend = 150, $cols = 4, $maxRows = false, $captionElements = array('title'),
218
$mediaLinkType = 'LIGHTBOX', $alternativeMediaUri = null, $galleryLinkUri = null ){
219
	
220
	if(!is_array($captionElements)){
221
		$captionElements = array();
222
	}
223
	//TODO correctly handle multiple media representation parts
224
	$_SESSION['cdm']['last_gallery']= substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'], "?q=")+3);
225
	// prevent from errors
226
	if(!isset($mediaList[0])){
227
		return;
228
	}
229

    
230
	$galleryID = "media_gallery_".$galleryName;
231

    
232
	// prepare media links
233
	$doLink = false;
234
	$linkAttributes = null;
235
	if($mediaLinkType != 'NONE'){
236
		$doLink = true;
237
	}
238
	if($mediaLinkType == 'LIGHTBOX'){
239
		$doLink = true;
240
		//_add_js_thickbox();
241
		//$linkAttributes = array("class"=>"thickbox", "rel"=>"media_gallerie".$galleryName);
242
		_add_js_lightbox($galleryID);
243
		$linkAttributes = array("class"=>"lightbox");
244
	}
245

    
246
	// render the media gallery grid
247
	$out = '<table id="'.$galleryID.'" class="media_gallery">';
248
	$out .= '<colgroup>';
249

    
250
	for($c = 0; $c < $cols; $c++){
251
		$out .= '<col width="'.(100 / $cols).'%">';
252
	}
253
	$out .= '</colgroup>';
254

    
255
	for($r = 0; ($r < $maxRows || !$maxRows) && count($mediaList) > 0; $r++){
256
		$captionParts = array();
257
		$out .= '<tr>';
258
		for($c = 0; $c < $cols; $c++){
259
			$media = array_shift($mediaList);
260
			if(isset($media->representations[0]->parts[0])){
261
				$contentTypeDirectory = media_content_type_dir($media->representations[0], 'image');
262
				$mediaIndex++;
263
				$mediaPartHtml = theme('cdm_media_gallerie_'.$contentTypeDirectory, $media->representations[0]->parts[0], $maxExtend, TRUE);
264

    
265
				// --- compose Media Link
266
				$mediaLinkUri = false;
267
				if($alternativeMediaUri){
268
					if(isset($alternativeMediaUri[$mediaIndex])){
269
						$mediaLinkUri = $alternativeMediaUri[$mediaIndex];
270
					}
271
					if(is_string($alternativeMediaUri)){
272
						$mediaLinkUri = $alternativeMediaUri;
273
					}
274
				} else {
275
					$mediaLinkUri = $media->representations[0]->parts[0]->uri;
276
				}
277

    
278
				// generate gallery caption
279
				_add_js_ahah();
280
				$content_url = cdm_compose_url(CDM_WS_MEDIA, $media->uuid);
281
				$cdm_proxy_url = url('cdm_api/proxy/'.urlencode($content_url)."/cdm_media_caption/".join(',',$captionElements));
282
				$captionPartHtml = '<div class="ahah-content" rel="'.$cdm_proxy_url.'"><span class="loading" style="display: none;">Loading ....</span></div>';
283

    
284
				// generate & add caption to lightbox
285
				$lightBoxCaptionElements = null;
286
				$cdm_proxy_url = url('cdm_api/proxy/'.urlencode($content_url)."/cdm_media_caption"); //.($lightBoxCaptionElements?'/'.join	(',',$lightBoxCaptionElements):''));
287
				$linkAttributes['alt'] = '<div class="ahah-content" rel="'.$cdm_proxy_url.'"><span class="loading" style="display: none;">Loading ....</span></div>';
288

    
289
				if(isset($captionElements['#uri'])){
290
					$captionPartHtml .= '<div>'.l($captionElements['#uri'], path_to_media($media->uuid), null, null, null, FALSE, TRUE).'</div>';
291
				}
292
				$captionParts[] = $captionPartHtml;
293

    
294
				// --- surround imagePart with link
295
				if($doLink){
296
					$mediaPartHtml = l($mediaPartHtml, $mediaLinkUri, $linkAttributes, null, null, FALSE, TRUE);
297
				}
298

    
299
			} else {
300
				$mediaPartHtml = '';
301
				$captionParts[] = '';
302
			}
303
			$out .= '<td>'.$mediaPartHtml.'</td>';
304
		}
305
		$out .= '</tr>'; // end of media parts
306
		if(isset($captionElements[0])){
307
			$out .= '<tr>';
308
			// add caption row
309
			foreach($captionParts as $captionPartHtml){
310
				$out .= '<td>'.$captionPartHtml.'</td>';
311
			}
312
			$out .= '</tr>';
313
		}
314
	}
315
	if($galleryLinkUri){
316
		if(count($mediaList) > 0){
317
			$moreHtml = count($mediaList).' '.t('more in gallery');
318
		} else {
319
			$moreHtml = t('open gallery');
320
		}
321
		$moreHtml = l($moreHtml, $galleryLinkUri);
322
		$out .= '<tr><td colspan="'.$cols.'">'.$moreHtml.'</td></tr>';
323
	}
324
	$out .= '</table>';
325
	return $out;
326
}
327

    
328
function theme_cdm_media_gallerie_image($mediaRepresentationPart, $maxExtend, $addPassePartout = FALSE, $attributes = null){
329
	//TODO merge with theme_cdm_media_mime_image?
330

    
331
	if(isset($mediaRepresentationPart)){
332
			
333
		$h = $mediaRepresentationPart->height;
334
		$w = $mediaRepresentationPart->width;
335
		if($w == 0 || $h == 0){
336
			$image_uri = str_replace(' ','%20',$mediaRepresentationPart->uri); //take url and replace spaces
337
			$imageDimensions = getimagesize_remote($image_uri);
338
			if(!$imageDimensions){
339
				return '<div>'.t('Image unavailable, uri:').$mediaRepresentationPart->uri.'</div>';
340
			}
341
			$w = $imageDimensions[0];
342
			$h = $imageDimensions[1];
343
		}
344
		$margins = '0 0 0 0';
345
		$ratio = $w / $h;
346
		if($ratio > 1){
347
			$displayHeight = round($maxExtend / $ratio);
348
			$displayWidth = $maxExtend;
349
			$m = round(($maxExtend - $displayHeight) / 2);
350
			$margins = 'margin:'.$m.'px 0 '.$m.'px 0;';
351
		} else {
352
			$displayHeight = $maxExtend;
353
			$displayWidth = round($maxExtend * $ratio);
354
			$m = round(($maxExtend - $displayWidth) / 2);
355
			$margins = 'margin:0 '.$m.'px 0 '.$m.'px;';
356
		}
357

    
358
		// turn attributes array into string
359
		$attrStr = ' ';
360
		//$attributes['title'] = 'h:'.$h.', w:'.$w.',ratio:'.$ratio;
361
		if(is_array($attributes)){
362
			foreach($attributes as $name=>$value){
363
				$attrStr .= $name.'="'.$value.'" ';
364
			}
365
		}
366

    
367
		//return  '<img src="'."http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/photos/Lapsana_communis_A_01.jpg".'" width="'.$maxExtend.'" height="'.$maxExtend.'" />';
368
		if($addPassePartout){
369
			$out .= '<div class="image-passe-partout" style="width:'.$maxExtend.'px; height:'.$maxExtend.'px;">';
370
		} else {
371
			// do not add margins if no pass partout is shown
372
			$margins = '';
373
		}
374
		$out .= '<img src="'.$mediaRepresentationPart->uri.'" width="'.$displayWidth.'" height="'.$displayHeight.'" style="'.$margins.'"'.$attrStr.' />';
375

    
376
		if($addPassePartout){
377
			$out .= '</div>';
378
		}
379
		return $out;
380
	}
381

    
382
}
383

    
384
function theme_cdm_openlayers_image($mediaRepresentationPart, $maxExtend){
385

    
386
	// see http://trac.openlayers.org/wiki/UsingCustomTiles#UsingTilesWithoutaProjection
387
	// and http://trac.openlayers.org/wiki/SettingZoomLevels
388
	
389
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/OpenLayers/OpenLayers.js', 'core', 'header');
390
      
391
  //TODO megre code below with code from theme_cdm_media_gallerie_image
392
	//var_dump("MEDIA URI: " . $mediaRepresentationPart->uri);
393
	//TODO merge code below with code from theme_cdm_media_gallerie_image
394
	$w = $mediaRepresentationPart->width;
395
	$h = $mediaRepresentationPart->height;
396

    
397
	if($w == 0 || $h == 0){
398
		$image_uri = str_replace(' ','%20',$mediaRepresentationPart->uri); //take url and replace spaces
399
		$imageDimensions = getimagesize_remote($image_uri);
400
		if(!$imageDimensions){
401
			return '<div>'.t('Image unavailable, uri:').$mediaRepresentationPart->uri.'</div>';
402
		}
403
		$w = $imageDimensions[0];
404
		$h = $imageDimensions[1];
405
	}
406

    
407
	// calculate  maxResolution (default is 360 deg / 256 px) and the bounds
408
	if($w > $h){
409
		$lat = 90;
410
		$lon = 90 * ($h / $w);
411
		$maxRes = $w / $maxExtend;
412
	} else {
413
		$lat = 90 * ($w / $h);
414
		$lon = 90;
415
		$maxRes =  $h / $maxExtend ;
416
	}
417

    
418
	$maxRes *= 1;
419
	drupal_add_js('
420
 var map;
421

    
422
 var imageLayerOptions={
423
     maxResolution: '.$maxRes.',
424
     maxExtent: new OpenLayers.Bounds(0, 0, '.$w.', '.$h.')
425
  };
426
  var mapOptions={
427
      controls: 
428
       [ 
429
         new OpenLayers.Control.PanZoom(),
430
         new OpenLayers.Control.Navigation({zoomWheelEnabled: false, handleRightClicks:true, zoomBoxKeyMask: OpenLayers.Handler.MOD_CTRL})
431
       ],
432
     restrictedExtent:  new OpenLayers.Bounds(0, 0, '.$w.', '.$h.')
433
  };
434
 
435
 var graphic = new OpenLayers.Layer.Image(
436
          \'Image Title\',
437
          \''.$mediaRepresentationPart->uri.'\',
438
          new OpenLayers.Bounds(0, 0, '.$w.', '.$h.'),
439
          new OpenLayers.Size('.$w.', '.$h.'),
440
          imageLayerOptions
441
          );
442
  
443
 function init() {
444
   map = new OpenLayers.Map(\'openlayers_image\', mapOptions);
445
   map.addLayers([graphic]);
446
   map.setCenter(new OpenLayers.LonLat(0, 0), 1);
447
   map.zoomToMaxExtent();
448
 }
449
 
450
$(document).ready(function(){
451
  init();
452

    
453
});'
454
, 'inline');
455
$out = '<div id="openlayers_image" class="image_viewer" style="width: '.$maxExtend.'px; height:'.($maxExtend).'px"></div>';
456
return $out;
457

    
458
}
459

    
460

    
461
function theme_cdm_preferredImage($media, $defaultRepresentationPart, $imageMaxExtend, $parameters = ''){
462

    
463
	if(isset($media[0])){
464
		$representationPart = $media[0]->representations[0]->parts[0];
465
		if($parameters){
466
			$representationPart->uri.$parameters;
467
		}
468
	} else {
469
		$representationPart = $defaultRepresentationPart;
470
	}
471

    
472
	//$widthAndHeight = ($imageWidth ? ' width="'.$imageWidth : '').($imageHeight ? '" height="'.$imageHeight : '');
473
	//  $imageUri = $preferredMedia ? $preferredMedia->representations[0]->parts[0]->uri . $parameters : $defaultImage;
474
	$attributes = array('alt'=>($preferredMedia ? $preferredMedia->representations[0]->parts[0]->uri : "no image available"));
475
	$out .= theme('cdm_media_gallerie_image', $representationPart, $imageMaxExtend, false, $attributes);
476
	// $out = '<img class="left" '.$widthAndHeight.' " src="'.$imageUri.'" alt="'.$altText.'" />';
477
	return $out;
478
}
Add picture from clipboard (Maximum size: 40 MB)