Browse Source

getting the fields to write to the file but I have introduced an error I have spent most of the day trying to run down fun times.

Shawna Spoor 7 years ago
parent
commit
f20d2690e7

+ 78 - 31
tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc

@@ -39,6 +39,11 @@ abstract class TripalFieldDownloader {
    */
   protected $outfile = '';
 
+  /**
+   * The remote site json data returned for the entity
+   */
+  protected $remote_entity = '';
+
   /**
    * Constructs a new instance of the TripalFieldDownloader class.
    * @param $bundle_name
@@ -133,7 +138,7 @@ abstract class TripalFieldDownloader {
             fwrite($fh, $line . "\r\n");
           }
         }*/
-        print_r($this->bundle_name);
+        //print_r($this->bundle_name);
         // Determine if the entity is remote or local.
         if (strpos($this->bundle_name, 'bio_data_') !== 0) {
           // Get all fields for that remote site/bundle name pairing, put them all in a fake entity
@@ -191,7 +196,31 @@ abstract class TripalFieldDownloader {
                           //print_r("\n \n");
                           // This entity needs to be pulled down and data grabbed.
                           $query = $entity_type . '/' . $single_id;
-                          $remote_entity = tripal_web_services_remote_request($site_id, $query);
+                          $this->remote_entity = tripal_web_services_remote_request($site_id, $query);
+                          /**
+                           * remote_entity looks like this:
+                           *  [@context] => http://demo.tripal.info/3.x/sites/default/files/tripal/ws/context/content.v0_1.gene.296.json
+                           *  [@id] => http://demo.tripal.info/3.x/web-services/content/v0.1/Gene/296
+                           *  [@type] => gene
+                           *  [label] => td01_000348m.g
+                           *  [ItemPage] => http://demo.tripal.info/3.x/bio_data/296
+                           *  [type] => Gene
+                           *  [organism] => Array
+                           *     (
+                           *         [label] => <i>Tripalus databasica</i>
+                           *          [genus] => Tripalus
+                           *          [species] => databasica
+                           *      )
+                           *
+                           *  [name] => td01_000348m.g
+                           *  [identifier] => td01_000348m.g
+                           *  [sequence_checksum] => d41d8cd98f00b204e9800998ecf8427e
+                           *  [time_accessioned] => 2011-06-30 17:00:58.050856
+                           *  [time_last_modified] => 2011-06-30 17:00:58.050856
+                           *  [sequence_coordinates] => http://demo.tripal.info/3.x/web-services/content/v0.1/Gene/296/Sequence+coordinates
+                           *  [relationship] => http://demo.tripal.info/3.x/web-services/content/v0.1/Gene/296/relationship
+                           */
+                          print_r($this->remote_entity);
                         }
                       }
                       
@@ -230,7 +259,7 @@ abstract class TripalFieldDownloader {
                     //print_r($entity_info);
                     // This entity needs to be pulled down and data grabbed.
                     $query = $entity_type . '/' . $entity_id;
-                    $remote_entity = tripal_web_services_remote_request($site_id, $query);
+                    $this->remote_entity = tripal_web_services_remote_request($site_id, $query);
                   }
                 }
               }
@@ -240,33 +269,29 @@ abstract class TripalFieldDownloader {
             // Need the label of the field so we can pull the data from the json.
             $fields = [];
             $fields = $this->fields;
+            print_r($fields);
+            print_r($site_vocab);
+            /**
+             * $site_vocab will look like this:
+             * [3] => Array(
+             *  [property] => data:0842
+             *  [hydra:title] => Identifier
+             *  [hydra:description] => 
+             *  [required] => 1
+             *  [readable] => 
+             *  [writeable] => 1
+             *  [tripal_formatters] => Array
+             *    (
+             *      [0] => TripalTabDownloader
+             *      [1] => TripalCSVDownloader
+             *    )
+             *  )
+             */
             foreach ($fields as $field) {
-              foreach ($site_vocab as $item) {
-                if (is_array($item)) {
-                  foreach ($item as $vocab_term) {
-                    if (!empty($vocab_term) && is_array($vocab_term)) {
-                      foreach ($vocab_term as $key => $term) {
-                        if (is_array($$key)) {
-                          print_r($key);
-                          if ($key['property'] === $field) {
-                            if (in_array('hydra:title', $key)) {
-                              $fields[] = $key['hydra:title'];
-                            }
-                          }
-                        
-                        } 
-                        else {
-
-                          if ($term['property'] === $field) {
-                            if (in_array('hydra:title', $term)) {
-                              $fields[] = $$term['hydra:title'];
-                            }
-                          }
-                        }
-                      }
-                    }
-                  }
-                }
+              $returned_array = $this->vocabSearch($site_vocab, $field, array());
+              if (in_array('hydra:title', $returned_array)) {
+                print_r($returned_array['hydra:title']);
+                $fields[] = $returned_array['hydra:title'];
               }
             }
 
@@ -285,8 +310,7 @@ abstract class TripalFieldDownloader {
                   ],
                 ],
               ];
-            }
-            //print_r($fake_tripal_entity);
+            };
             $lines = $this->formatEntity($fake_tripal_entity);
             foreach ($lines as $line) {
               fwrite($fh, $line . "\r\n");
@@ -396,6 +420,28 @@ abstract class TripalFieldDownloader {
 
   }
 
+  /**
+   * Recursive function to walk down the array looking for the passed value.
+   * 
+   * Returns the array of the field with title included.
+   */
+  public function vocabSearch($haystack, $needle, &$current_array) {
+    if (is_array($haystack)) {
+      foreach ($array as $item) {
+        if (is_array($item)) {
+          $current_array = $item;
+          vocabSearch($item, $needle, $current_array);
+        }
+        else {
+          print($item);
+          if (strtolower($item) == strtolower($needle)) {
+            return $current_array;
+          }
+        }
+      }
+    }
+  }
+
   /**
    * Formats the entity and the specified fields for output.
    *
@@ -419,4 +465,5 @@ abstract class TripalFieldDownloader {
    *  the header lines for an output file.
    */
   abstract protected function getHeader();
+
 }

+ 48 - 1
tripal/includes/TripalFieldDownloaders/TripalTabDownloader.inc

@@ -15,8 +15,55 @@ class TripalTabDownloader extends TripalFieldDownloader {
    * @see TripalFieldDownloader::format()
    */
   protected function formatEntity($entity) {
-    print_r($entity);
+   //print_r($entity);
     $row = array();
+     // 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) {
+        // Set the single bundle name for getting the Header.
+        $this->bundle_name = $bundle->bundle_name;
+        // Determine if the entity is remote or local.
+        if (strpos($this->bundle_name, 'bio_data_') !== 0) {
+          $this->remote_entity;
+        }
+        else {
+          foreach ($this->fields as $field_id) {
+            $field = field_info_field_by_id($field_id);
+            $field_name = $field['field_name'];
+      
+            if (!property_exists($entity, $field_name)) {
+              continue;
+            }
+      
+            // If we only have one element then this is good.
+            if (count($entity->{$field_name}['und']) == 1) {
+              $value = $entity->{$field_name}['und'][0]['value'];
+              // If the single element is not an array then this is good.
+              if (!is_array($value)) {
+                $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']);
+                }
+                else {
+                  $row[] = '';
+                }
+                // TODO: What to do with fields that are arrays?
+              }
+            }
+            else {
+              $row[] = '';
+              // TODO: What to do with fields that have multiple values?
+            }
+          }
+        }
+      }
+    }
+    else {
+      //Do it for one bundle
+    }
     foreach ($this->fields as $field_id) {
       $field = field_info_field_by_id($field_id);
       $field_name = $field['field_name'];