Browse Source

Annotation field can now delete items

Stephen Ficklin 7 years ago
parent
commit
011f2b1510

+ 1 - 1
tripal_chado/api/tripal_chado.mviews.api.inc

@@ -376,8 +376,8 @@ function tripal_populate_mview($mview_id) {
       chado_set_active($previous_db);
     }
     catch (Exception $e) {
-      chado_set_active($previous_db);
       $transaction->rollback();
+      chado_set_active($previous_db);
       // print and save the error message
       $record = new stdClass();
       $record->mview_id = $mview_id;

+ 6 - 3
tripal_chado/api/tripal_chado.query.api.inc

@@ -1666,10 +1666,13 @@ function chado_query($sql, $args = array()) {
         chado_set_active($previous_db);
       }
       catch (Exception $e) {
-        chado_set_active($previous_db);
-        throw $e;
+        try {
+          chado_set_active($previous_db);
+        }
+        catch (Exception $e2) {
+          throw Exception($e->getMessage() . $e2->getMessage());
+        }
       }
-
     }
     // for all other tables we should have everything in scope so just run the query
     else {

+ 3 - 8
tripal_chado/includes/TripalFields/sio__annotation/sio__annotation_formatter.inc

@@ -80,10 +80,6 @@ class sio__annotation_formatter extends ChadoFieldFormatter {
     $field_table = $this->instance['settings']['chado_table'];
     $schema = chado_get_schema($field_table);
 
-    if (array_key_exists('is_not', $schema['fields'])) {
-      $headers[] = 'Negates';
-    }
-
     $vocabulary_term = tripal_get_chado_semweb_term('cvterm', 'cv_id');
     $accession_term = tripal_get_chado_semweb_term('dbxref', 'accession');
     $definition_term = tripal_get_chado_semweb_term('cvterm', 'definition');
@@ -110,7 +106,9 @@ class sio__annotation_formatter extends ChadoFieldFormatter {
         );
 
         if (array_key_exists('is_not', $schema['fields'])) {
-          $row[] = $item['value'][$negation_term];
+          if ($negation_term == FALSE) {
+            $row[1] = 'NOT ' . $row[1];
+          }
         }
 
         $rows[] = $row;
@@ -119,9 +117,6 @@ class sio__annotation_formatter extends ChadoFieldFormatter {
 
     // Theme the results in a talbe.
     $caption = 'This record is associated with the following annotations.';
-    if (array_key_exists('is_not', $schema['fields'])) {
-      $caption .= 'The "Negates" column indicates if the term does NOT apply.';
-    }
     $table = array(
       'header' => $headers,
       'rows' => $rows,

+ 34 - 11
tripal_chado/includes/TripalFields/sio__annotation/sio__annotation_widget.inc

@@ -39,7 +39,6 @@ class sio__annotation_widget extends ChadoFieldWidget {
     $cvterm_id = '';
     $pub_id = '';
     $is_not = FALSE;
-    $rank = $delta;
     $cvterm_name = '';
     $cv_id = '';
     $cvterm = NULL;
@@ -57,9 +56,6 @@ class sio__annotation_widget extends ChadoFieldWidget {
         $pub_name = tripal_get_field_item_keyval($items, $delta, 'pub', $pub_name);
         $pub_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__pub_id', $pub_id);
       }
-      if (array_key_exists('rank', $schema['fields'])) {
-        $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
-      }
       if (array_key_exists('is_not', $schema['fields'])) {
         $is_not = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__is_not', $is_not);
       }
@@ -88,9 +84,6 @@ class sio__annotation_widget extends ChadoFieldWidget {
         $pub_name = $form_state[$vtype][$field_name]['und'][$delta]['pub'];
         $pub_id = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'];
       }
-      if (array_key_exists('rank', $schema['fields'])) {
-        $rank = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
-      }
       if (array_key_exists('is_not', $schema['fields'])) {
         $is_not = $form_state[$vtype][$field_name]['und'][$delta]['chado-' . $field_table . '__is_not'];
       }
@@ -108,9 +101,12 @@ class sio__annotation_widget extends ChadoFieldWidget {
     $widget['#prefix'] =  "<span id='$field_name-sio--annotation-$delta'>";
     $widget['#suffix'] =  "</span>";
 
+    // The value field isn't really used but it's needed because if
+    // it doesn't have a value the element won't be considered for
+    // insert/update.
     $widget['value'] = array(
       '#type' => 'value',
-      '#value' => key_exists($delta, $items) ? $items[$delta]['value'] : '',
+      '#value' => $cvterm_id,
     );
 
     $widget['chado-' . $field_table . '__' . $pkey] = array(
@@ -175,6 +171,12 @@ class sio__annotation_widget extends ChadoFieldWidget {
         '#disabled' => $cv_id ? FALSE : TRUE,
       );
     }
+    if (array_key_exists('rank', $schema['fields'])) {
+      $widget['chado-' . $field_table . '__rank'] = array(
+        '#type' => 'value',
+        '#default_value' => $delta,
+      );
+    }
   }
 
   /**
@@ -201,11 +203,9 @@ class sio__annotation_widget extends ChadoFieldWidget {
       $negation = tripal_get_chado_semweb_term($field_table, 'is_not');
     }
 
-
     $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
     $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn];
 
-
     // If a publication ID was provided then make sure the form_state
     // value for the pub_id is set correctly.
     if (array_key_exists('pub_id', $schema['fields'])) {
@@ -213,9 +213,14 @@ class sio__annotation_widget extends ChadoFieldWidget {
       if ($pub_name) {
         $pub = chado_generate_var('pub', array('uniquename' => $pub_name));
         if ($pub) {
-          $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
+          $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__pub_id'] = $pub->pub_id;
         }
       }
+      // Use the NULL pub.
+      else {
+        $pub = tripal_get_publication(array('uniquename' => 'null'));
+        $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__pub_id'] = $pub->pub_id;
+      }
     }
 
     // Make sure the rank is set.
@@ -234,6 +239,24 @@ class sio__annotation_widget extends ChadoFieldWidget {
     if ($cvterm) {
       $form_state['values'][$field_name]['und'][$delta]['cvterm_name'] = '';
       $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'] = $cvterm->cvterm_id;
+      $form_state['values'][$field_name]['und'][$delta]['value'] = $cvterm->cvterm_id;
+    }
+    // Remove all values so we can delete this record if there is no
+    // cvterm.
+    else {
+      // There must be some value set.
+      $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__cvterm_id'] = '';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey_lcolumn] = '';
+      if (array_key_exists('rank', $schema['fields'])) {
+        $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = '';
+      }
+      if (array_key_exists('pub_id', $schema['fields'])) {
+        $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '';
+      }
+      if (array_key_exists('is_not', $schema['fields'])) {
+        $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '_is_not'] = '';
+      }
     }
   }
 

+ 2 - 1
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -387,7 +387,8 @@ function tripal_chado_field_storage_write_merge_fields($fields, $entity_type, $e
     $field = field_info_field_by_id($field_id);
     $field_name = $field['field_name'];
     $instance = field_info_instance('TripalEntity', $field['field_name'], $entity->bundle);
-    // Some fields (e.g. chado_linker_cvterm_adder) don't add data to
+
+    // Some fields don't add data to
     // Chado so they don't have a table, but they are still attached to the
     // entity. Just skip these.
     if (!array_key_exists('chado_table', $instance['settings'])) {