Jelajahi Sumber

Tripal Bulk Loader: Added ability to group fields -allows you to insert 1+ records into the same table per line of spreadsheet

laceysanderson 14 tahun lalu
induk
melakukan
fb15e1aa3d

+ 3 - 1
theme_tripal/tripal_bulk_loader/tripal_bulk_loader_edit_template_base_form.tpl.php

@@ -9,7 +9,8 @@
   <table>
     <tr>
       <th>Field Name</th>
-      <th> Chado Table</th>
+      <th>Record Group</th>
+      <th>Chado Table</th>
       <th>Chado Field</th>
       <th>Worksheet</th>
       <th>Column</th>
@@ -19,6 +20,7 @@
   <?php for($i=1; $i<$form['fields']['total_fields']['#value']; $i++) { ?>
     <tr>
       <td><?php print drupal_render($form['fields']["field_name-$i"]);?></td>
+      <td><?php print drupal_render($form['fields']["field_group-$i"]);?></td>
       <td><?php print drupal_render($form['fields']["chado_table_name-$i"]);?></td>
       <td><?php print drupal_render($form['fields']["chado_field_name-$i"]);?></td>
       <td><?php print drupal_render($form['fields']["sheet_name-$i"]);?></td>

+ 23 - 17
theme_tripal/tripal_bulk_loader/tripal_bulk_loader_template.tpl.php

@@ -2,22 +2,26 @@
 	//dpm($template->template_array, 'Template Array (tpl)');
 	$fields = array();
 	$constants = array();
-	foreach ($template->template_array as $table => $table_array) {
-		if (!is_array($table_array)) {
+	foreach ($template->template_array as $table => $table_groups) {
+		if (!is_array($table_groups)) {
 			continue;
 		}
 		
-		foreach ($table_array['field'] as $field) {
-			if (preg_match('/table field/', $field['type'])) {
-				$field['table'] = $table;
-				$sheet = $field['spreadsheet sheet'];
-				$column = $field['spreadsheet column'];
-				$fields[$sheet.'-'.$column][] = $field;
-			} elseif ($field['type'] == 'constant') {
-				$field['table'] = $table;
-				$constants[] = $field;
-			}
-		}
+		foreach ($table_groups as $group => $table_array) {
+      foreach ($table_array['field'] as $field) {
+        if (preg_match('/table field/', $field['type'])) {
+          $field['table'] = $table;
+          $field['group'] = $group;
+          $sheet = $field['spreadsheet sheet'];
+          $column = $field['spreadsheet column'];
+          $fields[$sheet.'-'.$column][] = $field;
+        } elseif ($field['type'] == 'constant') {
+          $field['table'] = $table;
+          $field['group'] = $group;
+          $constants[] = $field;
+        }
+      }
+    }
 	}
 ?>
 
@@ -27,13 +31,14 @@
   
   <table id="tripal_bulk_loader-template_constant-table" class="tripal_bulk_loader-table tripal-table tripal-table-vert">
   <caption><b>Constants</b> -These values are applied to all records in the Spreadsheet</caption>
-    <tr><th rowspan="2">Field Name</th><th rowspan="2">Value</th><th colspan="2">Chado Database</th></tr>
-    <tr><th>Table</th><th>Field</th></tr>
+    <tr><th rowspan="2">Field Name</th><th rowspan="2">Value</th><th colspan="3">Chado Database</th></tr>
+    <tr><th>Group</th><th>Table</th><th>Field</th></tr>
     <?php $row = 'even' ?>
     <?php foreach ($constants as $field) {?>
       <tr class="tripal_bulk_loader-table-<?php print $row; ?>-row tripal-table-<?php print $row; ?>-row">
         <td><?php print $field['title'];?></td>
         <td><?php print $field['constant value']; ?></td>
+        <td><?php print $field['group'];?></td>
         <td><?php print $field['table'];?></td>
         <td><?php print $field['field'];?></td>	
       </tr>
@@ -43,8 +48,8 @@
   
   <table id="tripal_bulk_loader-template_fields-table" class="tripal_bulk_loader-table tripal-table tripal-table-vert">
   <caption><b>Fields</b> -Below is a mapping between Spreadsheet columns and the Chado Database</caption>
-  <tr><th rowspan="2">Field Name</th><th colspan="2">Spreadsheet</th><th colspan="2">Chado Datbase</th></tr>
-  <tr><th>Worksheet</th><th>Column</th><th>Table</th><th>Field</th></tr>
+  <tr><th rowspan="2">Field Name</th><th colspan="2">Spreadsheet</th><th colspan="3">Chado Datbase</th></tr>
+  <tr><th>Worksheet</th><th>Column</th><th>Group</th><th>Table</th><th>Field</th></tr>
   <?php $row = 'even' ?>
   <?php foreach ($fields as $column) {?>
     <?php foreach ($column as $field) {?>
@@ -52,6 +57,7 @@
       <td><?php print $field['title'];?></td>
       <td><?php print $field['spreadsheet sheet']; ?></td>
       <td><?php print $field['spreadsheet column'];?></td>
+      <td><?php print $field['group'];?></td>
       <td><?php print $field['table'];?></td>
       <td><?php print $field['field'];?></td>
     <tr>

+ 78 - 15
tripal_bulk_loader/tripal_bulk_loader.admin.inc

@@ -201,10 +201,21 @@ function tripal_bulk_loader_edit_template_base_form($form_state = NULL){
     
     // List Current Fields -------------------------------------------------------------
     $i=1;
-    foreach ($form_state['storage']['template'] as $table => $table_array) {
-      if (!is_array($table_array)) { continue; }
+    foreach ($form_state['storage']['template'] as $table => $table_groups) {
+      if (!is_array($table_groups)) { continue; }
       
-      foreach ($table_array['field'] as $field_index => $field) {
+      foreach ($table_groups as $group => $table_array) {
+        foreach ($table_array['field'] as $field_index => $field) {
+          $form['fields']["field_group-$i"] = array(
+            '#type' => 'item',
+            '#value' => $group
+          );
+
+          $form['fields']["field_group_hidden-$i"] = array(
+            '#type' => 'hidden',
+            '#value' => $group
+          );
+          
           $form['fields']["field_name-$i"] = array(
             '#type' => 'item',
             '#value' => $field['title'],
@@ -261,6 +272,7 @@ function tripal_bulk_loader_edit_template_base_form($form_state = NULL){
           );
           
           $i++;
+        }
       }
     }
     $form['fields']['total_fields'] = array(
@@ -307,10 +319,14 @@ function tripal_bulk_loader_edit_template_base_form_submit($form, &$form_state){
       $form_state = tripal_bulk_loader_edit_template_field_form_submit($form_state);
     } elseif (preg_match('/Delete Field #(\d+)/', $form_state['values']['op'], $matches)) {
       $table = $form_state['values']["chado_table_hidden-".$matches[1]];
+      $group = $form_state['values']["field_group_hidden-".$matches[1]];
       $field_key = $form_state['values']["field_index-".$matches[1]];
-      unset($form_state['storage']['template'][$table]['field'][$field_key]);
-      if (!$form_state['storage']['template'][$table]['field']) {
-        unset($form_state['storage']['template'][$table]);
+      unset($form_state['storage']['template'][$table][$group]['field'][$field_key]);
+      if (!$form_state['storage']['template'][$table][$group]['field']) {
+        unset($form_state['storage']['template'][$table][$group]);
+        if (!$form_state['storage']['template'][$table]) {
+          unset($form_state['storage']['template'][$table]);
+        }
       }   
     }
     
@@ -389,6 +405,7 @@ function tripal_bulk_loader_add_template_field_form ($form_state) {
   $form = array();
  
  	$base_table = $form_state['storage']['base_table'];
+ 	$table = ($form_state['storage']['add']['chado_table'])? $form_state['storage']['add']['chado_table'] : $base_table;
  	
 	$form['add_fields'] = array(
 		'#type' => 'fieldset',
@@ -412,12 +429,26 @@ function tripal_bulk_loader_add_template_field_form ($form_state) {
 			'effect' => 'fade'
 			),
 	);
-	
+
+  // check template array for groups already used for this table then add one more
+  if ($form_state['storage']['template'][$table]) {
+    $groups = array_keys($form_state['storage']['template'][$table]);
+  }
+  $groups[] = 'New Group';
+  $form['add_fields']['add-field_group']  = array(
+    '#type' => 'select',
+    '#title' => 'Group',
+    '#description' => 'This is used to group a set of fields together allowing '
+      .'multiple records to be inserted into the same table per line of the spreadsheet',
+    '#options' => $groups,
+    '#default_value' => 0
+  );
+  
 	$form['add_fields']['add-field_title'] = array(
 		'#type' => 'textfield',
 		'#title' => t('Human-readable Title for Field')
 	);
-
+  
 	// Spreadsheet column
 	$form['add_fields']['columns'] = array(
 		'#type' => ($field_type == 'table field')? 'fieldset' : 'hidden',
@@ -463,7 +494,6 @@ function tripal_bulk_loader_add_template_field_form ($form_state) {
 	);
 	
 	$related_tables = module_invoke_all('tripal_bulk_loader_'.$base_table.'_related_tables');
-	$table = ($form_state['storage']['add']['chado_table'])? $form_state['storage']['add']['chado_table'] : $base_table;
 	$form['add_fields']['chado']['add-chado_table'] = array(
 		'#type' => 'select',
 		'#title' => t('Chado Table'),
@@ -531,6 +561,7 @@ function tripal_bulk_loader_add_template_field_form ($form_state) {
 function tripal_bulk_loader_add_template_field_form_submit ($form_state) {
   //dpm($form_state, 'form state in add submit');
   
+  $form_state['storage']['add']['field_group'] = $form_state['values']['add-field_group'];
   $form_state['storage']['add']['field_title'] = $form_state['values']['add-field_title'];
   $form_state['storage']['add']['field_type'] = $form_state['values']['add-type'];
   $form_state['storage']['add']['sheet_name'] = $form_state['values']['add-sheet'];
@@ -566,8 +597,10 @@ function tripal_bulk_loader_add_template_field_form_submit ($form_state) {
         
       }
       
+      $group = $form_state['storage']['add']['field_group'];
+      $table = $form_state['storage']['add']['chado_table'];
       if ($form_state['storage']['add']['field_type'] == 'table field') {
-        $template[$form_state['storage']['add']['chado_table']]['field'][] = array(
+        $template[$table][$group]['field'][] = array(
           'type' => 'table field',
           'title' => $form_state['storage']['add']['field_title'],
           'field' => $form_state['storage']['add']['chado_field'],
@@ -582,7 +615,7 @@ function tripal_bulk_loader_add_template_field_form_submit ($form_state) {
           //),        
         );
       } elseif ($form_state['storage']['add']['field_type'] == 'constant') {
-        $template[$form_state['storage']['add']['chado_table']]['field'][] = array(
+        $template[$table][$group]['field'][] = array(
           'type' => 'constant',
           'title' => $form_state['storage']['add']['field_title'],
           'field' => $form_state['storage']['add']['chado_field'],
@@ -622,6 +655,7 @@ function tripal_bulk_loader_edit_template_field_form($form_state) {
   $form = array();
   
  	$base_table = $form_state['storage']['base_table'];
+ 	$table = ($form_state['storage']['edit']['chado_table'])? $form_state['storage']['edit']['chado_table'] : $base_table;
  	
 	$form['edit_fields'] = array(
 		'#type' => ($form_state['storage']['edit_field']) ? 'fieldset' : 'hidden',
@@ -645,7 +679,26 @@ function tripal_bulk_loader_edit_template_field_form($form_state) {
 			'effect' => 'fade'
 			),
 	);
-	
+
+  // check template array for groups already used for this table then add one more
+  if ($form_state['storage']['template'][$table]) {
+    $groups = array_keys($form_state['storage']['template'][$table]);
+  }
+  $groups[] = 'New Group';
+  $form['edit_fields']['edit-field_group']  = array(
+    '#type' => 'select',
+    '#title' => 'Group',
+    '#description' => 'This is used to group a set of fields together allowing '
+      .'multiple records to be inserted into the same table per line of the spreadsheet',
+    '#options' => $groups,
+    '#default_value' => ($form_state['storage']['edit']['field_group']) ? $form_state['storage']['edit']['field_group'] : 0
+  );
+  
+  $form['edit_fields']['edit-old_field_group'] = array(
+    '#type' => 'hidden',
+    '#value' => $form_state['storage']['edit_field']['group'],
+  );
+  
 	$form['edit_fields']['edit-field_title'] = array(
 		'#type' => 'textfield',
 		'#title' => t('Human-readable Title for Field'),
@@ -697,7 +750,6 @@ function tripal_bulk_loader_edit_template_field_form($form_state) {
 	);
 	
 	$related_tables = module_invoke_all('tripal_bulk_loader_'.$base_table.'_related_tables');
-	$table = ($form_state['storage']['edit']['chado_table'])? $form_state['storage']['edit']['chado_table'] : $base_table;
 	$form['edit_fields']['chado']['edit-chado_table'] = array(
 		'#type' => 'select',
 		'#title' => t('Chado Table'),
@@ -751,8 +803,10 @@ function tripal_bulk_loader_edit_template_field_form_submit ($form_state) {
   if (preg_match('/Edit Field #(\d+)/', $form_state['values']['op'], $matches)) {
     $table = $form_state['values']["chado_table_hidden-".$matches[1]];
     $field_key = $form_state['values']["field_index-".$matches[1]];
-    $form_state['storage']['edit_field'] = $form_state['storage']['template'][$table]['field'][$field_key];
+    $old_group = $form_state['values']["field_group_hidden-".$matches[1]];
+    $form_state['storage']['edit_field'] = $form_state['storage']['template'][$table][$old_group]['field'][$field_key];
     $form_state['storage']['edit_field']['table'] = $table;
+    $form_state['storage']['edit_field']['group'] = $old_group;
     $form_state['storage']['edit_field']['field_index'] = $field_key;
     
     $set_default = TRUE;
@@ -762,6 +816,7 @@ function tripal_bulk_loader_edit_template_field_form_submit ($form_state) {
   }
   
   if ($set_default) {
+    $form_state['storage']['edit']['field_group'] = $old_group;
     $form_state['storage']['edit']['field_title'] = $field_template['title'];
     $form_state['storage']['edit']['field_type'] = $field_template['type'];
     $form_state['storage']['edit']['sheet_name'] = $field_template['spreadsheet sheet'];
@@ -770,6 +825,7 @@ function tripal_bulk_loader_edit_template_field_form_submit ($form_state) {
     $form_state['storage']['edit']['chado_table'] = $field_template['table'];
     $form_state['storage']['edit']['chado_field'] = $field_template['field'];
   } else {
+    $form_state['storage']['edit']['field_group'] = $form_state['values']['edit-field_group'];
     $form_state['storage']['edit']['field_title'] = $form_state['values']['edit-field_title'];
     $form_state['storage']['edit']['field_type'] = $form_state['values']['edit-type'];
     $form_state['storage']['edit']['sheet_name'] = $form_state['values']['edit-sheet'];
@@ -809,8 +865,13 @@ function tripal_bulk_loader_edit_template_field_form_submit ($form_state) {
       }
       
       $table = $form_state['storage']['edit_field']['table'];
+      $group = $form_state['values']['edit-field_group'];
+      $old_group = $form_state['values']['edit-old_field_group'];
       $field_index = $form_state['storage']['edit_field']['field_index'];
-      $form_state['storage']['template'][$table]['field'][$field_index] = $field;
+      $form_state['storage']['template'][$table][$group]['field'][$field_index] = $field;
+      if ($group != $old_group) {
+        unset($form_state['storage']['template'][$table][$old_group]['field'][$field_index]);
+      }
       drupal_set_message('Successfully Updated Field'); 
       
       //reset form
@@ -819,6 +880,8 @@ function tripal_bulk_loader_edit_template_field_form_submit ($form_state) {
     }
   } 
   
+  //dpm($form_state, 'form state from end of edit submit');
+  
   return $form_state;
 }
 

+ 105 - 91
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -77,53 +77,46 @@ function tripal_bulk_loader_load_data($nid) {
   $field2column = array();
   $relationships2base = array();
   $all_tables = array();
-  foreach ($node->template->template_array as $table => $table_array) {
-    if (is_array($table_array)) {
-      $all_tables[$table] = $table;
-    
-      foreach ($table_array['field'] as $field_array) {
-        if (preg_match('/table field/', $field_array['type'])) {
-          $default_values_array[$table][$field_array['field']] = '';
-          $field2column[$table][$field_array['field']] = $field_array['spreadsheet column'];
-        } elseif (preg_match('/constant/', $field_array['type'])) {
-          $default_values_array[$table][$field_array['field']] = $field_array['constant value'];
-        } else {
-          print 'WARNING: Unsupported type: '. $field_array['type'] . ' for ' . $table . '.' . $field_array['field']."!\n";
-        }
-      }
-      
-      // Determine what relation is between this table and the base table-----------------------
-      // This is used later to link the various records
-      //print "===\n";
-      // Is there a foreign key to this table in the base table? 
-      // ie: feature.type_id (base) for cvterm (current)
-      //print "A) Is there fkey to ".$table." table in ".$base_table."?\n";
-      if ($related_tables['Foreign Key Relations'][$table]) {
-          //print "YES!\n";
-          $relationships2base['foreign key'][$table] = $base_table_desc['foreign keys'][$table];
-      }
-      
-      // Is there a foreign key in this table to the base table? 
-      // ie: featureloc.feature_id (current) for feature (base)
-      
-      //print "B) Does ".$table." contain a fkey to ".$base_table."?\n";
-      if ($related_tables['Direct Relations'][$table]) {
-          $table_desc = module_invoke_all('chado_'.$table.'_schema');
-          //print "YES!\n";
-          $relationships2base['direct'][$table] = $table_desc['foreign keys'][$base_table];
-      }
-      
-      // Is there a linking table which links this table with the base table
-      // ie: analysisfeature (current) links analysis and feature (base)
-      //print "C) Is ".$table." a linking table?\n";
-      if ($related_tables['Indirect Relations'][$table]) {
-        //print "YES!\n";
-        $table_desc = module_invoke_all('chado_'.$table.'_schema');
-        $relationships2base['indirect'][$table] = $table_desc['foreign keys'];
-      }
-
-    }
-  }
+  foreach ($node->template->template_array as $table => $table_groups) {
+  	foreach ($table_groups as $group => $table_array) {
+			if (is_array($table_array)) {
+				$all_tables[$table] = $table;
+			
+				foreach ($table_array['field'] as $field_array) {
+					if (preg_match('/table field/', $field_array['type'])) {
+						$default_values_array[$table][$group][$field_array['field']] = '';
+						$field2column[$table][$group][$field_array['field']] = $field_array['spreadsheet column'];
+					} elseif (preg_match('/constant/', $field_array['type'])) {
+						$default_values_array[$table][$group][$field_array['field']] = $field_array['constant value'];
+					} else {
+						print 'WARNING: Unsupported type: '. $field_array['type'] . ' for ' . $table . '.' . $field_array['field']."!\n";
+					}
+				}
+				
+				// Determine what relation is between this table and the base table-----------------------
+				// This is used later to link the various records
+				
+				// Is there a foreign key to this table in the base table? 
+				if ($related_tables['Foreign Key Relations'][$table]) {
+						$relationships2base['foreign key'][$table] = $base_table_desc['foreign keys'][$table];
+				}
+				
+				// Is there a foreign key in this table to the base table? 
+				// ie: featureloc.feature_id (current) for feature (base)
+				if ($related_tables['Direct Relations'][$table]) {
+						$table_desc = module_invoke_all('chado_'.$table.'_schema');
+						$relationships2base['direct'][$table] = $table_desc['foreign keys'][$base_table];
+				}
+				
+				// Is there a linking table which links this table with the base table
+				if ($related_tables['Indirect Relations'][$table]) {
+					$table_desc = module_invoke_all('chado_'.$table.'_schema');
+					$relationships2base['indirect'][$table] = $table_desc['foreign keys'];
+				}
+	
+			}
+		}
+  } //end of preprocessing
   
   //print "\nDefault Values Array: ".print_r($default_values_array, TRUE)."\n";
   //print "\nField to Column Mapping: ".print_r($field2column, TRUE)."\n";
@@ -147,9 +140,18 @@ function tripal_bulk_loader_load_data($nid) {
     
     // Insert base record-----------------------------------------------------------------------
     foreach ($relationships2base['foreign key'] as $table => $desc) {
+  		// check there is only 1 group & if more than one use first and warn
+  		if (sizeof($values[$table]) > 1) {
+  			print "WARNING: ".$table." has more than one group. There can only be one group for a "
+  				."table related to the base by a foreign key. Only the first will be used.\n";
+  		}
+  		
       // Ensure foreign key is present
-      $values[$table] = tripal_bulk_loader_supplement_values_array ($values[$table], $line, $field2column[$table]);
-      $success = tripal_bulk_loader_ensure_record ($table, $values[$table]);   
+      $group = key($values[$table]);
+      $sub_values = $values[$table][$group];
+      $sub_values = tripal_bulk_loader_supplement_values_array ($sub_values, $line, $field2column[$table][$group]);
+      $values[$table][$group] = $sub_values;
+      $success = tripal_bulk_loader_ensure_record ($table, $sub_values);   
       if ($success) {
         $num_records++;
       } else {
@@ -158,19 +160,25 @@ function tripal_bulk_loader_load_data($nid) {
       
       //Add to base values array
       foreach ($desc['columns'] as $foreign_key => $primary_key) {
-        $values[$base_table][$foreign_key] = $values[$table];
+				// check there is only 1 group & if more than one use first and warn
+				if (sizeof($values[$base_table]) > 1) {
+					print "WARNING: ".$table." has more than one group. There can only be one group for a "
+						."table related to the base by a foreign key. Only the first will be used.\n";
+				}   
+				$base_group = key($values[$base_table]);
+        $values[$base_table][$base_group][$foreign_key] = $values[$table][$group];
       }
       
       unset($tables[$table]);
     }
     
     // base table
-    $values[$base_table] = tripal_bulk_loader_supplement_values_array ($values[$base_table], $line, $field2column[$base_table]);
-    $success = tripal_bulk_loader_ensure_record ($base_table, $values[$base_table]);     
+    $values[$base_table][$base_group] = tripal_bulk_loader_supplement_values_array ($values[$base_table][$base_group], $line, $field2column[$base_table][$base_group]);
+    $success = tripal_bulk_loader_ensure_record ($base_table, $values[$base_table][$base_group]);     
     //print 'Base Table values array: '.print_r($values[$base_table], TRUE)."\n";
     if (!$success) {
       print "ERROR: Unable to insert base record where base table: ".$base_table.
-        " and values array: ".tripal_bulk_loader_flatten_array($values[$base_table])."\n";
+        " and values array: ".tripal_bulk_loader_flatten_array($values[$base_table][$base_group])."\n";
       $num_errors++;
     } else {
       $num_records++;
@@ -179,56 +187,62 @@ function tripal_bulk_loader_load_data($nid) {
     
     // Insert all tables with direct relationship to base ----------------------------------------
     foreach ($relationships2base['direct'] as $table => $desc) {
-      //Add base to values array
-      foreach ($desc['columns'] as $foreign_key => $primary_key) {
-        $values[$table][$foreign_key] = $values[$base_table];
-      }
+			foreach ($values[$table] as $group => $sub_values) {
+	      //Add base to values array
+  	    foreach ($desc['columns'] as $foreign_key => $primary_key) {  	
+        	$values[$table][$group][$foreign_key] = $values[$base_table][$base_group];
+        }
 
-      // Supplement and Add record
-      $values[$table] = tripal_bulk_loader_supplement_values_array ($values[$table], $line, $field2column[$table]);
-      $success = tripal_bulk_loader_ensure_record ($table, $values[$table]);
-      if ($success) {
-        $num_records++;
-      } else {
-        $num_errors++;
-      }
-      unset($tables[$table]);
+				// Supplement and Add record
+				$values[$table][$group] = tripal_bulk_loader_supplement_values_array ($values[$table][$group], $line, $field2column[$table][$group]);
+				$success = tripal_bulk_loader_ensure_record ($table, $values[$table][$group]);
+				if ($success) {
+					$num_records++;
+				} else {
+					$num_errors++;
+				}
+				unset($tables[$table]);
+			}
     } 
     
     // Add in all other tables -----------------------------------------------------------------
     foreach ($tables as $table) {
       // Don't insert if its an indirect relationship linking table
       if (!$relationships2base['indirect'][$table]) {
-        // Supplement and Add record
-        $values[$table] = tripal_bulk_loader_supplement_values_array ($values[$table], $line, $field2column[$table]);
-        $success = tripal_bulk_loader_ensure_record ($table, $values[$table]);    
-        if ($success) {
-          $num_records++;
-        } else {
-          $num_errors++;
-        }
+      	foreach ($values[$table] as $group => $sub_values) {
+					// Supplement and Add record
+					$values[$table][$group] = tripal_bulk_loader_supplement_values_array ($values[$table][$group], $line, $field2column[$table][$group]);
+					$success = tripal_bulk_loader_ensure_record ($table, $values[$table][$group]);    
+					if ($success) {
+						$num_records++;
+					} else {
+						$num_errors++;
+					}
+				}
       }
     }
     
     // Add in indirect relationships -----------------------------------------------------------
     foreach ($relationships2base['indirect'] as $table => $desc) {
-      // Add foreign keys to values array
-      foreach ($desc as $subtable => $subdesc) {
-        foreach ($subdesc['columns'] as $foreign_key => $primary_key) {
-          $values[$table][$foreign_key] = $values[$subtable];
-        }
-      }
-      
-      // Supplement and Add record
-      $values[$table] = tripal_bulk_loader_supplement_values_array ($values[$table], $line, $field2column[$table]);
-      $success = tripal_bulk_loader_ensure_record ($table, $values[$table]);      
-      if ($success) {
-        $num_records++;
-      } else {
-        $num_errors++;
-      }
-    }
-    
+    	foreach ($values[$table] as $group => $sub_values) {
+				// Add foreign keys to values array
+				foreach ($desc as $subtable => $subdesc) {
+					foreach ($subdesc['columns'] as $foreign_key => $primary_key) {
+						$values[$table][$group][$foreign_key] = $values[$subtable][$group];
+					}
+				}
+				
+				// Supplement and Add record
+				$values[$group][$table] = tripal_bulk_loader_supplement_values_array ($values[$table][$group], $line, $field2column[$table][$group]);
+				$success = tripal_bulk_loader_ensure_record ($table, $values[$table][$group]);      
+				if ($success) {
+					$num_records++;
+				} else {
+					$num_errors++;
+				}
+			}
+		}
+		
   } //end of file
   
   print "\nNumber of Records Inserted:".$num_records."\n";