Эх сурвалжийг харах

Bulk Loader: Added edit template and themed template display

laceysanderson 14 жил өмнө
parent
commit
485ce1b529

+ 37 - 44
theme_tripal/node-tripal_bulk_loader.tpl.php

@@ -1,46 +1,39 @@
-<?php
-//
-// Copyright 2009 Clemson University
-//
-?>
 
-   <?php if ($picture) {
-      print $picture;
-   }?>
-    
-   <div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
+<div id="tripal_bulk_loader-base-box" class="tripal_bulk_loader-info-box tripal-info-box">
+  <div class="tripal_bulk_loader-info-box-title tripal-info-box-title"></div>
+  <div class="tripal_bulk_loader-info-box-desc tripal-info-box-desc"></div>
+  
+	<table id="tripal_bulk_loader-base-table" class="tripal_bulk_loader-table tripal-table tripal-table-vert">
+		<tr class="tripal_bulk_loader-table-odd-row tripal-table-odd-row">
+			<th>Job Name</th>
+			<td><?php print $node->loader_name;?></td>
+		</tr>
+		<tr class="tripal_bulk_loader-table-even-row tripal-table-even-row">
+			<th>Submitted By</th>
+			<td><span class="author"><?php print theme('username', $node); ?></span></td>
+		</tr>
+		<tr class="tripal_bulk_loader-table-odd-row tripal-table-odd-row">
+			<th>Job Creation Date</th>
+			<td><?php print format_date($node->created, 'custom', "F j, Y, g:i a"); ?></td>
+		</tr>		
+		<tr class="tripal_bulk_loader-table-even-row tripal-table-even-row">
+			<th>Last Updated</th>
+			<td><?php print format_date($node->changed, 'custom', "F j, Y, g:i a"); ?></td>
+		</tr>	
+		<tr class="tripal_bulk_loader-table-odd-row tripal-table-odd-row">
+			<th>Template Name</th>
+			<td><?php print $node->template->name; ?></td>
+		</tr>
+		<tr class="tripal_bulk_loader-table-even-row tripal-table-even-row">
+			<th>Data File</th>
+			<td><?php print $node->file;?></td>
+		</tr>
+		<tr class="tripal_bulk_loader-table-odd-row tripal-table-odd-row">
+			<th>Job Status</th>
+			<td><?php print $node->job_status;?></td>
+		</tr>
+	</table>  
+</div>
 
-   <?php if ($page == 0) { ?><h2 class="nodeTitle"><a href="<?php print $node_url?>"><?php print $title?></a>
-	<?php global $base_url;
-	if ($sticky) { print '<img src="'.base_path(). drupal_get_path('theme','sanqreal').'/img/sticky.gif" alt="sticky icon" class="sticky" />'; } ?>
-	</h2><?php }; ?>
-    
-	<?php if (!$teaser): ?>
-	<?php if ($submitted): ?>
-      <div class="metanode"><p><?php print t('') .'<span class="author">'. theme('username', $node).'</span>' . t(' - Posted on ') . '<span class="date">'.format_date($node->created, 'custom', "d F Y").'</span>'; ?></p></div>
-      <div>
-      <!-- tripal_analysis theme -->
-         <table>
-            <tr><th>Loader Name</th><td><?php print $node->loader_name;?></td></tr>
-            <tr><th>Template</th><td><?php $t = db_result(db_query("SELECT name FROM {tripal_bulk_loader_template} WHERE template_id = %d", $node->template_id)); print $t;?></td></tr>
-            <tr><th>Data File</th><td><?php print $node->file;?></td></tr>
-         </table>
-      <!-- End of tripal_analysis theme-->
-	  </div> 
-    <?php endif; ?>
-    <?php endif; ?>
-    
-    <div class="content"><?php print $content?></div>
-    
-    <?php if (!$teaser): ?>
-    <?php if ($links) { ?><div class="links"><?php print $links?></div><?php }; ?>
-    <?php endif; ?>
-    
-    <?php if ($teaser): ?>
-    <?php if ($links) { ?><div class="linksteaser"><div class="links"><?php print $links?></div></div><?php }; ?>
-    <?php endif; ?>
-    
-    <?php if (!$teaser): ?>
-    <?php if ($terms) { ?><div class="taxonomy"><span><?php print t('tags') ?></span> <?php print $terms?></div><?php } ?>
-    <?php endif; ?>
-  </div>
+<h3>Template</h3>
+<?php print theme('tripal_bulk_loader_template', $node->template->template_id); ?>

+ 42 - 0
theme_tripal/tripal_bulk_loader/tripal_bulk_loader_edit_template_base_form.tpl.php

@@ -0,0 +1,42 @@
+
+<div id="tripal-bulk-loader-fields">
+
+<?php print drupal_render($form['template_name']); ?>
+
+<!-- For each field display details plus edit/delete buttons-->
+<fieldset><legend>Current Fields</legend>
+<?php if ($form['fields']['total_fields']['#value'] > 0) {?>
+  <table>
+    <tr>
+      <th>Field Name</th>
+      <th> Chado Table</th>
+      <th>Chado Field</th>
+      <th>Worksheet</th>
+      <th>Column</th>
+      <th>Constant Value</th>
+      <th></th>
+    </tr>
+  <?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']["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>
+      <td><?php print drupal_render($form['fields']["column_num-$i"]);?></td>
+      <td><?php print drupal_render($form['fields']["constant_value-$i"]);?></td>
+      <td>
+        <?php print drupal_render($form['fields']["edit-$i"]);?>
+        <?php print drupal_render($form['fields']["delete-$i"]);?>
+        <?php print drupal_render($form['fields']["field_index-$i"]);?>
+      </td>
+    </tr>
+  <?php } ?>
+  </table>
+<?php 
+  } 
+  unset($form['fields']);
+?>
+</fieldset>
+<!-- Display Rest of form -->
+<?php print drupal_render($form); ?>
+</div>

+ 51 - 0
theme_tripal/tripal_bulk_loader/tripal_bulk_loader_template.tpl.php

@@ -0,0 +1,51 @@
+<?php
+	//dpm($template->template_array, 'Template Array (tpl)');
+	$fields = array();
+	$constants = array();
+	foreach ($template->template_array as $table => $table_array) {
+		if (!is_array($table_array)) {
+			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;
+			}
+		}
+	}
+?>
+
+<table>
+<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>
+	<?php foreach ($constants as $field) {?>
+		<td><?php print $field['title'];?></td>
+		<td><?php print $field['constant value']; ?></td>
+		<td><?php print $field['table'];?></td>
+		<td><?php print $field['field'];?></td>	
+	<?php } ?>
+</table>
+
+<table>
+<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>
+<?php foreach ($fields as $column) {?>
+	<?php foreach ($column as $field) {?>
+	<tr>
+		<td><?php print $field['title'];?></td>
+		<td><?php print $field['spreadsheet sheet']; ?></td>
+		<td><?php print $field['spreadsheet column'];?></td>
+		<td><?php print $field['table'];?></td>
+		<td><?php print $field['field'];?></td>
+	<tr>
+<?php }} ?>
+</table>
+

+ 760 - 166
tripal_bulk_loader/tripal_bulk_loader.admin.inc

@@ -1,5 +1,5 @@
 <?php
-/*******************************************************************************
+/**
  * tripal_bulk_loader_admin_template
  */
 function tripal_bulk_loader_admin_template () {
@@ -10,17 +10,6 @@ function tripal_bulk_loader_admin_template () {
    return $output;
 }
 
-/*******************************************************************************
- * tripal_bulk_loader_admin_template_add
- */
-function tripal_bulk_loader_admin_template_add () {
-  $output = '';
-  
-  $output .= drupal_get_form('tripal_bulk_loader_create_template_base_form');
-  
-	return $output;
-}
-
 /**
  * Implements hook_tripal_bulk_loader_supported_modules
  * 
@@ -57,7 +46,11 @@ function tripal_bulk_loader_tripal_bulk_loader_feature_related_tables () {
   );
 }
 
-/*******************************************************************************
+//////////////////////////////////////////////////////////////////////////////////////
+// Create Template
+//////////////////////////////////////////////////////////////////////////////////////
+
+/**
  * tripal_bulk_loader_admin_template_form
  */
 function tripal_bulk_loader_create_template_base_form (&$form_state = NULL) {
@@ -93,113 +86,8 @@ function tripal_bulk_loader_create_template_base_form (&$form_state = NULL) {
 
   if ($base_table) {
     
-    // Add Field (Maps to column in spreadsheet)--------------------------------------
-    $form['add_fields'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Step 2: Add Fields'),
-      '#prefix' => '<div id="tripal-bulk-loader-field">',
-      '#suffix' => '</div>',
-    );
-
-    $field_type = ($form_state['storage']['field_type'])? $form_state['storage']['field_type'] : 'column';
-    $form['add_fields']['type'] = array(
-      '#type' => 'radios',
-      '#title' => t('Type of Field'),
-      '#options' => array(
-        'column' => t('Fields which maps to a Spreadsheet Column'),
-        'constant' => t('Field which remains Constant throughout the Spreadsheet'),
-      ),
-      '#default_value' => $field_type,
-      '#ahah' => array(
-        'path' => 'admin/tripal/tripal_bulk_loader_template/add/field_type_ahah',
-        'wrapper' => 'tripal-bulk-loader-field',
-        'effect' => 'fade'
-        ),
-    );
-    
-    $form['add_fields']['field_title'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Human-readable Title for Field')
-    );
-
-    // Spreadsheet column
-    $form['add_fields']['columns'] = array(
-      '#type' => ($field_type == 'column')? 'fieldset' : 'hidden',
-      '#title' => t('Spreadsheet Column'),
-      '#prefix' => '<div id="tripal-bulk-loader-spreadsheet-column">',
-      '#suffix' => '</div>',
-    );
-
-    $form['add_fields']['columns']['sheet'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Worksheet'),
-      '#description' => t('Specify the name of the worksheet.'),
-      '#size' => 5,
-      '#default_value' => ($form_state['storage']['sheet_name'])? $form_state['storage']['sheet_name'] : 'Sheet1',    
-    );
-    
-    $form['add_fields']['columns']['column'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Column'),
-      '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
-      '#size' => 5,
-      '#default_value' => $form_state['storage']['column_number'],
-    );
-
-    // Global Value
-    $form['add_fields']['constant'] = array(
-      '#type' => ($field_type == 'constant')? 'fieldset' : 'hidden',
-      '#title' => t('Constant'),
-      '#prefix' => '<div id="tripal-bulk-loader-constant-column">',
-      '#suffix' => '</div>',
-    );
-    
-    $form['add_fields']['constant']['constant_value'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Constant Value'),
-      '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
-      '#default_value' => $form_state['storage']['constant_value']
-    );
-    
-    // Chado Field
-    $form['add_fields']['chado'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Chado Field/Column Details'),
-      '#prefix' => '<div id="tripal-bulk-loader-chado-column">',
-      '#suffix' => '</div>',
-      '#description' => t('Specify the Table/Field in chado that this field maps to.'),
-    );
-    
-    $related_tables = module_invoke_all('tripal_bulk_loader_'.$base_table.'_related_tables');
-    $table = ($form_state['storage']['chado_table'])? $form_state['storage']['chado_table'] : $base_table;
-    $form['add_fields']['chado']['chado_table'] = array(
-      '#type' => 'select',
-      '#title' => t('Chado Table'),
-      '#options' => $related_tables,
-      '#default_value' => $table,
-      '#ahah' => array(
-        'path' => 'admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah',
-        'wrapper' => 'tripal-bulk-loader-chado-column',
-        'effect' => 'fade'
-        ),
-    );
-  
-    $table_description = module_invoke_all('chado_'.$table.'_schema');
-    $chado_fields = array();
-    foreach($table_description['fields'] as $field_name => $field_array) {
-      $chado_fields[$field_name] = $field_name;
-    }
-    $form['add_fields']['chado']['chado_field'] = array(
-      '#type' => 'select',
-      '#title' => t('Chado Field/Column'),
-      '#options' => $chado_fields
-    );
-  
-    $form['add_fields']['submit-add_field'] = array(
-        '#type' => 'submit',
-        '#value' => 'Add Field'
-    );
-    
+    // Add Fields---------------------------------------------------------------------
+		$form = array_merge($form, tripal_bulk_loader_add_template_field_form($form_state));
 
     // List Current Fields------------------------------------------------------------
     $form['current_fields'] = array(
@@ -207,12 +95,17 @@ function tripal_bulk_loader_create_template_base_form (&$form_state = NULL) {
       '#title' => t('Step 3: Confirm Fields')
     );
     
-    $form['current_fields']['items'] = array(
-      '#type' => 'item',
-      '#value' => print_r($form_state['storage']['template'],TRUE),
-      '#prefix' => '<pre>',
-      '#suffix' => '</pre>',
-    );
+    if ($form_state['storage']['template']) {
+			$form['current_fields']['items'] = array(
+				'#type' => 'item',
+				'#value' => theme('tripal_bulk_loader_template', $form_state['storage']['template_id']),
+			);
+    } else {
+    	$form['current_fields']['items'] = array(
+    		'#type' => 'item',
+    		'#value' => t('There are currently no fields added to this template.'),
+    	);
+    }
     
     // Submit-------------------------------------------------------------------------
     $form['submit-save'] = array(
@@ -229,7 +122,7 @@ function tripal_bulk_loader_create_template_base_form (&$form_state = NULL) {
 	return $form;
 }
 
-/************************************************************************
+/**
 * tripal_bulk_loader_admin_template_form_validate
 */
 function tripal_bulk_loader_create_template_base_form_validate($form, &$form_state){
@@ -255,7 +148,7 @@ function tripal_bulk_loader_create_template_base_form_validate($form, &$form_sta
   }
 }
 
-/************************************************************************
+/**
 * tripal_bulk_loader_admin_template_form_submit
 */
 function tripal_bulk_loader_create_template_base_form_submit($form, &$form_state){
@@ -265,70 +158,669 @@ function tripal_bulk_loader_create_template_base_form_submit($form, &$form_state
   $form_state['rebuild'] = TRUE;
   $form_state['storage']['base_table'] = $form_state['values']['chado_module'];
   $form_state['storage']['template_name'] = $form_state['values']['template_name'];
-  
-  $form_state['storage']['field_title'] = $form_state['values']['field_title'];
-  $form_state['storage']['field_type'] = $form_state['values']['type'];
-  $form_state['storage']['sheet_name'] = $form_state['values']['sheet'];
-  $form_state['storage']['column_number'] = $form_state['values']['column'];
-  $form_state['storage']['constant_value'] = $form_state['values']['constant_value'];
-  $form_state['storage']['chado_table'] = $form_state['values']['chado_table'];
-  $form_state['storage']['chado_field'] = $form_state['values']['chado_field'];
+	
+  if (!$form_state['ahah_submission']) {
+  		// When template is first defined
+  	if ($form_state['values']['op'] ==  'Next Step') {
+      $record = array(
+        'name' => $form_state['values']['template_name'],
+        'template_array' => ' '
+      );
+      drupal_write_record('tripal_bulk_loader_template', $record);
+      $form_state['storage']['template_id'] = $record['template_id'];
+      
+      // Add Field to Template----------------------------------------------
+    } elseif ($form_state['values']['op'] ==  'Add Field') {
+      
+      
+      // Save Template ----------------------------------------------------
+    } 
+    
+		$record = array(
+			'name' => $form_state['values']['template_name'],
+			'template_array' => serialize($form_state['storage']['template'])
+		);
+		//Check if template exists
+		$sql = "SELECT count(*) as count FROM tripal_bulk_loader_template WHERE name='%s'";
+		if (db_result(db_query($sql, $form_state['values']['template_name']))) {
+			// Update Previous
+			drupal_write_record('tripal_bulk_loader_template', $record, array('name'));
+		} else {
+			// Insert New
+			drupal_write_record('tripal_bulk_loader_template', $record);
+		}
+  } //end of if not ahah submission
+}
+
+//////////////////////////////////////////////////////////////////////////////////////
+// Edit Template
+//////////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * 
+ */
+function tripal_bulk_loader_edit_template_base_form($form_state = NULL){
+	$form = array();
+	
+	$sql = "SELECT * FROM {tripal_bulk_loader_template}";
+	$resource = db_query($sql);
+	$templates = array();
+	$templates[''] = 'Select a Template';
+	while ($r = db_fetch_object($resource)) {
+		$templates[$r->template_id] = $r->name;
+	}
+  $form['template_name'] = array(
+      '#title'         => t('Template'),
+      '#description'   => t('Please select the template you would like to edit.'),
+      '#type'          => 'select',
+      '#options'       => $templates,
+      '#default_value' => $form_state['storage']['template_id'],
+      '#weight'        => 0,
+      '#required'      => TRUE,
+	);	
+	
+	$form['fields'] = array(
+		'#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
+		'#title' => t('Fields'),
+	);
 
+	if ($form_state['storage']['template']) {	
+    
+    // List Current Fields -------------------------------------------------------------
+    $i=1;
+    foreach ($form_state['storage']['template'] as $table => $table_array) {
+      if (!is_array($table_array)) { continue; }
+      
+      foreach ($table_array['field'] as $field_index => $field) {
+          $form['fields']["field_name-$i"] = array(
+            '#type' => 'item',
+            '#value' => $field['title'],
+          );
+
+          $form['fields']["chado_table_name-$i"] = array(
+            '#type' => 'item',
+            '#value' => $table,
+          );
+
+          $form['fields']["chado_table_hidden-$i"] = array(
+            '#type' => 'hidden',
+            '#value' => $table,
+          );
+
+          $form['fields']["chado_field_name-$i"] = array(
+            '#type' => 'item',
+            '#value' => $field['field'],
+          );
+
+          $form['fields']["sheet_name-$i"] = array(
+            '#type' => 'item',
+            '#value' => $field['spreadsheet sheet'],
+          );
+
+          $form['fields']["column_num-$i"] = array(
+            '#type' => 'item',
+            '#value' => $field['spreadsheet column'],
+          );
+
+          $form['fields']["constant_value-$i"] = array(
+            '#type' => 'item',
+            '#value' => $field['constant value'],
+          );
+          
+          $form['fields']["field_index-$i"] = array(
+            '#type' => 'hidden',
+            '#value' => $field_index
+          );
+          
+          $form['fields']["edit-$i"] = array(
+            '#type' => 'submit',
+            '#value' => "Edit Field #$i",
+            '#ahah' => array(
+              'path' => 'admin/tripal/tripal_bulk_loader_template/edit/edit_fields_ahah',
+              'wrapper' => 'tripal_bulk_loader-edit_field',
+              'effect' => 'fade'
+            )
+          );
+
+          $form['fields']["delete-$i"] = array(
+            '#type' => 'submit',
+            '#value' => "Delete Field #$i",
+          );
+          
+          $i++;
+      }
+    }
+    $form['fields']['total_fields'] = array(
+      '#type' => 'item',
+      '#value' => $i,
+    );
+    
+    // Edit Field Form --------------------------------------------------------------------
+    $form = array_merge($form, tripal_bulk_loader_edit_template_field_form($form_state));
+    
+    // Add Field Form ----------------------------------------------------------------------
+		$form = array_merge($form, tripal_bulk_loader_add_template_field_form($form_state));
+		$form['add_fields']['#title'] = 'Add Fields';
+	}
+
+	$value = ($form_state['storage']['template_id'])? 'Save Template' : 'Edit Template';
+	$form['submit'] = array(
+		'#type' => 'submit',
+		'#value' => $value
+	);
+	
+	return $form;
+}
+
+/**
+ * 
+ */
+function tripal_bulk_loader_edit_template_base_form_submit($form, &$form_state){
+	
+  $form_state['rebuild'] = TRUE;
+	$form_state['storage']['template_id'] = $form_state['values']['template_name'];
+	
+	$sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
+	$result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
+	$form_state['storage']['template'] = unserialize($result->template_array);
+	$form_state['storage']['base_table'] = $form_state['storage']['template']['module'];
+  
   if (!$form_state['ahah_submission']) {
+      
       // Add Field to Template----------------------------------------------
+    if (preg_match('/Add Field/', $form_state['values']['op'])) {
+      $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);
+    } elseif (preg_match('/Edit Field$/', $form_state['values']['op'])) {
+      $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]];
+      $field_key = $form_state['values']["field_index-".$matches[1]];
+      unset($form_state['storage']['template'][$table]['field'][$field_key]);
+      
+      $set_default = TRUE;
+      $field_template = $form_state['storage']['edit_field'];      
+    }
+    
+    // Save Template ----------------------------------------------------
+		$record = array(
+			'template_id' => $form_state['values']['template_name'],
+			'template_array' => serialize($form_state['storage']['template'])
+		);
+		drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
+  } //end of if not ahah submission
+  else {
+    //Add AHAH associated with Add Form
+    $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);    
+    $form_state = tripal_bulk_loader_edit_template_field_form_submit($form_state);
+  }
+}
+
+//////////////////////////////////////////////////////////////////////////////////////
+// Delete Template
+//////////////////////////////////////////////////////////////////////////////////////
+
+function tripal_bulk_loader_delete_template_base_form () {
+	$form = array();
+
+	$sql = "SELECT * FROM {tripal_bulk_loader_template}";
+	$resource = db_query($sql);
+	$templates = array();
+	$templates[''] = 'Select a Template';
+	while ($r = db_fetch_object($resource)) {
+		$templates[$r->template_id] = $r->name;
+	}
+  $form['template_name'] = array(
+      '#title'         => t('Template'),
+      '#description'   => t('Please select the template you would like to edit.'),
+      '#type'          => 'select',
+      '#options'       => $templates,
+      '#weight'        => 0,
+      '#required'      => TRUE,
+	);	
+	
+	$form['submit'] = array(
+		'#type' => 'submit',
+		'#value' => 'Delete Template',
+	);
+	
+	return $form;
+}
+
+function tripal_bulk_loader_delete_template_base_form_submit ($form, &$form_state) {
+	$sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
+	db_query($sql, $form_state['values']['template_name']);
+}
+
+//////////////////////////////////////////////////////////////////////////////////////
+// Add/Edit Field Forms (meant to be returned as part of a larger form
+//////////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Add Field Form
+ *
+ * This form meant to be part of a larger form. Currently it is part of the following:
+ *  - tripal_bulk_loader_add_template_base_form
+ *  - tripal_bulk_loader_edit_template_base_form
+ * 
+ * @param $form_state
+ *   The $form_state from the parent form
+ * @return
+ *   The parent form with the edit field fieldset added
+ *
+ * Usage:
+ * @code
+    $form = array_merge($form, tripal_bulk_loader_add_template_field_form($form_state));
+ * @endcode
+ */
+function tripal_bulk_loader_add_template_field_form ($form_state) {
+  $form = array();
+ 
+ 	$base_table = $form_state['storage']['base_table'];
+ 	
+	$form['add_fields'] = array(
+		'#type' => 'fieldset',
+		'#title' => t('Step 2: Add Fields'),
+		'#prefix' => '<div id="tripal_bulk_loader-add_field">',
+		'#suffix' => '</div>',
+	);
+
+	$field_type = ($form_state['storage']['add']['field_type'])? $form_state['storage']['add']['field_type'] : 'table field';
+	$form['add_fields']['add-type'] = array(
+		'#type' => 'radios',
+		'#title' => t('Type of Field'),
+		'#options' => array(
+			'table field' => t('Fields which maps to a Spreadsheet Column'),
+			'constant' => t('Field which remains Constant throughout the Spreadsheet'),
+		),
+		'#default_value' => $field_type,
+		'#ahah' => array(
+			'path' => 'admin/tripal/tripal_bulk_loader_template/add/field_type_ahah',
+			'wrapper' => 'tripal_bulk_loader-add_field',
+			'effect' => 'fade'
+			),
+	);
+	
+	$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',
+		'#title' => t('Spreadsheet Column'),
+	);
+
+	$form['add_fields']['columns']['add-sheet'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Worksheet'),
+		'#description' => t('Specify the name of the worksheet.'),
+		'#size' => 5,
+		'#default_value' => ($form_state['storage']['add']['sheet_name'])? $form_state['storage']['add']['sheet_name'] : 'Sheet1',    
+	);
+	
+	$form['add_fields']['columns']['add-column'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Column'),
+		'#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
+		'#size' => 5,
+		'#default_value' => $form_state['storage']['add']['column_number'],
+	);
+
+	// Global Value
+	$form['add_fields']['constant'] = array(
+		'#type' => ($field_type == 'constant')? 'fieldset' : 'hidden',
+		'#title' => t('Constant'),
+	);
+	
+	$form['add_fields']['constant']['add-constant_value'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Constant Value'),
+		'#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
+		'#default_value' => $form_state['storage']['add']['constant_value']
+	);
+	
+	// Chado Field
+	$form['add_fields']['chado'] = array(
+		'#type' => 'fieldset',
+		'#title' => t('Chado Field/Column Details'),
+		'#prefix' => '<div id="tripal_bulk_loader-add_chado_column">',
+		'#suffix' => '</div>',
+		'#description' => t('Specify the Table/Field in chado that this field maps to.'),
+	);
+	
+	$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'),
+		'#options' => $related_tables,
+		'#default_value' => $table,
+		'#ahah' => array(
+			'path' => 'admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah',
+			'wrapper' => 'tripal_bulk_loader-add_chado_column',
+			'effect' => 'fade'
+			),
+	);
+
+	$table_description = module_invoke_all('chado_'.$table.'_schema');
+	$chado_fields = array();
+	foreach($table_description['fields'] as $field_name => $field_array) {
+		$chado_fields[$field_name] = $field_name;
+	}
+	$form['add_fields']['chado']['add-chado_field'] = array(
+		'#type' => 'select',
+		'#title' => t('Chado Field/Column'),
+		'#options' => $chado_fields
+	);
+
+	$form['add_fields']['submit-add_field'] = array(
+			'#type' => 'submit',
+			'#value' => 'Add Field'
+	);
+ 
+ return $form;
+}
+
+/**
+ * Add Field Submit
+ *
+ * This submit meant to be part of a larger form submit. Currently it is part of the following:
+ *  - tripal_bulk_loader_add_template_base_form_submit
+ *  - tripal_bulk_loader_edit_template_base_form_submit
+ * 
+ * @param $form_state
+ *   The $form_state from the parent submit
+ * @return
+ *   The parent form_state with any changes made
+ *
+ * Usage:
+ * @code
+    $form_state = tripal_bulk_loader_add_template_field_form_submit($form_state);
+ * @endcode
+ */
+function tripal_bulk_loader_add_template_field_form_submit ($form_state) {
+  $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'];
+  $form_state['storage']['add']['column_number'] = $form_state['values']['add-column'];
+  $form_state['storage']['add']['constant_value'] = $form_state['values']['add-constant_value'];
+  $form_state['storage']['add']['chado_table'] = $form_state['values']['add-chado_table'];
+  $form_state['storage']['add']['chado_field'] = $form_state['values']['add-chado_field'];
+
+  if (!$form_state['ahah_submission']) {
     if ($form_state['values']['op'] ==  'Add Field') {
       $template = $form_state['storage']['template'];
       
-      if ($form_state['storage']['field_type'] == 'column') {
-        $template[$form_state['storage']['chado_table']]['field'][] = array(
+      $template['module'] = $form_state['storage']['base_table'];
+      
+      if ($form_state['storage']['add']['field_type'] == 'table field') {
+        $template[$form_state['storage']['add']['chado_table']]['field'][] = array(
           'type' => 'table field',
-          'title' => $form_state['storage']['field_title'],
-          'field' => $form_state['storage']['chado_field'],
+          'title' => $form_state['storage']['add']['field_title'],
+          'field' => $form_state['storage']['add']['chado_field'],
           //'required' => <true|false>,
           //'allowed values' => empty by default,
-          'spreadsheet sheet' => $form_state['storage']['sheet_name'],
-          'spreadsheet column' => $form_state['storage']['column_number'],
+          'spreadsheet sheet' => $form_state['storage']['add']['sheet_name'],
+          'spreadsheet column' => $form_state['storage']['add']['column_number'],
           //'exposed' => 'true|false'  If exposed, will give a select box first from allowed values if set, second from database if values not set.
           //'mapping' => array(
           //   'from' => 'to'
           //   '/from re/' => 'to'
           //),        
         );
-      } elseif ($form_state['storage']['field_type'] == 'constant') {
-        $template[$form_state['storage']['chado_table']]['field'][] = array(
+      } elseif ($form_state['storage']['add']['field_type'] == 'constant') {
+        $template[$form_state['storage']['add']['chado_table']]['field'][] = array(
           'type' => 'constant',
-          'title' => $form_state['storage']['field_title'],
-          'field' => $form_state['storage']['chado_field'],
+          'title' => $form_state['storage']['add']['field_title'],
+          'field' => $form_state['storage']['add']['chado_field'],
           //'required' => <true|false>,
           //'allowed values' => empty by default,
-          'constant value' => $form_state['storage']['constant_value'],
+          'constant value' => $form_state['storage']['add']['constant_value'],
           //'exposed' => 'true|false'  If exposed, will give a select box first from allowed values if set, second from database if values not set.     
         );      
       }
+      drupal_set_message('Successfully Added Field to Template');
       
-      dpm($template, 'template');
       $form_state['storage']['template'] = $template;
-      
-      // Save Template ----------------------------------------------------
-    } elseif ($form_state['values']['op'] ==  'Save Template') { 
-      $record = array(
-        'name' => $form_state['values']['template_name'],
-        'template_array' => print_r($form_state['storage']['template'],TRUE)
-      );
-      //Check if template exists
-      $sql = "SELECT count(*) as count FROM tripal_bulk_loader_template WHERE name='%s'";
-      if (db_result(db_query($sql, $form_state['values']['template_name']))) {
-        // Update Previous
-        drupal_write_record('tripal_bulk_loader_template', $record, array('name'));
-      } else {
-        // Insert New
-        drupal_write_record('tripal_bulk_loader_template', $record);
+    }
+  }  
+
+  return $form_state;
+}
+
+/**
+ * Edit Field Form
+ *
+ * This form meant to be part of a larger form. Currently it is part of the following:
+ *  - tripal_bulk_loader_edit_template_base_form
+ * 
+ * @param $form_state
+ *   The $form_state from the parent form
+ * @return
+ *   The parent form with the edit field fieldset added
+ *
+ * Usage:
+ * @code
+    $form = array_merge($form, tripal_bulk_loader_edit_template_field_form($form_state));
+ * @endcode
+ */
+function tripal_bulk_loader_edit_template_field_form($form_state) {
+  $form = array();
+  
+ 	$base_table = $form_state['storage']['base_table'];
+ 	
+	$form['edit_fields'] = array(
+		'#type' => 'fieldset',
+		'#title' => t('Edit Fields'),
+		'#prefix' => '<div id="tripal_bulk_loader-edit_field">',
+		'#suffix' => '</div>',
+	);
+
+	$field_type = ($form_state['storage']['edit']['field_type'])? $form_state['storage']['edit']['field_type'] : 'table field';
+	$form['edit_fields']['edit-type'] = array(
+		'#type' => 'radios',
+		'#title' => t('Type of Field'),
+		'#options' => array(
+			'table field' => t('Fields which maps to a Spreadsheet Column'),
+			'constant' => t('Field which remains Constant throughout the Spreadsheet'),
+		),
+		'#default_value' => $field_type,
+		'#ahah' => array(
+			'path' => 'admin/tripal/tripal_bulk_loader_template/edit/field_type_ahah',
+			'wrapper' => 'tripal_bulk_loader-edit_field',
+			'effect' => 'fade'
+			),
+	);
+	
+	$form['edit_fields']['edit-field_title'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Human-readable Title for Field'),
+		'#default_value' => $form_state['storage']['edit']['field_title'],
+	);
+
+	// Spreadsheet column
+	$form['edit_fields']['columns'] = array(
+		'#type' => ($field_type == 'table field')? 'fieldset' : 'hidden',
+		'#title' => t('Spreadsheet Column'),
+	);
+
+	$form['edit_fields']['columns']['edit-sheet'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Worksheet'),
+		'#description' => t('Specify the name of the worksheet.'),
+		'#size' => 5,
+		'#default_value' => ($form_state['storage']['edit']['sheet_name'])? $form_state['storage']['edit']['sheet_name'] : 'Sheet1',    
+	);
+	
+	$form['edit_fields']['columns']['edit-column'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Column'),
+		'#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
+		'#size' => 5,
+		'#default_value' => $form_state['storage']['edit']['column_number'],
+	);
+
+	// Global Value
+	$form['edit_fields']['constant'] = array(
+		'#type' => ($field_type == 'constant')? 'fieldset' : 'hidden',
+		'#title' => t('Constant'),
+	);
+	
+	$form['edit_fields']['constant']['edit-constant_value'] = array(
+		'#type' => 'textfield',
+		'#title' => t('Constant Value'),
+		'#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
+		'#default_value' => $form_state['storage']['edit']['constant_value']
+	);
+	
+	// Chado Field
+	$form['edit_fields']['chado'] = array(
+		'#type' => 'fieldset',
+		'#title' => t('Chado Field/Column Details'),
+		'#prefix' => '<div id="tripal_bulk_loader-edit_chado_column">',
+		'#suffix' => '</div>',
+		'#description' => t('Specify the Table/Field in chado that this field maps to.'),
+	);
+	
+	$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'),
+		'#options' => $related_tables,
+		'#default_value' => $table,
+		'#ahah' => array(
+			'path' => 'admin/tripal/tripal_bulk_loader_template/edit/chado_column_ahah',
+			'wrapper' => 'tripal_bulk_loader-edit_chado_column',
+			'effect' => 'fade'
+			),
+	);
+
+	$table_description = module_invoke_all('chado_'.$table.'_schema');
+	$chado_fields = array();
+	foreach($table_description['fields'] as $field_name => $field_array) {
+		$chado_fields[$field_name] = $field_name;
+	}
+	$form['edit_fields']['chado']['edit-chado_field'] = array(
+		'#type' => 'select',
+		'#title' => t('Chado Field/Column'),
+		'#options' => $chado_fields,
+		'#default_value' => $form_state['storage']['edit']['chado_field'],
+	);
+
+	$form['edit_fields']['submit-Edit_field'] = array(
+			'#type' => 'submit',
+			'#value' => 'Edit Field'
+	);
+	
+  return $form;
+}
+
+/**
+ * Edit Field Submit
+ *
+ * This submit meant to be part of a larger form submit. Currently it is part of the following:
+ *  - tripal_bulk_loader_edit_template_base_form_submit
+ * 
+ * @param $form_state
+ *   The $form_state from the parent submit
+ * @return
+ *   The parent form_state with any changes made
+ *
+ * Usage:
+ * @code
+    $form_state = tripal_bulk_loader_edit_template_field_form_submit($form_state);
+ * @endcode
+ */
+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];
+    $form_state['storage']['edit_field']['table'] = $table;
+    $form_state['storage']['edit_field']['field_index'] = $field_key;
+    
+    $set_default = TRUE;
+    $field_template = $form_state['storage']['edit_field'];
+  } else {
+    $set_default = FALSE;
+  }
+  
+  if ($set_default) {
+    $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'];
+    $form_state['storage']['edit']['column_number'] = $field_template['spreadsheet column'];
+    $form_state['storage']['edit']['constant_value'] = $field_template['constant value'];
+    $form_state['storage']['edit']['chado_table'] = $field_template['table'];
+    $form_state['storage']['edit']['chado_field'] = $field_template['field'];
+  } else {
+    $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'];
+    $form_state['storage']['edit']['column_number'] = $form_state['values']['edit-column'];
+    $form_state['storage']['edit']['constant_value'] = $form_state['values']['edit-constant_value'];
+    $form_state['storage']['edit']['chado_table'] = $form_state['values']['edit-chado_table'];
+    $form_state['storage']['edit']['chado_field'] = $form_state['values']['edit-chado_field'];
+  }
+  
+  if (!$form_state['ahah_submission']) {
+    if ($form_state['values']['op'] ==  'Edit Field') {
+      dpm($form_state, 'form_state before');
+      if ($form_state['storage']['edit']['field_type'] == 'table field') {
+        $field = array(
+          'type' => 'table field',
+          'title' => $form_state['storage']['edit']['field_title'],
+          'field' => $form_state['storage']['edit']['chado_field'],
+          //'required' => <true|false>,
+          //'allowed values' => empty by default,
+          'spreadsheet sheet' => $form_state['storage']['edit']['sheet_name'],
+          'spreadsheet column' => $form_state['storage']['edit']['column_number'],
+          //'exposed' => 'true|false'  If exposed, will give a select box first from allowed values if set, second from database if values not set.
+          //'mapping' => array(
+          //   'from' => 'to'
+          //   '/from re/' => 'to'
+          //),        
+        );
+      } elseif ($form_state['storage']['edit']['field_type'] == 'constant') {
+        $field = array(
+          'type' => 'constant',
+          'title' => $form_state['storage']['edit']['field_title'],
+          'field' => $form_state['storage']['edit']['chado_field'],
+          //'required' => <true|false>,
+          //'allowed values' => empty by default,
+          'constant value' => $form_state['storage']['edit']['constant_value'],
+          //'exposed' => 'true|false'  If exposed, will give a select box first from allowed values if set, second from database if values not set.     
+        );      
       }
+      
+      $table = $form_state['storage']['edit_field']['table'];
+      $field_index = $form_state['storage']['edit_field']['field_index'];
+      $form_state['storage']['template'][$table]['field'][$field_index] = $field;
+      drupal_set_message('Successfully Updated Field'); 
+      
+      //reset form
+      unset($form_state['storage']['edit_field']);
+      unset($form_state['storage']['edit']);
     }
-  } //end of if not ahah submission
+  } 
+  
+  return $form_state;
 }
 
-function tripal_bulk_loader_chado_column_ahah () {
+//////////////////////////////////////////////////////////////////////////////////////
+// AHAH Callbacks
+//////////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * AHAH Function: Replace $form['add_fields']['chado'] in tripal_bulk_loader_add_template_field_form
+ *
+ * @return
+ *  JSON Data printed to the screen
+ */
+function tripal_bulk_loader_add_chado_column_ahah () {
 
   $form_state = array('storage' => NULL, 'submitted' => FALSE);
   $form_build_id = $_POST['form_build_id'];
@@ -354,7 +846,13 @@ function tripal_bulk_loader_chado_column_ahah () {
   exit();
 }
 
-function tripal_bulk_loader_field_type_ahah () {
+/**
+ * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_base_form
+ *
+ * @return
+ *  JSON Data printed to the screen
+ */
+function tripal_bulk_loader_add_field_type_ahah () {
 
   $form_state = array('storage' => NULL, 'submitted' => FALSE);
   $form_build_id = $_POST['form_build_id'];
@@ -375,6 +873,102 @@ function tripal_bulk_loader_field_type_ahah () {
   $output = theme('status_messages');
   $output .= drupal_render($form_element);
 
+  // Final rendering callback.
+  print drupal_json(array('status' => TRUE, 'data' => $output));
+  exit();
+}
+
+/**
+ * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_base_form
+ *
+ * @return
+ *  JSON Data printed to the screen
+ */
+function tripal_bulk_loader_edit_fields_ahah () {
+
+  $form_state = array('storage' => NULL, 'submitted' => FALSE);
+  $form_build_id = $_POST['form_build_id'];
+  $form = form_get_cache($form_build_id, $form_state);
+  $args = $form['#parameters'];
+  $form_id = array_shift($args);
+  $form_state['post'] = $form['#post'] = $_POST;
+  // Enable the submit/validate handlers to determine whether AHAH-submittted.
+  $form_state['ahah_submission'] = TRUE;
+  $form['#programmed'] = $form['#redirect'] = FALSE;
+  drupal_process_form($form_id, $form, $form_state);
+  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
+  
+  $form_element = $form['edit_fields'];
+  // Remove the wrapper so we don't double it up.
+  unset($form_element['#prefix'], $form_element['#suffix']);
+
+  $output = theme('status_messages');
+  $output .= drupal_render($form_element);
+
+  // Final rendering callback.
+  print drupal_json(array('status' => TRUE, 'data' => $output));
+  exit();
+}
+
+/**
+ * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
+ *
+ * @return
+ *  JSON Data printed to the screen
+ */
+function tripal_bulk_loader_edit_field_type_ahah () {
+
+  $form_state = array('storage' => NULL, 'submitted' => FALSE);
+  $form_build_id = $_POST['form_build_id'];
+  $form = form_get_cache($form_build_id, $form_state);
+  $args = $form['#parameters'];
+  $form_id = array_shift($args);
+  $form_state['post'] = $form['#post'] = $_POST;
+  // Enable the submit/validate handlers to determine whether AHAH-submittted.
+  $form_state['ahah_submission'] = TRUE;
+  $form['#programmed'] = $form['#redirect'] = FALSE;
+  drupal_process_form($form_id, $form, $form_state);
+  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
+  
+  $form_element = $form['edit_fields'];
+  // Remove the wrapper so we don't double it up.
+  unset($form_element['#prefix'], $form_element['#suffix']);
+
+  $output = theme('status_messages');
+  $output .= drupal_render($form_element);
+
+  // Final rendering callback.
+  print drupal_json(array('status' => TRUE, 'data' => $output));
+  exit();
+}
+
+/**
+ * AHAH Function: Replace $form['edit_fields']['chado'] in tripal_bulk_loader_edit_template_field_form
+ *
+ * @return
+ *  JSON Data printed to the screen
+ */
+function tripal_bulk_loader_edit_chado_column_ahah () {
+
+  $form_state = array('storage' => NULL, 'submitted' => FALSE);
+  $form_build_id = $_POST['form_build_id'];
+  $form = form_get_cache($form_build_id, $form_state);
+  $args = $form['#parameters'];
+  $form_id = array_shift($args);
+  $form_state['post'] = $form['#post'] = $_POST;
+  // Enable the submit/validate handlers to determine whether AHAH-submittted.
+  $form_state['ahah_submission'] = TRUE;
+  $form['#programmed'] = $form['#redirect'] = FALSE;
+  drupal_process_form($form_id, $form, $form_state);
+  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
+  
+  $form_element = $form['edit_fields']['chado'];
+  // Remove the wrapper so we don't double it up.
+  unset($form_element['#prefix'], $form_element['#suffix']);
+
+  $output = theme('status_messages');
+  $output .= drupal_render($form_element);
+
   // Final rendering callback.
   print drupal_json(array('status' => TRUE, 'data' => $output));
   exit();

+ 69 - 33
tripal_bulk_loader/tripal_bulk_loader.module

@@ -32,19 +32,48 @@ function tripal_bulk_loader_menu() {
 	$items['admin/tripal/tripal_bulk_loader_template/add'] = array(
       'title' => 'Create Bulk Loader Template',
       'description' => 'Create loader template for loading tab-delimited data',
-      'page callback' => 'tripal_bulk_loader_admin_template_add',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('tripal_bulk_loader_create_template_base_form'),
       'access arguments' => array('administer site configuration'),
       'type' => MENU_NORMAL_ITEM,   
 		'file' => 'tripal_bulk_loader.admin.inc',
 	);
 	$items['admin/tripal/tripal_bulk_loader_template/add/chado_column_ahah'] = array(
-      'page callback' => 'tripal_bulk_loader_chado_column_ahah',
+      'page callback' => 'tripal_bulk_loader_add_chado_column_ahah',
       'access arguments' => array('administer site configuration'),
       'type' => MENU_CALLBACK,   
 		  'file' => 'tripal_bulk_loader.admin.inc',
 	);
 	$items['admin/tripal/tripal_bulk_loader_template/add/field_type_ahah'] = array(
-      'page callback' => 'tripal_bulk_loader_field_type_ahah',
+      'page callback' => 'tripal_bulk_loader_add_field_type_ahah',
+      'access arguments' => array('administer site configuration'),
+      'type' => MENU_CALLBACK,   
+		  'file' => 'tripal_bulk_loader.admin.inc',
+	);
+	//tripal_bulk_loader_edit_template_base_form
+	$items['admin/tripal/tripal_bulk_loader_template/edit'] = array(
+      'title' => 'Edit Bulk Loader Template',
+      'description' => 'Edit bulk loader template',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('tripal_bulk_loader_edit_template_base_form'),
+      'access arguments' => array('administer site configuration'),
+      'type' => MENU_NORMAL_ITEM,   
+			'file' => 'tripal_bulk_loader.admin.inc',
+	);	
+	$items['admin/tripal/tripal_bulk_loader_template/edit/edit_fields_ahah'] = array(
+      'page callback' => 'tripal_bulk_loader_edit_fields_ahah',
+      'access arguments' => array('administer site configuration'),
+      'type' => MENU_CALLBACK,   
+		  'file' => 'tripal_bulk_loader.admin.inc',
+	);
+	$items['admin/tripal/tripal_bulk_loader_template/edit/chado_column_ahah'] = array(
+      'page callback' => 'tripal_bulk_loader_edit_chado_column_ahah',
+      'access arguments' => array('administer site configuration'),
+      'type' => MENU_CALLBACK,   
+		  'file' => 'tripal_bulk_loader.admin.inc',
+	);
+	$items['admin/tripal/tripal_bulk_loader_template/edit/field_type_ahah'] = array(
+      'page callback' => 'tripal_bulk_loader_edit_field_type_ahah',
       'access arguments' => array('administer site configuration'),
       'type' => MENU_CALLBACK,   
 		  'file' => 'tripal_bulk_loader.admin.inc',
@@ -52,7 +81,8 @@ function tripal_bulk_loader_menu() {
 	$items['admin/tripal/tripal_bulk_loader_template/delete'] = array(
       'title' => 'Delete Bulk Loader Template',
       'description' => 'Delete bulk loader template',
-      'page callback' => 'tripal_bulk_loader_admin_template_delete',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('tripal_bulk_loader_delete_template_base_form'),
       'access arguments' => array('administer site configuration'),
       'type' => MENU_NORMAL_ITEM,   
 		'file' => 'tripal_bulk_loader.admin.inc',
@@ -110,7 +140,7 @@ function tripal_bulk_loader_perm(){
 function tripal_bulk_loader_node_info() {
 	$nodes = array();
 	$nodes['tripal_bulk_loader'] = array(
-      'name' => t('Bulk Loader'),
+      'name' => t('Bulk Loading Job'),
       'module' => 'tripal_bulk_loader',
       'description' => t('A bulk loader for inserting tab-delimited data into chado database'),
       'has_title' => TRUE,
@@ -131,28 +161,25 @@ function tripal_bulk_loader_form ($node){
    while ($template = db_fetch_object ($results)) {
    	$templates [$template->template_id] = $template->name;
    }
+   
    if (!$templates) {
    	$form['label'] = array(
    	'#type' => 'item',
       '#description' => t("Loader template needs to be created before any bulk loader can be added. Go to 'Tripal Management > Bulk Loader Template' to create the template."),
       '#weight'        => 0,
 		);
+		
 		return $form;
    }
+   
    $form['loader_name'] = array(
    	'#type'          => 'textfield',
-      '#title'         => t('Loader Name'),
+      '#title'         => t('Loading Job Name'),
       '#weight'        => -3,
       '#required'      => TRUE,
    	'#default_value' => $node->loader_name
    );
    
-   $sql = "SELECT * FROM {tripal_bulk_loader_template}";
-   $results = db_query($sql);
-   $templates = array ();
-   while ($template = db_fetch_object ($results)) {
-   	$templates [$template->template_id] = $template->name;
-   }
    $form['template_id'] = array(
       '#type' => 'select',
       '#title' => t('Template'),
@@ -162,6 +189,7 @@ function tripal_bulk_loader_form ($node){
       '#required'      => TRUE,
    	'#default_value' => $node->template_id
    );
+   
    $form['file']= array(
       '#type'          => 'textfield',
       '#title'         => t('Data File'),
@@ -169,11 +197,7 @@ function tripal_bulk_loader_form ($node){
       '#weight'        => -1,
    	'#default_value' => $node->file
    );
-   $form['job']= array(
-      '#type'          => 'checkbox',
-      '#title'         => t('Submit a job to load the data file using selected template'),
-      '#weight'        => 0
-   );
+
    return $form;
 }
 
@@ -185,6 +209,14 @@ function tripal_bulk_loader_theme() {
     'tripal_bulk_loader_node_form' => array(
       'arguments' => array('form' => NULL),
     ),
+    'tripal_bulk_loader_template' => array(
+    	'arguments'=> array('template_id' => NULL),
+    	'template' => 'tripal_bulk_loader_template'
+    ),
+    'tripal_bulk_loader_edit_template_base_form' => array(
+    	'arguments' => array('form' => NULL),
+    	'template' => 'tripal_bulk_loader_edit_template_base_form',
+    ),
   );
 }
 
@@ -196,12 +228,7 @@ function theme_tripal_bulk_loader_node_form($form) {
 	if($form['label']['#type'] == "item") {
 		unset($form['buttons']);
 	}
-	unset($form['menu']);
-	unset($form['revision_information']);
-	unset($form['author']);
-	unset($form['path']);
-	unset($form['comment_settings']);
-	unset($form['options']);
+
 	return drupal_render($form);
 }
 
@@ -210,17 +237,17 @@ function theme_tripal_bulk_loader_node_form($form) {
  */
 function tripal_bulk_loader_load($node){
 	$sql = "SELECT * FROM {tripal_bulk_loader} WHERE nid = %d";
-	$properties = db_fetch_object(db_query($sql, $node->nid));
-	return $properties;
-}
+	$node = db_fetch_object(db_query($sql, $node->nid));
+
+	$node->title = 'Bulk Loading Job: '.$node->loader_name;
+	
+	// Add the loader template
+	$sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
+	$results = db_fetch_object(db_query($sql, $node->template_id));
+	$template = unserialize($results->template_array);
+	$node->template = $results;
+	$node->template->template_array = $template;
 
-/*******************************************************************************
- * tripal_bulk_loader_view
- */
-function tripal_bulk_loader_view ($node, $teaser = FALSE, $page = FALSE) {
-	if (!$teaser) {
-		$node = node_prepare($node, $teaser);
-	}
 	return $node;
 }
 
@@ -283,4 +310,13 @@ function tripal_bulk_loader_update ($node) {
 function tripal_bulk_loader_load_data ($loader_name, $template_id, $file) {
 	
 
+}
+
+function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
+
+	$sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
+	$template = db_fetch_object(db_query($sql, $variables['template_id']));
+	$template->template_array = unserialize($template->template_array);	
+	$variables['template'] = $template;
+	
 }