Project

General

Profile

Download (19.3 KB) Statistics
| Branch: | Tag: | Revision:
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); // using@ to avoid php user warnings
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, $mediaUrl = false){
81
	$provider = '';
82
	if(strpos($mediaUrl, 'www.biodiversitylibrary.org') > 0){
83
    $provider = '-BHL';
84
	}
85
	$icon_image = $feature->representation_L10n.'-media'.$provider.'.png';
86
	$icon_url = drupal_get_path('module', 'cdm_dataportal').'/images/'.$icon_image;
87
	return '<img src="'.$icon_url.'" height="14px" alt="'.$feature->representation_L10n.'" />';
88
}
89

    
90
function theme_cdm_media_mime_application($mediaRepresentation, $feature){
91

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

    
101
function theme_cdm_media_mime_image($mediaRepresentation, $feature){
102
	$out = '';
103
	//TODO thickbox is not used anymore -> delete ?
104
	$attributes = array('class'=>'thickbox', 'rel'=>'representation-'.$representation->uuid, 'title'=>$feature->representation_L10n);
105
	for($i = 0; $part = $representation->representationParts[$i]; $i++){
106
		if($i == 0){
107
			$out .= l(theme('cdm_mediaTypeTerm', $feature, $part->uri), $part->uri, $attributes, NULL, NULL, TRUE, TRUE);
108
		} else {
109
			$out .= l('', $part->uri, $attributes, NULL, NULL, TRUE);
110
		}
111
	}
112
	return $out;
113
}
114

    
115
function theme_cdm_media_mime_text($representation, $feature){
116

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

    
125

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

    
128
	$media_metadata = cdm_read_media_metadata($media);
129

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

    
204

    
205
/**
206
 * @param $mediaList an array of Media entities
207
 * @param $maxExtend
208
 * @param $cols
209
 * @param $maxRows
210
 * @param $captionElements an array possible values are like in the following example: array('title', 'description', 'file', 'filename'),
211
 *         to add a link to the caption: array('titlecache', '#uri'=>t('open Image'));
212
 * @param $mediaLinkType valid values:
213
 *      "NONE": do not link the images,
214
 *      "LIGHTBOX": open the link in a light box,
215
 *      "NORMAL": link to the image page or to the $alternativeMediaUri if it is defined
216
 * @param $alternativeMediaUri an array of alternative URIs to link the images wich will overwrite the URIs of the media parts.
217
 *     The order of URI in this array must correspond with the order of images in $mediaList
218
 * @param $galleryLinkUri an URI to link the the hint on more images to; if null no link is created
219
 * @return unknown_type
220
 */
221
function theme_cdm_media_gallerie($mediaList, $galleryName,
222
  $maxExtend = 150, $cols = 4, $maxRows = false, $captionElements = array('title'),
223
  $mediaLinkType = 'LIGHTBOX', $alternativeMediaUri = null, $galleryLinkUri = null ){
224

    
225
	if(!is_array($captionElements)){
226
		$captionElements = array();
227
	}
228
	//TODO correctly handle multiple media representation parts
229
	$_SESSION['cdm']['last_gallery']= substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'], "?q=")+3);
230
	// prevent from errors
231
	if(!isset($mediaList[0])){
232
		return;
233
	}
234

    
235
	$galleryID = "media_gallery_".$galleryName;
236
	$mediaPartLinkAttributes = array();
237
	$openMediaLinkAttributes = array();
238

    
239
	// prepare media links
240
	$doLink = false;
241
	if($mediaLinkType != 'NONE'){
242
		$doLink = true;
243
	}
244
	if($mediaLinkType == 'LIGHTBOX'){
245
		$doLink = true;
246
		_add_js_lightbox($galleryID);
247
	}
248

    
249
	// render the media gallery grid
250
	$out = '<table id="'.$galleryID.'" class="media_gallery">';
251
	$out .= '<colgroup>';
252

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

    
258
	for($r = 0; ($r < $maxRows || !$maxRows) && count($mediaList) > 0; $r++){
259
		$captionParts = array();
260
		$out .= '<tr>';
261
		for($c = 0; $c < $cols; $c++){
262
			$media = array_shift($mediaList);
263
			if(isset($media->representations[0]->parts[0])){
264

    
265
				//
266
				// find preferred representation
267
				//
268
				$preferred_media_representations_list = cdm_preferred_media_representations(
269
				   $media,
270
				   array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'),
271
				   $maxExtend, $maxExtend);
272
				if(count($preferred_media_representations_list) == 0){
273
					// fallback to using the first one in the list
274
					$preferred_media_representations_list = $media->representations;
275
				}
276
        $preferred_media_representation = array_shift($preferred_media_representations_list);
277

    
278
        // $preferred_media_representation->parts[0]->uri = "http://127.0.0.1/images/palmae/palm_tc_14415_1.jpg";
279
				$contentTypeDirectory = media_content_type_dir($preferred_media_representation, 'application');
280
				$mediaIndex++;
281
				$mediaPartHtml = theme('cdm_media_gallerie_'.$contentTypeDirectory, $preferred_media_representation->parts[0], $maxExtend, TRUE);
282
				// --- compose Media Link
283
				$mediaLinkUri = false;
284
				if($alternativeMediaUri){
285
					if(isset($alternativeMediaUri[$mediaIndex])){
286
						$mediaLinkUri = $alternativeMediaUri[$mediaIndex];
287
					}
288
					if(is_string($alternativeMediaUri)){
289
						$mediaLinkUri = $alternativeMediaUri;
290
					}
291
				} else {
292
					$mediaLinkUri = $preferred_media_representation->parts[0]->uri;
293
				}
294

    
295
				_add_js_ahah();
296
				$content_url = cdm_compose_url(CDM_WS_PORTAL_MEDIA, $media->uuid);
297
				$cdm_proxy_url = url('cdm_api/proxy/'.urlencode($content_url)."/cdm_media_caption/".serialize($captionElements));
298
				$captionPartHtml = '<div class="ahah-content" rel="'.$cdm_proxy_url.'"><span class="loading" style="display: none;">Loading ....</span></div>';
299

    
300
				// generate gallery caption
301
				if($mediaLinkType == 'LIGHTBOX' && $contentTypeDirectory == 'image'){
302
          $mediaPartLinkAttributes['class'] = "lightbox";
303
				} else {
304
					$mediaPartLinkAttributes['target'] = "specimen";
305
					$openMediaLinkAttributes['target'] = "specimen";
306
				}
307
				// generate & add caption to lightbox
308
				$lightBoxCaptionElements = null;
309
				$cdm_proxy_url = url('cdm_api/proxy/'.urlencode($content_url)."/cdm_media_caption"); //.($lightBoxCaptionElements?'/'.join	(',',$lightBoxCaptionElements):''));
310
				$mediaPartLinkAttributes['alt'] = '<div class="ahah-content" rel="'.$cdm_proxy_url.'"><span class="loading" style="display: none;">Loading ....</span></div>';
311

    
312
				if(isset($captionElements['#uri'])){
313
					if($contentTypeDirectory == 'image'){
314
						$captionPartHtml .= '<div>'.l($captionElements['#uri'], path_to_media($media->uuid), null, null, null, FALSE, TRUE).'</div>';
315
					} else {
316
						$captionPartHtml .= '<div>'.l($captionElements['#uri'], $mediaLinkUri, $openMediaLinkAttributes, null, null, TRUE, TRUE).'</div>';
317
					}
318
				}
319
				$captionParts[] = $captionPartHtml;
320

    
321
				// --- surround imagePart with link
322
				if($doLink){
323
					$mediaPartHtml = l($mediaPartHtml, $mediaLinkUri, $mediaPartLinkAttributes, null, null, FALSE, TRUE);
324
				}
325

    
326
			} else {
327
				$mediaPartHtml = '';
328
				$captionParts[] = '';
329
			}
330
			$out .= '<td>'.$mediaPartHtml.'</td>';
331
		}
332
		$out .= '</tr>'; // end of media parts
333
		if(count($captionElements) > 0){
334
			$out .= '<tr>';
335
			// add caption row
336
			foreach($captionParts as $captionPartHtml){
337
				$out .= '<td>'.$captionPartHtml.'</td>';
338
			}
339
			$out .= '</tr>';
340
		}
341
	}
342
	if($galleryLinkUri){
343
		if(count($mediaList) > 0){
344
			$moreHtml = count($mediaList).' '.t('more in gallery');
345
		} else {
346
			$moreHtml = t('open gallery');
347
		}
348
		$moreHtml = l($moreHtml, $galleryLinkUri);
349
		$out .= '<tr><td colspan="'.$cols.'">'.$moreHtml.'</td></tr>';
350
	}
351
	$out .= '</table>';
352
	return $out;
353
}
354

    
355
function theme_cdm_media_gallerie_image($mediaRepresentationPart, $maxExtend, $addPassePartout = FALSE, $attributes = null){
356
	//TODO merge with theme_cdm_media_mime_image?
357

    
358
	if(isset($mediaRepresentationPart)){
359

    
360
		$h = $mediaRepresentationPart->height;
361
		$w = $mediaRepresentationPart->width;
362
		if($w == 0 || $h == 0){
363
			$image_uri = str_replace(' ','%20',$mediaRepresentationPart->uri); //take url and replace spaces
364
			$imageDimensions = getimagesize_remote($image_uri);
365
			if(!$imageDimensions){
366
				return '<div>'.t('Image unavailable, uri: ').$mediaRepresentationPart->uri.'</div>';
367
			}
368
			$w = $imageDimensions[0];
369
			$h = $imageDimensions[1];
370
		}
371
		$margins = '0 0 0 0';
372
		$ratio = $w / $h;
373
		if($ratio > 1){
374
			$displayHeight = round($maxExtend / $ratio);
375
			$displayWidth = $maxExtend;
376
			$m = round(($maxExtend - $displayHeight) / 2);
377
			$margins = 'margin:'.$m.'px 0 '.$m.'px 0;';
378
		} else {
379
			$displayHeight = $maxExtend;
380
			$displayWidth = round($maxExtend * $ratio);
381
			$m = round(($maxExtend - $displayWidth) / 2);
382
			$margins = 'margin:0 '.$m.'px 0 '.$m.'px;';
383
		}
384

    
385
		// turn attributes array into string
386
		$attrStr = ' ';
387
		//$attributes['title'] = 'h:'.$h.', w:'.$w.',ratio:'.$ratio;
388
		if(is_array($attributes)){
389
			foreach($attributes as $name=>$value){
390
				$attrStr .= $name.'="'.$value.'" ';
391
			}
392
		}
393

    
394
		//return  '<img src="'."http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/photos/Lapsana_communis_A_01.jpg".'" width="'.$maxExtend.'" height="'.$maxExtend.'" />';
395
		if($addPassePartout){
396
			$out .= '<div class="image-passe-partout" style="width:'.$maxExtend.'px; height:'.$maxExtend.'px;">';
397
		} else {
398
			// do not add margins if no pass partout is shown
399
			$margins = '';
400
		}
401
		$out .= '<img src="'.$mediaRepresentationPart->uri.'" width="'.$displayWidth.'" height="'.$displayHeight.'" style="'.$margins.'"'.$attrStr.' />';
402

    
403
		if($addPassePartout){
404
			$out .= '</div>';
405
		}
406
		return $out;
407
	}
408

    
409
}
410

    
411
function theme_cdm_media_gallerie_application($mediaRepresentationPart, $maxExtend, $addPassePartout = FALSE, $attributes = null){
412

    
413
  if(isset($mediaRepresentationPart)){
414

    
415
    if($addPassePartout){
416
      $out .= '<div class="image-passe-partout" style="width:'.$maxExtend.'px; height:'.$maxExtend.'px;">';
417
    } else {
418
      // do not add margins if no pass partout is shown
419
      $margins = '';
420
    }
421
    $out .= '<div class="appication">Web Application</div>';
422

    
423
    if($addPassePartout){
424
      $out .= '</div>';
425
    }
426
    return $out;
427
  }
428

    
429
}
430

    
431
function theme_cdm_media_gallerie_text($mediaRepresentationPart, $maxExtend, $addPassePartout = FALSE, $attributes = null){
432

    
433
  if(isset($mediaRepresentationPart)){
434

    
435
    if($addPassePartout){
436
      $out .= '<div class="image-passe-partout" style="width:'.$maxExtend.'px; height:'.$maxExtend.'px;">';
437
    } else {
438
      // do not add margins if no pass partout is shown
439
      $margins = '';
440
    }
441
    $out .= '<div class="appication">Web Application</div>';
442

    
443
    if($addPassePartout){
444
      $out .= '</div>';
445
    }
446
    return $out;
447
  }
448

    
449
}
450

    
451
function theme_cdm_openlayers_image($mediaRepresentationPart, $maxExtend){
452

    
453
	// see http://trac.openlayers.org/wiki/UsingCustomTiles#UsingTilesWithoutaProjection
454
	// and http://trac.openlayers.org/wiki/SettingZoomLevels
455

    
456
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/OpenLayers/OpenLayers.js', 'core', 'header');
457

    
458
  //TODO megre code below with code from theme_cdm_media_gallerie_image
459
	//var_dump("MEDIA URI: " . $mediaRepresentationPart->uri);
460
	//TODO merge code below with code from theme_cdm_media_gallerie_image
461
	$w = $mediaRepresentationPart->width;
462
	$h = $mediaRepresentationPart->height;
463

    
464
	if($w == 0 || $h == 0){
465
		$image_uri = str_replace(' ','%20',$mediaRepresentationPart->uri); //take url and replace spaces
466
		$imageDimensions = getimagesize_remote($image_uri);
467
		if(!$imageDimensions){
468
			return '<div>'.t('Image unavailable, uri:').$mediaRepresentationPart->uri.'</div>';
469
		}
470
		$w = $imageDimensions[0];
471
		$h = $imageDimensions[1];
472
	}
473

    
474
	// calculate  maxResolution (default is 360 deg / 256 px) and the bounds
475
	if($w > $h){
476
		$lat = 90;
477
		$lon = 90 * ($h / $w);
478
		$maxRes = $w / $maxExtend;
479
	} else {
480
		$lat = 90 * ($w / $h);
481
		$lon = 90;
482
		$maxRes =  $h / $maxExtend ;
483
	}
484

    
485
	$maxRes *= 1;
486
	drupal_add_js('
487
 var map;
488

    
489
 var imageLayerOptions={
490
     maxResolution: '.$maxRes.',
491
     maxExtent: new OpenLayers.Bounds(0, 0, '.$w.', '.$h.')
492
  };
493
  var mapOptions={
494
      controls:
495
       [
496
         new OpenLayers.Control.PanZoom(),
497
         new OpenLayers.Control.Navigation({zoomWheelEnabled: false, handleRightClicks:true, zoomBoxKeyMask: OpenLayers.Handler.MOD_CTRL})
498
       ],
499
     restrictedExtent:  new OpenLayers.Bounds(0, 0, '.$w.', '.$h.')
500
  };
501

    
502
 var graphic = new OpenLayers.Layer.Image(
503
          \'Image Title\',
504
          \''.$mediaRepresentationPart->uri.'\',
505
          new OpenLayers.Bounds(0, 0, '.$w.', '.$h.'),
506
          new OpenLayers.Size('.$w.', '.$h.'),
507
          imageLayerOptions
508
          );
509

    
510
 function init() {
511
   map = new OpenLayers.Map(\'openlayers_image\', mapOptions);
512
   map.addLayers([graphic]);
513
   map.setCenter(new OpenLayers.LonLat(0, 0), 1);
514
   map.zoomToMaxExtent();
515
 }
516

    
517
$(document).ready(function(){
518
  init();
519

    
520
});'
521
, 'inline');
522
$out = '<div id="openlayers_image" class="image_viewer" style="width: '.$maxExtend.'px; height:'.($maxExtend).'px"></div>';
523
return $out;
524

    
525
}
526

    
527

    
528
function theme_cdm_preferredImage($media, $defaultRepresentationPart, $imageMaxExtend, $parameters = ''){
529

    
530
	if(isset($media[0])){
531
		$representationPart = $media[0]->representations[0]->parts[0];
532
		if($parameters){
533
			$representationPart->uri.$parameters;
534
		}
535
	} else {
536
		$representationPart = $defaultRepresentationPart;
537
	}
538

    
539
	//$widthAndHeight = ($imageWidth ? ' width="'.$imageWidth : '').($imageHeight ? '" height="'.$imageHeight : '');
540
	//  $imageUri = $preferredMedia ? $preferredMedia->representations[0]->parts[0]->uri . $parameters : $defaultImage;
541
	$attributes = array('alt'=>($preferredMedia ? $preferredMedia->representations[0]->parts[0]->uri : "no image available"));
542
	$out .= theme('cdm_media_gallerie_image', $representationPart, $imageMaxExtend, false, $attributes);
543
	// $out = '<img class="left" '.$widthAndHeight.' " src="'.$imageUri.'" alt="'.$altText.'" />';
544
	return $out;
545
}
(3-3/8)