Browse Source

Fixed organism image so it doesn't disappear. Now uses Drupal managed files

Stephen Ficklin 10 years ago
parent
commit
2fe14582ad

+ 1 - 1
tripal_organism/api/tripal_organism.DEPRECATED.inc

@@ -89,5 +89,5 @@ function tripal_organism_get_image_url($organism, $nid = NULL) {
     )
   );
 
-  return tripal_get_organism_image($organism, $nid);
+  return tripal_get_organism_image_url($organism);
 }

+ 33 - 22
tripal_organism/api/tripal_organism.api.inc

@@ -174,23 +174,34 @@ function tripal_get_organism_select_options($syncd_only = TRUE) {
  *
  * @param $organism
  *   An organism table record
- * @param $nid
- *   (Optional). The node id of the organism node. if not supplied it will be looked up
- * @param $type
- *   (Optional). Specify the type of path, either 'url' or path'. Default is 'path'
  *
  * @return
  *   If the type parameter is 'url' (the default) then the fully qualified 
- *   url to the image is returend. If the type is 'path' then the full 
- *   filesystem is returned.
+ *   url to the image is returend. If no image is present then NULL is returned
  */
-function tripal_get_organism_image($organism, $nid = NULL, $type = 'url') {
+function tripal_get_organism_image_url($organism) {
   $url = '';
+  
+  // Get the organism's node
+  $nid = chado_get_nid_from_id('organism', $organism->organism_id);
+  
+  // Look in the file_usage table of Drupal for the image file. This
+  // is the current way for handling uploaded images. It allows the file to
+  // keep it's proper name and extension.
+  $fid = db_select('file_usage', 'fu')
+    ->fields('fu', array('fid'))
+    ->condition('module', 'tripal_organism')
+    ->condition('type', 'organism_image')
+    ->condition('id', $nid)
+    ->execute()
+    ->fetchField();
+  if ($fid) {
+    $file = file_load($fid);
+    return file_create_url($file->uri);
+  }
 
-  // first look for an image with the genus/species name.  This is old-style tripal
-  // and we keep it for backwards compatibility.  If we don't find that file
-  // then look for the image with the node ID in the name. If we don't find that then
-  // no image tag is generated
+  // First look for an image with the genus/species name.  This is old-style tripal
+  // and we keep it for backwards compatibility.  
   $base_path = realpath('.');
   $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
   $image_name =  $organism->genus . "_" . $organism->species . ".jpg";
@@ -198,19 +209,19 @@ function tripal_get_organism_image($organism, $nid = NULL, $type = 'url') {
 
   if (file_exists($image_path)) {
     $url = file_create_url("$image_dir/$image_name");
+    return $url;
   }
-  else {
-     $image_name = $nid . ".jpg";
-     $image_path = "$base_path/$image_dir/$image_name";
-     if (file_exists($image_path)) {
-       $url = file_create_url("$image_dir/$image_name");
-     }
-  }
-  if ($type == "path") {
-    return $image_path;
-  }
-  else {
+  
+  // If we don't find the file using the genus ans species then look for the 
+  // image with the node ID in the name. This method was used for Tripal 1.1
+  // and 2.x-alpha version.
+  $image_name = $nid . ".jpg";
+  $image_path = "$base_path/$image_dir/$image_name";
+  if (file_exists($image_path)) {
+    $url = file_create_url("$image_dir/$image_name");
     return $url;
   }
+
+  return NULL;
 }
 

+ 38 - 40
tripal_organism/includes/tripal_organism.chado_node.inc

@@ -171,11 +171,13 @@ function chado_organism_form($node, $form_state) {
     '#title' => t('Description'),
     '#default_value' => $description,
   );
+  
   $form['organism_image']= array(
-    '#type' => 'file',
+    '#type' => 'managed_file',
     '#title' => t('Organism Image'),
-    '#description' => 'Add an image for this organism',
+    '#description' => t('Add an image to display for this organism.'),
     '#progress_indicator' => 'bar',
+    '#upload_location' => 'public://tripal/tripal_organism/images/',
   );
 
   // PROPERTIES FORM
@@ -350,6 +352,7 @@ function chado_organism_insert($node) {
  * @ingroup tripal_organism
  */
 function chado_organism_update($node) {
+
   // remove any white space around values
   $node->genus        = trim($node->genus);
   $node->species      = trim($node->species);
@@ -374,10 +377,8 @@ function chado_organism_update($node) {
     'comment' => $node->description
   );
   $org_status = chado_update_record('organism', $match, $values);
-
-  // add the image
   chado_organism_add_image($node);
-
+  
   // * Properties Form *
   $details = array(
     'property_table' => 'organismprop',   // the name of the prop table
@@ -396,6 +397,38 @@ function chado_organism_update($node) {
   chado_update_node_form_dbxrefs($node, $details);
 }
 
+/**
+ * Adds the image to the organism node and cleans up any old images.
+ * 
+ * @param $node
+ *   The node object.
+ */
+function chado_organism_add_image($node) {
+  
+  // If there is already an organism image, then remove it it if 
+  // no other modules are using it
+  $fid = db_select('file_usage', 'fu')
+    ->fields('fu', array('fid'))
+    ->condition('module', 'tripal_organism')
+    ->condition('type', 'organism_image')
+    ->condition('id', $node->nid)
+    ->execute()
+    ->fetchField();
+  if ($fid) {
+    $file = file_load($fid);
+    file_usage_delete($file, 'tripal_organism', 'organism_image', $node->nid);
+    file_delete($file);
+  }
+
+  // Save the uploaded file
+  $file = file_load($node->organism_image);
+  if ($file) {
+    $file->status = FILE_STATUS_PERMANENT;
+    file_save($file);
+    file_usage_add($file, 'tripal_organism', 'organism_image', $node->nid);
+  }
+}
+
 /**
  * Implements hook_delete().
  *
@@ -438,41 +471,6 @@ function chado_organism_delete($node) {
     drupal_set_message(t("Warning: other data depends on this organism. The organism page was removed from this site but the organism was removed from Chado."), 'warning');
   }
 }
-
-/**
- * Add an image to an organims node
- *
- * @param $node
- *   The node to add an image to
- *
- * The file is specified in the $_FILES array created by Drupal
- *
- * @ingroup tripal_organism
- */
-function chado_organism_add_image($node) {
-  // check to see if a file was uploaded. If so then copy it to the images
-  // directory for display with the organism
-  if (isset($_FILES['files']) &&
-  $_FILES['files']['name']['organism_image'] &&
-  is_uploaded_file($_FILES['files']['tmp_name']['organism_image'])) {
-
-    // make sure the destination directory exists
-    $dest = tripal_get_files_dir() . "/tripal_organism/images";
-    file_prepare_directory($dest, FILE_CREATE_DIRECTORY);
-
-    // now move the file
-    $validators = array('file_validate_is_image' => array());
-    $destination = "public://tripal/tripal_organism/images/";
-    $file = file_save_upload('organism_image', $validators, $destination);
-    if (!$file) {
-      drupal_set_message(t("Organism image was not uploaded."));
-    }
-    else {
-      file_move($file, $destination . "/" . $node->nid . ".jpg", FILE_EXISTS_REPLACE);
-    }
-  }
-}
-
 /**
  *  Implements hook_load().
  *

+ 1 - 1
tripal_organism/theme/templates/tripal_organism_base.tpl.php

@@ -7,7 +7,7 @@ $organism = chado_expand_var($organism,'field','organism.comment'); ?>
 
 // generate the image tag
 $image = '';
-$image_url = tripal_organism_get_image_url($organism, $node->nid); 
+$image_url = tripal_get_organism_image_url($organism); 
 if ($image_url) {
   $image = "<img class=\"tripal-organism-img\" src=\"$image_url\">";
 }