Project

General

Profile

Download (23 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

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

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

    
147

    
148
  // Filename.
149
  if (!empty($media->representations[0]->parts[0]->uri)) {
150
    $fileUri = $media->representations[0]->parts[0]->uri;
151
    $filename = substr($fileUri, strrpos($fileUri, "/") + 1);
152
    $metadata['filename'] = $filename;
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->artist->titleCache)) {
176
    $metadata['artist'] = $media->artist->titleCache;
177
  } else if (!empty($media_metadata->Artist)) {
178
    $metadata['artist'] = '' . $media_metadata->Artist;
179
  }
180

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

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

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

    
206
  /*
207
   // Creation date.
208
   if($media_metadata["Modify Date"])
209
   $metadata['mediacreated'] = $media_metadata["Modify Date"];
210
   else
211
   */
212
  if (!empty($media->mediaCreated)) {
213
    $metadata['mediacreated'] =  timePeriodToString($media->mediaCreated);
214
  }
215

    
216
  return $metadata;
217
}
218

    
219

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

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

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

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

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

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

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

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

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

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

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

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

    
343
  for ($r = 0; ($r < $maxRows || !$maxRows) && count($mediaList) > 0; $r++) {
344
    $captionParts = array();
345
    $mediaIndex = 0;
346
    $out .= '<tr>';
347
    for ($c = 0; $c < $cols; $c++) {
348
      $media = array_shift($mediaList);
349
        $complete_media = cdm_ws_get(CDM_WS_MEDIA, $media->uuid);
350
      if (isset($complete_media->representations[0]->parts[0])) {
351

    
352
        //
353
        // Find preferred representation.
354
        //
355

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

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

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

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

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

    
427
        // --- preparing the media caption
428

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

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

    
454
        $captionParts[] = $captionPartHtml;
455

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

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

    
494
  return $out;
495
}
496

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

    
509
  $out = '';
510

    
511
  // TODO merge with theme_cdm_media_mime_image?
512
  if (isset($mediaRepresentationPart)) {
513

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

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

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

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

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

    
570
  return $out;
571
}
572

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

    
585
  $out = '';
586

    
587
  if (isset($mediaRepresentationPart)) {
588

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

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

    
599
  return $out;
600
}
601

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

    
614
  $out = '';
615

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

    
621
    $out .= '<div class="application">Web Application</div>';
622

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

    
628
  return $out;
629
}
630

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

    
647
  _add_js_openlayers();
648

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

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

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

    
674
  $maxRes *= 1;
675

    
676
  drupal_add_js('
677
 var map;
678

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

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

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

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