Project

General

Profile

« Previous | Next » 

Revision 2a3b0c8e

Added by Andreas Kohlbecker over 7 years ago

turning useless media related theme-functions into compose or plain markup functions - step 2:
moving functions to includes file

View differences:

modules/cdm_dataportal/theme/cdm_dataportal.media.theme
271 271
  return $out;
272 272
}
273 273

  
274
/**
275
 * Return HTML for a media gallery
276
 *
277
 * @param array $configuration
278
 *   An associative array containing:
279
 *   - mediaList: An array of Media entities.
280
 *   - maxExtend
281
 *   - cols
282
 *   - maxRows
283
 *   - showCaption:  boolean value, whether to show captions or not.
284
 *   - captionElements: An array of caption elements to be shown. In case the array
285
 *        is empty of NULL  all available caption elements will be show. In order to
286
 *        supress all captions  set 'showCaption' to FALSE
287
 *        example:
288
 *          1) Show 'title', 'description', 'file', 'filename' in the caption:
289
 *            array('title', 'description', 'file', 'filename'),
290
 *          2) To add an addtional link at the bottom of  the caption:
291
 *            array('titlecache', '#uri'=>t('Open Image')) this will cause a link
292
 *            to be rendered with label 'Open image' which will open the according
293
 *            media object.
294
 *   - mediaLinkType: Valid values:
295
 *      - "NONE": do not link the images,
296
 *      - "LIGHTBOX": open the link in a light box,
297
 *      - "NORMAL": link to the image page or to the $alternativeMediaUri if
298
 *        it is defined.
299
 *   - alternativeMediaUri: A fix string or an array of alternative URIs to link the images
300
 *     which will overwrite the URIs of the media parts.
301
 *     The order of URI in this array must correspond with the order of
302
 *     images in $mediaList.
303
 *   - galleryLinkUri: An URI to link the the hint on more images to;
304
 *     if NULL no link is created.
305
 *
306
 * @return array
307
 *   A render array for the image gallery
308
 * @ingroup: compose
309
 */
310
function compose_cdm_media_gallerie($configuration) {
311

  
312
  $mediaList = $configuration['mediaList'];
313

  
314
  // Do not show an empty gallery.
315
  if (count($mediaList) == 0) {
316
    return '';
317
  }
318

  
319
  // merge with default
320
  $configuration = array_merge(
321
    array(
322
      'mediaList' => NULL,
323
      'galleryName' => NULL,
324
      'maxExtend' => 150,
325
      'cols' => 4,
326
      'maxRows' => FALSE,
327
      'captionElements' => array('title'),
328
      'mediaLinkType' => 'LIGHTBOX',
329
      'alternativeMediaUri' => NULL,
330
      'galleryLinkUri' => NULL,
331
      'showCaption' => TRUE,
332
    ),
333
    $configuration);
334

  
335
  $galleryName = $configuration['galleryName'];
336
  $maxExtend = $configuration['maxExtend'];
337
  $cols = $configuration['cols'];
338
  $maxRows = $configuration['maxRows'];
339
  $captionElements = $configuration['captionElements'];
340
  $mediaLinkType = $configuration['mediaLinkType'];
341
  $alternativeMediaUri = $configuration['alternativeMediaUri'];
342
  $galleryLinkUri = $configuration['galleryLinkUri'];
343
  $showCaption = $configuration['showCaption'];
344

  
345
  $caption_link_uri = '';
346
  if(isset($captionElements['#uri'])){
347
    $caption_link_uri = $captionElements['#uri'];
348
    unset($captionElements['#uri']);
349
  }
350
  if (!is_array($captionElements) || count($captionElements) == 0) {
351
    $captionElements = NULL;
352
  }
353

  
354
  // TODO correctly handle multiple media representation parts
355
  $_SESSION['cdm']['last_gallery'] = current_path();
356
  // Prevent from errors.
357
  if (!isset($mediaList[0])) {
358
   // return;
359
  }
360

  
361
  // --- Duplicate supression: media can be reused but should only be shown
362
  // once.
363
  $tempMediaList = array();
364
  $tempMediaUuids = array();
365
  foreach ($mediaList as $media) {
366
    if (!in_array($media->uuid, $tempMediaUuids)) {
367
      $tempMediaList[] = $media;
368
      $tempMediaUuids[] = $media->uuid;
369
    }
370
  }
371
  $mediaList = $tempMediaList;
372

  
373
  // ---
374
  $galleryID = "media_gallery_" . $galleryName;
375

  
376
  $mediaPartLinkAttributes = array();
377
  $openMediaLinkAttributes = array();
378

  
379
  // Prepare media links.
380
  $doLink = FALSE;
381
  if ($mediaLinkType != 'NONE') {
382
    $doLink = TRUE;
383
  }
384
  if ($mediaLinkType == 'LIGHTBOX') {
385
    $doLink = TRUE;
386
    _add_js_lightbox($galleryID);
387
  }
388

  
389
  // Render the media gallery grid.
390
  $out = '<table id="' . $galleryID . '" class="media_gallery">';
391
  $out .= '<colgroup>';
392
  for ($c = 0; $c < $cols; $c++) {
393
    $out .= '<col style="width:' . (100 / $cols) . '%;">';
394
  }
395
  $out .= '</colgroup>';
396

  
397
  for ($r = 0; ($r < $maxRows || !$maxRows) && count($mediaList) > 0; $r++) {
398
    $captionParts = array();
399
    $mediaIndex = 0;
400
    $out .= '<tr>';
401
    for ($c = 0; $c < $cols; $c++) {
402
      $media = array_shift($mediaList);
403

  
404
      if (isset($media->representations[0]->parts[0])) {
405

  
406
        //
407
        // Find preferred representation.
408
        //
409
        $preferred_media_representations_list = cdm_preferred_media_representations($media, array(
410
          'image/jpg',
411
          'image/jpeg',
412
          'image/png',
413
          'image/gif',
414
        ), $maxExtend, $maxExtend);
415
        if (count($preferred_media_representations_list) == 0) {
416
          // Fallback to using the first one in the list.
417
          $preferred_media_representations_list = $media->representations;
418
        }
419
        $preferred_media_representation = array_shift($preferred_media_representations_list);
420

  
421
        // $preferred_media_representation->parts[0]->uri =
422
        // "http://127.0.0.1/images/palmae/palm_tc_14415_1.jpg";
423
        $contentTypeDirectory = media_content_type_dir($preferred_media_representation, 'application');
424

  
425
         $mediaPartHtml = call_user_func_array(
426
           'cdm_media_gallerie_' . $contentTypeDirectory,
427
           array( $preferred_media_representation->parts[0], $maxExtend, TRUE)
428
         );
429
        // --- Compose Media Link.
430
        $mediaLinkUri = FALSE;
431
        if ($alternativeMediaUri) {
432
          if (isset($alternativeMediaUri[$mediaIndex])) {
433
            $mediaLinkUri = $alternativeMediaUri[$mediaIndex];
434
          }
435
          if (is_string($alternativeMediaUri)) {
436
            $mediaLinkUri = $alternativeMediaUri;
437
          }
438
        }
439
        else {
440
          $mediaLinkUri = $preferred_media_representation->parts[0]->uri;
441
        }
442
        $mediaIndex++;
443

  
444
        // media captions will be loaded via AHAH
445
        _add_js_ahah();
446
        $content_url = cdm_compose_url(CDM_WS_PORTAL_MEDIA, $media->uuid);
447
        $cdm_proxy_url_caption = url('cdm_api/proxy/' . urlencode($content_url) . "/cdm_media_caption/" . serialize($captionElements));
448
        $ahah_media_caption =  '<div class="ahah-content" data-cdm-ahah-url="' . $cdm_proxy_url_caption . '">'
449
          . '<span class="loading" style="display: none;">' . loading_image_html() . '</span></div>';
450

  
451
        // preparing the part link (= click on image iteself) which can be handled in two ways
452
        //
453
        //  1. open image in lightbox, the captions in the lighhtbox will be loaded via AHAH
454
        //  2. open the media in a new window with target 'specimen'
455
        if ($mediaLinkType == 'LIGHTBOX' && $contentTypeDirectory == 'image') {
456
          $mediaPartLinkAttributes['class'] = array('lightbox');
457
        }
458
        else {
459
          $mediaPartLinkAttributes['target'] = "specimen";
460
          $openMediaLinkAttributes['target'] = "specimen";
461
        }
462
        $mediaPartLinkAttributes['alt'] = htmlentities($ahah_media_caption);
463

  
464
        // --- preparing the media caption
465

  
466
        /* old comment: "no caption elements to show up here except the $caption_link_uri, if at all"
467
         *
468
         * a.kohlbecker 2013-03-14 :
469
         *   It is unclear why no caption elements should be shown, Was it a technical reason?
470
         *   see commit r16723 740177eb-a1d8-4ec3-a630-accd905eb3da
471
         *   If not problems arise with this remove it after some weeks
472
         */
473
        $captionPartHtml = $ahah_media_caption;
474

  
475
        if ($caption_link_uri) {
476
          if ($contentTypeDirectory == 'image') {
477
          // it is an image, so open it in the media page
478
            $captionPartHtml .= '<div class="media-caption-link">' . l($caption_link_uri, path_to_media($media->uuid), array(
479
              'attributes' => array(), 'html' => TRUE,
480
            )) . '</div>';
481
          }
482
          else {
483
            // otherwise open it directly and let the the browser handle the media type
484
            $openMediaLinkAttributes['absolute'] = TRUE;
485
            $captionPartHtml .= '<div class="media-caption-link">' . l($caption_link_uri, $mediaLinkUri, array(
486
              'attributes' => $openMediaLinkAttributes, 'html' => TRUE,
487
            )) . '</div>';
488
          }
489
        }
490

  
491
        $captionParts[] = $captionPartHtml;
492

  
493
        // --- Surround imagePart with link, this .
494
        if ($doLink) {
495
          $mediaPartHtml = l($mediaPartHtml, $mediaLinkUri, array(
496
            'attributes' => $mediaPartLinkAttributes, 'html' => TRUE,
497
          ));
498
        }
499
      }
500
      else {
501
        $mediaPartHtml = '';
502
        $captionParts[] = '';
503
      }
504
      $out .= '<td class="media">' . $mediaPartHtml . '</td>';
505
    }
506
    $out .= '</tr>'; // End of media parts.
507
    if ($showCaption) {
508
      if ( (is_array($captionElements) && count($captionElements) > 0) || $caption_link_uri) {
509
        $out .= '<tr>';
510
        // Add caption row.
511
        foreach ($captionParts as $captionPartHtml) {
512
          $out .= '<td class="caption">' . $captionPartHtml . '</td>';
513
        }
514
        $out .= '</tr>';
515
      }
516
    }
517
  }
518

  
519
  if ($galleryLinkUri) {
520
    if (count($mediaList) > 0) {
521
      $moreHtml = count($mediaList) . ' ' . t('more in gallery');
522
    }
523
    else {
524
      $moreHtml = t('open gallery');
525
    }
526
    $moreHtml = l($moreHtml, $galleryLinkUri);
527
    $out .= '<tr><td colspan="' . $cols . '">' . $moreHtml . '</td></tr>';
528
  }
529
  $out .= '</table>';
530
  return $out;
531
}
532

  
533
/**
534
 * Creates markup for a CDM MediaRepresentation which is referencing an image.
535
 *
536
 * @param $mediaRepresentationPart
537
 * @param $maxExtend
538
 * @param $addPassePartout
539
 * @param $attributes
540
 * @return string
541
 *   The markup for the media representation
542
 */
543
function cdm_media_gallerie_image($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
544

  
545
  $out = '';
546

  
547
  // TODO merge with theme_cdm_media_mime_image?
548
  if (isset($mediaRepresentationPart)) {
549

  
550
    $h = $mediaRepresentationPart->height;
551
    $w = $mediaRepresentationPart->width;
552
    if ($w == 0 || $h == 0) {
553
      // Take url and replace spaces.
554
      $image_uri = str_replace(' ', '%20', $mediaRepresentationPart->uri);
555
      $imageDimensions = getimagesize_remote($image_uri);
556
      if (!$imageDimensions) {
557
        return '<div>' . t('Image unavailable, uri: ') . $mediaRepresentationPart->uri . '</div>';
558
      }
559
      $w = $imageDimensions[0];
560
      $h = $imageDimensions[1];
561
    }
562

  
563
    $ratio = $w / $h;
564
    if ($ratio > 1) {
565
      $displayHeight = round($maxExtend / $ratio);
566
      $displayWidth = $maxExtend;
567
      $m = round(($maxExtend - $displayHeight) / 2);
568
      $margins = 'margin:' . $m . 'px 0 ' . $m . 'px 0;';
569
    }
570
    else {
571
      $displayHeight = $maxExtend;
572
      $displayWidth = round($maxExtend * $ratio);
573
      $m = round(($maxExtend - $displayWidth) / 2);
574
      $margins = 'margin:0 ' . $m . 'px 0 ' . $m . 'px;';
575
    }
576

  
577
    // Turn attributes array into string.
578
    if(!is_array($attributes)){
579
      $attributes = array();
580
    }
581
    if(!isset($attributes['alt'])){
582
      $attributes['alt'] = check_plain($mediaRepresentationPart->uri);
583
    }
584
    $attrStr = ' ';
585
    // $attributes['title'] = 'h:'.$h.', w:'.$w.',ratio:'.$ratio;
586
    if (is_array($attributes)) {
587
      foreach ($attributes as $name => $value) {
588
        $attrStr .= $name . '="' . $value . '" ';
589
      }
590
    }
591

  
592
    if ($addPassePartout) {
593
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
594
    }
595
    else {
596
      // Do not add margins if no pass partout is shown.
597
      $margins = '';
598
    }
599
    $out .= '<img src="' . $mediaRepresentationPart->uri . '" width="' . $displayWidth . '" height="' . $displayHeight . '" style="' . $margins . '"' . $attrStr . ' />';
600

  
601
    if ($addPassePartout) {
602
      $out .= '</div>';
603
    }
604
  }
605

  
606
  return $out;
607
}
608

  
609
/**
610
 * Creates markup for a CDM MediaRepresentation which is referencing an web application.
611
 *
612
 * @param $mediaRepresentationPart
613
 * @param $maxExtend
614
 * @param $addPassePartout
615
 * @param $attributes
616
 * @return string
617
 *   The markup for the media representation
618
 */
619
function cdm_media_gallerie_application($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
620

  
621
  $out = '';
622

  
623
  if (isset($mediaRepresentationPart)) {
624

  
625
    if ($addPassePartout) {
626
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
627
    }
628
    $out .= '<div class="application">Web Application</div>';
629

  
630
    if ($addPassePartout) {
631
      $out .= '</div>';
632
    }
633
  }
634

  
635
  return $out;
636
}
637

  
638
/**
639
 * Creates markup for a CDM MediaRepresentation which is referencing an web application.
640
 *
641
 * @param $mediaRepresentationPart
642
 * @param $maxExtend
643
 * @param $addPassePartout
644
 * @param $attributes
645
 * @return string
646
 *   The markup for the media representation
647
 */
648
function cdm_media_gallerie_text($mediaRepresentationPart, $maxExtend, $addPassePartout, $attributes = array()) {
649

  
650
  $out = '';
651

  
652
  if (isset($mediaRepresentationPart)) {
653
    if ($addPassePartout) {
654
      $out .= '<div class="image-passe-partout" style="width:' . $maxExtend . 'px; height:' . $maxExtend . 'px;">';
655
    }
656

  
657
    $out .= '<div class="application">Web Application</div>';
658

  
659
    if ($addPassePartout) {
660
      $out .= '</div>';
661
    }
662
  }
663

  
664
  return $out;
665
}
666

  
667
/**
668
 * Adds the OpenLayers based image viewer to the page.
669
 *
670
 * The OpenLayers based image viewer allows to zoom and pan the displayed image.
671
 *
672
 * Documentation related to using Openlayers in order to view images is found here:
673
 *  - @see http://trac.openlayers.org/wiki/UsingCustomTiles#UsingTilesWithoutaProjection
674
 *  - @see http://trac.openlayers.org/wiki/SettingZoomLevels
675
 *
676
 * @param array $variables
677
 *   An associative array of theme variables:
678
 *   - mediaRepresentationPart: The CDM MediaRepresentationPart instance to be displayed.
679
 *   - maxExtend: The maximum extend of the image viewer view port.
680
 */
681
function cdm_openlayers_image($mediaRepresentationPart, $maxExtend) {
682

  
683
  _add_js_openlayers();
684

  
685
  // TODO merge code below with code from theme_cdm_media_gallerie_image
686
  // var_dump("MEDIA URI: " . $mediaRepresentationPart->uri);
687
  // TODO merge code below with code from theme_cdm_media_gallerie_image
688
  $w = $mediaRepresentationPart->width;
689
  $h = $mediaRepresentationPart->height;
690

  
691
  if ($w == 0 || $h == 0) {
692
    // Take url and replace spaces.
693
    $image_uri = str_replace(' ', '%20', $mediaRepresentationPart->uri);
694
    $imageDimensions = getimagesize_remote($image_uri);
695
    if (!$imageDimensions) {
696
      return '<div>' . t('Image unavailable, uri:') . $mediaRepresentationPart->uri . '</div>';
697
    }
698
    $w = $imageDimensions[0];
699
    $h = $imageDimensions[1];
700
  }
701

  
702
  // Calculate maxResolution
703
  if ($w > $h) {
704
    $maxRes = $w / $maxExtend;
705
  }
706
  else {
707
    $maxRes = $h / $maxExtend;
708
  }
709

  
710
  $maxRes *= 1;
711

  
712
  drupal_add_js('
713
 var map;
714

  
715
 var imageLayerOptions={
716
     maxResolution: ' . $maxRes . ',
717
     maxExtent: new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . ')
718
  };
719
  var mapOptions={
720
      controls:
721
       [
722
         new OpenLayers.Control.PanZoom(),
723
         new OpenLayers.Control.Navigation({zoomWheelEnabled: false, handleRightClicks:true, zoomBoxKeyMask: OpenLayers.Handler.MOD_CTRL})
724
       ],
725
     restrictedExtent:  new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . ')
726
  };
727

  
728
 var graphic = new OpenLayers.Layer.Image(
729
          \'Image Title\',
730
          \'' . $mediaRepresentationPart->uri . '\',
731
          new OpenLayers.Bounds(0, 0, ' . $w . ', ' . $h . '),
732
          new OpenLayers.Size(' . $w . ', ' . $h . '),
733
          imageLayerOptions
734
          );
735

  
736
 function init() {
737
   map = new OpenLayers.Map(\'openlayers_image\', mapOptions);
738
   map.addLayers([graphic]);
739
   map.setCenter(new OpenLayers.LonLat(0, 0), 1);
740
   map.zoomToMaxExtent();
741
 }
742

  
743
jQuery(document).ready(function(){
744
  init();
745
});', array('type' => 'inline'));
746
  $out = '<div id="openlayers_image" class="image_viewer" style="width: ' . $maxExtend . 'px; height:' . ($maxExtend) . 'px"></div>';
747
  return $out;
748
}
749

  

Also available in: Unified diff