commit 275b2642c2438013cb1ac06bb986dceb388592d7
Author: Andreas Kohlbecker <a.kohlbecker@bgbm.org>
Date:   Tue Oct 11 15:35:01 2016 +0200

    fix #5948 supressing media when uri is missing, also turning theme function into plain markup function

diff --git a/modules/cdm_dataportal/cdm_api/cdm_api.module b/modules/cdm_dataportal/cdm_api/cdm_api.module
index 092998f9..23b640b8 100644
--- a/modules/cdm_dataportal/cdm_api/cdm_api.module
+++ b/modules/cdm_dataportal/cdm_api/cdm_api.module
@@ -469,23 +469,6 @@ function get_last_taxon_page_tab() {
 }
 
 /**
- * @todo Improve the documentation of this function.
- *
- * media Array [4]
- * representations Array [3]
- * mimeType image/jpeg
- * representationParts Array [1]
- * duration 0
- * heigth 0
- * size 0
- * uri
- * http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/jpeg/Acanthocephalus_p1.jpg
- * uuid 15c687f1-f79d-4b79-992f-7ba0f55e610b
- * width 0
- * suffix jpg
- * uuid 930b7d51-e7b6-4350-b21e-8124b14fe29b
- * title
- * uuid 17e514f1-7a8e-4daa-87ea-8f13f8742cf9
  *
  * @param object $media
  * @param array $mimeTypes
@@ -519,8 +502,14 @@ function cdm_preferred_media_representations($media, array $mimeTypes, $width =
         $mimeTypes = array();
         $dwa = 0;
         $dw = 0;
+        $valid_parts_cnt = 0;
         // Look for part with the best matching size.
         foreach ($representation->parts as $part) {
+          if(empty($part->uri)){
+            // skip part if URI is missing
+            continue;
+          }
+          $valid_parts_cnt++;
           if (isset($part->width) && isset($part->height)) {
             $dw = $part->width * $part->height - $height * $width;
           }
@@ -529,8 +518,10 @@ function cdm_preferred_media_representations($media, array $mimeTypes, $width =
           }
           $dwa += $dw;
         }
-        $dwa = (count($representation->parts) > 0) ? $dwa / count($representation->parts) : 0;
-        $prefRepr[$dwa . '_'] = $representation;
+        if($valid_parts_cnt > 0){
+          $dwa = $dwa / $valid_parts_cnt;
+          $prefRepr[$dwa . '_'] = $representation;
+        }
       }
     }
   }
diff --git a/modules/cdm_dataportal/includes/name.inc b/modules/cdm_dataportal/includes/name.inc
index 1c328757..9e817eba 100644
--- a/modules/cdm_dataportal/includes/name.inc
+++ b/modules/cdm_dataportal/includes/name.inc
@@ -474,16 +474,15 @@ function render_taxon_or_name($taxon_name_or_taxon_base, $nameLink = NULL, $refe
             $second_citation = '[& ' . $description_element->multilanguageText_L10n->text . '].';
           }
           $descriptionHtml .= $second_citation;
-          $descriptionHtml .= theme("cdm_media", array(
-              'descriptionElement' => $description_element,
-              'mimeTypePreference' => array(
+          $descriptionHtml .= cdm_description_element_media(
+              $description_element,
+              array(
                 'application/pdf',
                 'image/png',
                 'image/jpeg',
                 'image/gif',
                 'text/html',
               )
-            )
           );
 
         }
diff --git a/modules/cdm_dataportal/theme/cdm_dataportal.media.theme b/modules/cdm_dataportal/theme/cdm_dataportal.media.theme
index 5c4c30d0..27dc9819 100644
--- a/modules/cdm_dataportal/theme/cdm_dataportal.media.theme
+++ b/modules/cdm_dataportal/theme/cdm_dataportal.media.theme
@@ -52,17 +52,23 @@ function getimagesize_remote($image_url) {
 }
 
 /**
- * @todo Please document this function.
- * @see http://drupal.org/node/1354
+ * Creates the markup for the media associated a DescriptionElement instance.
+ *
+ * @param $descriptionElement
+ *    the DescriptionElement instance
+ * @param $mimeTypePreference array
+ *    An array of mime type strings. the order of the mimetpes is the oerder of preference.
+ *    E.g.: array('application/pdf','image/jpeg')
+ *
+ * @return string
+ *    The markup
  */
-function theme_cdm_media($variables) {
-  $descriptionElement = $variables['descriptionElement'];
-  $mimeTypePreference = $variables['mimeTypePreference'];
+function cdm_description_element_media($descriptionElement, $mimeTypePreference) {
+
   $out = '';
 
   _add_js_thickbox();
 
-  $uuid = $descriptionElement->uuid;
   $feature = $descriptionElement->feature;
   $medias = $descriptionElement->media;
 
@@ -76,8 +82,10 @@ function theme_cdm_media($variables) {
       $out .= theme('cdm_media_mime_' . $contentTypeDirectory, array('mediaRepresentation' => $mediaRepresentation, 'feature' => $feature));
     }
     else {
-      // No media available, so display just the type term.
-      $out .= $feature->representation_L10n;
+      // Media has empty or corrupt representation
+      if(user_is_logged_in()){
+        drupal_set_message('The media entity (' . l($media->uuid, path_to_media($media->uuid)) .') has empty or corrupt representation parts. Maybe the URI is empty.' , 'warning');
+      }
     }
   }
   return $out;
diff --git a/modules/cdm_dataportal/theme/theme_registry.inc b/modules/cdm_dataportal/theme/theme_registry.inc
index ee37f6f2..baf53cd0 100644
--- a/modules/cdm_dataportal/theme/theme_registry.inc
+++ b/modules/cdm_dataportal/theme/theme_registry.inc
@@ -85,7 +85,6 @@ function cdm_dataportal_theme() {
     'cdm_block_Uses' => array('variables' => array('taxonUuid' => NULL)),
 
     // Themes in cdm_dataportal.media.theme.
-    'cdm_media' => array('variables' => array('descriptionElement' => NULL, 'mimeTypePreference' => NULL)),
     'cdm_media_mime_application' => array('variables' => array('mediaRepresentation' => NULL, 'feature' => NULL)),
     'cdm_media_mime_image' => array('variables' => array('mediaRepresentation' => NULL, 'feature' => NULL)),
     'cdm_media_mime_text' => array('variables' => array('mediaRepresentation' => NULL, 'feature' => NULL)),
