Browse Source

adding the fix so that content ends up in the correct column adn the header has single values

Shawna Spoor 7 years ago
parent
commit
fc0416c7ae

+ 72 - 9
tripal/includes/TripalFieldDownloaders/TripalCSVDownloader.inc

@@ -15,8 +15,12 @@ class TripalCSVDownloader extends TripalFieldDownloader {
    * @see TripalFieldDownloader::format()
    */
   protected function formatEntity($entity) {
-    $row = array();
+       $row = array();
+    $row_with_tabs = array();
     $bundle_name = $entity->bundle;
+    
+    // Grab the headers to ensure we tab the fields properly.
+    $header =  $this->header_array;
 
     // Determine if the entity is remote or local.
     if (strpos($bundle_name, 'bio_data_') !== 0) {
@@ -39,8 +43,26 @@ class TripalCSVDownloader extends TripalFieldDownloader {
       if ($external) {
         $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[] = $value;
+          $row[] = $tabs . $value;
         }
       }
       else {
@@ -49,13 +71,24 @@ class TripalCSVDownloader extends TripalFieldDownloader {
         
         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[] = $value;
+            $row[] = $tabs . $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']);
+              $row[] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
             }
             else {
               $row[] = '';
@@ -69,18 +102,18 @@ class TripalCSVDownloader extends TripalFieldDownloader {
         }
       }
     }
-    return array(implode("\t", $row));
+    return array(implode('', $row));
   }
 
   /**
    * @see TripalFieldDownloader::getHeader()
    */
   protected function getHeader() {
-    $row = array();
+   $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);
@@ -94,20 +127,50 @@ class TripalCSVDownloader extends TripalFieldDownloader {
       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'];
-          $row[] = $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);
-          $row[] = $instance['label'];
+          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));
   }
 

+ 14 - 2
tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc

@@ -48,6 +48,11 @@ abstract class TripalFieldDownloader {
    */
   protected $outfile = '';
 
+  /**
+   * An array of the header.
+   */
+  protected $header_array = array();
+
   /**
    * The remote site json data returned for the entity
    */
@@ -97,14 +102,15 @@ abstract class TripalFieldDownloader {
 
     // Make sure the user directory exists
     $user_dir = 'public://tripal/users/' . $user->uid;
+
     if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
       $message = 'Could not access the directory on the server for storing this file.';
       watchdog('tripal', $message, array(), WATCHDOG_ERROR);
-      /*drupal_json_output(array(
+      drupal_json_output(array(
         'status'  => 'failed',
         'message' => $message,
         'file_id' => '',
-      ));*/
+      ));
       return;
     }
 
@@ -349,6 +355,8 @@ abstract class TripalFieldDownloader {
           $fields[$remote_fields]['label'] = $machine_name;
           // Get the value from the previously created entity.
           $fields[$remote_fields]['value'] = $this->remote_entity[$machine_name];
+          // Add the accession information.
+          $fields[$remote_fields]['accession'] = $remote_fields;
         }
       }
       else {
@@ -363,6 +371,8 @@ abstract class TripalFieldDownloader {
               $fields[$field]['label'] = $machine_name;
               // Get the value from the previously created entity.
               $fields[$field]['value'] = $this->remote_entity[$machine_name];
+              // Add the accession information.
+              $fields[$field]['accession'] = $field;
             }
           }
         }
@@ -382,6 +392,7 @@ abstract class TripalFieldDownloader {
                 'field_name' => $field['field_name'],
                 'label' => $field['label'],
                 'value' => $field['value'],
+                'accession' => $field['accession'],
               ],
             ],
           ];
@@ -394,6 +405,7 @@ abstract class TripalFieldDownloader {
               'field_name' => $fields[$remote_fields]['field_name'],
               'label' => $fields[$remote_fields]['label'],
               'value' => $fields[$remote_fields]['value'],
+              'accession' => $fields[$remote_fields]['accession'],
             ],
           ],
         ];

+ 70 - 6
tripal/includes/TripalFieldDownloaders/TripalTabDownloader.inc

@@ -16,7 +16,11 @@ class TripalTabDownloader extends TripalFieldDownloader {
    */
   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;
 
     // Determine if the entity is remote or local.
     if (strpos($bundle_name, 'bio_data_') !== 0) {
@@ -39,8 +43,26 @@ class TripalTabDownloader extends TripalFieldDownloader {
       if ($external) {
         $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[] = $value;
+          $row[] = $tabs . $value;
         }
       }
       else {
@@ -49,13 +71,24 @@ class TripalTabDownloader extends TripalFieldDownloader {
         
         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[] = $value;
+            $row[] = $tabs . $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']);
+              $row[] = $tabs . strip_tags($entity->{$field_name}['und'][0]['value']['rdfs:label']);
             }
             else {
               $row[] = '';
@@ -69,7 +102,7 @@ class TripalTabDownloader extends TripalFieldDownloader {
         }
       }
     }
-    return array(implode("\t", $row));
+    return array(implode('', $row));
   }
 
   /**
@@ -77,6 +110,7 @@ class TripalTabDownloader extends TripalFieldDownloader {
    */
   protected function getHeader() {
     $row = array();
+    $accession_ids = array();
     $bundle_collections = $this->collection_bundles;
 
     foreach ($bundle_collections as $bundle_collection) {
@@ -93,20 +127,50 @@ class TripalTabDownloader extends TripalFieldDownloader {
       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'];
-          $row[] = $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);
-          $row[] = $instance['label'];
+          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));
   }