Browse Source

Merge branch 'collections' of github.com:tripal/tripal into collections

Stephen Ficklin 7 years ago
parent
commit
77615f556b

+ 120 - 53
tripal/includes/TripalEntityCollection.inc

@@ -60,10 +60,16 @@ class TripalEntityCollection {
     }
 
     try {
+      // Delete from the tripal collection table.
       db_delete('tripal_collection')
         ->condition('collection_id', $this->collection_id)
         ->execute();
 
+      // Delete the field groups from the tripal_bundle_collection table.
+      db_delete('tripal_collection_bundle')
+        ->condition('collection_id', $this->collection_id)
+        ->execute();
+
       // Remove any files that may have been created
       foreach ($this->downloaders as $class_name => $label) {
         tripal_load_include_downloader_class($class_name);
@@ -125,7 +131,7 @@ class TripalEntityCollection {
     $bundle_name = "";
     if (count($this->bundles) > 1) {
       foreach ($this->bundles as $bundle) {
-        $bundle_name = $this->bundles[0]->bundle_name;
+        $bundle_name = $bundle->bundle_name;
         $ids[$bundle_name] = $this->getEntityIDs($bundle_name);
         $fields[$bundle_name] = $this->getFields($bundle_name);
       }
@@ -144,30 +150,66 @@ class TripalEntityCollection {
     // supported for this basket.
     foreach ($this->fields as $field_group) {
       foreach ($field_group as $field_id) {
-        $field = field_info_field_by_id($field_id);
-        if (!$field) {
-          continue;
+        // Check is $field_groups is an array because if it is that means we
+        // nested arrays we need to deal with.
+        if (is_array($field_id)) {
+          foreach ($field_id as $field) {
+            $field_info = field_info_field_by_id($field);
+            if (!$field_info) {
+              continue;
+            }
+            $field_name = $field_info['field_name'];
+            $field_type = $field_info['type'];
+            $field_module = $field_info['module'];
+            $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+    
+            $downloaders = array();
+    
+            // All fields should support the Tab and CSV downloaders.
+            tripal_load_include_downloader_class('TripalTabDownloader');
+            $this->downloaders['TripalTabDownloader'] = TripalTabDownloader::$label;
+            tripal_load_include_downloader_class('TripalCSVDownloader');
+            $this->downloaders['TripalCSVDownloader'] = TripalCSVDownloader::$label;
+    
+            if (tripal_load_include_field_class($field_type)) {
+              $settings = $field_type::$default_instance_settings;
+              if (array_key_exists('download_formatters', $settings)) {
+                foreach ($settings['download_formatters'] as $class_name) {
+                  if (!array_key_exists($class_name, $this->downloaders)) {
+                    tripal_load_include_downloader_class($class_name);
+                    $this->downloaders[$class_name] = $class_name::$label;
+                  }
+                }
+              }
+            }
+          }
         }
-        $field_name = $field['field_name'];
-        $field_type = $field['type'];
-        $field_module = $field['module'];
-        $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
-
-        $downloaders = array();
-
-        // All fields should support the Tab and CSV downloaders.
-        tripal_load_include_downloader_class('TripalTabDownloader');
-        $this->downloaders['TripalTabDownloader'] = TripalTabDownloader::$label;
-        tripal_load_include_downloader_class('TripalCSVDownloader');
-        $this->downloaders['TripalCSVDownloader'] = TripalCSVDownloader::$label;
-
-        if (tripal_load_include_field_class($field_type)) {
-          $settings = $field_type::$default_instance_settings;
-          if (array_key_exists('download_formatters', $settings)) {
-            foreach ($settings['download_formatters'] as $class_name) {
-              if (!array_key_exists($class_name, $this->downloaders)) {
-                tripal_load_include_downloader_class($class_name);
-                $this->downloaders[$class_name] = $class_name::$label;
+        else {
+          $field = field_info_field_by_id($field_id);
+          if (!$field) {
+            continue;
+          }
+          $field_name = $field['field_name'];
+          $field_type = $field['type'];
+          $field_module = $field['module'];
+          $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+
+          $downloaders = array();
+
+          // All fields should support the Tab and CSV downloaders.
+          tripal_load_include_downloader_class('TripalTabDownloader');
+          $this->downloaders['TripalTabDownloader'] = TripalTabDownloader::$label;
+          tripal_load_include_downloader_class('TripalCSVDownloader');
+          $this->downloaders['TripalCSVDownloader'] = TripalCSVDownloader::$label;
+
+          if (tripal_load_include_field_class($field_type)) {
+            $settings = $field_type::$default_instance_settings;
+            if (array_key_exists('download_formatters', $settings)) {
+              foreach ($settings['download_formatters'] as $class_name) {
+                if (!array_key_exists($class_name, $this->downloaders)) {
+                  tripal_load_include_downloader_class($class_name);
+                  $this->downloaders[$class_name] = $class_name::$label;
+                }
               }
             }
           }
@@ -177,16 +219,14 @@ class TripalEntityCollection {
   }
 
   /**
-   * Creates a new collection.
+   * Creates a new unique collection ID used as a look up against the
+   * tripal_collection_bundle to find fields, ids, and bundles.
    *
    * @param  $details
    *   An association array containing the details for a collection. The
    *   details must include the following key/value pairs:
    *   - uid:  The ID of the user that owns the collection
    *   - collection_name:  The name of the collection
-   *   - bundle_name:  The name of the TripalEntity content type.
-   *   - ids:  An array of the entity IDs that form the collection.
-   *   - fields: An array of the field IDs that the collection is limited to.
    *   - description:  A user supplied description for the collection.
    *
    * @throws Exception
@@ -222,7 +262,7 @@ class TripalEntityCollection {
         ))
         ->execute();
       // Now add the second table with bundle info.
-      $this->add($details, $collection_id);
+      $this->addFields($details, $collection_id);
       // Now load the job into this object.
       $this->load($collection_id);
     }
@@ -243,7 +283,7 @@ class TripalEntityCollection {
    *
    * @throws Exception
    */
-  public function add($details, $collection_id) {
+  public function addFields($details, $collection_id) {
     if (!$details['bundle_name']) {
       throw new Exception("Must provide a 'bundle_name' key to TripalEntityCollection::add().");
     }
@@ -446,7 +486,6 @@ class TripalEntityCollection {
     $extension = $formatter::$default_extension;
     $create_date = $this->getCreateDate(FALSE);
     $outfile = preg_replace('/[^\w]/', '_', ucwords($this->collection_name)) . '_collection' . '_' . $create_date . '.' . $extension;
-
     return $outfile;
   }
 
@@ -486,7 +525,6 @@ class TripalEntityCollection {
    */
   public function getOutfileURL($formatter) {
     $outfile = $this->getOutfilePath($formatter);
-    return file_create_url($outfile);
   }
 
   /**
@@ -508,10 +546,10 @@ class TripalEntityCollection {
     }
 
     $outfile = $this->getOutfile($formatter);
-
-    $downloader = new $formatter($this->bundles, $this->ids, $this->fields, $outfile, $this->user->uid);
-
-    return $downloader->getURL();
+    // Make sure the user directory exists
+    $user_dir = 'public://tripal/users/' . $this->user->uid;
+    $outfilePath = $user_dir. '/' . $outfile;
+    return $outfilePath;
   }
 
   /**
@@ -539,33 +577,62 @@ class TripalEntityCollection {
     $supported_fields = array();
     foreach ($this->fields as $field_group) {
       foreach ($field_group as $field_id) {
-        // If the formatter is TripalTabDownloader or TripalCSVDownloader then
-        // we always want to support the field.
-        if ($formatter == 'TripalTabDownloader' or $formatter == 'TripalCSVDownloader') {
-          if (!in_array($field_id, $supported_fields)) {
-            $supported_fields[] = $field_id;
+        // Check is $field_id is an array because if it is that means we
+        // nested arrays we need to deal with.
+        if (is_array($field_id)) {
+          foreach ($field_id as $field) {
+            // If the formatter is TripalTabDownloader or TripalCSVDownloader then
+            // we always want to support the field.
+            if ($formatter == 'TripalTabDownloader' or $formatter == 'TripalCSVDownloader') {
+              if (!in_array($field, $supported_fields)) {
+                $supported_fields[] = $field;
+              }
+              continue;
+            }
+
+            // Otherwise, find out if the formatter specified is supporte by the
+            // field and if so then add it to our list of supported fields.
+            $field_info = field_info_field_by_id($field);
+            $field_name = $field_info['field_name'];
+            $field_type = $field_info['type'];
+            if (tripal_load_include_field_class($field_type)) {
+              $settings = $field_type::$default_instance_settings;
+              if (array_key_exists('download_formatters', $settings)) {
+                if (in_array($formatter, $settings['download_formatters'])) {
+                  $supported_fields[] = $field;
+                }
+              }
+            }
           }
-          continue;
         }
-
-        // Otherwise, find out if the formatter specified is supporte by the
-        // field and if so then add it to our list of supported fields.
-        $field = field_info_field_by_id($field_id);
-        $field_name = $field['field_name'];
-        $field_type = $field['type'];
-        if (tripal_load_include_field_class($field_type)) {
-          $settings = $field_type::$default_instance_settings;
-          if (array_key_exists('download_formatters', $settings)) {
-            if (in_array($formatter, $settings['download_formatters'])) {
+        else {
+          // If the formatter is TripalTabDownloader or TripalCSVDownloader then
+          // we always want to support the field.
+          if ($formatter == 'TripalTabDownloader' or $formatter == 'TripalCSVDownloader') {
+            if (!in_array($field_id, $supported_fields)) {
               $supported_fields[] = $field_id;
             }
+            continue;
+          }
+
+          // Otherwise, find out if the formatter specified is supporte by the
+          // field and if so then add it to our list of supported fields.
+          $field = field_info_field_by_id($field_id);
+          $field_name = $field['field_name'];
+          $field_type = $field['type'];
+          if (tripal_load_include_field_class($field_type)) {
+            $settings = $field_type::$default_instance_settings;
+            if (array_key_exists('download_formatters', $settings)) {
+              if (in_array($formatter, $settings['download_formatters'])) {
+                $supported_fields[] = $field_id;
+              }
+            }
           }
         }
       }
     }
 
     $downloader = new $formatter($this->bundles, $this->ids, $supported_fields, $outfile, $this->user->uid);
-    //print_r($downloader);
     $downloader->write();
 
   }

+ 1 - 0
tripal/includes/TripalFieldDownloaders/TripalCSVDownloader.inc

@@ -32,6 +32,7 @@ class TripalCSVDownloader extends TripalFieldDownloader {
           $row[] = '"' . $value . '"';
         }
         else {
+
           if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
             $row[] = strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
           }

+ 99 - 27
tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc

@@ -91,15 +91,15 @@ abstract class TripalFieldDownloader {
    */
   public function delete() {
     $fid = db_select('file_managed', 'fm')
-     ->fields('fm', array('fid'))
-     ->condition('uri', $this->outfile)
-     ->execute()
-     ->fetchField();
-     if ($fid) {
-       $file = file_load($fid);
-       file_usage_delete($file, 'tripal', 'data-collection');
-       file_delete($file, TRUE);
-     }
+      ->fields('fm', array('fid'))
+      ->condition('uri', $this->outfile)
+      ->execute()
+      ->fetchField();
+    if ($fid) {
+      $file = file_load($fid);
+      file_usage_delete($file, 'tripal', 'data-collection');
+      file_delete($file, TRUE);
+    }
   }
 
   /**
@@ -107,37 +107,109 @@ abstract class TripalFieldDownloader {
    */
   public function write() {
     global $user;
-
     $fh = fopen(drupal_realpath($this->outfile), "w");
+
     if (!$fh) {
       throw new Exception("Cannout open collection file: " . $this->outfile);
     }
+ 
+    // If more than one bundle is supplied we need to break the headers and entities
+    // apart so that headers and content correspond.
+    if (count($this->bundle_name) > 1) {
+      foreach ($this->bundle_name as $bundle) {
+        $bundle_id = $bundle->bundle_name;
+        // Get the bundle display name so it makes sense to the end user.
+        $bundle_label = db_select('tripal_bundle', 'TB')
+          ->fields('TB', array('label'))
+          ->condition('TB.name', $bundle_id, '=')
+          ->execute()
+          ->fetchAssoc();
+        fwrite($fh, $bundle_label['label'] . "\r\n");
 
-    $headers = $this->getHeader();
-    if ($headers) {
-      foreach ($headers as $line) {
-        fwrite($fh, $line . "\r\n");
+        // Set the single bundle name for getting the Header.
+        $this->bundle_name = $bundle_id;
+
+        $headers = $this->getHeader();
+        if ($headers) {
+          foreach ($headers as $line) {
+            fwrite($fh, $line . "\r\n");
+          }
+        }
+        foreach ($this->entity_ids[$bundle_id] as $entity_id) {
+          if (is_array($entity_id)) {
+            foreach ($entity_id as $single_entity) {
+              if (is_array($single_entity)) {
+                foreach ($single_entity as $entity) {
+                  $result = tripal_load_entity('TripalEntity', array($entity), FALSE, $this->fields);
+                  $entity_info = $result[$entity];
+                  $lines = $this->formatEntity($entity_info);
+                  foreach ($lines as $line) {
+                    fwrite($fh, $line . "\r\n");
+                  }
+                }
+              }
+              else {
+                $result = tripal_load_entity('TripalEntity', array($single_entity), FALSE, $this->fields);
+                $entity = $result[$single_entity];
+                $lines = $this->formatEntity($entity);
+                foreach ($lines as $line) {
+                  fwrite($fh, $line . "\r\n");
+                }
+              }
+            }
+          }
+          else {
+            $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, $this->fields);
+            $entity = $result[$entity_id];
+            $lines = $this->formatEntity($entity);
+            foreach ($lines as $line) {
+              fwrite($fh, $line . "\r\n");
+            }
+          }
+        }
       }
     }
-    foreach ($this->entity_ids as $entity_id) {
-      if (is_array($entity_id)) {
-        foreach ($entity_id as $single_entity) {
-          $result = tripal_load_entity('TripalEntity', array($single_entity), FALSE, $this->fields);
-          $entity = $result[$single_entity];
+    else {
+      $bundle_id = $this->bundle_name[0]->bundle_name;
+      $this->bundle_name = $bundle_id;
+      $headers = $this->getHeader();
+      if ($headers) {
+        foreach ($headers as $line) {
+          fwrite($fh, $line . "\r\n");
+        }
+      }
+      foreach ($this->entity_ids as $entity_id) {
+        if (is_array($entity_id)) {
+          foreach ($entity_id as $single_entity) {
+            if (is_array($single_entity)) {
+              foreach ($single_entity as $entity) {
+                $result = tripal_load_entity('TripalEntity', array($entity), FALSE, $this->fields);
+                $entity_info = $result[$entity];
+                $lines = $this->formatEntity($entity_info);
+                foreach ($lines as $line) {
+                  fwrite($fh, $line . "\r\n");
+                }
+              }
+            }
+            else {
+              $result = tripal_load_entity('TripalEntity', array($single_entity), FALSE, $this->fields);
+              $entity = $result[$single_entity];
+              $lines = $this->formatEntity($entity);
+              foreach ($lines as $line) {
+                fwrite($fh, $line . "\r\n");
+              }
+            }
+          }
+        }
+        else {
+          $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, $this->fields);
+          $entity = $result[$entity_id];
           $lines = $this->formatEntity($entity);
           foreach ($lines as $line) {
             fwrite($fh, $line . "\r\n");
           }
         }
       }
-      else {
-        $result = tripal_load_entity('TripalEntity', array($entity_id), FALSE, $this->fields);
-        $entity = $result[$entity_id];
-        $lines = $this->formatEntity($entity);
-        foreach ($lines as $line) {
-          fwrite($fh, $line . "\r\n");
-        }
-      }
     }
     fclose($fh);
 

+ 4 - 1
tripal/includes/tripal.collections.inc

@@ -22,10 +22,13 @@ function tripal_user_collections_page() {
 
     $downloads = array();
     $formatters = $collection->getDownloadFormatters();
+
     foreach ($formatters as $class_name => $label) {
+     
       $outfile = $collection->getOutfilePath($class_name);
-      $outfileURL = file_create_url($outfile);
+
       if (file_exists($outfile)) {
+        $outfileURL = file_create_url($outfile);
         $downloads[] = l($label, $outfileURL);
       }
       else {