Browse Source

update to change the header functionality and the processing of the tabs within the format entity

Shawna Spoor 7 years ago
parent
commit
c9064d0398

+ 30 - 105
tripal/includes/TripalFieldDownloaders/TripalCSVDownloader.inc

@@ -22,13 +22,16 @@ class TripalCSVDownloader extends TripalFieldDownloader {
    * @see TripalFieldDownloader::format()
    */
   protected function formatEntity($entity) {
-       $row = array();
-    $row_with_tabs = array();
+    $row = array();
     $bundle_name = $entity->bundle;
     
     // Grab the headers to ensure we tab the fields properly.
     $header =  $this->header_array;
-
+    if (!empty($header)) { 
+      // Count the number of items in the header and build an array with that many items.
+      $header_count = count($header);
+      $row = array_fill(0, $header_count, NULL);
+    }
     // Determine if the entity is remote or local.
     if (strpos($bundle_name, 'bio_data_') !== 0) {
       $external = TRUE;
@@ -51,57 +54,40 @@ class TripalCSVDownloader extends TripalFieldDownloader {
         $field_name = $entity->$field['und'][0]['label'];
         $value = $entity->$field['und'][0]['value'];
         $accession = $entity->$field['und'][0]['accession'];
-        // Add the tabs so it's in the right place.
         if (!empty($header)) {
-          $tabs_needed = array_search($accession, $header);
-          // The number of existing rows needs to be taken into account
-          // because all rows will be combined in the end into one line.
-          $row_count = count($row);
-          $tabs_needed = $tabs_needed - $row_count;
-          
-          if ($tabs_needed > 0) {
-            for ($i = 0; $i <= $tabs_needed; $i++) {
-              $tabs .= "\t";
-            }
-          }
-          else {
-            $tabs = '';
-          }
-        }
-        if (!is_array($value)) {
-          $row[] = $tabs . $value;
+          //Now find the accession in the header array and place it where it needs to go.
+          $position = array_search($accession, $header);
+          $row[$position] = $value;
         }
       }
       else {
         $field_info = field_info_field_by_id($field);
         $field_name = $field_info['field_name'];
+        $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+        // Build the accession incase someone renames a cord field name.
+        $accession = $instance['settings']['term_vocabulary'].$instance['settings']['term_accession'];
 
         if (count($entity->{$field_name}['und']) == 1) {
           $value = $entity->{$field_name}['und'][0]['value'];
-          // Add the tabs so it's in the right place.
-          if (!empty($header)) {
-            $tabs_needed = array_search($field_name, $header);
-            // The number of existing rows needs to be taken into account
-            // because all rows will be combined in the end into one line.
-            $row_count = count($row);
-            $tabs_needed = $tabs_needed - $row_count;
-            for ($i = 0; $i <= $tabs_needed; $i++) {
-              $tabs .= "\t";
-            }
-          } 
-          // If the single element is not an array then this is good.
-          if (!is_array($value)) {
-            $row[] = $tabs . $value;
-          }
-          else {
-            if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
-              $row[] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
+
+          if (!empty($header)) { 
+            //Now find the accession in the header array and place it where it needs to go.
+            $position = array_search($accession, $header);
+            // If the single element is not an array then this is good.
+            if (!is_array($value)) {
+              $row[$position] = $value;
             }
             else {
-              $row[] = '';
+              if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
+                $row[$position] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
+              }
+              else {
+                $row[$position] = '';
+              }
+              // TODO: What to do with fields that are arrays?
             }
-            // TODO: What to do with fields that are arrays?
-          }
+
+          } 
         }
         else {
           $row[] = '';
@@ -109,76 +95,15 @@ class TripalCSVDownloader extends TripalFieldDownloader {
         }
       }
     }
-    return array(implode('', $row));
+    return array(implode("\t", $row));
   }
 
   /**
    * @see TripalFieldDownloader::getHeader()
    */
   protected function getHeader() {
-   $row = array();
-    $accession_ids = array();
     $bundle_collections = $this->collection_bundles;
-
-    foreach ($bundle_collections as $bundle_collection) {
-      $collection_id = $bundle_collection->collection_bundle_id;
-      $bundle_name = $bundle_collection->bundle_name;
-      $fields = unserialize($bundle_collection->fields);
-      $entity_ids = unserialize($bundle_collection->ids);
-      $site_id = $bundle_collection->site_id;
-
-      // Determine if the entity is remote or local.
-      if (strpos($bundle_name, 'bio_data_') !== 0) {
-        $external = TRUE;
-      }
-      else {
-        $external = FALSE;
-      }
-
-      foreach ($fields as $field) {
-        if ($external) {
-          $fake_tripal_entity = $this->getRemoteEntity($entity_ids, $site_id, $field, $bundle_name);
-          $field_name = $fake_tripal_entity->$field['und'][0]['field_name'];
-          $accession = $fake_tripal_entity->$field['und'][0]['accession'];
-          if (!empty($accession_ids) && !empty($row)) {
-            // If the accession is already in the accession_ids list skip.
-            if (array_search($accession, $accession_ids)) {
-              continue;
-            }
-            else {
-              $row[] = $field_name;
-              $accession_ids[] = $accession;
-            }
-          } 
-          else {
-            $row[] = $field_name;
-            $accession_ids[] = $accession;
-          }
-        }
-        else {
-          $field_info = field_info_field_by_id($field);
-          //$field_name is the accession info.
-          $field_name = $field_info['field_name'];
-          $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
-          if (!empty($accession_ids) && !empty($row)) {
-            // If the accession is already in the accession_ids list skip.
-            if (array_search($accession, $accession_ids)) {
-              continue;
-            }
-            else {
-              $accession_ids[] = $field_name;
-              $row[] = $instance['label'];
-            }
-          } 
-          else {
-            $accession_ids[] = $field_name;
-            $row[] = $instance['label'];
-          }
-        }
-      }
-    }
-    $this->header_array = $row;
-    return array(implode("\t", $row));
+    return $this->processTabHeader($bundle_collections);
   }
 
 }

+ 95 - 5
tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc

@@ -265,13 +265,26 @@ abstract class TripalFieldDownloader {
   }
 
   /**
-   * Find the header info for the remote field
+   * Build and return a fake entity from a remote site using
+   * tripal web services calls.
    *
-   * @param $field
-   *   The accession which is stored as the field id.
+   * @param $remote_ids
+   *   Array of the remote ids.
+   * 
+   * @param $site_id
+   *   Unique site id assigned in the tripal_sites table when
+   *   a new site is created via the web services interface.
+   * 
+   * @param $remote_fields
+   *   Array of the remote fields.
+   * 
+   * @param $bundle_name
+   *   Bundle name of the remote field, in this instance it will be 
+   *   the accession of the field.
    *
-   * @return
-   *   .
+   * @return $fake_tripal_entity
+   *    This is a fake entity structured to allow the format
+   *    entity function to process and return the info.
    */
   public function getRemoteEntity($remote_ids, $site_id, $remote_fields, $bundle_name) {
     // Before a request can be made we need to get the entity type
@@ -421,6 +434,83 @@ abstract class TripalFieldDownloader {
     return $fake_tripal_entity;
   }
 
+  /**
+   * Process and return a header for tab delimited file formats.
+   *
+   * @param $bundle_collections
+   *   The accession which is stored as the field id.
+   *
+   * @return array
+   *    The impoded array for the header.
+   */
+  public function processTabHeader($bundle_collections) {
+    $row = array();
+    $accession_ids = array();
+
+    foreach ($bundle_collections as $bundle_collection) {
+      $collection_id = $bundle_collection->collection_bundle_id;
+      $bundle_name = $bundle_collection->bundle_name;
+      $fields = unserialize($bundle_collection->fields);
+      $entity_ids = unserialize($bundle_collection->ids);
+      $site_id = $bundle_collection->site_id;
+
+      // Determine if the entity is remote or local.
+      if (strpos($bundle_name, 'bio_data_') !== 0) {
+        $external = TRUE;
+      }
+      else {
+        $external = FALSE;
+      }
+
+      foreach ($fields as $field) {
+        if ($external) {
+          $fake_tripal_entity = $this->getRemoteEntity($entity_ids, $site_id, $field, $bundle_name);
+          $field_name = $fake_tripal_entity->$field['und'][0]['field_name'];
+          $accession = $fake_tripal_entity->$field['und'][0]['accession'];
+          if (!empty($accession_ids) && !empty($row)) {
+            // If the accession is already in the accession_ids list skip.
+            if (array_search($accession, $accession_ids)) {
+              continue;
+            }
+            else {
+              $row[] = $field_name;
+              $accession_ids[] = $accession;
+            }
+          } 
+          else {
+            $row[] = $field_name;
+            $accession_ids[] = $accession;
+          }
+        }
+        else {
+          $field_info = field_info_field_by_id($field);
+          //$field_name is the accession info.
+          $field_name = $field_info['field_name'];
+          $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+
+          // Build the accession incase someone renames a cord field name.
+          $accession = $instance['settings']['term_vocabulary'] . $instance['settings']['term_accession'];
+          if (!empty($accession_ids) && !empty($row)) {
+            // If the accession is already in the accession_ids list skip.
+            if (array_search($accession, $accession_ids)) {
+              continue;
+            }
+            else {
+              $accession_ids[] = $accession;
+              $row[] = $instance['label'];
+            }
+          } 
+          else {
+            $accession_ids[] = $accession;
+            $row[] = $instance['label'];
+          }
+        }
+      }
+    }
+    $this->header_array = $accession_ids;
+    return array(implode("\t", $row));
+  }
+
   /**
    * Formats the entity and the specified fields for output.
    *

+ 31 - 105
tripal/includes/TripalFieldDownloaders/TripalTabDownloader.inc

@@ -21,14 +21,17 @@ class TripalTabDownloader extends TripalFieldDownloader {
   /**
    * @see TripalFieldDownloader::format()
    */
-  protected function formatEntity($entity) {
+ protected function formatEntity($entity) {
     $row = array();
-    $row_with_tabs = array();
     $bundle_name = $entity->bundle;
     
     // Grab the headers to ensure we tab the fields properly.
     $header =  $this->header_array;
-
+    if (!empty($header)) { 
+      // Count the number of items in the header and build an array with that many items.
+      $header_count = count($header);
+      $row = array_fill(0, $header_count, NULL);
+    }
     // Determine if the entity is remote or local.
     if (strpos($bundle_name, 'bio_data_') !== 0) {
       $external = TRUE;
@@ -51,57 +54,41 @@ class TripalTabDownloader extends TripalFieldDownloader {
         $field_name = $entity->$field['und'][0]['label'];
         $value = $entity->$field['und'][0]['value'];
         $accession = $entity->$field['und'][0]['accession'];
-        // Add the tabs so it's in the right place.
         if (!empty($header)) {
-          $tabs_needed = array_search($accession, $header);
-          // The number of existing rows needs to be taken into account
-          // because all rows will be combined in the end into one line.
-          $row_count = count($row);
-          $tabs_needed = $tabs_needed - $row_count;
-          
-          if ($tabs_needed > 0) {
-            for ($i = 0; $i <= $tabs_needed; $i++) {
-              $tabs .= "\t";
-            }
-          }
-          else {
-            $tabs = '';
-          }
-        }
-        if (!is_array($value)) {
-          $row[] = $tabs . $value;
+          //Now find the accession in the header array and place it where it needs to go.
+          $position = array_search($accession, $header);
+          $row[$position] = $value;
         }
       }
       else {
         $field_info = field_info_field_by_id($field);
         $field_name = $field_info['field_name'];
+        $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+        // Build the accession incase someone renames a cord field name.
+        $accession = $instance['settings']['term_vocabulary'] . $instance['settings']['term_accession'];
 
         if (count($entity->{$field_name}['und']) == 1) {
           $value = $entity->{$field_name}['und'][0]['value'];
-          // Add the tabs so it's in the right place.
-          if (!empty($header)) {
-            $tabs_needed = array_search($field_name, $header);
-            // The number of existing rows needs to be taken into account
-            // because all rows will be combined in the end into one line.
-            $row_count = count($row);
-            $tabs_needed = $tabs_needed - $row_count;
-            for ($i = 0; $i <= $tabs_needed; $i++) {
-              $tabs .= "\t";
-            }
-          } 
-          // If the single element is not an array then this is good.
-          if (!is_array($value)) {
-            $row[] = $tabs . $value;
-          }
-          else {
-            if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
-              $row[] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
+
+          if (!empty($header)) { 
+            //Now find the accession in the header array and place it where it needs to go.
+            $position = array_search($accession, $header);
+
+            // If the single element is not an array then this is good.
+            if (!is_array($value)) {
+              $row[$position] = $value;
             }
             else {
-              $row[] = '';
+              if (array_key_exists('rdfs:label', $entity->{$field_name}['und'][0]['value'])) {
+                $row[$position] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
+              }
+              else {
+                $row[$position] = '';
+              }
+              // TODO: What to do with fields that are arrays?
             }
-            // TODO: What to do with fields that are arrays?
-          }
+
+          } 
         }
         else {
           $row[] = '';
@@ -109,76 +96,15 @@ class TripalTabDownloader extends TripalFieldDownloader {
         }
       }
     }
-    return array(implode('', $row));
+    return array(implode("\t", $row));
   }
 
   /**
    * @see TripalFieldDownloader::getHeader()
    */
   protected function getHeader() {
-    $row = array();
-    $accession_ids = array();
     $bundle_collections = $this->collection_bundles;
-
-    foreach ($bundle_collections as $bundle_collection) {
-      $collection_id = $bundle_collection->collection_bundle_id;
-      $bundle_name = $bundle_collection->bundle_name;
-      $fields = unserialize($bundle_collection->fields);
-      $entity_ids = unserialize($bundle_collection->ids);
-      $site_id = $bundle_collection->site_id;
-
-      // Determine if the entity is remote or local.
-      if (strpos($bundle_name, 'bio_data_') !== 0) {
-        $external = TRUE;
-      }
-      else {
-        $external = FALSE;
-      }
-
-      foreach ($fields as $field) {
-        if ($external) {
-          $fake_tripal_entity = $this->getRemoteEntity($entity_ids, $site_id, $field, $bundle_name);
-          $field_name = $fake_tripal_entity->$field['und'][0]['field_name'];
-          $accession = $fake_tripal_entity->$field['und'][0]['accession'];
-          if (!empty($accession_ids) && !empty($row)) {
-            // If the accession is already in the accession_ids list skip.
-            if (array_search($accession, $accession_ids)) {
-              continue;
-            }
-            else {
-              $row[] = $field_name;
-              $accession_ids[] = $accession;
-            }
-          } 
-          else {
-            $row[] = $field_name;
-            $accession_ids[] = $accession;
-          }
-        }
-        else {
-          $field_info = field_info_field_by_id($field);
-          //$field_name is the accession info
-          $field_name = $field_info['field_name'];
-          $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
-          if (!empty($accession_ids) && !empty($row)) {
-            // If the accession is already in the accession_ids list skip.
-            if (array_search($accession, $accession_ids)) {
-              continue;
-            }
-            else {
-              $accession_ids[] = $field_name;
-              $row[] = $instance['label'];
-            }
-          } 
-          else {
-            $accession_ids[] = $field_name;
-            $row[] = $instance['label'];
-          }
-        }
-      }
-    }
-    $this->header_array = $row;
-    return array(implode("\t", $row));
+    return $this->processTabHeader($bundle_collections);
   }
 
 }