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

Merge branch '7.x-2.x' of git.drupal.org:sandbox/spficklin/1337878 into 7.x-2.x

Lacey Sanderson 11 жил өмнө
parent
commit
620f64acbf

+ 319 - 345
tripal_analysis/includes/tripal_analysis.form.inc

@@ -5,131 +5,104 @@
  *
  * @ingroup tripal_analysis
  */
-function chado_analysis_form(&$node, $form_state = NULL) {
-  tripal_core_ahah_init_form();
+function chado_analysis_form($node, &$form_state) {
   $form = array();
 
-  $analysis = $node->analysis;
+  // if the organism is part of the node object then we are editing. If not we are inserting
+  $analysis_id = null;
+  if (property_exists($node, 'analysis')) {
 
-  // add in the description column. It is a text field and may not be included
-  // if the text is too big.
-  $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
-
-  // get form defaults
-  $analysis_id = $node->analysis_id;
-  if (!$analysis_id) {
+    $analysis = $node->analysis;
+    $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
     $analysis_id = $analysis->analysis_id;
+
+    // get form defaults
+    $analysisname   = property_exists($node, 'analysisname')   ? property_exists($node, 'analysisname')   : $analysis->analysisname;
+    $program        = property_exists($node, 'program')        ? property_exists($node, 'program')        : $analysis->program;
+    $programversion = property_exists($node, 'programversion') ? property_exists($node, 'programversion') : $analysis->programversion;
+    $algorithm      = property_exists($node, 'algorithm')      ? property_exists($node, 'algorithm')      : $analysis->algorithm;
+    $sourcename     = property_exists($node, 'sourcename')     ? property_exists($node, 'sourcename')     : $analysis->sourcename;
+    $sourceversion  = property_exists($node, 'sourceversion')  ? property_exists($node, 'sourceversion')  : $analysis->sourceversion;
+    $sourceuri      = property_exists($node, 'sourceuri')      ? property_exists($node, 'sourceuri')      : $analysis->sourceuri;
+    $timeexecuted   = property_exists($node, 'timeexecuted')   ? property_exists($node, 'timeexecuted')   : $analysis->timeexecuted;
+    $description    = property_exists($node, 'description')    ? property_exists($node, 'description')    : $analysis->description;
+     
+    // set the organism_id in the form
+    $form['analysis_id'] = array(
+      '#type' => 'value',
+      '#value' => $analysis->analysis_id,
+    );
   }
-  $analysisname = $node->analysisname;
-  if (!$analysisname) {
-    $analysisname = $analysis->name;
-  }
-  $program = $node->program;
-  if (!$program) {
-    $program = $analysis->program;
-  }
-  $programversion = $node->programversion;
-  if (!$programversion) {
-    $programversion = $analysis->programversion;
-  }
-  $algorithm = $node->algorithm;
-  if (!$algorithm) {
-    $algorithm = $analysis->algorithm;
-  }
-  $sourcename = $node->sourcename;
-  if (!$sourcename) {
-    $sourcename = $analysis->sourcename;
-  }
-  $sourceversion = $node->sourceversion;
-  if (!$sourceversion) {
-    $sourceversion = $analysis->sourceversion;
-  }
-  $sourceuri = $node->sourceuri;
-  if (!$sourceuri) {
-    $sourceuri = $analysis->sourceuri;
-  }
-  $timeexecuted = $node->timeexecuted;
-  if (!$timeexecuted) {
-    $timeexecuted = $analysis->timeexecuted;
-  }
-  $description = $node->description;
-  if (!$description) {
-    $description = $analysis->description;
+  else {
+    $analysisname   = property_exists($node, 'analysisname')   ? property_exists($node, 'analysisname')   : '';
+    $program        = property_exists($node, 'program')        ? property_exists($node, 'program')        : '';
+    $programversion = property_exists($node, 'programversion') ? property_exists($node, 'programversion') : '';
+    $algorithm      = property_exists($node, 'algorithm')      ? property_exists($node, 'algorithm')      : '';
+    $sourcename     = property_exists($node, 'sourcename')     ? property_exists($node, 'sourcename')     : '';
+    $sourceversion  = property_exists($node, 'sourceversion')  ? property_exists($node, 'sourceversion')  : '';
+    $sourceuri      = property_exists($node, 'sourceuri')      ? property_exists($node, 'sourceuri')      : '';
+    $timeexecuted   = property_exists($node, 'timeexecuted')   ? property_exists($node, 'timeexecuted')   : '';
+    $description    = property_exists($node, 'description')    ? property_exists($node, 'description')    : '';
   }
   
-  // on AHAH callbacks we want to keep a list of all the properties that have been removed
-  // we'll store this info in a hidden field and retrieve it here
-  $d_removed = $form_state['values']['removed'];
-
-  // get the number of new fields that have been aded via AHAH callbacks
-  $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
-
-  // initialze default properties array. This is where we store the property defaults
-  $d_properties = array();
-  
-  
   $form['title']= array(
-      '#type' => 'hidden',
-      '#default_value' => $node->title,
-  );
-  $form['analysis_id']= array(
-      '#type' => 'hidden',
-      '#default_value' => $analysis_id,
-  );
+    '#type' => 'value',
+    '#default_value' => $node->title,
+  );  
   $form['analysisname']= array(
-      '#type' => 'textfield',
-      '#title' => t('Analysis Name'),
-      '#required' => TRUE,
-      '#default_value' => $analysisname,
-      '#description' => t("This should be a brief name that
-         describes the analysis succintly. This name will helps the user find analyses."),
+    '#type' => 'textfield',
+    '#title' => t('Analysis Name'),
+    '#required' => TRUE,
+    '#default_value' => $analysisname,
+    '#description' => t("This should be a brief name that
+       describes the analysis succintly. This name will helps the user find analyses."),
   );
   $form['program']= array(
-      '#type' => 'textfield',
-      '#title' => t('Program'),
-      '#required' => TRUE,
-      '#default_value' => $program,
-      '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
+    '#type' => 'textfield',
+    '#title' => t('Program'),
+    '#required' => TRUE,
+    '#default_value' => $program,
+    '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
   );
   $form['programversion']= array(
-      '#type' => 'textfield',
-      '#title' => t('Program Version'),
-      '#required' => TRUE,
-      '#default_value' => $programversion,
-      '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available."),
+    '#type' => 'textfield',
+    '#title' => t('Program Version'),
+    '#required' => TRUE,
+    '#default_value' => $programversion,
+    '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available."),
   );
   $form['algorithm']= array(
-      '#type' => 'textfield',
-      '#title' => t('Algorithm'),
-      '#required' => FALSE,
-      '#default_value' => $algorithm,
-      '#description' => t("Algorithm name, e.g. blast."),
+    '#type' => 'textfield',
+    '#title' => t('Algorithm'),
+    '#required' => FALSE,
+    '#default_value' => $algorithm,
+    '#description' => t("Algorithm name, e.g. blast."),
   );
   $form['sourcename']= array(
-      '#type' => 'textfield',
-      '#title' => t('Source Name'),
-      '#required' => TRUE,
-      '#default_value' => $sourcename,
-      '#description' => t('The name of the source data.  This could be a file name, data set name or a
-           small description for how the data was collected.  For long descriptions use the description field below'),
-
+    '#type' => 'textfield',
+    '#title' => t('Source Name'),
+    '#required' => TRUE,
+    '#default_value' => $sourcename,
+    '#description' => t('The name of the source data.  This could be a file name, data set name or a
+         small description for how the data was collected.  For long descriptions use the description field below'),
   );
   $form['sourceversion']= array(
-      '#type' => 'textfield',
-      '#title' => t('Source Version'),
-      '#required' => FALSE,
-      '#default_value' => $sourceversion,
-      '#description' => t('If the source dataset has a version, include it here'),
+    '#type' => 'textfield',
+    '#title' => t('Source Version'),
+    '#required' => FALSE,
+    '#default_value' => $sourceversion,
+    '#description' => t('If the source dataset has a version, include it here'),
   );
   $form['sourceuri']= array(
-      '#type' => 'textfield',
-      '#title' => t('Source URI'),
-      '#required' => FALSE,
-      '#default_value' => $sourceuri,
-      '#description' => t("This is a permanent URL or URI for the source of the analysis.
-         Someone could recreate the analysis directly by going to this URI and
-         fetching the source data (e.g. the blast database, or the training model)."),
+    '#type' => 'textfield',
+    '#title' => t('Source URI'),
+    '#required' => FALSE,
+    '#default_value' => $sourceuri,
+    '#description' => t("This is a permanent URL or URI for the source of the analysis.
+       Someone could recreate the analysis directly by going to this URI and
+       fetching the source data (e.g. the blast database, or the training model)."),
   );
+
   // Get time saved in chado
   $default_time = $timeexecuted;
   $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
@@ -143,24 +116,40 @@ function chado_analysis_form(&$node, $form_state = NULL) {
     $day   = format_date($default_time, 'custom', 'j');
   }
   $form['timeexecuted']= array(
-      '#type' => 'date',
-      '#title' => t('Time Executed'),
-      '#required' => TRUE,
-      '#default_value' => array(
-         'year' => $year,
-         'month' => $month,
-         'day' => $day,
-      ),
+    '#type' => 'date',
+    '#title' => t('Time Executed'),
+    '#required' => TRUE,
+    '#default_value' => array(
+       'year' => $year,
+       'month' => $month,
+       'day' => $day,
+    ),
   );
   $form['description']= array(
-      '#type' => 'textarea',
-      '#rows' => 15,
-      '#title' => t('Materials & Methods (Description and/or Program Settings)'),
-      '#required' => FALSE,
-      '#default_value' => $description,
-      '#description' => t('Please provide all necessary information to allow
-         someone to recreate the analysis, including materials and methods
-         for collection of the source data and performing the analysis'),
+    '#type' => 'textarea',
+    '#rows' => 15,
+    '#title' => t('Materials & Methods (Description and/or Program Settings)'),
+    '#required' => FALSE,
+    '#default_value' => $description,
+    '#description' => t('Please provide all necessary information to allow
+       someone to recreate the analysis, including materials and methods
+       for collection of the source data and performing the analysis'),
+  );
+  $form['properties'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Analysis Details'),
+    '#description' => t('You may add additional properties by
+      selecting a property type from the dropdown and adding text.  You may add 
+      as many properties as desired by clicking the plus button on the right.  To 
+      remove a property, click the minus button.  If a property is not available
+      you may add it by ' . l('adding the term', 'admin/tripal/tripal_cv/cvterm/add') . '
+      to the <b>analysis_property</b> vocabulary within the <b>tripal</b> database'),
+  );
+  $form['properties']['table'] = array(
+    '#type' => 'markup',
+    '#value' =>  '',
+    '#prefix' => '<div id="tripal-analysis-edit-properties-table">',
+    '#suffix' => '</div>',
   );
   
   // get the analysis properties
@@ -182,193 +171,100 @@ function chado_analysis_form(&$node, $form_state = NULL) {
     $properties_list[$prop->cvterm_id] = $prop;
   }
   
-  $form['properties'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Analysis Details'),
-    '#description' => t('You may add additional 
-      properties by
-      selecting a property type from the dropdown and adding text.  You may add 
-      as many properties as desired by clicking the plus button on the right.  To 
-      remove a property, click the minus button.  If a property is not available
-      you may add it by ' . l('adding the term', 'admin/tripal/tripal_cv/cvterm/add') . '
-      to the <b>analysis_property</b> vocabulary within the <b>tripal</b> database'),
-  );
-  $form['properties']['table'] = array(
-    '#type' => 'markup',
-    '#value' =>  '',
-    '#prefix' => '<div id="tripal-analysis-edit-properties-table">',
-    '#suffix' => '</div>',
-  );
+  // initialize our arrays of properties and properties removed
+  $d_properties = array();
+  $d_removed = array();
+ 
+  // on AJAX callbacks we want to keep a list of all the properties that have been removed
+  // we'll store this info in a hidden field and retrieve it here
+  if (array_key_exists('values', $form_state) and array_key_exists('removed', $form_state['values'])) {
+    // add the removed item to our array and get the number of new fields that have been added
+    $d_removed = $form_state['values']['removed'];
+    $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
+  }
   
-  // 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 the properties from the Chado analysisprop table (only pertains to existing analyses)
+  $num_properties = 0;
+  if ($analysis_id) {
+    $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);
 
+  // add an empty row of field to allow for addition of a new property
+  chado_analysis_node_form_add_new_empty_props($form, $form_state, $properties_select);
+  
+  $form['#theme'] = 'chado_analysis_form';
   return $form;
 }
 
 /**
- * Validates the user input before creating an analysis node
  *
- * @ingroup tripal_analysis
- */
-function chado_analysis_validate($node, &$form) {
-   // use the analysis parent to validate the node
-  tripal_analysis_validate($node, $form);
-}
-
-/**
- * This validation is being used for three activities:
- *   CASE A: Update a node that exists in both drupal and chado
- *   CASE B: Synchronizing a node from chado to drupal
- *   CASE C: Inserting a new node that exists in niether drupal nor chado
  *
- * @ingroup tripal_analysis
  */
-function tripal_analysis_validate($node, &$form) {
-  $node->analysisname = trim($node->analysisname);
-  $node->description = trim($node->description);
-  $node->program = trim($node->program);
-  $node->programversion = trim($node->programversion);
-  $node->algorithm = trim($node->algorithm);
-  $node->sourcename = trim($node->sourcename);
-  $node->sourceversion = trim($node->sourceversion);
-  $node->sourceuri = trim($node->sourceuri);
- 
-  // Only nodes being updated will have an nid already
-  if (!is_null($node->nid)) {    
-    // CASE A: We are validating a form for updating an existing node
+function chado_analysis_node_form_add_new_empty_props(&$form, &$form_state, $properties_select) {
+  
+  $description = '';
+  if (array_key_exists('values', $form_state) and 
+      array_key_exists('new_id', $form_state['values'])) {
     
-    // get the existing node    
-    $values = array('analysis_id' => $node->analysis_id);      
-    $result = tripal_core_chado_select('analysis', array('*'), $values);
-    $analysis = $result[0];
-      
-    // if the name has changed make sure it doesn't conflict with an existing name
-    if ($analysis->name != $node->analysisname) {
-      $values = array('name' => $node->analysisname);
-      $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
-      if ($result and count($result) > 0) {
-        form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
-        return;
-      }  
-    }
+    $id = $form_state['values']['new_id'];
+    $values = array('cvterm_id' => $id);
+    $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
     
-    // if the unique constraint has changed check to make sure it doesn't conflict with an
-    // existing record
-    if ($analysis->program != $node->program or $analysis->programversion != $node->programversion or 
-       $analysis->sourcename != $node->sourcename) {
-      $values = array(
-        'program' => $node->program,
-        'programversion' => $node->programversion,
-        'sourcename' => $node->sourcename,
-      );
-      $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
-      if ($result and count($result) > 0) {
-        if ($analysis->program != $node->program) {
-          $field = 'program';  
-        }
-        if ($analysis->programversion != $node->programversion) {
-          $field = 'programversion';  
-        }
-        if ($analysis->sourcename != $node->sourcename) {
-          $field = 'sourcename';  
-        }
-        form_set_error($field, 'Cannot update the analysis with this program,
-          program version and source name. An analysis with these values already exists.');
-        return;
-      }  
+    if ($cvterm[0]->definition) {
+      $description = $cvterm[0]->definition;
     }
   }
-  else {
-    // To differentiate if we are syncing or creating a new analysis altogther, see if an
-    // analysis_id already exists
-    if ($node->analysis_id and $node->analysis_id != 0) {
-      // CASE B: Synchronizing a node from chado to drupal
-      // we don't need to do anything.
-    }
-    else {
-      // CASE C: We are validating a form for inserting a new node
-      // The unique constraint for the chado analysis table is: program, programversion, sourcename
-      $values = array(
-        'program' => $node->program,
-        'programversion' => $node->programversion,
-        'sourcename' => $node->sourcename,
-      );
-      $analysis = tripal_core_chado_select('analysis', array('analysis_id'), $values);
-      if ($analysis and count($analysis) > 0) {
-        form_set_error('program', 'Cannot add the analysis with this program,
-          program version and source name. An analysis with these values already exists.');
-        return;
-      }
-      
-      // make sure we have a unique analysis name. This is not a requirement 
-      // for the analysis table but we use the analysis name for the Drupal node
-      // title, so it should be unique      
-      $values = array('name' => $node->analysisname);
-      $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
-      if ($result and count($result) > 0) {
-        form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
-        return;
-      }
-    }
-  }
-}
-
-/*
- *
- */
-function chado_analysis_node_form_add_new_empty_props(&$form, $properties_select) {
-
+  
   // add one more blank set of property fields
   $form['properties']['table']['new']["new_id"] = array(
     '#type'          => 'select',
     '#options'       => $properties_select,
     '#ajax' => array(
       'callback' => "tripal_analysis_property_get_description",
-      'wrapper'  => 'tripal-analysis-new_value-desc',
+      'wrapper'  => 'tripal-analysis-new_value',
       'effect'   => 'fade',
-      'event'    => 'change',
-      'method'   => 'replace',          
-  ),
+      'method'   => 'replace',
+    ),
   );
   $form['properties']['table']['new']["new_value"] = array(
     '#type'          => 'textarea',
     '#default_value' => '',
     '#cols'          => 5,
-    '#rows'          => $rows,
-    '#description'   => '<div id="tripal-analysis-new_value-desc"></div>'
-    );
-    $form['properties']['table']['new']["add"] = array(
-    '#type'         => 'image_button',      
-    '#value'        => t('Add'),
-    '#src'          => drupal_get_path('theme', 'tripal') . '/images/add.png',
+    '#rows'          => 2,
+    '#prefix'        => '<div id="tripal-analysis-new_value">',
+    '#description'   => $description,
+    '#sffix'          => '</div>',
+  );
+  
+  $form['properties']['table']['new']["add"] = array(
+    '#type'           => 'image_button',
+    '#default_value'  => t('Add'),
+    '#src'            => drupal_get_path('module', 'tripal_core') . '/theme/images/add.png',
     '#ajax' => array(
       'callback' => "tripal_analysis_property_add",
       'wrapper'  => 'tripal-analysis-edit-properties-table',
       'effect'   => 'fade',
       'event'    => 'click',
-      'method'   => 'replace',          
+      'method'   => 'replace',
     ),
     '#attributes' => array('onClick' => 'return false;'),
-    );
+  );
+  
 }
 /*
  *
- */
-function chado_analysis_node_form_add_new_props(&$form, $form_state, &$d_properties, &$d_removed) {
+*/
+function chado_analysis_node_form_add_new_props(&$form, &$form_state, &$d_properties, &$d_removed) {
    
   // first, add in all of the new properties that were added through a previous AHAH callback
   $j = 0;
-  $num_properties++;
+  $num_properties = 0;
 
   // we need to find the
-  if ($form_state['values']) {
+  if (array_key_exists('values', $form_state)) {
     foreach ($form_state['values'] as $element_name => $value) {
       if (preg_match('/new_value-(\d+)-(\d+)/', $element_name, $matches)) {
         $new_id = $matches[1];
@@ -397,7 +293,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;
 
-
         // add the new fields
         $form['properties']['table']['new'][$new_id][$rank]["new_id-$new_id-$rank"] = array(
           '#type'          => 'item',
@@ -414,14 +309,14 @@ function chado_analysis_node_form_add_new_props(&$form, $form_state, &$d_propert
         $form['properties']['table']['new'][$new_id][$rank]["remove-$new_id-$rank"] = array(
           '#type'         => 'image_button',
           '#value'        => t('Remove'),
-          '#src'          => drupal_get_path('theme', 'tripal') . '/images/minus.png',
+          '#src'          => drupal_get_path('theme', 'tripal') . '/images/minus.png',          
           '#ajax' => array(
             'callback' => "tripal_analysis/properties/minus/$new_id/$rank",
             'wrapper'  => 'tripal-analysis-edit-properties-table',
             'effect'   => 'fade',
             'event'    => 'click',
             'method'   => 'replace',
-        ),
+          ),
           '#attributes' => array('onClick' => 'return false;'),
         );
       }
@@ -430,7 +325,7 @@ function chado_analysis_node_form_add_new_props(&$form, $form_state, &$d_propert
 
 
   // second add in any new properties added during this callback
-  if ($form_state['post']['add']) {
+  if (array_key_exists('post', $form_state) and array_key_exists('add', $form_state['post'])) {
     $new_id = $form_state['values']['new_id'];
     $new_value = $form_state['values']['new_value'];
 
@@ -472,7 +367,7 @@ function chado_analysis_node_form_add_new_props(&$form, $form_state, &$d_propert
         'wrapper' => 'tripal-analysis-edit-properties-table',
         'event'   => 'click',
         'method'  => 'replace',
-    ),
+      ),
       '#attributes' => array('onClick' => 'return false;'),
     );
 
@@ -482,7 +377,7 @@ function chado_analysis_node_form_add_new_props(&$form, $form_state, &$d_propert
 }
 /*
  *
- */
+*/
 function chado_analysis_node_form_add_analysisprop_table_props(&$form, $form_state, $analysis_id, &$d_properties, &$d_removed) {
 
   // get the properties for this analysis
@@ -505,7 +400,7 @@ function chado_analysis_node_form_add_analysisprop_table_props(&$form, $form_sta
 
     $type_id = $prop->cvterm_id;
     $rank = count($d_properties[$type_id]);
-    
+
     // skip any properties that the user requested to delete through a previous
     // AHAH callback or through the current AHAH callback
     if ($d_removed["$type_id-$rank"]) {
@@ -543,19 +438,129 @@ function chado_analysis_node_form_add_analysisprop_table_props(&$form, $form_sta
         'wrapper' => 'tripal-analysis-edit-properties-table',
         'event'   => 'click',
         'method'  => 'replace',
-    ),
+      ),
       '#attributes' => array('onClick' => 'return false;'),
     );
   }
   return $num_properties;
 }
+
+/**
+ * Validates the user input before creating an analysis node
+ *
+ * @ingroup tripal_analysis
+ */
+function chado_analysis_validate($node, &$form) {
+   // use the analysis parent to validate the node
+  tripal_analysis_validate($node, $form);
+}
+
+/**
+ * This validation is being used for three activities:
+ *   CASE A: Update a node that exists in both drupal and chado
+ *   CASE B: Synchronizing a node from chado to drupal
+ *   CASE C: Inserting a new node that exists in niether drupal nor chado
+ *
+ * @ingroup tripal_analysis
+ */
+function tripal_analysis_validate($node, &$form) {
+  $node->analysisname = trim($node->analysisname);
+  $node->description = trim($node->description);
+  $node->program = trim($node->program);
+  $node->programversion = trim($node->programversion);
+  $node->algorithm = trim($node->algorithm);
+  $node->sourcename = trim($node->sourcename);
+  $node->sourceversion = trim($node->sourceversion);
+  $node->sourceuri = trim($node->sourceuri);
+ 
+  // Only nodes being updated will have an nid already
+  if (!is_null($node->nid)) {    
+    // CASE A: We are validating a form for updating an existing node
+    
+    // get the existing node    
+    $values = array('analysis_id' => $node->analysis_id);      
+    $result = tripal_core_chado_select('analysis', array('*'), $values);
+    $analysis = $result[0];
+      
+    // if the name has changed make sure it doesn't conflict with an existing name
+    if ($analysis->name != $node->analysisname) {
+      $values = array('name' => $node->analysisname);
+      $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
+      if ($result and count($result) > 0) {
+        form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
+        return;
+      }  
+    }
+    
+    // if the unique constraint has changed check to make sure it doesn't conflict with an
+    // existing record
+    if ($analysis->program != $node->program or $analysis->programversion != $node->programversion or 
+       $analysis->sourcename != $node->sourcename) {
+      $values = array(
+        'program' => $node->program,
+        'programversion' => $node->programversion,
+        'sourcename' => $node->sourcename,
+      );
+      $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
+      if ($result and count($result) > 0) {
+        if ($analysis->program != $node->program) {
+          $field = 'program';  
+        }
+        if ($analysis->programversion != $node->programversion) {
+          $field = 'programversion';  
+        }
+        if ($analysis->sourcename != $node->sourcename) {
+          $field = 'sourcename';  
+        }
+        form_set_error($field, 'Cannot update the analysis with this program,
+          program version and source name. An analysis with these values already exists.');
+        return;
+      }  
+    }
+  }
+  else {
+    // To differentiate if we are syncing or creating a new analysis altogther, see if an
+    // analysis_id already exists
+    if ($node->analysis_id and $node->analysis_id != 0) {
+      // CASE B: Synchronizing a node from chado to drupal
+      // we don't need to do anything.
+    }
+    else {
+      // CASE C: We are validating a form for inserting a new node
+      // The unique constraint for the chado analysis table is: program, programversion, sourcename
+      $values = array(
+        'program' => $node->program,
+        'programversion' => $node->programversion,
+        'sourcename' => $node->sourcename,
+      );
+      $analysis = tripal_core_chado_select('analysis', array('analysis_id'), $values);
+      if ($analysis and count($analysis) > 0) {
+        form_set_error('program', 'Cannot add the analysis with this program,
+          program version and source name. An analysis with these values already exists.');
+        return;
+      }
+      
+      // make sure we have a unique analysis name. This is not a requirement 
+      // for the analysis table but we use the analysis name for the Drupal node
+      // title, so it should be unique      
+      $values = array('name' => $node->analysisname);
+      $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
+      if ($result and count($result) > 0) {
+        form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
+        return;
+      }
+    }
+  }
+}
+
+
 /*
  *
  */
 function tripal_analysis_theme_node_form_properties($form) {
   $rows = array();
 
-  if ($form['properties']) {
+  if (array_key_exists('properties', $form)) {
 
     // first add in the properties derived from the analysisprop table
     // the array tree for these properties looks like this:
@@ -596,99 +601,68 @@ function tripal_analysis_theme_node_form_properties($form) {
     // finally add in a set of blank field for adding a new property
     $rows[] = array(
     drupal_render($form['properties']['table']['new']['new_id']),
-    drupal_render($form['properties']['table']['new']['new_value']),
+    array(
+      'data' => drupal_render($form['properties']['table']['new']['new_value']),
+      'width' => '60%',
+    ),
     drupal_render($form['properties']['table']['new']['add']),
     );
   }
 
   $headers = array('Property Type', 'Value', '');
-  return theme('table', $headers, $rows);
+  $table = array(
+    'header' => $headers,
+    'rows' => $rows,
+    'attributes' => array(
+      'id' => 'tripal_analysis-edit-properties-table',
+    ),
+    'sticky' => TRUE,
+    'caption' => '',
+    'colgroups' => array(),
+    'empty' => '',
+  );
+  
+  return theme_table($table); 
 }
 
-/*
- *
+/**
+ * Form AJAX callback for adding a blank property row
+ * 
+ * We only want to return the properties as that's all we'll replace with this callback
  */
-function tripal_analysis_property_add() {
-  $status = TRUE;
-
-  // prepare and render the form
-  $form = tripal_core_ahah_prepare_form();
-
-  // we only want to return the properties as that's all we'll replace with this AHAh callback
-  $data = tripal_analysis_theme_node_form_properties($form);
-
-  // bind javascript events to the new objects that will be returned
-  // so that AHAH enabled elements will work.
-  $settings = tripal_core_ahah_bind_events();
-
-  // return the updated JSON
-  drupal_json(
-    array(
-      'status'   => $status, 
-      'data'     => $data,
-      'settings' => $settings,
-    )
-  );
+function tripal_analysis_property_add($form, $form_state) {
+  return tripal_analysis_theme_node_form_properties($form);
 }
-/*
+/**
+ * Form AJAX callback for deleting properties
  *
  */
-function tripal_analysis_property_delete() {
-  $status = TRUE;
-
-  // prepare and render the form
-  $form = tripal_core_ahah_prepare_form();
-
-  // we only want to return the properties as that's all we'll replace with this AHAh callback
-  $data = tripal_analysis_theme_node_form_properties($form);
-
-  // bind javascript events to the new objects that will be returned
-  // so that AHAH enabled elements will work.
-  $settings = tripal_core_ahah_bind_events();
-
-  // return the updated JSON
-  drupal_json(
-    array(
-      'status'   => $status, 
-      'data'     => $data,
-      'settings' => $settings,
-    )
-  );
+function tripal_analysis_property_delete($form, $form_state) {
+  return tripal_analysis_theme_node_form_properties($form);
 }
-/*
- *
+/**
+ * Form AJAX callback for updating a property description. This 
+ * function only gets called when the property drop down is changed
+ * on the bottom (empty) row of properties
  */
-function tripal_analysis_property_get_description() {
-  $new_id = $_POST['new_id'];
-
-  $values = array('cvterm_id' => $new_id);
-  $cvterm = tripal_core_chado_select('cvterm', array('definition'), $values);
-
-  $description = '&nbsp;';
-  if ($cvterm[0]->definition) {
-    $description = $cvterm[0]->definition;
-  }
-  drupal_json(
-    array(
-      'status' => TRUE,
-      'data'   => '<div id="tripal-analysis-new_value-desc">' . $description . '</div>',
-    )
-  );
+function tripal_analysis_property_get_description($form, $form_state) {
+  return $form['properties']['table']['new']["new_value"];
 }
-/*
- *
+
+/**
+ * We need to theme the analysis form so that the properties fields look good
  */
-function theme_chado_analysis_node_form($form) {
+function theme_chado_analysis_form($variables) {
+  $form = $variables['form'];
 
   $properties_table = tripal_analysis_theme_node_form_properties($form);
-  $markup .= $properties_table;
+  $markup = $properties_table;
 
   $form['properties']['table'] = array(
-    '#type' => 'markup',
-    '#value' =>  $markup,
+    '#markup' => $markup,
     '#prefix' => '<div id="tripal-analysis-edit-properties-table">',
     '#suffix' => '</div>',
   );
-  $form['buttons']['#weight'] = 50;
-  return drupal_render($form);
+  $form['buttons']['#weight'] = 50; 
+  return drupal_render_children($form);
 }

+ 91 - 47
tripal_analysis/theme/tripal_analysis/tripal_analysis_base.tpl.php

@@ -1,53 +1,97 @@
 <?php
-
 $analysis = $variables['node']->analysis;
-
-// the description is a text field so we want to expand that
 $analysis = tripal_core_expand_chado_vars($analysis,'field','analysis.description');
-
-
 ?>
 <div id="tripal_analysis-base-box" class="tripal_analysis-info-box tripal-info-box">
-  <div class="tripal_analysis-info-box-title tripal-info-box-title">Details</div>
-   <table id="tripal_analysis-table-base" class="tripal_analysis-table tripal-table tripal-table-vert">
-      <tr class="tripal_analysis-table-odd-row tripal-table-even-row">
-        <th>Analysis Name</th>
-        <td><?php print $analysis->name; ?></td>
-      </tr>
-      <tr class="tripal_analysis-table-odd-row tripal-table-odd-row">
-        <th nowrap>Software</th>
-        <td><?php 
-          print $analysis->program; 
-          if($analysis->programversion and $analysis->programversion != 'n/a'){
-             print " (" . $analysis->programversion . ")"; 
-          }
-          if($analysis->algorithm){
-             print ". " . $analysis->algorithm; 
-          }
-          ?>
-        </td>
-      </tr>
-      <tr class="tripal_analysis-table-odd-row tripal-table-even-row">
-        <th nowrap>Source</th>
-        <td><?php 
-          if($analysis->sourceuri){
-             print "<a href=\"$analysis->sourceuri\">$analysis->sourcename</a>"; 
-          } else {
-             print $analysis->sourcename; 
-          }
-          if($analysis->sourceversion){
-             print " (" . $analysis->sourceversion . ")"; 
-          }
-          ?>
-          </td>
-      </tr>
-      <tr class="tripal_analysis-table-odd-row tripal-table-odd-row">
-        <th nowrap>Date performed</th>
-        <td><?php print preg_replace("/^(\d+-\d+-\d+) .*/","$1",$analysis->timeexecuted); ?></td>
-      </tr>
-      <tr class="tripal_analysis-table-odd-row tripal-table-even-row">
-        <th nowrap>Materials & Methods</th>
-        <td><?php print $analysis->description; ?></td>
-      </tr>             	                                
-   </table>   
+  <div class="tripal_analysis-info-box-title tripal-info-box-title">Details</div><?php 
+    
+  // the $headers array is an array of fields to use as the colum headers. 
+  // additional documentation can be found here 
+  // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
+  // This table for the organism has a vertical header (down the first column)
+  // so we do not provide headers here, but specify them in the $rows array below.
+  $headers = array();
+  
+  // the $rows array contains an array of rows where each row is an array
+  // of values for each column of the table in that row.  Additional documentation
+  // can be found here:
+  // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7 
+  $rows = array();
+
+  // Analysis Name row
+  $rows[] = array(
+    array(
+      'data' => 'Analysis Name',
+      'header' => TRUE
+    ),
+    $analysis->name
+  );
+
+  // Software row
+  $software = '';
+  if($analysis->programversion and $analysis->programversion != 'n/a'){
+    $software =  " (" . $analysis->programversion . ")";
+  }
+  if($analysis->algorithm){
+    $software = ". " . $analysis->algorithm;
+  }
+  $rows[] = array(
+    array(
+      'data' => 'Software',
+      'header' => TRUE
+    ),
+    $software
+  );
+ 
+  // Source row
+  $source = '';
+  if($analysis->sourceuri){
+    $source = "<a href=\"$analysis->sourceuri\">$analysis->sourcename</a>";
+  } 
+  else {
+    $source = $analysis->sourcename;
+  }
+  if($analysis->sourceversion){
+    $source = " (" . $analysis->sourceversion . ")";
+  }
+  $rows[] = array(
+    array(
+      'data' => 'Source',
+      'header' => TRUE
+    ),
+    $source
+  );
+  
+  // Date performed row
+  $rows[] = array(
+    array(
+      'data' => 'Date performed',
+      'header' => TRUE
+    ),
+    preg_replace("/^(\d+-\d+-\d+) .*/","$1", $analysis->timeexecuted),
+  ); 
+  
+  // the $table array contains the headers and rows array as well as other
+  // options for controlling the display of the table.  Additional
+  // documentation can be found here:
+  // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
+  $table = array(
+    'header' => $headers,
+    'rows' => $rows,
+    'attributes' => array(
+      'id' => 'tripal_analysis-table-base',
+    ),
+    'sticky' => FALSE,
+    'caption' => '',
+    'colgroups' => array(),
+    'empty' => '',
+  );
+ 
+  // once we have our table array structure defined, we call Drupal's theme_table()
+  // function to generate the table.
+  print theme_table($table); 
+  if (property_exists($analysis, 'comment')) { ?>
+    <b>Description</b> <?php 
+    print $analysis->description; 
+  } ?>
 </div>

+ 1 - 1
tripal_analysis/tripal_analysis.install

@@ -102,7 +102,7 @@ function tripal_analysis_add_cvterms() {
   
   
   // the 'analysis_property' vocabulary is for user definable properties.
-  tripal_cv_add_cvterm(array('name' => 'Analysis Type', 'def' => 'The type of analysis was performed.'), 
+  tripal_cv_add_cvterm(array('name' => 'Analysis Type', 'def' => 'The type of analysis that was performed.'), 
      'analysis_property', 0, 1, 'tripal');
 }
 

+ 5 - 19
tripal_analysis/tripal_analysis.module

@@ -56,13 +56,7 @@ function tripal_analysis_node_info() {
  * @ingroup tripal_analysis
  */
 function tripal_analysis_menu() {
-  //Sync analysis
-  $items['chado_sync_analyses'] = array(
-    'title' => 'Sync Data',
-    'page callback' => 'tripal_analysis_sync_analyses',
-    'access arguments' => array('administer tripal analyses'),
-    'type' => MENU_CALLBACK
-  );
+  
   // Tripal Analysis administrative settings
   $items['admin/tripal/chado/tripal_analysis'] = array(
     'title' => 'Analyses',
@@ -98,14 +92,6 @@ function tripal_analysis_menu() {
     'type' => MENU_NORMAL_ITEM,
   );
 
-  $items['tripal_analysis/properties/minus/%/%'] = array(
-    'page callback' => 'tripal_analysis_property_delete',
-    'page arguments' => array(3, 4),
-    'access arguments' => array('edit chado_analysis content'),
-    'type ' => MENU_CALLBACK,
-  );
-
-
   return $items;
 }
 
@@ -188,10 +174,10 @@ function tripal_analysis_theme($existing, $type, $theme, $path) {
       'variables' =>  array(NULL),
       'path' => "$path/theme",
     ),
-    'chado_analysis_node_form' => array(
-      'variables' => array('form'),
-      'path' => $theme_path,
-    ),
+    'chado_analysis_form' => array(
+      'render element' => 'form',
+    )
+    
   );
 
   return $items;

BIN
tripal_core/theme/images/add.png


+ 0 - 3
tripal_organism/theme/tripal_organism/tripal_organism_base.tpl.php

@@ -1,9 +1,6 @@
 <?php
-// the comment field is a database text field so we have to expand it so that
-// it is included in the organism object
 $organism  = $variables['node']->organism;
 $organism = tripal_core_expand_chado_vars($organism,'field','organism.comment');
- 
 ?>
 <div id="tripal_organism-base-box" class="tripal_organism-info-box tripal-info-box">
   <div class="tripal_organism-info-box-title tripal-info-box-title">Details</div>

+ 1 - 1
tripal_organism/tripal_organism.module

@@ -184,7 +184,7 @@ function tripal_organism_theme($existing, $type, $theme, $path) {
     ),
     'tripal_organism_help' => array(
       'template' => 'tripal_organism_help',
-      'arguments' =>  array(NULL),
+      'variables' =>  array(NULL),
       'path' => "$path/theme",
     ),
   );