Browse Source

constants can now be set on the bulk loading job node page with values being stored in tripal_bulk_loader_constants

Lacey Sanderson 13 years ago
parent
commit
4de69b581d

+ 1 - 1
base/tripal_bulk_loader/tripal_bulk_loader.info

@@ -4,4 +4,4 @@ core = 6.x
 project = tripal_bulk_loader
 package = Tripal
 dependencies[] = tripal_core
-version = 6.x-0.3.1b-1.0
+version = 6.x-0.3.1b-1.5

+ 49 - 0
base/tripal_bulk_loader/tripal_bulk_loader.install

@@ -104,5 +104,54 @@ function tripal_bulk_loader_schema() {
 	  ),
 	  'primary key' => array('tripal_bulk_loader_inserted_id'),
 	);
+	$schema['tripal_bulk_loader_constants'] = array(
+	  'fields' => array(
+	    'constant_id' => array(
+	      'type' => 'serial',
+	      'not null' => TRUE
+	    ),
+      'nid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+			),
+			'chado_table' => array(
+			  'type' => 'varchar',
+			  'not null' => TRUE,
+			),
+			'chado_field' => array(
+			  'type' => 'varchar',
+			  'not null' => TRUE,
+			),
+			'record_id' => array(
+			  'type' => 'int',
+			  'not null' => TRUE
+			),
+			'field_id' => array(
+			  'type' => 'int',
+			  'not null' => TRUE
+			),
+			'value' => array(
+			  'type' => 'text',
+			),
+	  ),
+	  'primary key' => array('constant_id'),
+	);
+	
 	return $schema;
 }
+
+/**
+ * Update schema for version 6.x-0.3.1b-1.5
+ * - Add the tripal_bulk_loader_constants table
+ */
+function tripal_bulk_loader_update_6150 () {
+
+  // Create tripal_bulk_loader_constants table
+  $schema = tripal_bulk_loader_schema();
+  $ret = array();
+  db_create_table($ret, 'tripal_bulk_loader_constants', $schema['tripal_bulk_loader_constants']);
+  
+  return $ret;
+  
+}

+ 340 - 52
base/tripal_bulk_loader/tripal_bulk_loader.module

@@ -110,6 +110,11 @@ function tripal_bulk_loader_menu() {
 		  'file' => 'tripal_bulk_loader.admin.inc',  
   );	
 	// AHAH ---------
+	$items['node/add/tripal-bulk-loader/ahah'] = array(
+      'page callback' => 'tripal_bulk_loader_create_node_ahah',
+      'access arguments' => array('create tripal_bulk_loader'),
+      'type' => MENU_CALLBACK,   
+	);
 	$items['admin/tripal/tripal_bulk_loader_template/add_field_ahah'] = array(
       'page callback' => 'tripal_bulk_loader_add_field_ahah',
       'access arguments' => array('administer site configuration'),
@@ -242,60 +247,116 @@ function tripal_bulk_loader_node_info() {
  * Implements node_form
  * Used to gather the extra details stored with a Bulk Loading Job Node
  */
-function tripal_bulk_loader_form ($node){
-   $form = array();
-   $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;
-   }
-   
-   if (!$templates) {
-   	$form['label'] = array(
-   	'#type' => 'item',
+function tripal_bulk_loader_form ($node, $form_state){
+  $form = array();
+  
+  if (isset($form_state['values'])) {
+    $node = $form_state['values'] + (array)$node;
+    $node = (object) $node;
+  }
+  
+  $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;
+  }
+  
+  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'        => -10,
-		);
-		
-		return $form;
-   }
-   
-   $form['loader_name'] = array(
-   	'#type'          => 'textfield',
-      '#title'         => t('Loading Job Name'),
-      '#weight'        => -10,
-      '#required'      => TRUE,
-   	'#default_value' => $node->loader_name
-   );
-   
-   $form['template_id'] = array(
-      '#type' => 'select',
-      '#title' => t('Template'),
-   	'#description'   => t('Please specify a template for this loader'),
-    	'#options'       => $templates,
-   	'#weight'        => -9,
-      '#required'      => TRUE,
-   	'#default_value' => $node->template_id
-   );
-   
-   $form['file']= array(
-      '#type'          => 'textfield',
-      '#title'         => t('Data File'),
-      '#description'   => t('Please specify the data file to be loaded.'),
-      '#weight'        => -8,
-   	'#default_value' => $node->file
-   );
-   
-   $form['has_header'] = array(
+    );
+    
+    return $form;
+  }
+  
+  $form['loader'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Basic Details'),
+  );
+  
+  $form['loader']['loader_name'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Loading Job Name'),
+    '#weight'        => -10,
+    '#required'      => TRUE,
+    '#default_value' => $node->loader_name
+  );
+  
+  $form['loader']['template_id'] = array(
+    '#type' => 'select',
+    '#title' => t('Template'),
+    '#description'   => t('Please specify a template for this loader'),
+    '#options'       => $templates,
+    '#weight'        => -9,
+    '#required'      => TRUE,
+    '#default_value' => $node->template_id,
+    '#ahah' => array(
+      'path' => 'node/add/tripal-bulk-loader/ahah',
+      'wrapper' => 'set-constants',
+      'event' => 'change',
+      'method' => 'replace',
+      'effect' => 'fade',
+    ),
+  );
+  
+  $form['loader']['file']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('Data File'),
+    '#description'   => t('Please specify the data file to be loaded.'),
+    '#weight'        => -8,
+    '#default_value' => $node->file
+  );
+  
+  $form['loader']['has_header'] = array(
     '#type' => 'radios',
-    '#title' => 'File has a Header',
+    '#title' => t('File has a Header'),
     '#options' => array( 1 => 'Yes', 2 => 'No'),
     '#weight' => -7,
     '#default_value' => $node->file_has_header,
-   );
+  );
+  
+  return $form;
+}
 
-   return $form;
+/**
+ * AHAH Function: Replace $form['constants'] in tripal_bulk_loader_form
+ *
+ * @return
+ *  JSON Data printed to the screen
+ */
+function tripal_bulk_loader_create_node_ahah () {
+
+  // The form is generated in an include file which we need to include manually.
+  include_once 'modules/node/node.pages.inc';
+  
+  $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['exposed_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();
+  
 }
 
 /**
@@ -322,6 +383,19 @@ function tripal_bulk_loader_load($node){
     $node->inserted_records->{$r->table_inserted_into} = $r;
   }
   
+  // Add constants
+  $sql = 'SELECT * FROM {tripal_bulk_loader_constants} WHERE nid=%d ORDER BY record_id, field_id';
+  $resource = db_query($sql,$node->nid);
+  while ($r = db_fetch_object($resource)) {
+    $node->constants[$r->record_id][$r->field_id] = array(
+      'chado_table'=>$r->chado_table, 
+      'chado_field'=>$r->chado_field, 
+      'record_id'=>$r->record_id, 
+      'field_id'=>$r->field_id, 
+      'value'=>$r->value
+    );
+  }
+  
 	return $node;
 }
 
@@ -331,14 +405,17 @@ function tripal_bulk_loader_load($node){
  */
 function tripal_bulk_loader_insert ($node) {
   
-	$sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status) VALUES (%d, '%s', %d, '%s', %d, '%s')";
-	db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, 'Initialized');
+  // Insert into tripal_bulk_loader
+  $sql = "INSERT INTO {tripal_bulk_loader} (nid, loader_name, template_id, file, file_has_header, job_status) VALUES (%d, '%s', %d, '%s', %d, '%s')";
+  db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, 'Initialized');
+	
+	// Update title
 	$node->title =$node->loader_name;
 	drupal_write_record('node',$node,'nid');
 	drupal_write_record('node_revision',$node,'nid');	
-	
-	drupal_set_message('After reviewing the details, please Submit this Job (by clicking the "Submit Job" button below). No data will be loaded until the submitted job is reached in the queue.');
-
+  
+  drupal_set_message('After reviewing the details, please Submit this Job (by clicking the "Submit Job" button below). No data will be loaded until the submitted job is reached in the queue.');
+  
 }
 
 /**
@@ -355,8 +432,11 @@ function tripal_bulk_loader_delete ($node) {
  * Updates the data submitted by the node form on edit
  */
 function tripal_bulk_loader_update ($node) {
+  
+  // Update tripal_bulk_loader
 	$sql = "UPDATE {tripal_bulk_loader} SET nid = %d, loader_name = '%s', template_id = %d, file = '%s', file_has_header = '%s' WHERE nid = %d";
 	db_query($sql, $node->nid, $node->loader_name, $node->template_id, $node->file, $node->has_header, $node->nid);
+	
 	// Add a job if the user want to load the data
 	global $user;
 	if($node->job) {
@@ -370,8 +450,215 @@ function tripal_bulk_loader_update ($node) {
 			drupal_set_message("Can not open $node->file. Job not scheduled.");
 		}
 	}
+  
+}
+
+///////////////////////////////////////////////////////////
+// Set Constants Form
+///////////////////////////////////////////////////////////
+
+/**
+ * Set constants (exposed fields in template)
+ *
+ * @param $form_state 
+ *   The current state of the form
+ * @param $node
+ *   The node to set constants for
+ *
+ * @return
+ *   A form array to be rendered by drupal_get_form()
+ */
+function tripal_bulk_loader_set_constants_form ($form_state, $node) {
+  $form = array();
+  
+  $form['nid'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->nid
+  );
+  
+  $form['exposed_fields'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Constant Values'),
+    '#collapsible' => TRUE,
+    '#collapsed' => ($node->template_id) ? FALSE : TRUE,
+    '#prefix' => '<div id="set-constants">',
+    '#suffix' => '</div>',
+  );
+  
+  $form['exposed_fields']['explanation'] = array(
+    '#type' => 'item',
+    '#value' => t('The following fields are constants in the selected template that you need to set values for.')
+  );
+  
+  // Add textifelds for exposed fields of the current template
+  $exposed_fields = FALSE;
+  $indexes = array();
+  if ($node->template_id) {
+    if (isset($node->template)) {
+      foreach ($node->template->template_array as $record_id => $record) {
+        foreach ($record['fields'] as $field_id => $field) {
+          if ($field['exposed']) {
+            $exposed_fields = TRUE;
+            $indexes[$record_id][] = $field_id;
+            $form['exposed_fields'][$record_id.'-'.$field_id] = array(
+              '#type' => 'textfield',
+              '#title' => t($field['title']),
+              '#description' => t('Enter the case-sensitive value of this constant for your spreadsheet'),
+              '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
+            );
+            $form['exposed_fields'][$record_id.'-'.$field_id.'-table'] = array(
+              '#type' => 'hidden',
+              '#value' => $record['table'],
+            );
+            $form['exposed_fields'][$record_id.'-'.$field_id.'-field'] = array(
+              '#type' => 'hidden',
+              '#value' => $field['field'],
+            );
+          }
+        }
+      }
+    }
+  }
+  $form['template'] = array(
+    '#type' => 'hidden',
+    '#value' => serialize($node->template->template_array)
+  );
+  
+  $form['exposed_fields']['indexes'] = array(
+    '#type' => 'hidden',
+    '#value' => serialize($indexes),
+  );
+  
+  if (!$exposed_fields) {
+    $form['exposed_fields']['explanation'] = array(
+      '#type' => 'item',
+      '#value' => t('There are no exposed fields for this template.')
+    );    
+  }
+  
+  $form['exposed_fields']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save Changes')
+  );
+  
+  return $form;
 }
 
+/**
+ * Validate that the values entered exist in the database
+ * if indicated in hte template array
+ */
+function tripal_bulk_loader_set_constants_form_validate ($form_state, $form) {
+
+  $template = unserialize($form_state['template']['#value']);
+  $indexes = unserialize($form_state['exposed_fields']['indexes']['#value']);
+  
+  foreach ($indexes as $record_id => $array) {
+    foreach($array as $field_id) {
+      if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
+        $result = db_fetch_object(db_query(
+          "SELECT 1 as valid FROM %s WHERE %s='%s'",
+          $template[$record_id]['table'],
+          $template[$record_id]['fields'][$field_id]['field'],
+          $form_state['exposed_fields'][$record_id.'-'.$field_id]['#value']
+        ));
+
+        if (!$result->valid) {
+          $msg = 'A '.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#title'].' of "'.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#value'].'" must already exist!';
+          form_set_error($record_id.'-'.$field_id, $msg);
+        } else {
+          drupal_set_message('Confirmed a '.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#title'].' of "'.$form_state['exposed_fields'][$record_id.'-'.$field_id]['#value'].'" already exists.');
+        }
+      }
+    }
+  }
+  
+}
+
+/**
+ * Insert/update the constants associated with this node
+ */
+function tripal_bulk_loader_set_constants_form_submit ($form, $form_state) {
+
+	// Insert/Update constants
+  $template = unserialize($form_state['values']['template']);
+  $indexes = unserialize($form_state['values']['indexes']);
+  
+  foreach ($indexes as $record_id => $array) {
+    foreach($array as $field_id) {	
+      tripal_bulk_loader_update_constant(
+        $form_state['values'][nid], 
+        $form_state['values'][$record_id.'-'.$field_id.'-table'], 
+        $form_state['values'][$record_id.'-'.$field_id.'-field'],
+        $record_id,
+        $field_id,
+        $form_state['values'][$record_id.'-'.$field_id]
+        );
+    }
+  }
+  
+}
+
+/**
+ * Inserts/Updates a tripal bulk loading job constant
+ *
+ * @param $nid
+ *   The node ID of the the tripal bulk loading job the constant is associated with
+ * @param $table
+ *   The chado table the constant is associated with
+ * @param $field
+ *   The chado field the constant is associated with
+ * @param $record_id
+ *   The index in the template array for this record
+ * @param $field_id
+ *   The index in the template array for this field
+ *
+ * NOTE: $template_array[$record_id]['table'] = $table and $template_array[$record_id]['fields'][$field_id]['field'] = $field
+ *   both are included as a means of double-checking the constant still is still in thesame place in the template array.
+ *   For example, that the template was not edited and the records moved around after the job was submitted but before it was run.
+ *
+ * @return
+ *   On success it returns the object (with primary key if inserted);
+ *   on failure it returns FALSE
+ */
+function tripal_bulk_loader_update_constant ($nid, $table, $field, $record_id, $field_id, $value) {
+  
+  $record = array(
+    'nid'=>$nid, 
+    'chado_table'=>$table, 
+    'chado_field'=>$field, 
+    'record_id'=>$record_id, 
+    'field_id'=>$field_id, 
+    'value'=>$value
+  );
+  
+  // Check to see if already exists
+  $exists = db_fetch_object(db_query(
+    "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=%d AND record_id=%d AND field_id=%d",
+    $record['nid'],
+    $record['record_id'],
+    $record['field_id']
+  ));
+  if ($exists->constant_id) {
+    $record['constant_id'] = $exists->constant_id;
+    $status = drupal_write_record('tripal_bulk_loader_constants',$record,'constant_id');
+    if ($status) {
+      return $record;
+    } else {
+      return FALSE;
+    }
+  } else {
+    $status = drupal_write_record('tripal_bulk_loader_constants',$record);
+    if ($status) {
+      return $record;
+    } else {
+      return FALSE;
+    }
+  }
+}
+
+///////////////////////////////////////////////////////////
+
 /**
  * Preprocessor function for the tripal_bulk_loader template
  */
@@ -407,3 +694,4 @@ function tripal_bulk_loader_job_describe_args($callback,$args){
   }
   
 }
+