Project

General

Profile

Download (22.1 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;
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_metadata->ObjectName)) {
159
    $metadata['title'] = $media_metadata->ObjectName;
160
  }
161
  elseif (!empty($media_metadata_aux['Object Name'])) {
162
    $metadata['title'] = $media_metadata_aux['Object Name'];
163
  }
164
  elseif (!empty($media->title_L10n)) {
165
    $metadata['title'] = $media->title_L10n;
166
  }
167
  elseif (!empty($media->titleCache)) {
168
    $metadata['title'] = $media->titleCache;
169
  }
170

    
171
  // Append description to title.
172
  if (!empty($media->description_L10n)) {
173
    $metadata['title'] .= '<span class="media-description">' . $media->description_L10n . '<span>';
174
  }
175

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

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

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

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

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

    
217
  return $metadata;
218
}
219

    
220

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

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

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

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

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

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

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

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

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

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

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

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

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

    
351
      if (isset($media->representations[0]->parts[0])) {
352

    
353
        //
354
        // Find preferred representation.
355
        //
356
        $preferred_media_representations_list = cdm_preferred_media_representations($media, array(
357
          'image/jpg',
358
          'image/jpeg',
359
          'image/png',
360
          'image/gif',
361
        ), $maxExtend, $maxExtend);
362
        if (count($preferred_media_representations_list) == 0) {
363
          // Fallback to using the first one in the list.
364
          $preferred_media_representations_list = $media->representations;
365
        }
366
        $preferred_media_representation = array_shift($preferred_media_representations_list);
367

    
368
        // $preferred_media_representation->parts[0]->uri =
369
        // "http://127.0.0.1/images/palmae/palm_tc_14415_1.jpg";
370
        $contentTypeDirectory = media_content_type_dir($preferred_media_representation, 'application');
371

    
372
        $mediaPartHtml = call_user_func_array(
373
          'cdm_media_gallerie_' . $contentTypeDirectory,
374
          array( $preferred_media_representation->parts[0], $maxExtend, TRUE)
375
        );
376
        // --- Compose Media Link.
377
        $mediaLinkUri = FALSE;
378
        if ($alternativeMediaUri) {
379
          if (isset($alternativeMediaUri[$mediaIndex])) {
380
            $mediaLinkUri = $alternativeMediaUri[$mediaIndex];
381
          }
382
          if (is_string($alternativeMediaUri)) {
383
            $mediaLinkUri = $alternativeMediaUri;
384
          }
385
        }
386
        else {
387
          $mediaLinkUri = $preferred_media_representation->parts[0]->uri;
388
        }
389
        $mediaIndex++;
390

    
391
        // media captions will be loaded via AHAH
392
        _add_js_ahah();
393
        $content_url = cdm_compose_url(CDM_WS_PORTAL_MEDIA, $media->uuid);
394
        $cdm_proxy_url_caption = url('cdm_api/proxy/' . urlencode($content_url) . "/cdm_media_caption/" . serialize($captionElements));
395
        $ahah_media_caption =  '<div class="ahah-content" data-cdm-ahah-url="' . $cdm_proxy_url_caption . '">'
396
          . '<span class="loading" style="display: none;">' . loading_image_html() . '</span></div>';
397

    
398
        // preparing the part link (= click on image iteself) which can be handled in two ways
399
        //
400
        //  1. open image in lightbox, the captions in the lighhtbox will be loaded via AHAH
401
        //  2. open the media in a new window with target 'specimen'
402
        if ($mediaLinkType == 'LIGHTBOX' && $contentTypeDirectory == 'image') {
403
          $mediaPartLinkAttributes['class'] = array('lightbox');
404
        }
405
        else {
406
          $mediaPartLinkAttributes['target'] = "specimen";
407
          $openMediaLinkAttributes['target'] = "specimen";
408
        }
409
        $mediaPartLinkAttributes['alt'] = htmlentities($ahah_media_caption);
410

    
411
        // --- preparing the media caption
412

    
413
        /* old comment: "no caption elements to show up here except the $caption_link_uri, if at all"
414
         *
415
         * a.kohlbecker 2013-03-14 :
416
         *   It is unclear why no caption elements should be shown, Was it a technical reason?
417
         *   see commit r16723 740177eb-a1d8-4ec3-a630-accd905eb3da
418
         *   If not problems arise with this remove it after some weeks
419
         */
420
        $captionPartHtml = $ahah_media_caption;
421

    
422
        if ($caption_link_uri) {
423
          if ($contentTypeDirectory == 'image') {
424
            // it is an image, so open it in the media page
425
            $captionPartHtml .= '<div class="media-caption-link">' . l($caption_link_uri, path_to_media($media->uuid), array(
426
                'attributes' => array(), 'html' => TRUE,
427
              )) . '</div>';
428
          }
429
          else {
430
            // otherwise open it directly and let the the browser handle the media type
431
            $openMediaLinkAttributes['absolute'] = TRUE;
432
            $captionPartHtml .= '<div class="media-caption-link">' . l($caption_link_uri, $mediaLinkUri, array(
433
                'attributes' => $openMediaLinkAttributes, 'html' => TRUE,
434
              )) . '</div>';
435
          }
436
        }
437

    
438
        $captionParts[] = $captionPartHtml;
439

    
440
        // --- Surround imagePart with link, this .
441
        if ($doLink) {
442
          $mediaPartHtml = l($mediaPartHtml, $mediaLinkUri, array(
443
            'attributes' => $mediaPartLinkAttributes, 'html' => TRUE,
444
          ));
445
        }
446
      }
447
      else {
448
        $mediaPartHtml = '';
449
        $captionParts[] = '';
450
      }
451
      $out .= '<td class="media">' . $mediaPartHtml . '</td>';
452
    }
453
    $out .= '</tr>'; // End of media parts.
454
    if ($showCaption) {
455
      if ( (is_array($captionElements) && count($captionElements) > 0) || $caption_link_uri) {
456
        $out .= '<tr>';
457
        // Add caption row.
458
        foreach ($captionParts as $captionPartHtml) {
459
          $out .= '<td class="caption">' . $captionPartHtml . '</td>';
460
        }
461
        $out .= '</tr>';
462
      }
463
    }
464
  }
465

    
466
  if ($galleryLinkUri) {
467
    if (count($mediaList) > 0) {
468
      $moreHtml = count($mediaList) . ' ' . t('more in gallery');
469
    }
470
    else {
471
      $moreHtml = t('open gallery');
472
    }
473
    $moreHtml = l($moreHtml, $galleryLinkUri);
474
    $out .= '<tr><td colspan="' . $cols . '">' . $moreHtml . '</td></tr>';
475
  }
476
  $out .= '</table>';
477
  return $out;
478
}
479

    
480
/**
481
 * Creates markup for a CDM MediaRepresentation which is referencing an image.
482
 *
483
 * @param $mediaRepresentationPart
484
 * @param $maxExtend
485
 * @param $addPassePartout
486
 * @param $attributes
487
 * @return string
488
 *   The markup for the media representation
489
 */
490
function cdm_media_gallerie_image($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
491

    
492
  $out = '';
493

    
494
  // TODO merge with theme_cdm_media_mime_image?
495
  if (isset($mediaRepresentationPart)) {
496

    
497
    $h = $mediaRepresentationPart->height;
498
    $w = $mediaRepresentationPart->width;
499
    if ($w == 0 || $h == 0) {
500
      // Take url and replace spaces.
501
      $image_uri = str_replace(' ', '%20', $mediaRepresentationPart->uri);
502
      $imageDimensions = getimagesize_remote($image_uri);
503
      if (!$imageDimensions) {
504
        return '<div>' . t('Image unavailable, uri: ') . $mediaRepresentationPart->uri . '</div>';
505
      }
506
      $w = $imageDimensions[0];
507
      $h = $imageDimensions[1];
508
    }
509

    
510
    $ratio = $w / $h;
511
    if ($ratio > 1) {
512
      $displayHeight = round($maxExtend / $ratio);
513
      $displayWidth = $maxExtend;
514
      $m = round(($maxExtend - $displayHeight) / 2);
515
      $margins = 'margin:' . $m . 'px 0 ' . $m . 'px 0;';
516
    }
517
    else {
518
      $displayHeight = $maxExtend;
519
      $displayWidth = round($maxExtend * $ratio);
520
      $m = round(($maxExtend - $displayWidth) / 2);
521
      $margins = 'margin:0 ' . $m . 'px 0 ' . $m . 'px;';
522
    }
523

    
524
    // Turn attributes array into string.
525
    if(!is_array($attributes)){
526
      $attributes = array();
527
    }
528
    if(!isset($attributes['alt'])){
529
      $attributes['alt'] = check_plain($mediaRepresentationPart->uri);
530
    }
531
    $attrStr = ' ';
532
    // $attributes['title'] = 'h:'.$h.', w:'.$w.',ratio:'.$ratio;
533
    if (is_array($attributes)) {
534
      foreach ($attributes as $name => $value) {
535
        $attrStr .= $name . '="' . $value . '" ';
536
      }
537
    }
538

    
539
    if ($addPassePartout) {
540
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
541
    }
542
    else {
543
      // Do not add margins if no pass partout is shown.
544
      $margins = '';
545
    }
546
    $out .= '<img src="' . $mediaRepresentationPart->uri . '" width="' . $displayWidth . '" height="' . $displayHeight . '" style="' . $margins . '"' . $attrStr . ' />';
547

    
548
    if ($addPassePartout) {
549
      $out .= '</div>';
550
    }
551
  }
552

    
553
  return $out;
554
}
555

    
556
/**
557
 * Creates markup for a CDM MediaRepresentation which is referencing an web application.
558
 *
559
 * @param $mediaRepresentationPart
560
 * @param $maxExtend
561
 * @param $addPassePartout
562
 * @param $attributes
563
 * @return string
564
 *   The markup for the media representation
565
 */
566
function cdm_media_gallerie_application($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
567

    
568
  $out = '';
569

    
570
  if (isset($mediaRepresentationPart)) {
571

    
572
    if ($addPassePartout) {
573
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
574
    }
575
    $out .= '<div class="application">Web Application</div>';
576

    
577
    if ($addPassePartout) {
578
      $out .= '</div>';
579
    }
580
  }
581

    
582
  return $out;
583
}
584

    
585
/**
586
 * Creates markup for a CDM MediaRepresentation which is referencing an web application.
587
 *
588
 * @param $mediaRepresentationPart
589
 * @param $maxExtend
590
 * @param $addPassePartout
591
 * @param $attributes
592
 * @return string
593
 *   The markup for the media representation
594
 */
595
function cdm_media_gallerie_text($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
596

    
597
  $out = '';
598

    
599
  if (isset($mediaRepresentationPart)) {
600
    if ($addPassePartout) {
601
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
602
    }
603

    
604
    $out .= '<div class="application">Web Application</div>';
605

    
606
    if ($addPassePartout) {
607
      $out .= '</div>';
608
    }
609
  }
610

    
611
  return $out;
612
}
613

    
614
/**
615
 * Adds the OpenLayers based image viewer to the page.
616
 *
617
 * The OpenLayers based image viewer allows to zoom and pan the displayed image.
618
 *
619
 * Documentation related to using Openlayers in order to view images is found here:
620
 *  - @see http://trac.openlayers.org/wiki/UsingCustomTiles#UsingTilesWithoutaProjection
621
 *  - @see http://trac.openlayers.org/wiki/SettingZoomLevels
622
 *
623
 * @param array $variables
624
 *   An associative array of theme variables:
625
 *   - mediaRepresentationPart: The CDM MediaRepresentationPart instance to be displayed.
626
 *   - maxExtend: The maximum extend of the image viewer view port.
627
 */
628
function cdm_openlayers_image($mediaRepresentationPart, $maxExtend) {
629

    
630
  _add_js_openlayers();
631

    
632
  // TODO merge code below with code from theme_cdm_media_gallerie_image
633
  // var_dump("MEDIA URI: " . $mediaRepresentationPart->uri);
634
  // TODO merge code below with code from theme_cdm_media_gallerie_image
635
  $w = $mediaRepresentationPart->width;
636
  $h = $mediaRepresentationPart->height;
637

    
638
  if ($w == 0 || $h == 0) {
639
    // Take url and replace spaces.
640
    $image_uri = str_replace(' ', '%20', $mediaRepresentationPart->uri);
641
    $imageDimensions = getimagesize_remote($image_uri);
642
    if (!$imageDimensions) {
643
      return '<div>' . t('Image unavailable, uri:') . $mediaRepresentationPart->uri . '</div>';
644
    }
645
    $w = $imageDimensions[0];
646
    $h = $imageDimensions[1];
647
  }
648

    
649
  // Calculate maxResolution
650
  if ($w > $h) {
651
    $maxRes = $w / $maxExtend;
652
  }
653
  else {
654
    $maxRes = $h / $maxExtend;
655
  }
656

    
657
  $maxRes *= 1;
658

    
659
  drupal_add_js('
660
 var map;
661

    
662
 var imageLayerOptions={
663
     maxResolution: ' . $maxRes . ',
664
     maxExtent: new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . ')
665
  };
666
  var mapOptions={
667
      controls:
668
       [
669
         new OpenLayers.Control.PanZoom(),
670
         new OpenLayers.Control.Navigation({zoomWheelEnabled: false, handleRightClicks:true, zoomBoxKeyMask: OpenLayers.Handler.MOD_CTRL})
671
       ],
672
     restrictedExtent:  new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . ')
673
  };
674

    
675
 var graphic = new OpenLayers.Layer.Image(
676
          \'Image Title\',
677
          \'' . $mediaRepresentationPart->uri . '\',
678
          new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . '),
679
          new OpenLayers.Size(' . $w . ', ' . $h . '),
680
          imageLayerOptions
681
          );
682

    
683
 function init() {
684
   map = new OpenLayers.Map(\'openlayers_image\', mapOptions);
685
   map.addLayers([graphic]);
686
   map.setCenter(new OpenLayers.LonLat(0, 0), 1);
687
   map.zoomToMaxExtent();
688
 }
689

    
690
jQuery(document).ready(function(){
691
  init();
692
});', array('type' => 'inline'));
693
  $out = '<div id="openlayers_image" class="image_viewer" style="width: ' . $maxExtend . 'px; height:' . ($maxExtend) . 'px"></div>';
694
  return $out;
695
}
(4-4/10)