Browse Source

Add to the analysis form for creating/editing analysisprop

Chun-Huai Cheng 12 years ago
parent
commit
004b5890ef

+ 1 - 1
tripal_analysis/api/tripal_analysis.api.inc

@@ -72,7 +72,7 @@ function tripal_analysis_get_property($analysis_id, $property) {
  * @ingroup tripal_analysis_api
  */
 function tripal_analysis_insert_property($analysis_id, $property, $value, $update_if_present = 0) {
-    return tripal_core_insert_property('analysis', $analysis_id, $property, 'tripal', $value, $update_if_present);
+    return tripal_core_insert_property('analysis', $analysis_id, $property, 'analysis_property', $value, $update_if_present);
 }
 
 /**

+ 29 - 7
tripal_analysis/includes/tripal_analysis.form.inc

@@ -5,7 +5,7 @@
  *
  * @ingroup tripal_analysis
  */
-function chado_analysis_form($node) {
+function chado_analysis_form(&$node, $form_state) {
   tripal_core_ahah_init_form();
   $form = array();
 
@@ -162,6 +162,33 @@ function chado_analysis_form($node) {
          someone to recreate the analysis, including materials and methods
          for collection of the source data and performing the analysis'),
   );
+  
+  // get the analysis properties
+  $properties_select = array();
+  $properties_select[] = 'Select a Property';
+  $properties_list = array();
+  $sql = "
+    SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
+    FROM  {cvterm} CVT
+      INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
+    WHERE
+      CV.name = 'analysis_property' AND
+      NOT CVT.is_obsolete = 1
+    ORDER BY CVT.name ASC
+  ";
+  $prop_types = chado_query($sql);
+  while ($prop = db_fetch_object($prop_types)) {
+  	$properties_select[$prop->cvterm_id] = $prop->name;
+  	$properties_list[$prop->cvterm_id] = $prop;
+  }
+  // add in the properties from the analysisprop table
+  $num_properties += chado_analysis_node_form_add_analysisprop_table_props($form, $form_state, $analysis_id, $d_properties, $d_removed);
+  
+  // add in any new properties that have been added by the user through an AHAH callback
+  $num_new = chado_analysis_node_form_add_new_props($form, $form_state, $d_properties, $d_removed);
+  
+  // add an empty row of field to allow for addition of a new property
+  chado_analysis_node_form_add_new_empty_props($form, $properties_select);
 
   return $form;
 }
@@ -393,12 +420,6 @@ function chado_analysis_node_form_add_new_props(&$form, $form_state, &$d_propert
 
     // determine how many rows we need in the textarea
     $rows = 1;
-    if (preg_match('/Abstract/', $cvterm[0]->name)) {
-      $rows = 10;
-    }
-    if ($cvterm[0]->name == 'Authors') {
-      $rows = 2;
-    }
 
     // add the new fields
     $form['properties']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
@@ -642,5 +663,6 @@ function theme_chado_analysis_node_form($form) {
     '#type' => 'markup',
     '#value' =>  $markup,
   );
+  $form['buttons']['#weight'] = 50;
   return drupal_render($form);
 }

+ 15 - 2
tripal_analysis/tripal_analysis.install

@@ -52,6 +52,21 @@ function tripal_analysis_update_6100() {
 
   return $ret;
 }
+
+/**
+ *  Update for Drupal 6.x, Tripal 1.1, Analysis Module 1.1
+ *  This update adds a new analysis_property cv and 'Analysis Type' cvterm
+ */
+function tripal_analysis_update_6101() {
+   tripal_cv_add_cvterm(array('name' => 'Analysis Type','def' => 'The type of analysis was performed.'), 
+     'analysis_property', 0, 1, 'tripal');
+
+	$ret = array(
+			'#finished' => 1,
+	);
+	return $ret;
+}
+
 /*
  * 
  */
@@ -112,8 +127,6 @@ function tripal_analysis_add_cvterms(){
   // to directly modify properties set by extension modules for an analysis.
   tripal_cv_add_cvterm(array('name' => 'Analysis Type','def' => 'The type of analysis was performed.'), 
      'analysis_property', 0, 1, 'tripal');
-  tripal_cv_add_cvterm(array('name' => 'Analysis Type','def' => 'The type of analysis was performed.'), 
-     'analysis_property', 0, 1, 'tripal');
 }
 /**
  * Implementation of hook_uninstall().

+ 158 - 53
tripal_analysis/tripal_analysis.module

@@ -17,7 +17,7 @@
 require('api/tripal_analysis.api.inc');
 require('includes/tripal_analysis_privacy.inc');
 require('includes/tripal_analysis.admin.inc');
-require('includes/tripal_analysis.fofrm.inc');
+require('includes/tripal_analysis.form.inc');
 
 
 /**
@@ -188,6 +188,55 @@ function chado_analysis_insert($node) {
    // use by other analysis modules that may be using this function
     $node->analysis = $analysis;
     $node->analysis_id = $analysis_id; // we need to set this for children
+    
+    // now add the properties
+    $properties = array(); // stores all of the properties we need to add    
+    // get the list of properties for easy lookup (without doing lots of database queries
+    $properties_list = array();
+    $sql = "
+      SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
+      FROM  {cvterm} CVT
+        INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
+      WHERE
+        CV.name = 'analysis_property' AND
+        NOT CVT.is_obsolete = 1
+      ORDER BY CVT.name ASC
+    ";
+    $prop_types = chado_query($sql);
+    while ($prop = db_fetch_object($prop_types)) {
+    	$properties_list[$prop->cvterm_id] = $prop->name;
+    }
+    
+    // get the properties that should be added. Properties are in one of two forms:
+    //  1) prop_value-[type id]-[index]
+    //  2) new_value-[type id]-[index]
+    //  3) new_id, new_value
+
+    foreach ($node as $name => $value) {
+    	if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
+    		$type_id = $matches[1];
+    		$index = $matches[2];
+    		$name = $properties_list[$type_id];
+    		$properties[$name][$index] = trim($value);
+    	}
+    }
+    if ($node->new_id and $node->new_value) {
+    	$type_id = $node->new_id;
+    	$name = $properties_list[$type_id];
+    	$index = count($properties[$name]);
+    	$properties[$name][$index] = trim($node->new_value);
+    }
+    // now add in the properties
+    foreach ($properties as $property => $elements) {
+      foreach ($elements as $rank => $value) {
+    	  $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE);
+        if (!$status) {
+            drupal_set_message("Error cannot add property: $property", "error");
+            watchdog('t_analysis', "Error cannot add property: %prop",
+              array('%property' => $property), WATCHDOG_ERROR);
+    		}
+      }
+    }
 }
 
 /**
@@ -234,59 +283,115 @@ function chado_analysis_delete($node) {
  * @ingroup tripal_analysis
  */
 function chado_analysis_update($node) {
-  global $user;
-  if ($node->revision) {
-    // TODO -- decide what to do about revisions
-  }
-  // Create a timestamp so we can insert it into the chado database
-  $time = $node->timeexecuted;
-  $month = $time['month'];
-  $day = $time['day'];
-  $year = $time['year'];
-  $timestamp = $month . '/' . $day . '/' . $year;
-
-  // get the analysis_id for this node:
-  $sql = "SELECT analysis_id ".
-           "FROM {chado_analysis} ".
-           "WHERE nid = %d";
-  $analysis_id = db_fetch_object(db_query($sql, $node->nid))->analysis_id;
-
-  $sql = "UPDATE {analysis} ".
-           "SET name = '%s', ".
-           "    description = '%s', ".
-           "    program = '%s', ".
-           "    programversion = '%s', ".
-           "    algorithm = '%s', ".
-           "    sourcename = '%s', ".
-           "    sourceversion = '%s', ".
-           "    sourceuri = '%s', ".
-           "    timeexecuted = '%s' ".
-           "WHERE analysis_id = %d ";
-
-  chado_query($sql, $node->analysisname, $node->description, $node->program,
-    $node->programversion, $node->algorithm, $node->sourcename,
-    $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
-
-  // Create a title for the analysis node using the unique keys so when the
-  // node is saved, it will have a title
-  $record = new stdClass();
-  // If the analysis has a name, use it as the node title. If not, construct
-  // the title using program, programversion, and sourcename
-  if ($node->analysisname) {
-    $record->title = $node->analysisname;
-  }
-  else {
-    //Construct node title as "program (version)
-    $record->title = "$node->program ($node->programversion)";
-  }
-
-  $record->nid = $node->nid;
-  drupal_write_record('node', $record, 'nid');
-  drupal_write_record('node_revisions', $record, 'nid');
+	global $user;
+	if ($node->revision) {
+		// TODO -- decide what to do about revisions
+	}
+	// Create a timestamp so we can insert it into the chado database
+	$time = $node->timeexecuted;
+	$month = $time['month'];
+	$day = $time['day'];
+	$year = $time['year'];
+	$timestamp = $month . '/' . $day . '/' . $year;
+
+	// get the analysis_id for this node:
+	$sql = "SELECT analysis_id ".
+			"FROM {chado_analysis} ".
+			"WHERE nid = %d";
+	$analysis_id = db_fetch_object(db_query($sql, $node->nid))->analysis_id;
+
+	$sql = "UPDATE {analysis} ".
+			"SET name = '%s', ".
+			"    description = '%s', ".
+			"    program = '%s', ".
+			"    programversion = '%s', ".
+			"    algorithm = '%s', ".
+			"    sourcename = '%s', ".
+			"    sourceversion = '%s', ".
+			"    sourceuri = '%s', ".
+			"    timeexecuted = '%s' ".
+			"WHERE analysis_id = %d ";
+
+	chado_query($sql, $node->analysisname, $node->description, $node->program,
+	$node->programversion, $node->algorithm, $node->sourcename,
+	$node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
+
+	// Create a title for the analysis node using the unique keys so when the
+	// node is saved, it will have a title
+	$record = new stdClass();
+	// If the analysis has a name, use it as the node title. If not, construct
+	// the title using program, programversion, and sourcename
+	if ($node->analysisname) {
+		$record->title = $node->analysisname;
+	}
+	else {
+		//Construct node title as "program (version)
+		$record->title = "$node->program ($node->programversion)";
+	}
+
+	$record->nid = $node->nid;
+	drupal_write_record('node', $record, 'nid');
+	drupal_write_record('node_revisions', $record, 'nid');
+
+	// now update the properties
+	$properties = array(); // stores all of the properties we need to add
+	// get the list of properties for easy lookup (without doing lots of database queries
+	$properties_list = array();
+	$sql = "
+			SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
+			FROM  {cvterm} CVT
+			INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
+			WHERE
+			CV.name = 'analysis_property' AND
+			NOT CVT.is_obsolete = 1
+			ORDER BY CVT.name ASC
+			";
+	$prop_types = chado_query($sql);
+	while ($prop = db_fetch_object($prop_types)) {
+		$properties_list[$prop->cvterm_id] = $prop->name;
+	}
+
+	// get the properties that should be added. Properties are in one of three forms:
+	//  1) prop_value-[type id]-[index]
+	//  2) new_value-[type id]-[index]
+	//  3) new_id, new_value
+	//  dpm($node);
+	foreach ($node as $key => $value) {
+		if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
+			$type_id = $matches[1];
+			$index = $matches[2];
+			$name = $properties_list[$type_id];
+			$properties[$name][$index] = trim($value);
+		}
+		if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
+			$type_id = $matches[1];
+			$index = $matches[2];
+			$name = $properties_list[$type_id];
+			$properties[$name][$index] = trim($value);
+		}
+	}
+	if ($node->new_id and $node->new_value) {
+		$type_id = $node->new_id;
+		$name = $properties_list[$type_id];
+		$index = count($properties[$name]);
+		$properties[$name][$index] = trim($node->new_value);
+	}
+
+	// now add in the properties by first removing any the analysis
+	// already has and adding the ones we have
+	tripal_core_chado_delete('analysisprop', array('analysis_id' => $analysis_id));
+	foreach ($properties as $property => $elements) {
+		foreach ($elements as $rank => $value) {
+			$status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE);
+			if (!$status) {
+				drupal_set_message("Error cannot add property: '$property'", "error");
+				watchdog('t_analysis', "Error cannot add property: '%prop'",
+				array('%prop' => $property), WATCHDOG_ERROR);
+			}
+		}
+	}
 }
 
-
-
  /**
   *  When a node is requested by the user this function is called to allow us
   *  to add auxiliary data to the node object.
@@ -523,4 +628,4 @@ function tripal_analysis_views_api() {
 function tripal_analysis_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == "chado_analysis_node_form") {
   }
-}
+}