Ver Fonte

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

Stephen Ficklin há 11 anos atrás
pai
commit
042134717b

+ 125 - 103
tripal_core/api/tripal_core.chado_nodes.dbxrefs.api.inc

@@ -39,7 +39,7 @@
     );
 
     // Finally, and add the additional form elements to the form
-    tripal_core_additional_dbxrefs_form($form, $form_state, $details);
+    chado_node_additional_dbxrefs_form($form, $form_state, $details);
 
     return $form;
   }
@@ -58,7 +58,7 @@
       // Add all additional database references
       // This function will create new database references as needed and select existing ones.
       // Existing _dbxref links will be cleared and then re-added
-      tripal_core_additional_dbxrefs_form_update_dbxrefs(
+      chado_node_additional_dbxrefs_form_update_dbxrefs(
         $node,              // the node object passed in via hook_insert()
         'example_dbxref',   // the name of the _dbxref linking table
         'example_id',       // key to link to the chado content created by this node
@@ -80,7 +80,7 @@
       // Update all additional database references
       // This function will create new database references as needed and select existing ones.
       // Existing _dbxref links will be cleared and then re-added
-      tripal_core_additional_dbxrefs_form_update_dbxrefs(
+      chado_node_additional_dbxrefs_form_update_dbxrefs(
         $node,              // the node object passed in via hook_insert()
         'example_dbxref',   // the name of the _dbxref linking table
         'example_id',       // key to link to the chado content created by this node
@@ -116,15 +116,30 @@
  *   Optional keys include:
  *     - fieldset_title: the non-translated title for this fieldset
  *     - additional_instructions: a non-translated string providing additional instructions
+ *     - select_options: must be an array where the [key] is a valid db_id and
+ *       the [value] is the human-readable name of the option. This includes all databases
+ *       in the chado db table by default
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
+function chado_node_additional_dbxrefs_form(&$form, &$form_state, $details) {
 
   // Set Defaults for optional fields
   $details['fieldset_title'] = 'Additional Database References';
   $details['additional_instructions'] = '';
 
+  // Get Property Types for the Select List
+  if (isset($details['select_options'])) {
+    $db_options = $details['select_options'];
+  }
+  else {
+    $db_options = array(0 => 'Select a Database');
+    $select = tripal_core_chado_select('db', array('db_id','name'), array(), array('order_by' => array('name' => 'ASC')));
+    foreach($select as $db) {
+      $db_options[$db->db_id] = $db->name;
+    }
+  }
+
   // the fieldset of the dbxref elements
   $form['addtl_dbxrefs'] = array(
     '#type' => 'fieldset',
@@ -144,7 +159,7 @@ function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
     '#tree' => TRUE,
     '#prefix' => '<div id="tripal-generic-edit-addtl_dbxrefs-table">',
     '#suffix' => '</div>',
-    '#theme' => 'tripal_core_additional_dbxrefs_form_table'
+    '#theme' => 'chado_node_additional_dbxrefs_form_table'
   );
 
   /* DBxrefs can come to us in two ways:
@@ -204,81 +219,83 @@ function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
    * an element to the form for each one.
    */
   foreach ($existing_dbxrefs as $dbxref) {
+    if (array_key_exists($dbxref->db_id, $db_options)) {
+
+      // Since the version is part of the key, when it is '' we need to use something
+      // in the key to indicate this case. Otherwise, you wouldn't be able to select
+      // those elements from the array (ie: $form['addtl_dbxrefs']['dbxref_table'][9999]['']
+      // doesn't work as expected whereas $form['addtl_dbxrefs']['dbxref_table'][9999][NONE]
+      // is much better)
+      $version = (!empty($dbxref->version)) ? $dbxref->version : 'NONE';
+
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id] = array(
+        '#type' => 'markup',
+        '#value' => ''
+      );
 
-    // Since the version is part of the key, when it is '' we need to use something
-    // in the key to indicate this case. Otherwise, you wouldn't be able to select
-    // those elements from the array (ie: $form['addtl_dbxrefs']['dbxref_table'][9999]['']
-    // doesn't work as expected whereas $form['addtl_dbxrefs']['dbxref_table'][9999][NONE]
-    // is much better)
-    $version = (!empty($dbxref->version)) ? $dbxref->version : 'NONE';
-
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id] = array(
-      '#type' => 'markup',
-      '#value' => ''
-    );
-
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version] = array(
-      '#type' => 'markup',
-      '#value' => ''
-    );
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version] = array(
+        '#type' => 'markup',
+        '#value' => ''
+      );
 
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $dbxref->db_id
-    );
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db_id'] = array(
+        '#type' => 'hidden',
+        '#value' => $dbxref->db_id
+      );
 
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['accession'] = array(
-      '#type' => 'hidden',
-      '#value' => $dbxref->accession
-    );
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['accession'] = array(
+        '#type' => 'hidden',
+        '#value' => $dbxref->accession
+      );
 
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $dbxref->dbxref_id
-    );
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_id'] = array(
+        '#type' => 'hidden',
+        '#value' => $dbxref->dbxref_id
+      );
 
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db'] = array(
-      '#type' => 'markup',
-      '#markup' => $dbxref->db_name
-    );
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db'] = array(
+        '#type' => 'markup',
+        '#markup' => $dbxref->db_name
+      );
 
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_version'] = array(
-      '#type' => 'markup',
-      '#markup' => $dbxref->version
-    );
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_version'] = array(
+        '#type' => 'markup',
+        '#markup' => $dbxref->version
+      );
 
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_accession'] = array(
-      '#type' => 'markup',
-      '#markup' => $dbxref->accession
-    );
-    // remove button
-    $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_action'] = array(
-      '#type' => 'submit',
-      '#value' => t('Remove'),
-      '#name' => "dbxref_remove-".$dbxref->db_id.'-'.$version,
-      '#ajax' => array(
-        'callback' => "tripal_core_additional_dbxrefs_form_ajax_update",
-        'wrapper' => 'tripal-generic-edit-addtl_dbxrefs-table',
-        'effect'   => 'fade',
-        'method'   => 'replace',
-        'prevent'  => 'click'
-      ),
-      // When this button is clicked, the form will be validated and submitted.
-      // Therefore, we set custom submit and validate functions to override the
-      // default node form submit.  In the validate function we validate only the
-      // additional dbxref fields and in the submit we remove the indicated dbxref
-      // from the chado_additional_dbxrefs array. In order to keep validate errors
-      // from the node form validate and Drupal required errors for non-dbxref fields
-      // preventing the user from removing dbxrefs we set the #limit_validation_errors below
-      '#validate' => array('tripal_core_additional_dbxrefs_form_remove_button_validate'),
-      '#submit' => array('tripal_core_additional_dbxrefs_form_remove_button_submit'),
-      // Limit the validation of the form upon clicking this button to the dbxref_table tree
-      // No other fields will be validated (ie: no fields from the main form or any other api
-      // added form).
-      '#limit_validation_errors' => array(
-        array('dbxref_table')  // Validate all fields within $form_state['values']['dbxref_table']
-      )
-    );
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_accession'] = array(
+        '#type' => 'markup',
+        '#markup' => $dbxref->accession
+      );
+      // remove button
+      $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_action'] = array(
+        '#type' => 'submit',
+        '#value' => t('Remove'),
+        '#name' => "dbxref_remove-".$dbxref->db_id.'-'.$version,
+        '#ajax' => array(
+          'callback' => "chado_node_additional_dbxrefs_form_ajax_update",
+          'wrapper' => 'tripal-generic-edit-addtl_dbxrefs-table',
+          'effect'   => 'fade',
+          'method'   => 'replace',
+          'prevent'  => 'click'
+        ),
+        // When this button is clicked, the form will be validated and submitted.
+        // Therefore, we set custom submit and validate functions to override the
+        // default node form submit.  In the validate function we validate only the
+        // additional dbxref fields and in the submit we remove the indicated dbxref
+        // from the chado_additional_dbxrefs array. In order to keep validate errors
+        // from the node form validate and Drupal required errors for non-dbxref fields
+        // preventing the user from removing dbxrefs we set the #limit_validation_errors below
+        '#validate' => array('chado_node_additional_dbxrefs_form_remove_button_validate'),
+        '#submit' => array('chado_node_additional_dbxrefs_form_remove_button_submit'),
+        // Limit the validation of the form upon clicking this button to the dbxref_table tree
+        // No other fields will be validated (ie: no fields from the main form or any other api
+        // added form).
+        '#limit_validation_errors' => array(
+          array('dbxref_table')  // Validate all fields within $form_state['values']['dbxref_table']
+        )
+      );
+    }
   }
 
   // Form elements for adding a new dbxref
@@ -290,11 +307,6 @@ function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
   );
 
   // add in the existing databases
-  $db_options = array(0 => 'Select a Database');
-  $select = tripal_core_chado_select('db', array('db_id','name'), array(), array('order_by' => array('name' => 'ASC')));
-  foreach($select as $db) {
-    $db_options[$db->db_id] = $db->name;
-  }
   $form['addtl_dbxrefs']['dbxref_table']['new']['db'] = array(
     '#type' => 'select',
     '#options' => $db_options,
@@ -314,7 +326,7 @@ function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
     '#value' => t('Add'),
     '#name' => "dbxref-add",
     '#ajax' => array(
-      'callback' => "tripal_core_additional_dbxrefs_form_ajax_update",
+      'callback' => "chado_node_additional_dbxrefs_form_ajax_update",
       'wrapper' => 'tripal-generic-edit-addtl_dbxrefs-table',
       'effect'   => 'fade',
       'method'   => 'replace',
@@ -327,8 +339,8 @@ function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
     // array. In order to keep validate errors from the node form validate and Drupal
     // required errors for non-dbxref fields preventing the user from adding dbxrefs we
     // set the #limit_validation_errors below
-    '#validate' => array('tripal_core_additional_dbxrefs_form_add_button_validate'),
-    '#submit' => array('tripal_core_additional_dbxrefs_form_add_button_submit'),
+    '#validate' => array('chado_node_additional_dbxrefs_form_add_button_validate'),
+    '#submit' => array('chado_node_additional_dbxrefs_form_add_button_submit'),
     // Limit the validation of the form upon clicking this button to the dbxref_table tree
     // No other fields will be validated (ie: no fields from the main form or any other api
     // added form).
@@ -341,11 +353,11 @@ function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
 
 /**
  * Validate the user input for creating a new dbxref
- * Called by the add button in tripal_core_additional_dbxrefs_form
+ * Called by the add button in chado_node_additional_dbxrefs_form
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_add_button_validate($form, &$form_state) {
+function chado_node_additional_dbxrefs_form_add_button_validate($form, &$form_state) {
 
   // Ensure the db_id is supplied & Valid
   $db = tripal_core_chado_select(
@@ -367,19 +379,19 @@ function tripal_core_additional_dbxrefs_form_add_button_validate($form, &$form_s
 }
 
 /**
- * Called by the add button in tripal_core_additional_dbxrefs_form
+ * Called by the add button in chado_node_additional_dbxrefs_form
  *
  * Create an array of additional dbxrefs in the form state. This array will then be
  * used to rebuild the form in subsequent builds
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_add_button_submit(&$form, &$form_state) {
+function chado_node_additional_dbxrefs_form_add_button_submit(&$form, &$form_state) {
 
   // if the chado_additional_dbxrefs array is not set then this is the first time modifying the
   // dbxref table. this means we need to include all the dbxrefs from the db
   if (!isset($form_state['chado_additional_dbxrefs'])) {
-    tripal_core_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
+    chado_node_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
   }
 
   // get details for the new dbxref
@@ -401,26 +413,26 @@ function tripal_core_additional_dbxrefs_form_add_button_submit(&$form, &$form_st
  * There is no user input for the remove buttons so there is no need to validate
  * However, both a submit & validate need to be specified so this is just a placeholder
  *
- * Called by the many remove buttons in tripal_core_additional_dbxrefs_form
+ * Called by the many remove buttons in chado_node_additional_dbxrefs_form
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_remove_button_validate($form, $form_state) {
+function chado_node_additional_dbxrefs_form_remove_button_validate($form, $form_state) {
   // No Validation needed for remove
 }
 
 /**
  * Remove the correct dbxref from the form
- * Called by the many remove buttons in tripal_core_additional_dbxrefs_form
+ * Called by the many remove buttons in chado_node_additional_dbxrefs_form
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_remove_button_submit(&$form, &$form_state) {
+function chado_node_additional_dbxrefs_form_remove_button_submit(&$form, &$form_state) {
 
   // if the chado_additional_dbxrefs array is not set then this is the first time modifying the
   // dbxref table. this means we need to include all the dbxrefs from the db
   if (!isset($form_state['chado_additional_dbxrefs'])) {
-    tripal_core_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
+    chado_node_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
   }
 
   // remove the specified dbxref from the form dbxref table
@@ -439,7 +451,7 @@ function tripal_core_additional_dbxrefs_form_remove_button_submit(&$form, &$form
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_ajax_update($form, $form_state) {
+function chado_node_additional_dbxrefs_form_ajax_update($form, $form_state) {
   return $form['addtl_dbxrefs']['dbxref_table'];
 }
 
@@ -461,7 +473,7 @@ function tripal_core_additional_dbxrefs_form_ajax_update($form, $form_state) {
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_create_dbxref_formstate_array($form, &$form_state) {
+function chado_node_additional_dbxrefs_form_create_dbxref_formstate_array($form, &$form_state) {
 
   $form_state['chado_additional_dbxrefs'] = array();
 
@@ -489,7 +501,7 @@ function tripal_core_additional_dbxrefs_form_create_dbxref_formstate_array($form
  *
  * @ingroup tripal_chado_node_api
  */
-function theme_tripal_core_additional_dbxrefs_form_table($variables) {
+function theme_chado_node_additional_dbxrefs_form_table($variables) {
   $element = $variables['element'];
 
   $header = array(
@@ -546,7 +558,7 @@ function theme_tripal_core_additional_dbxrefs_form_table($variables) {
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_retreive($node) {
+function chado_node_additional_dbxrefs_form_retreive($node) {
   $dbxrefs = array();
 
   if (isset($node->dbxref_table)) {
@@ -569,23 +581,33 @@ function tripal_core_additional_dbxrefs_form_retreive($node) {
  *
  * @param $node
  *    The node passed into hook_insert & hook_update
- * @param $linking_table
- *    The name of the _dbxref linking table (ie: feature_dbxref)
- * @param $foreignkey_name
- *    The name of the foreign key used to link to the node content (ie: feature_id)
- * @param $foreignkey_value
- *    The value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
+ * @param $details
+ *   - linking_table: the name of the _dbxref linking table (ie: feature_dbxref)
+ *   - foreignkey_name: the name of the foreign key used to link to the node content (ie: feature_id)
+ *   - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
+ * @param $retrieved_dbxrefs
+ *   An array of databa references from chado_node_additional_dbxrefs_form_retreive($node).
+ *   This can be used if you need special handling for some of the database references
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_additional_dbxrefs_form_update_dbxrefs($node, $linking_table, $foreignkey_name, $foreignkey_value) {
+function chado_node_additional_dbxrefs_form_update_dbxrefs($node, $details, $retrieved_dbxrefs = FALSE) {
+
+  $linking_table = $details['linking_table'];
+  $foreignkey_name = $details['foreignkey_name'];
+  $foreignkey_value = $details['foreignkey_value'];
 
   if (isset($node->dbxref_table) AND ($foreignkey_value > 0)) {
     // First remove existing dbxref links
     tripal_core_chado_delete($linking_table, array($foreignkey_name => $foreignkey_value));
 
     // Add back in dbxref links and insert dbxrefs as needed
-    $dbxrefs = tripal_core_additional_dbxrefs_form_retreive($node);
+    if ($retrieved_dbxrefs) {
+      $dbxrefs = $retrieved_dbxrefs;
+    }
+    else {
+      $dbxrefs = chado_node_additional_dbxrefs_form_retreive($node);
+    }
     foreach ($dbxrefs as $db_id => $versions) {
       foreach ($versions as $version => $elements) {
         foreach ($elements as $dbxref_id => $accession) {

+ 60 - 57
tripal_core/api/tripal_core.chado_nodes.properties.api.inc

@@ -598,73 +598,76 @@ function chado_node_properties_form(&$form, &$form_state, $details) {
    * NOTE: The main difference is the key
    *
    * Loop on the array elements of the $existing_properties array and add
-   * an element to the form for each one.
+   * an element to the form for each one as long as it's also in the
+   * $properties_options array.
    */
   foreach ($existing_properties as $property) {
+    if (array_key_exists($property->type_id, $property_options)) {
 
-    $form['properties']['property_table'][$property->type_id]['#type'] = 'markup';
-    $form['properties']['property_table'][$property->type_id]['#value'] = '';
+      $form['properties']['property_table'][$property->type_id]['#type'] = 'markup';
+      $form['properties']['property_table'][$property->type_id]['#value'] = '';
 
-    $form['properties']['property_table'][$property->type_id][$property->rank]['#type'] = 'markup';
-    $form['properties']['property_table'][$property->type_id][$property->rank]['#value'] = '';
+      $form['properties']['property_table'][$property->type_id][$property->rank]['#type'] = 'markup';
+      $form['properties']['property_table'][$property->type_id][$property->rank]['#value'] = '';
 
-    $form['properties']['property_table'][$property->type_id][$property->rank]['prop_type_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $property->type_id
-    );
+      $form['properties']['property_table'][$property->type_id][$property->rank]['prop_type_id'] = array(
+        '#type' => 'hidden',
+        '#value' => $property->type_id
+      );
 
-    $form['properties']['property_table'][$property->type_id][$property->rank]['prop_value'] = array(
-      '#type' => 'hidden',
-      '#value' => $property->value
-    );
+      $form['properties']['property_table'][$property->type_id][$property->rank]['prop_value'] = array(
+        '#type' => 'hidden',
+        '#value' => $property->value
+      );
 
-    $form['properties']['property_table'][$property->type_id][$property->rank]['property_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $property->property_id
-    );
+      $form['properties']['property_table'][$property->type_id][$property->rank]['property_id'] = array(
+        '#type' => 'hidden',
+        '#value' => $property->property_id
+      );
 
-    $form['properties']['property_table'][$property->type_id][$property->rank]['type'] = array(
-      '#type' => 'markup',
-      '#markup' => $property->type_name
-    );
+      $form['properties']['property_table'][$property->type_id][$property->rank]['type'] = array(
+        '#type' => 'markup',
+        '#markup' => $property->type_name
+      );
 
-    $form['properties']['property_table'][$property->type_id][$property->rank]['value'] = array(
-      '#type' => 'markup',
-      '#markup' => $property->value
-    );
+      $form['properties']['property_table'][$property->type_id][$property->rank]['value'] = array(
+        '#type' => 'markup',
+        '#markup' => $property->value
+      );
 
-    $form['properties']['property_table'][$property->type_id][$property->rank]['rank'] = array(
-      '#type' => 'markup',
-      '#markup' => $property->rank
-    );
-    // remove button
-    $form['properties']['property_table'][$property->type_id][$property->rank]['property_action'] = array(
-      '#type' => 'submit',
-      '#value' => t('Remove'),
-      '#name' => "property_remove-".$property->type_id.'-'.$property->rank,
-      '#ajax' => array(
-        'callback' => "chado_node_properties_form_ajax_update",
-        'wrapper' => 'tripal-generic-edit-properties-table',
-        'effect'   => 'fade',
-        'method'   => 'replace',
-        'prevent'  => 'click'
-      ),
-      // When this button is clicked, the form will be validated and submitted.
-      // Therefore, we set custom submit and validate functions to override the
-      // default node form submit.  In the validate function we validate only the
-      // property fields and in the submit we remove the indicated property
-      // from the chado_properties array. In order to keep validate errors
-      // from the node form validate and Drupal required errors for non-property fields
-      // preventing the user from removing properties we set the #limit_validation_errors below
-      '#validate' => array('chado_node_properties_form_remove_button_validate'),
-      '#submit' => array('chado_node_properties_form_remove_button_submit'),
-      // Limit the validation of the form upon clicking this button to the property_table tree
-      // No other fields will be validated (ie: no fields from the main form or any other api
-      // added form).
-      '#limit_validation_errors' => array(
-        array('property_table')  // Validate all fields within $form_state['values']['property_table']
-      )
-    );
+      $form['properties']['property_table'][$property->type_id][$property->rank]['rank'] = array(
+        '#type' => 'markup',
+        '#markup' => $property->rank
+      );
+      // remove button
+      $form['properties']['property_table'][$property->type_id][$property->rank]['property_action'] = array(
+        '#type' => 'submit',
+        '#value' => t('Remove'),
+        '#name' => "property_remove-".$property->type_id.'-'.$property->rank,
+        '#ajax' => array(
+          'callback' => "chado_node_properties_form_ajax_update",
+          'wrapper' => 'tripal-generic-edit-properties-table',
+          'effect'   => 'fade',
+          'method'   => 'replace',
+          'prevent'  => 'click'
+        ),
+        // When this button is clicked, the form will be validated and submitted.
+        // Therefore, we set custom submit and validate functions to override the
+        // default node form submit.  In the validate function we validate only the
+        // property fields and in the submit we remove the indicated property
+        // from the chado_properties array. In order to keep validate errors
+        // from the node form validate and Drupal required errors for non-property fields
+        // preventing the user from removing properties we set the #limit_validation_errors below
+        '#validate' => array('chado_node_properties_form_remove_button_validate'),
+        '#submit' => array('chado_node_properties_form_remove_button_submit'),
+        // Limit the validation of the form upon clicking this button to the property_table tree
+        // No other fields will be validated (ie: no fields from the main form or any other api
+        // added form).
+        '#limit_validation_errors' => array(
+          array('property_table')  // Validate all fields within $form_state['values']['property_table']
+        )
+      );
+    }
   }
 
   // Form elements for adding a new property

+ 202 - 133
tripal_core/api/tripal_core.chado_nodes.relationships.api.inc

@@ -40,7 +40,7 @@
     );
 
     // Finally, and add the additional form elements to the form
-    tripal_core_relationships_form($form, $form_state, $details);
+    chado_node_relationships_form($form, $form_state, $details);
 
     return $form;
   }
@@ -59,7 +59,7 @@
       // Add all relationships
       // Existing _relationship links with the current example as either the subject_id
       // or object_id will be cleared and then re-added
-      tripal_core_relationships_form_update_relationships(
+      chado_node_relationships_form_update_relationships(
         $node,
         'example_relationship',
         $node->example_id
@@ -80,7 +80,7 @@
       // Update all additional database references
       // Existing _relationship links with the current example as either the subject_id
       // or object_id will be cleared and then re-added
-      tripal_core_relationships_form_update_relationships(
+      chado_node_relationships_form_update_relationships(
         $node,
         'example_relationship',
         $node->example_id
@@ -116,10 +116,12 @@
  *     - fieldset_title: the non-translated title for this fieldset
  *     - additional_instructions: a non-translated string providing additional instructions
  *     - nodetype_plural: the non-translated plural title of this node type
+ *     - select_options: must be an array where the [key] is a valid cvterm_id and
+ *       the [value] is the human-readable name of the option. This is generated from the cv_name/id by default
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form(&$form, &$form_state, $details) {
+function chado_node_relationships_form(&$form, &$form_state, $details) {
 
   $form_state['rebuild'] = TRUE;
 
@@ -128,23 +130,44 @@ function tripal_core_relationships_form(&$form, &$form_state, $details) {
   $details['additional_instructions'] = (isset($details['additional_instructions'])) ? $details['additional_instructions'] : '';
   $details['nodetype_plural']  = (isset($details['nodetype_plural'])) ? $details['nodetype_plural'] : $details['nodetype'] . 's';
 
-  // Add defaults into form_state to be used elsewhere
-  $form['rel_details'] = array(
-    '#type' => 'hidden',
-    '#value' => serialize($details)
-  );
-
-  // Get relationship type options
-  if (isset($details['cv_id'])) {
-    $query = "SELECT cvterm_id, name FROM {cvterm} cvterm WHERE cv_id = :cv_id";
-    $result = chado_query($query, array(':cv_id' => $details['cv_id']));
-  } elseif (isset($details['cv_name'])) {
-    $query = "SELECT cvterm_id, name FROM {cvterm} cvterm WHERE cv_id IN (SELECT cv_id FROM cv WHERE name = :cv_name)";
-    $result = chado_query($query, array(':cv_name' => $details['cv_name']));
+  // Get Property Types for the Select List
+  if (isset($details['select_options'])) {
+    $type_options = $details['select_options'];
   }
-  $type_options = array(0 => 'Select a Type');
-  foreach ($result as $cvterm) {
-    $type_options[ $cvterm->cvterm_id ] = $cvterm->name;
+  else {
+    if (isset($details['cv_name'])) {
+      $type_options = array();
+      $type_options[] = 'Select a Property';
+      $sql = "
+        SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
+        FROM  {cvterm} CVT
+          INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
+        WHERE
+          CV.name = :cv_name AND
+          NOT CVT.is_obsolete = 1
+        ORDER BY CVT.name ASC
+      ";
+      $prop_types = chado_query($sql, array(':cv_name' => $details['cv_name']));
+      while ($prop = $prop_types->fetchObject()) {
+        $type_options[$prop->cvterm_id] = $prop->name;
+      }
+    } elseif (isset($details['cv_id'])) {
+      $type_options = array();
+      $type_options[] = 'Select a Property';
+      $sql = "
+        SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
+        FROM  {cvterm} CVT
+          INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
+        WHERE
+          CV.cv_id = :cv_id AND
+          NOT CVT.is_obsolete = 1
+        ORDER BY CVT.name ASC
+      ";
+      $prop_types = chado_query($sql, array(':cv_id' => $details['cv_id']));
+      while ($prop = $prop_types->fetchObject()) {
+        $type_options[$prop->cvterm_id] = $prop->name;
+      }
+    }
   }
 
   $form['relationships'] = array(
@@ -166,7 +189,13 @@ function tripal_core_relationships_form(&$form, &$form_state, $details) {
     '#tree' => TRUE,
     '#prefix' => '<div id="tripal-generic-edit-relationships-table">',
     '#suffix' => '</div>',
-    '#theme' => 'tripal_core_relationships_form_table'
+    '#theme' => 'chado_node_relationships_form_table'
+  );
+
+  // Add defaults into form_state to be used elsewhere
+  $form['relationships']['relationship_table']['details'] = array(
+    '#type' => 'hidden',
+    '#value' => serialize($details)
   );
 
   // Add relationships already attached to the node
@@ -232,75 +261,77 @@ function tripal_core_relationships_form(&$form, &$form_state, $details) {
    * an element to the form for each one.
    */
   foreach ($existing_rels as $relationship) {
+    if (array_key_exists($relationship->type_id, $type_options)) {
 
-    $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = 'markup';
-    $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = '';
+      $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = 'markup';
+      $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = '';
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#type'] = 'markup';
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#value'] = '';
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#type'] = 'markup';
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['#value'] = '';
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $relationship->object_id
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_id'] = array(
+        '#type' => 'hidden',
+        '#value' => $relationship->object_id
+      );
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $relationship->subject_id
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_id'] = array(
+        '#type' => 'hidden',
+        '#value' => $relationship->subject_id
+      );
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_id'] = array(
-      '#type' => 'hidden',
-      '#value' => $relationship->type_id
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_id'] = array(
+        '#type' => 'hidden',
+        '#value' => $relationship->type_id
+      );
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_name'] = array(
-      '#type' => 'markup',
-      '#markup' => $relationship->object_name
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['object_name'] = array(
+        '#type' => 'markup',
+        '#markup' => $relationship->object_name
+      );
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_name'] = array(
-      '#type' => 'markup',
-      '#markup' => $relationship->type_name
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['type_name'] = array(
+        '#type' => 'markup',
+        '#markup' => $relationship->type_name
+      );
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_name'] = array(
-      '#type' => 'markup',
-      '#markup' => $relationship->subject_name
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['subject_name'] = array(
+        '#type' => 'markup',
+        '#markup' => $relationship->subject_name
+      );
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rank'] = array(
-      '#type' => 'markup',
-      '#markup' => $relationship->rank
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rank'] = array(
+        '#type' => 'markup',
+        '#markup' => $relationship->rank
+      );
 
-    $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rel_action'] = array(
-      '#type' => 'submit',
-      '#value' => t('Remove'),
-      '#name' => "rel_remove-".$relationship->type_id.'-'.$relationship->rank,
-      '#ajax' => array(
-        'callback' => 'tripal_core_relationships_form_ajax_update',
-        'wrapper' => 'tripal-generic-edit-relationships-table',
-        'effect'   => 'fade',
-        'method'   => 'replace',
-        'prevent'  => 'click'
-      ),
-      // When this button is clicked, the form will be validated and submitted.
-      // Therefore, we set custom submit and validate functions to override the
-      // default node form submit.  In the validate function we validate only the
-      // relationship fields and in the submit we remove the indicated relationship
-      // from the chado_relationships array. In order to keep validate errors
-      // from the node form validate and Drupal required errors for non-relationship fields
-      // preventing the user from removing relationships we set the #limit_validation_errors below
-      '#validate' => array('tripal_core_relationships_form_remove_button_validate'),
-      '#submit' => array('tripal_core_relationships_form_remove_button_submit'),
-      // Limit the validation of the form upon clicking this button to the relationship_table tree
-      // No other fields will be validated (ie: no fields from the main form or any other api
-      // added form).
-      '#limit_validation_errors' => array(
-        array('relationship_table')  // Validate all fields within $form_state['values']['relationship_table']
-      )
-    );
+      $form['relationships']['relationship_table'][$relationship->type_id][$relationship->rank]['rel_action'] = array(
+        '#type' => 'submit',
+        '#value' => t('Remove'),
+        '#name' => "rel_remove-".$relationship->type_id.'-'.$relationship->rank,
+        '#ajax' => array(
+          'callback' => 'chado_node_relationships_form_ajax_update',
+          'wrapper' => 'tripal-generic-edit-relationships-table',
+          'effect'   => 'fade',
+          'method'   => 'replace',
+          'prevent'  => 'click'
+        ),
+        // When this button is clicked, the form will be validated and submitted.
+        // Therefore, we set custom submit and validate functions to override the
+        // default node form submit.  In the validate function we validate only the
+        // relationship fields and in the submit we remove the indicated relationship
+        // from the chado_relationships array. In order to keep validate errors
+        // from the node form validate and Drupal required errors for non-relationship fields
+        // preventing the user from removing relationships we set the #limit_validation_errors below
+        '#validate' => array('chado_node_relationships_form_remove_button_validate'),
+        '#submit' => array('chado_node_relationships_form_remove_button_submit'),
+        // Limit the validation of the form upon clicking this button to the relationship_table tree
+        // No other fields will be validated (ie: no fields from the main form or any other api
+        // added form).
+        '#limit_validation_errors' => array(
+          array('relationship_table')  // Validate all fields within $form_state['values']['relationship_table']
+        )
+      );
+    }
   }
 
   $form['relationships']['relationship_table']['new']['object_name'] = array(
@@ -336,7 +367,7 @@ function tripal_core_relationships_form(&$form, &$form_state, $details) {
     '#value' => t('Add'),
     '#name' => 'rel_add',
     '#ajax' => array(
-      'callback' => 'tripal_core_relationships_form_ajax_update',
+      'callback' => 'chado_node_relationships_form_ajax_update',
       'wrapper' => 'tripal-generic-edit-relationships-table',
       'effect'   => 'fade',
       'method'   => 'replace',
@@ -349,8 +380,8 @@ function tripal_core_relationships_form(&$form, &$form_state, $details) {
     // array. In order to keep validate errors from the node form validate and Drupal
     // required errors for non-relationship fields preventing the user from adding relationships we
     // set the #limit_validation_errors below
-    '#validate' => array('tripal_core_relationships_form_add_button_validate'),
-    '#submit' => array('tripal_core_relationships_form_add_button_submit'),
+    '#validate' => array('chado_node_relationships_form_add_button_validate'),
+    '#submit' => array('chado_node_relationships_form_add_button_submit'),
     // Limit the validation of the form upon clicking this button to the relationship_table tree
     // No other fields will be validated (ie: no fields from the main form or any other api
     // added form).
@@ -363,13 +394,13 @@ function tripal_core_relationships_form(&$form, &$form_state, $details) {
 
 /**
  * Validate the user input for creating a new relationship
- * Called by the add button in tripal_core_relationships_form
+ * Called by the add button in chado_node_relationships_form
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_add_button_validate($form, &$form_state) {
+function chado_node_relationships_form_add_button_validate($form, &$form_state) {
 
-  $details = unserialize($form_state['values']['rel_details']);
+  $details = unserialize($form_state['values']['relationship_table']['details']);
 
   // At least one of the participants must be the current node
   if (!($form_state['values']['relationship_table']['new']['subject_is_current'] OR $form_state['values']['relationship_table']['new']['object_is_current'])) {
@@ -446,58 +477,64 @@ function tripal_core_relationships_form_add_button_validate($form, &$form_state)
 }
 
 /**
- * Called by the add button in tripal_core_relationships_form
+ * Called by the add button in chado_node_relationships_form
  *
  * Create an array of additional relationships in the form state. This array will then be
  * used to rebuild the form in subsequent builds
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_add_button_submit(&$form, &$form_state) {
+function chado_node_relationships_form_add_button_submit(&$form, &$form_state) {
 
-  $details = unserialize($form_state['values']['rel_details']);
+  $details = unserialize($form_state['values']['relationship_table']['details']);
 
   // if the chado_relationships array is not set then this is the first time modifying the
   // relationship table. this means we need to include all the relationships from the db
   if (!isset($form_state['chado_relationships'])) {
-    tripal_core_relationships_form_create_relationship_formstate_array($form, $form_state);
+    chado_node_relationships_form_create_relationship_formstate_array($form, $form_state);
   }
 
+  $name = (isset($form_state['node']->{$details['base_table']}->uniquename)) ? $form_state['node']->{$details['base_table']}->uniquename : 'CURRENT';
+
   // get details for the new relationship
   if ($form_state['values']['relationship_table']['new']['subject_is_current']) {
 
     $relationship = array(
       'type_id' => $form_state['values']['relationship_table']['new']['type_id'],
-      'object_id' => $form_state['values']['relationship_table']['new']['object_id'],
-      'subject_id' => $form_state['values'][ $details['base_foreign_key'] ],
       'type_name' => $form_state['values']['relationship_table']['new']['type_name'],
+      'object_id' => $form_state['values']['relationship_table']['new']['object_id'],
       'object_name' => $form_state['values']['relationship_table']['new']['object_name'],
-      'subject_name' => $form_state['values']['uniquename'],
-      'rank' => '0'
+      'subject_id' => $form_state['node']->{$details['base_table']}->{$details['base_foreign_key']},
+      'subject_name' => $name,
+      'rank' => '0',
     );
   }
   else {
     $relationship = array(
       'type_id' => $form_state['values']['relationship_table']['new']['type_id'],
-      'object_id' => $form_state['values'][ $details['base_foreign_key'] ],
-      'subject_id' => $form_state['values']['relationship_table']['new']['subject_id'],
       'type_name' => $form_state['values']['relationship_table']['new']['type_name'],
-      'object_name' => $form_state['values']['uniquename'],
+      'object_id' => $form_state['node']->{$details['base_table']}->{$details['base_foreign_key']},
+      'object_name' => $name,
+      'subject_id' => $form_state['values']['relationship_table']['new']['subject_id'],
       'subject_name' => $form_state['values']['relationship_table']['new']['subject_name'],
-      'rank' => '0'
+      'rank' => '0',
     );
   }
 
   // get max rank
-  $rank = tripal_core_get_max_chado_rank(
-    $details['relationship_table'],
-    array(
-      'subject_id' => $relationship['subject_id'],
-      'type_id' => $relationship['type_id'],
-      'object_id' => $relationship['object_id'],
-    )
-  );
-  $relationship['rank'] = strval($rank + 1);
+  /**
+  if (isset($form_state['node']->{$details['base_table']}->{$details['base_foreign_key']})) {
+    $rank = tripal_core_get_max_chado_rank(
+      $details['relationship_table'],
+      array(
+        'subject_id' => $relationship['subject_id'],
+        'type_id' => $relationship['type_id'],
+        'object_id' => $relationship['object_id'],
+      )
+    );
+    $relationship['rank'] = strval($rank + 1);
+  }
+  */
 
   $key = $relationship['type_id'] . '-' . $relationship['rank'];
   $form_state['chado_relationships'][$key] = (object) $relationship;
@@ -509,26 +546,26 @@ function tripal_core_relationships_form_add_button_submit(&$form, &$form_state)
  * There is no user input for the remove buttons so there is no need to validate
  * However, both a submit & validate need to be specified so this is just a placeholder
  *
- * Called by the many remove buttons in tripal_core_relationships_form
+ * Called by the many remove buttons in chado_node_relationships_form
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_remove_button_validate($form, $form_state) {
+function chado_node_relationships_form_remove_button_validate($form, $form_state) {
   // No Validation needed for remove
 }
 
 /**
  * Remove the correct relationship from the form
- * Called by the many remove buttons in tripal_core_relationships_form
+ * Called by the many remove buttons in chado_node_relationships_form
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_remove_button_submit(&$form, &$form_state) {
+function chado_node_relationships_form_remove_button_submit(&$form, &$form_state) {
 
   // if the chado_relationships array is not set then this is the first time modifying the
   // relationship table. this means we need to include all the relationships from the db
   if (!isset($form_state['chado_relationships'])) {
-    tripal_core_relationships_form_create_relationship_formstate_array($form, $form_state);
+    chado_node_relationships_form_create_relationship_formstate_array($form, $form_state);
   }
 
   // remove the specified relationship from the form relationship table
@@ -547,7 +584,7 @@ function tripal_core_relationships_form_remove_button_submit(&$form, &$form_stat
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_ajax_update($form, $form_state) {
+function chado_node_relationships_form_ajax_update($form, $form_state) {
   return $form['relationships']['relationship_table'];
 }
 
@@ -569,7 +606,7 @@ function tripal_core_relationships_form_ajax_update($form, $form_state) {
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_create_relationship_formstate_array($form, &$form_state) {
+function chado_node_relationships_form_create_relationship_formstate_array($form, &$form_state) {
 
   $form_state['chado_relationships'] = array();
 
@@ -598,7 +635,7 @@ function tripal_core_relationships_form_create_relationship_formstate_array($for
  *
  * @ingroup tripal_chado_node_api
  */
-function theme_tripal_core_relationships_form_table($variables) {
+function theme_chado_node_relationships_form_table($variables) {
   $element = $variables['element'];
 
   $header = array(
@@ -666,12 +703,12 @@ function theme_tripal_core_relationships_form_table($variables) {
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_retreive($node) {
+function chado_node_relationships_form_retreive($node) {
   $rels = array();
 
-  if (isset($node->rel_table)) {
-    foreach ($node->rel_table as $type_id => $elements) {
-      if ($type_id != 'new') {
+  if (isset($node->relationship_table)) {
+    foreach ($node->relationship_table as $type_id => $elements) {
+      if ($type_id != 'new' AND $type_id != 'details') {
         foreach ($elements as $rank => $relationships) {
           $rels[$type_id][$rank]['subject_id'] = $relationships['subject_id'];
           $rels[$type_id][$rank]['object_id'] = $relationships['object_id'];
@@ -689,35 +726,67 @@ function tripal_core_relationships_form_retreive($node) {
  *
  * @param $node
  *    The node passed into hook_insert & hook_update
- * @param $relationship_table
- *    The name of the _relationship linking table (ie: feature_relationship)
- * @param $current_id
- *    The value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
+ * @param $details
+ *  - relationship_table: the name of the _relationship linking table (ie: feature_relationship)
+ *  - foreignkey_value: the value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
+ * @param $retrieved_relationships
+ *   An array of relationships from chado_node_relationships_form_retreive($node). This
+ *   can be used if you need special handling for some of the relationships.
  *
  * @ingroup tripal_chado_node_api
  */
-function tripal_core_relationships_form_update_relationships($node, $relationship_table, $current_id) {
+function chado_node_relationships_form_update_relationships($node, $details, $retrieved_relationships = FALSE) {
+
+  $relationship_table = $details['relationship_table'];
+  $current_id = $details['foreignkey_value'];
 
-  if (isset($node->rel_table) AND ($current_id > 0)) {
+  if (isset($node->relationship_table) AND ($current_id > 0)) {
     // First remove existing relationships links
     tripal_core_chado_delete($relationship_table, array('subject_id' => $current_id));
     tripal_core_chado_delete($relationship_table, array('object_id' => $current_id));
 
-    // Add back in dbxref links and insert dbxrefs as needed
-    $relationships = tripal_core_relationships_form_retreive($node);
+    // Add back in relationships as needed
+    if ($retrieved_relationships) {
+      $relationships = $retrieved_relationships;
+    }
+    else {
+      $relationships = chado_node_relationships_form_retreive($node);
+    }
     foreach ($relationships as $type_id => $ranks) {
       foreach ($ranks as $rank => $element) {
 
-        // add relationship
-        $success_link = tripal_core_chado_insert(
+        $values = array(
+          'subject_id' => $element['subject_id'],
+          'type_id' => $type_id,
+          'object_id' => $element['object_id'],
+          'rank' => $rank
+        );
+
+        // Set the current id if not already
+        // this is usually only necessary in an insert
+        if (empty($values['subject_id'])) {
+          $values['subject_id'] = $current_id;
+        }
+        if (empty($values['object_id'])) {
+          $values['object_id'] = $current_id;
+        }
+
+        // Ensure that the rank is Set & Current
+        $rank_select = tripal_core_get_max_chado_rank(
           $relationship_table,
           array(
-            'subject_id' => $element['subject_id'],
-            'type_id' => $type_id,
-            'object_id' => $element['object_id'],
-            'rank' => $rank
+            'subject_id' => $values['subject_id'],
+            'type_id' => $values['type_id'],
+            'object_id' => $values['object_id'],
           )
         );
+        $values['rank'] = $rank_select + 1;
+
+        // add relationship
+        $success_link = tripal_core_chado_insert(
+          $relationship_table,
+          $values
+        );
 
       }
     }

+ 4 - 4
tripal_core/tripal_core.module

@@ -440,13 +440,13 @@ function tripal_core_theme($existing, $type, $theme, $path) {
       'render element' => 'element',
     ),
     // Additional Dbxrefs Nore Form
-    'tripal_core_additional_dbxrefs_form_table' => array(
-      'function' => 'theme_tripal_core_additional_dbxrefs_form_table',
+    'chado_node_additional_dbxrefs_form_table' => array(
+      'function' => 'theme_chado_node_additional_dbxrefs_form_table',
       'render element' => 'element',
     ),
     // Relationships Nore Form
-    'tripal_core_relationships_form_table' => array(
-      'function' => 'theme_tripal_core_relationships_form_table',
+    'chado_node_relationships_form_table' => array(
+      'function' => 'theme_chado_node_relationships_form_table',
       'render element' => 'element',
     ),
   );

+ 36 - 20
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -537,6 +537,7 @@ function chado_pub_insert($node) {
 
     // get the properties from the form
     $properties = chado_node_properties_form_retreive($node);
+    ddl($properties, 'clean properties');
 
     // get the list of properties for easy lookup (without doing lots of database queries
     $properties_list = array();
@@ -557,7 +558,10 @@ function chado_pub_insert($node) {
       // both as a property and as the uniquename for the
       // pub and we want it stored in both the pub table and the pubprop table
       if ($prop->name == 'Citation') {
-        $properties[$prop->name][0] = $node->uniquename;
+        $citation_id = $prop->cvterm_id;
+        if (!empty($node->uniquename)) {
+          $properties[$citation_id] = $node->uniquename;
+        }
       }
     }
 
@@ -572,8 +576,9 @@ function chado_pub_insert($node) {
     $pubplace = '';
     $miniref = '';
     $cross_refs = array();
-    foreach ($properties as $name => $element) {
+    foreach ($properties as $type_id => $element) {
       $value = trim($element[0]);
+      $name = $properties_list[$type_id];
 
       // populate our $pub_array for building a citation
       $pub_arr[$name] = $value;
@@ -581,27 +586,27 @@ function chado_pub_insert($node) {
       // remove properties that are stored in the pub table
       if ($name == "Volume") {
         $volume = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Volume Title") {
         $volumetitle = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Issue") {
         $issue = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Pages") {
         $pages = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Publisher") {
         $publisher = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Series Name" or $name == "Journal Name" or $name == "Conference Name") {
         $series_name = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Journal Country" or $name == "Published Location") {
         $pubplace = $value;
@@ -615,13 +620,13 @@ function chado_pub_insert($node) {
       }
     }
     // generate a citation for this pub if one doesn't already exist
-    if (!$node->uniquename and array_key_exists('Citation', $properties)) {
+    if (!$node->uniquename and !array_key_exists($citation_id, $properties)) {
       $pub_type = tripal_cv_get_cvterm_by_id($node->type_id);
       $pub_arr['Title'] = $node->pubtitle;
       $pub_arr['Publication Type'][0] = $pub_type->name;
       $pub_arr['Year'] = $node->pyear;
       $node->uniquename = tripal_pub_create_citation($pub_arr);
-      $properties['Citation'][0] = $node->uniquename;
+      $properties[$citation_id][0] = $node->uniquename;
     }
 
     // insert the pub record
@@ -650,6 +655,13 @@ function chado_pub_insert($node) {
 
     // now add in the properties
     // Only adds in those not used in the pub record
+    ddl($properties, 'properties before update');
+    $details = array(
+      'property_table' => 'pubprop',
+      'base_table' => 'pub',
+      'foreignkey_name' => 'pub_id',
+      'foreignkey_value' => $pub_id
+    );
     chado_node_properties_form_update_properties($node, $details, $properties);
 
     // add in any database cross-references
@@ -732,7 +744,10 @@ function chado_pub_update($node) {
     // both as a property and as the uniquename for the
     // pub and we want it stored in both the pub table and the pubprop table
     if ($prop->name == 'Citation') {
-      $properties[$prop->name][0] = $node->uniquename;
+      $citation_id = $prop->cvterm_id;
+      if (!empty($node->uniquename)) {
+        $properties[$citation_id][0] = $node->uniquename;
+      }
     }
   }
 
@@ -747,35 +762,36 @@ function chado_pub_update($node) {
   $pubplace = '';
   $miniref = '';
   $cross_refs = array();
-  foreach ($properties as $name => $element) {
+  foreach ($properties as $type_id => $element) {
     foreach ($element as $index => $value) {
+      $name = $properties_list[$type_id];
       // populate our $pub_array for building a citation
       $pub_arr[$name] = $value;
 
       // remove properties that are stored in the pub table
       if ($name == "Volume") {
         $volume = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Volume Title") {
         $volumetitle = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Issue") {
         $issue = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Pages") {
         $pages = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Publisher") {
         $publisher = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Journal Name" or $name == "Conference Name") {
         $series_name = $value;
-        unset($properties[$name]);
+        unset($properties[$type_id]);
       }
       elseif ($name == "Journal Country" or $name == "Published Location") {
         $pubplace = $value;
@@ -796,7 +812,7 @@ function chado_pub_update($node) {
     $pub_arr['Publication Type'][0] = $pub_type->name;
     $pub_arr['Year'] = $node->pyear;
     $node->uniquename = tripal_pub_create_citation($pub_arr);
-    $properties['Citation'][0] = $node->uniquename;
+    $properties[$citation_id][0] = $node->uniquename;
   }
 
   // update the pub record
@@ -833,7 +849,7 @@ function chado_pub_update($node) {
     'foreignkey_name' => 'pub_id',
     'foreignkey_value' => $pub_id
   );
-  chado_node_properties_form_update_properties($node, $details, $$properties);
+  chado_node_properties_form_update_properties($node, $details, $properties);
 
   // add in any database cross-references after first removing
   tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));

+ 39 - 33
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -315,7 +315,7 @@ function chado_stock_form($node, $form_state) {
     'base_foreign_key' => 'stock_id',
     'base_key_value' => $stock_id
   );
-  tripal_core_additional_dbxrefs_form($form, $form_state, $details);
+  chado_node_additional_dbxrefs_form($form, $form_state, $details);
 
   // RELATIONSHIPS FORM
   //---------------------------------------------
@@ -328,7 +328,7 @@ function chado_stock_form($node, $form_state) {
     'nodetype' => 'stock',
     'cv_id' => variable_get('chado_stock_relationship_cv', 0)
   );
-  tripal_core_relationships_form($form, $form_state, $details);
+  chado_node_relationships_form($form, $form_state, $details);
 
   return $form;
 }
@@ -545,18 +545,19 @@ function chado_stock_insert($node) {
       chado_node_properties_form_update_properties($node, $details);
 
       // Now add the additional references
-      tripal_core_additional_dbxrefs_form_update_dbxrefs(
-        $node,
-        'stock_dbxref',
-        'stock_id',
-        $stock_id
+      $details = array(
+        'linking_table' => 'stock_dbxref',
+        'foreignkey_name' => 'stock_id',
+        'foreignkey_value' => $stock_id
       );
+      chado_node_additional_dbxrefs_form_update_dbxrefs($node, $details);
+
       // Now add in relationships
-      tripal_core_relationships_form_update_relationships(
-        $node,
-        'stock_relationship',
-        $stock_id
+      $details = array(
+        'relationship_table' => 'stock_relationship',
+        'foreignkey_value' => $node->stock_id
       );
+      chado_node_relationships_form_update_relationships($node, $details);
     }
   } //end of adding stock to chado
   else {
@@ -609,7 +610,10 @@ function chado_stock_update($node) {
   }
 
   //update dbxref
+  $dbxref_status = NULL;
+  $dbxref_present = FALSE;
   if ($node->database) {
+    $dbxref_present = TRUE;
     if ($node->accession) {
       $dbxref_mode = '';
       $stock = tripal_core_chado_select(
@@ -659,15 +663,15 @@ function chado_stock_update($node) {
         }
       }
     }
-  }
 
-  if (!$dbxref_status) {
-    watchdog(
-      'tripal_stock',
-      'Stock Update: Unable to %mode main stock dbxref with values: %values',
-      array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
-      WATCHDOG_WARNING
-    );
+    if (!$dbxref_status) {
+      watchdog(
+        'tripal_stock',
+        'Stock Update: Unable to %mode main stock dbxref with values: %values',
+        array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
+        WATCHDOG_WARNING
+      );
+    }
   }
 
   //can't change stock id which is all thats stored in drupal thus only update chado
@@ -678,11 +682,13 @@ function chado_stock_update($node) {
     'description' => $node->stock_description,
     'type_id' => $node->type_id,
   );
-  if ($dbxref_status) {
-    $update_values['dbxref_id'] = array(
-      'db_id' => $node->database,
-      'accession' => $node->accession
-    );
+  if ($dbxref_present) {
+    if ($dbxref_status) {
+      $update_values['dbxref_id'] = array(
+        'db_id' => $node->database,
+        'accession' => $node->accession
+      );
+    }
   }
   $status = tripal_core_chado_update('stock', array('stock_id' => $node->stock_id), $update_values);
 
@@ -717,21 +723,21 @@ function chado_stock_update($node) {
 
   // now update the additional dbxrefs
   if ($node->stock_id > 0) {
-    tripal_core_additional_dbxrefs_form_update_dbxrefs(
-      $node,
-      'stock_dbxref',
-      'stock_id',
-      $node->stock_id
+    $details = array(
+      'linking_table' => 'stock_dbxref',
+      'foreignkey_name' => 'stock_id',
+      'foreignkey_value' => $node->stock_id
     );
+    chado_node_additional_dbxrefs_form_update_dbxrefs($node, $details);
   }
 
   // now update relationships
   if ($node->stock_id > 0) {
-    tripal_core_relationships_form_update_relationships(
-      $node,
-      'stock_relationship',
-      $node->stock_id
+    $details = array(
+      'relationship_table' => 'stock_relationship',
+      'foreignkey_value' => $node->stock_id
     );
+    chado_node_relationships_form_update_relationships($node, $details);
   }
 }