Project

General

Profile

Download (22.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Functions for dealing with CDM entities of type SpeciemenOrOccurrences
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 *
15
 * @author
16
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
17
 */
18

    
19
/**
20
 * Returns an array of render array entries for a HTML description list.
21
 *
22
 * @see theme_description_list()
23
 *
24
 * @param array $rights_list
25
 *   array of CDM Rights entities
26
 *
27
 * @return array
28
 *   the render array of the groups for the HTML description list
29
 */
30
function cdm_rights_as_dl_groups($rights_list) {
31
  $copyrights = array();
32

    
33
  $licenses = array();
34
  $access_rights = array();
35
  $unknown = array();
36

    
37
  foreach ($rights_list as $right) {
38
    if (!is_object($right)) {
39
      continue;
40
    }
41
    $type_uuid = isset($right->type->uuid) ? $right->type->uuid : 'UNKNOWN';
42
    switch ($type_uuid) {
43

    
44
      case UUID_RIGHTS_COPYRIGHT:
45
        $text = '';
46
        if (isset($right->text) ){
47
          // sanitize potential '(c)' away
48
          $text  = preg_replace('/^\(c\)/', '', $right->text);
49
        } if (isset($right->agent) ){
50
          $text  .= (strlen($text) > 0 ? ', ' : '') . $right->agent->titleCache;
51
        }
52

    
53
        $copyrights[] = array('#markup' => '&copy; ' . $text);
54
        break;
55

    
56
      case UUID_RIGHTS_LICENCE:
57
        $license_str = '';
58
        if (isset($right->abbreviatedText)) {
59
          $license_str .= $right->abbreviatedText;
60
        }
61
        if (isset($right->uri)) {
62
          if (strlen($license_str) > 0) {
63
            $license_str = l($license_str, $right->uri);
64
          }
65
          else {
66
            $license_str = l(t('link'), $right->uri);
67
          }
68
        }
69
        if (strlen($license_str) > 0 && isset($right->text)) {
70
          $license_str .= ': ' . $right->text;
71
        }
72
        $licenses[] = array('#markup' => $license_str);
73
        break;
74

    
75
      case UUID_RIGHTS_ACCESS_RIGHTS:
76
        $access_rights[] = array('#markup' => $right->text);
77
        break;
78

    
79
      default:
80
        $unknown_groups[] = array('#markup' => $right->text);
81
    }
82
  }
83

    
84
  $groups = array();
85
  if (count($copyrights) > 0) {
86
    _description_list_group_add($groups, t('Copyright') . ':', $copyrights);
87
  }
88
  if (count($licenses) > 0) {
89
    _description_list_group_add($groups, t('Licenses') . ':', $licenses);
90
  }
91
  if (count($access_rights) > 0) {
92
    _description_list_group_add($groups, t('Access rights') . ':', $access_rights);
93
  }
94
  if (count($unknown) > 0) {
95
    _description_list_group_add($groups, t('Rights') . ':', $unknown);
96
  }
97

    
98
  return $groups;
99

    
100
}
101

    
102

    
103
/**
104
 * Provides the markup for an icon to represent a media which is associated with the given $feature.
105
 *
106
 * @param $feature
107
 *   the cdm Feature term
108
 * @param $media_url
109
 *   Optional, currently unused. May be used in future to display different
110
 *   icons for different media urls, like the fav-icon of the referenced
111
 * @return string
112
 *   The markup for the icon
113
 */
114
function media_feature_icon($feature, $media_url = NULL) {
115
  return font_awesome_icon_markup('fa-book', array('alt' => $feature->representation_L10n));
116
}
117

    
118
/**
119
 * Gets the metadata info such as title or artist and source references of a media file.
120
 *
121
 * The function tries at first to get all the info from the file metadata
122
 * and if it is not available look at the media file info stored at the database.
123
 *
124
 * @param mixed $media
125
 *   The media file object for which to get the metadata.
126
 *
127
 * @return array
128
 *   The array with the available specified metadata info.
129
 */
130
function read_media_metadata($media) {
131

    
132
  $metadata = array(
133
    'title' => '',// Media_metadata and media.
134
    'artist' => '',// Media_metadata and media.
135
    'rights',// Media_metadata and media.
136
    'location',// Media_metadata.
137
    'filename' => '',// Media.
138
    'mediacreated' => '', // Media.
139
    'description' => ''
140
  );
141

    
142
  // Getting the media metadata.
143
  $media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
144
  $media_metadata_aux = (array) $media_metadata;
145

    
146

    
147
  // Filename.
148
  if (!empty($media->representations[0]->parts[0]->uri)) {
149
    $fileUri = $media->representations[0]->parts[0]->uri;
150
    $filename = substr($fileUri, strrpos($fileUri, "/") + 1);
151
    $metadata['filename'] = $filename;
152
  }
153
  else {
154
    $metadata['filename'] = '';
155
  }
156

    
157
  // Title.
158
  if (!empty($media->title_L10n)) {
159
    $metadata['title'] = $media->title_L10n;
160
  } else if ($media->protectedTitleCache && !empty($media->titleCache)) {
161
    $metadata['title'] = $media->titleCache;
162
  } else if (!empty($media_metadata->ObjectName)) {
163
    $metadata['title'] = $media_metadata->ObjectName;
164
  } else  if (!empty($media_metadata_aux['Object Name'])) {
165
    $metadata['title'] = $media_metadata_aux['Object Name'];
166
  } else if (!empty($media->titleCache)) {
167
    $metadata['title'] = $media->titleCache;
168
  }
169

    
170
  if(!empty($media->description_L10n)){
171
    $metadata['description'] = $media->description_L10n;
172
  }
173

    
174
  // Artist.
175
  if (!empty($media_metadata->Artist)) {
176
    $metadata['artist'] = '' . $media_metadata->Artist;
177
  }
178
  elseif (!empty($media->artist->titleCache)) {
179
    $metadata['artist'] = $media->artist->titleCache;
180
  }
181

    
182
  // Rights
183
  $metadata['rights'] = array();
184
  if (!empty($media_metadata->Copyright)) {
185
    $rightsObj =  new stdClass();
186
    $rightsObj->type = new stdClass();
187
    $rightsObj->type->uuid = UUID_RIGHTS_COPYRIGHT;
188
    $rightsObj->type->titleCache = 'copyright';
189
    $rightsObj->type->representation_L10n = 'copyright';
190
    $rightsObj->type->text = $media_metadata->Copyright;
191
    $metadata['rights'][] = $rightsObj;
192
  }
193
  if(isset($media->rights) && is_array($media->rights)) {
194
    $metadata['rights'] = array_merge($metadata['rights'], $media->rights);
195
  }
196

    
197
  // Filling the description (though there is no description in the db???).
198
  // $metadata_caption['description'] = $media->description_L10n;
199

    
200
  // Location.
201
  $metadata['location'] = array();
202
  $metadata['location']['sublocation'] = !empty($media_metadata->Sublocation) ? $media_metadata->Sublocation : FALSE;
203
  $metadata['location']['city'] = !empty($media_metadata->City) ? $media_metadata->City : FALSE;
204
  $metadata['location']['province'] = !empty($media_metadata->Province) ? $media_metadata->Province : FALSE;
205
  $metadata['location']['country'] = !empty($media_metadata->Country)? $media_metadata->Country : FALSE;
206

    
207
  /*
208
   // Creation date.
209
   if($media_metadata["Modify Date"])
210
   $metadata_caption['mediacreated'] = $media_metadata["Modify Date"];
211
   else
212
   $metadata_caption['mediacreated'] = $media->created;
213
   */
214

    
215
  return $metadata;
216
}
217

    
218

    
219
/**
220
 * Return HTML for a media gallery
221
 *
222
 * @param array $configuration
223
 *   An associative array containing:
224
 *   - mediaList: An array of Media entities.
225
 *   - maxExtend
226
 *   - cols
227
 *   - maxRows
228
 *   - showCaption:  boolean value, whether to show captions or not.
229
 *   - captionElements: An array of caption elements to be shown. In case the array
230
 *        is empty of NULL  all available caption elements will be show. In order to
231
 *        supress all captions  set 'showCaption' to FALSE
232
 *        example:
233
 *          1) Show 'title', 'description', 'file', 'filename' in the caption:
234
 *            array('title', 'description', 'file', 'filename'),
235
 *          2) To add an addtional link at the bottom of  the caption:
236
 *            array('titlecache', '#uri'=>t('Open Image')) this will cause a link
237
 *            to be rendered with label 'Open image' which will open the according
238
 *            media object.
239
 *   - mediaLinkType: Valid values:
240
 *      - "NONE": do not link the images,
241
 *      - "LIGHTBOX": open the link in a light box,
242
 *      - "NORMAL": link to the image page or to the $alternativeMediaUri if
243
 *        it is defined.
244
 *   - alternativeMediaUri: A fix string or an array of alternative URIs to link the images
245
 *     which will overwrite the URIs of the media parts.
246
 *     The order of URI in this array must correspond with the order of
247
 *     images in $mediaList.
248
 *   - galleryLinkUri: An URI to link the the hint on more images to;
249
 *     if NULL no link is created.
250
 *
251
 * @return array
252
 *   A render array for the image gallery
253
 * @ingroup: compose
254
 */
255
function compose_cdm_media_gallerie($configuration) {
256

    
257
  $mediaList = $configuration['mediaList'];
258

    
259
  // Do not show an empty gallery.
260
  if (count($mediaList) == 0) {
261
    return '';
262
  }
263

    
264
  // merge with default
265
  $configuration = array_merge(
266
    array(
267
      'mediaList' => NULL,
268
      'galleryName' => NULL,
269
      'maxExtend' => 150,
270
      'cols' => 4,
271
      'maxRows' => FALSE,
272
      'captionElements' => array('title'),
273
      'mediaLinkType' => 'LIGHTBOX',
274
      'alternativeMediaUri' => NULL,
275
      'galleryLinkUri' => NULL,
276
      'showCaption' => TRUE,
277
    ),
278
    $configuration);
279

    
280
  $galleryName = $configuration['galleryName'];
281
  $maxExtend = $configuration['maxExtend'];
282
  $cols = $configuration['cols'];
283
  $maxRows = $configuration['maxRows'];
284
  $captionElements = $configuration['captionElements'];
285
  $mediaLinkType = $configuration['mediaLinkType'];
286
  $alternativeMediaUri = $configuration['alternativeMediaUri'];
287
  $galleryLinkUri = $configuration['galleryLinkUri'];
288
  $showCaption = $configuration['showCaption'];
289

    
290
  $caption_link_uri = '';
291
  if(isset($captionElements['#uri'])){
292
    $caption_link_uri = $captionElements['#uri'];
293
    unset($captionElements['#uri']);
294
  }
295
  if (!is_array($captionElements) || count($captionElements) == 0) {
296
    $captionElements = NULL;
297
  }
298

    
299
  // TODO correctly handle multiple media representation parts
300
  $_SESSION['cdm']['last_gallery'] = current_path();
301
  // Prevent from errors.
302
  if (!isset($mediaList[0])) {
303
    // return;
304
  }
305

    
306
  // --- Duplicate suppression: media can be reused but should only be shown
307
  // once.
308
  $tempMediaList = array();
309
  $tempMediaUuids = array();
310
  foreach ($mediaList as $media) {
311
    if (!in_array($media->uuid, $tempMediaUuids)) {
312
      $tempMediaList[] = $media;
313
      $tempMediaUuids[] = $media->uuid;
314
    }
315
  }
316
  $mediaList = $tempMediaList;
317

    
318
  // ---
319
  $galleryID = "media_gallery_" . $galleryName;
320

    
321
  $mediaPartLinkAttributes = array();
322
  $openMediaLinkAttributes = array();
323

    
324
  // Prepare media links.
325
  $doLink = FALSE;
326
  if ($mediaLinkType != 'NONE') {
327
    $doLink = TRUE;
328
  }
329
  if ($mediaLinkType == 'LIGHTBOX') {
330
    $doLink = TRUE;
331
    _add_js_lightbox($galleryID);
332
  }
333

    
334
  // Render the media gallery grid.
335
  $out = '<table id="' . $galleryID . '" class="media_gallery">';
336
  $out .= '<colgroup>';
337
  for ($c = 0; $c < $cols; $c++) {
338
    $out .= '<col style="width:' . (100 / $cols) . '%;">';
339
  }
340
  $out .= '</colgroup>';
341

    
342
  for ($r = 0; ($r < $maxRows || !$maxRows) && count($mediaList) > 0; $r++) {
343
    $captionParts = array();
344
    $mediaIndex = 0;
345
    $out .= '<tr>';
346
    for ($c = 0; $c < $cols; $c++) {
347
      $media = array_shift($mediaList);
348

    
349
      if (isset($media->representations[0]->parts[0])) {
350

    
351
        //
352
        // Find preferred representation.
353
        //
354
        $thumbnail_representations = cdm_preferred_media_representations($media, array(
355
            'image/jpg',
356
            'image/jpeg',
357
            'image/png',
358
            'image/gif',
359
          ),
360
          $maxExtend,
361
          $maxExtend
362
        );
363
        // due to a bug the portal/taxon/{uuid}/media service only delivers a filtered media object
364
        // which only contains the thumbnail representation even if the height and width filters are not set.
365
        // --> #6970
366
        // to get hold of the full resolution images we send a separate request :
367
        $complete_media = cdm_ws_get(CDM_WS_MEDIA, $media->uuid);
368
        $full_size_representations = cdm_preferred_media_representations($complete_media, array(
369
            'image/jpg',
370
            'image/jpeg',
371
            'image/png',
372
            'image/gif',
373
          )
374
        );
375
        if (count($thumbnail_representations) == 0) {
376
          // Fallback to using the first one in the list.
377
          $thumbnail_representations = $media->representations;
378
          $full_size_representations = $media->representations;
379
        }
380
        $thumbnail_representation = array_shift($thumbnail_representations);
381
        $full_size_representation = array_shift($full_size_representations);
382

    
383
        // $preferred_media_representation->parts[0]->uri =
384
        // "http://127.0.0.1/images/palmae/palm_tc_14415_1.jpg";
385
        $contentTypeDirectory = media_content_type_dir($thumbnail_representation, 'application');
386

    
387
        $mediaPartHtml = call_user_func_array(
388
          'cdm_media_gallerie_' . $contentTypeDirectory,
389
          array( $thumbnail_representation->parts[0], $maxExtend, TRUE)
390
        );
391
        // --- Compose Media Link.
392
        $mediaLinkUri = FALSE;
393
        if ($alternativeMediaUri) {
394
          if (isset($alternativeMediaUri[$mediaIndex])) {
395
            $mediaLinkUri = $alternativeMediaUri[$mediaIndex];
396
          }
397
          if (is_string($alternativeMediaUri)) {
398
            $mediaLinkUri = $alternativeMediaUri;
399
          }
400
        }
401
        else {
402
          $mediaLinkUri = $full_size_representation->parts[0]->uri;
403
        }
404
        $mediaIndex++;
405

    
406
        // media captions will be loaded via AHAH
407
        _add_js_ahah();
408
        $content_url = cdm_compose_url(CDM_WS_PORTAL_MEDIA, $media->uuid);
409
        $cdm_proxy_url_caption = url('cdm_api/proxy/' . urlencode($content_url) . "/cdm_media_caption/" . serialize($captionElements));
410
        $ahah_media_caption =  '<div class="ahah-content" data-cdm-ahah-url="' . $cdm_proxy_url_caption . '">'
411
          . '<span class="loading" style="display: none;">' . loading_image_html() . '</span></div>';
412

    
413
        // preparing the part link (= click on image itself) which can be handled in two ways
414
        //
415
        //  1. open image in lightbox, the captions in the lightbox will be loaded via AHAH
416
        //  2. open the media in a new window with target 'specimen'
417
        if ($mediaLinkType == 'LIGHTBOX' && $contentTypeDirectory == 'image') {
418
          $mediaPartLinkAttributes['class'] = array('lightbox');
419
        } else {
420
          $mediaPartLinkAttributes['target'] = "specimen";
421
          $openMediaLinkAttributes['target'] = "specimen";
422
        }
423
        $mediaPartLinkAttributes['alt'] = htmlentities($ahah_media_caption);
424

    
425
        // --- preparing the media caption
426

    
427
        /* old comment: "no caption elements to show up here except the $caption_link_uri, if at all"
428
         *
429
         * a.kohlbecker 2013-03-14 :
430
         *   It is unclear why no caption elements should be shown, Was it a technical reason?
431
         *   see commit r16723 740177eb-a1d8-4ec3-a630-accd905eb3da
432
         *   If not problems arise with this remove it after some weeks
433
         */
434
        $captionPartHtml = $ahah_media_caption;
435

    
436
        if ($caption_link_uri) {
437
          if ($contentTypeDirectory == 'image') {
438
            // it is an image, so open it in the media page
439
            $captionPartHtml .= '<div class="media-caption-link">' . l($caption_link_uri, path_to_media($media->uuid), array(
440
                'attributes' => array(), 'html' => TRUE,
441
              )) . '</div>';
442
          }
443
          else {
444
            // otherwise open it directly and let the the browser handle the media type
445
            $openMediaLinkAttributes['absolute'] = TRUE;
446
            $captionPartHtml .= '<div class="media-caption-link">' . l($caption_link_uri, $mediaLinkUri, array(
447
                'attributes' => $openMediaLinkAttributes, 'html' => TRUE,
448
              )) . '</div>';
449
          }
450
        }
451

    
452
        $captionParts[] = $captionPartHtml;
453

    
454
        // --- Surround imagePart with link, this .
455
        if ($doLink) {
456
          $mediaPartHtml = l($mediaPartHtml, $mediaLinkUri, array(
457
            'attributes' => $mediaPartLinkAttributes, 'html' => TRUE,
458
          ));
459
        }
460
      }
461
      else {
462
        $mediaPartHtml = '';
463
        $captionParts[] = '';
464
      }
465
      $out .= '<td class="media">' . $mediaPartHtml . '</td>';
466
    }
467
    $out .= '</tr>'; // End of media parts.
468
    if ($showCaption) {
469
      if ( (is_array($captionElements) && count($captionElements) > 0) || $caption_link_uri) {
470
        $out .= '<tr>';
471
        // Add caption row.
472
        foreach ($captionParts as $captionPartHtml) {
473
          $out .= '<td class="caption">' . $captionPartHtml . '</td>';
474
        }
475
        $out .= '</tr>';
476
      }
477
    }
478
  }
479

    
480
  if ($galleryLinkUri) {
481
    if (count($mediaList) > 0) {
482
      $moreHtml = count($mediaList) . ' ' . t('more in gallery');
483
    }
484
    else {
485
      $moreHtml = t('open gallery');
486
    }
487
    $moreHtml = l($moreHtml, $galleryLinkUri);
488
    $out .= '<tr><td colspan="' . $cols . '">' . $moreHtml . '</td></tr>';
489
  }
490
  $out .= '</table>';
491

    
492
  return $out;
493
}
494

    
495
/**
496
 * Creates markup for a CDM MediaRepresentation which is referencing an image.
497
 *
498
 * @param $mediaRepresentationPart
499
 * @param $maxExtend
500
 * @param $addPassePartout
501
 * @param $attributes
502
 * @return string
503
 *   The markup for the media representation
504
 */
505
function cdm_media_gallerie_image($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
506

    
507
  $out = '';
508

    
509
  // TODO merge with theme_cdm_media_mime_image?
510
  if (isset($mediaRepresentationPart)) {
511

    
512
    $h = $mediaRepresentationPart->height;
513
    $w = $mediaRepresentationPart->width;
514
    if ($w == 0 || $h == 0) {
515
      // Take url and replace spaces.
516
      $image_uri = str_replace(' ', '%20', $mediaRepresentationPart->uri);
517
      $imageDimensions = getimagesize_remote($image_uri);
518
      if (!$imageDimensions) {
519
        return '<div>' . t('Image unavailable, uri: ') . $mediaRepresentationPart->uri . '</div>';
520
      }
521
      $w = $imageDimensions[0];
522
      $h = $imageDimensions[1];
523
    }
524

    
525
    $ratio = $w / $h;
526
    if ($ratio > 1) {
527
      $displayHeight = round($maxExtend / $ratio);
528
      $displayWidth = $maxExtend;
529
      $m = round(($maxExtend - $displayHeight) / 2);
530
      $margins = 'margin:' . $m . 'px 0 ' . $m . 'px 0;';
531
    }
532
    else {
533
      $displayHeight = $maxExtend;
534
      $displayWidth = round($maxExtend * $ratio);
535
      $m = round(($maxExtend - $displayWidth) / 2);
536
      $margins = 'margin:0 ' . $m . 'px 0 ' . $m . 'px;';
537
    }
538

    
539
    // Turn attributes array into string.
540
    if(!is_array($attributes)){
541
      $attributes = array();
542
    }
543
    if(!isset($attributes['alt'])){
544
      $attributes['alt'] = check_plain($mediaRepresentationPart->uri);
545
    }
546
    $attrStr = ' ';
547
    // $attributes['title'] = 'h:'.$h.', w:'.$w.',ratio:'.$ratio;
548
    if (is_array($attributes)) {
549
      foreach ($attributes as $name => $value) {
550
        $attrStr .= $name . '="' . $value . '" ';
551
      }
552
    }
553

    
554
    if ($addPassePartout) {
555
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
556
    }
557
    else {
558
      // Do not add margins if no pass partout is shown.
559
      $margins = '';
560
    }
561
    $out .= '<img src="' . $mediaRepresentationPart->uri . '" width="' . $displayWidth . '" height="' . $displayHeight . '" style="' . $margins . '"' . $attrStr . ' />';
562

    
563
    if ($addPassePartout) {
564
      $out .= '</div>';
565
    }
566
  }
567

    
568
  return $out;
569
}
570

    
571
/**
572
 * Creates markup for a CDM MediaRepresentation which is referencing an web application.
573
 *
574
 * @param $mediaRepresentationPart
575
 * @param $maxExtend
576
 * @param $addPassePartout
577
 * @param $attributes
578
 * @return string
579
 *   The markup for the media representation
580
 */
581
function cdm_media_gallerie_application($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
582

    
583
  $out = '';
584

    
585
  if (isset($mediaRepresentationPart)) {
586

    
587
    if ($addPassePartout) {
588
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
589
    }
590
    $out .= '<div class="application">Web Application</div>';
591

    
592
    if ($addPassePartout) {
593
      $out .= '</div>';
594
    }
595
  }
596

    
597
  return $out;
598
}
599

    
600
/**
601
 * Creates markup for a CDM MediaRepresentation which is referencing an web application.
602
 *
603
 * @param $mediaRepresentationPart
604
 * @param $maxExtend
605
 * @param $addPassePartout
606
 * @param $attributes
607
 * @return string
608
 *   The markup for the media representation
609
 */
610
function cdm_media_gallerie_text($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
611

    
612
  $out = '';
613

    
614
  if (isset($mediaRepresentationPart)) {
615
    if ($addPassePartout) {
616
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
617
    }
618

    
619
    $out .= '<div class="application">Web Application</div>';
620

    
621
    if ($addPassePartout) {
622
      $out .= '</div>';
623
    }
624
  }
625

    
626
  return $out;
627
}
628

    
629
/**
630
 * Adds the OpenLayers based image viewer to the page.
631
 *
632
 * The OpenLayers based image viewer allows to zoom and pan the displayed image.
633
 *
634
 * Documentation related to using Openlayers in order to view images is found here:
635
 *  - @see http://trac.openlayers.org/wiki/UsingCustomTiles#UsingTilesWithoutaProjection
636
 *  - @see http://trac.openlayers.org/wiki/SettingZoomLevels
637
 *
638
 * @param array $variables
639
 *   An associative array of theme variables:
640
 *   - mediaRepresentationPart: The CDM MediaRepresentationPart instance to be displayed.
641
 *   - maxExtend: The maximum extend of the image viewer view port.
642
 */
643
function cdm_openlayers_image($mediaRepresentationPart, $maxExtend) {
644

    
645
  _add_js_openlayers();
646

    
647
  // TODO merge code below with code from theme_cdm_media_gallerie_image
648
  // var_dump("MEDIA URI: " . $mediaRepresentationPart->uri);
649
  // TODO merge code below with code from theme_cdm_media_gallerie_image
650
  $w = $mediaRepresentationPart->width;
651
  $h = $mediaRepresentationPart->height;
652

    
653
  if ($w == 0 || $h == 0) {
654
    // Take url and replace spaces.
655
    $image_uri = str_replace(' ', '%20', $mediaRepresentationPart->uri);
656
    $imageDimensions = getimagesize_remote($image_uri);
657
    if (!$imageDimensions) {
658
      return '<div>' . t('Image unavailable, uri:') . $mediaRepresentationPart->uri . '</div>';
659
    }
660
    $w = $imageDimensions[0];
661
    $h = $imageDimensions[1];
662
  }
663

    
664
  // Calculate maxResolution
665
  if ($w > $h) {
666
    $maxRes = $w / $maxExtend;
667
  }
668
  else {
669
    $maxRes = $h / $maxExtend;
670
  }
671

    
672
  $maxRes *= 1;
673

    
674
  drupal_add_js('
675
 var map;
676

    
677
 var imageLayerOptions={
678
     maxResolution: ' . $maxRes . ',
679
     maxExtent: new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . ')
680
  };
681
  var mapOptions={
682
      controls:
683
       [
684
         new OpenLayers.Control.PanZoom(),
685
         new OpenLayers.Control.Navigation({zoomWheelEnabled: false, handleRightClicks:true, zoomBoxKeyMask: OpenLayers.Handler.MOD_CTRL})
686
       ],
687
     restrictedExtent:  new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . ')
688
  };
689

    
690
 var graphic = new OpenLayers.Layer.Image(
691
          \'Image Title\',
692
          \'' . $mediaRepresentationPart->uri . '\',
693
          new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . '),
694
          new OpenLayers.Size(' . $w . ', ' . $h . '),
695
          imageLayerOptions
696
          );
697

    
698
 function init() {
699
   map = new OpenLayers.Map(\'openlayers_image\', mapOptions);
700
   map.addLayers([graphic]);
701
   map.setCenter(new OpenLayers.LonLat(0, 0), 1);
702
   map.zoomToMaxExtent();
703
 }
704

    
705
jQuery(document).ready(function(){
706
  init();
707
});', array('type' => 'inline'));
708
  $out = '<div id="openlayers_image" class="image_viewer" style="width: ' . $maxExtend . 'px; height:' . ($maxExtend) . 'px"></div>';
709
  return $out;
710
}
(4-4/10)