Просмотр исходного кода

Working on fixing fields for updated TripalField class

Stephen Ficklin 8 лет назад
Родитель
Сommit
a3846391e0

+ 17 - 9
tripal_chado/includes/TripalFields/chado_linker__cvterm_adder.inc

@@ -23,6 +23,14 @@ class chado_linker__cvterm_adder extends TripalField {
   // this field.
   public static $default_storage = 'field_chado_storage';
 
+  /**
+   * @see TripalField::formatterView()
+   */
+  public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
+    // This field should never be viewed. It's to help add new cvterms
+    // when editing an entity.  So return nothing.
+    return '';
+  }
 
   /**
    * @see TripalField::widgetForm()
@@ -73,38 +81,38 @@ class chado_linker__cvterm_adder extends TripalField {
       '#limit_validation_errors' => array(array($this->field['field_name'])),
     );
   }
-  
+
   public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
 
     // Add the new field to the entity
     if (array_key_exists('triggering_element', $form_state) and
         $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
-    
+
           $form_state['rebuild'] = TRUE;
           $field_name = $element['#field_name'];
           $entity_type = $element['#entity']->type;
           $bundle = $element['#entity']->bundle;
-    
+
           // Get the base table name from the field annotations.
           $field = field_info_field($field_name);
           $base_table = $field['settings']['base_table'];
-    
+
           // Get the vocabulary.
           //$cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
           $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
-    
+
           if (!$cv) {
             form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
             return;
           }
-    
+
           $type_field_name = $field_name . '__' . $cv->cv_id;
-    
+
           // The field name is the table name in this case. We want to get the
           // primary key as this should be the field that maps th the value.
           $schema = chado_get_schema($field_name);
           $pkey = $schema['primary key'][0];
-    
+
           // Add the field if it doesn't already exists.
           $field = field_info_field('cvterm');
           if (!$field) {
@@ -124,7 +132,7 @@ class chado_linker__cvterm_adder extends TripalField {
             );
             $field = field_create_field($create_info);
           }
-    
+
           // Attach the field to the bundle if it isn't already.
           if (!$field or !array_key_exists('bundles', $field) or
               !array_key_exists('TripalEntity', $field['bundles']) or

+ 5 - 3
tripal_chado/includes/TripalFields/chado_linker__dbxref.inc

@@ -38,7 +38,9 @@ class chado_linker__dbxref extends TripalField {
    * @see TripalField::formatterView()
    */
   public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
+
     $chado_table = $this->field['settings']['chado_table'];
+    $content = '';
     foreach ($items as $delta => $item) {
       if (!$item['value']) {
         continue;
@@ -53,7 +55,7 @@ class chado_linker__dbxref extends TripalField {
       );
     }
 
-    if (count($items) == 0) {
+    if (count($element) == 0) {
       $element[0] = array(
         '#type' => 'markup',
         '#markup' => '',
@@ -285,9 +287,9 @@ class chado_linker__dbxref extends TripalField {
         }
       }
       // If the db_id and accession are not set, then remove the linker FK
-      // value to the base table.
+      // value to the base table, but leave the primary key so the record
+      // can be deleted.
       else {
-        $items[$delta]['chado-' . $table_name . '__' . $pkey] = '';
         $items[$delta]['chado-' . $table_name . '__' . $fkey] = '';
         $items[$delta]['chado-' . $table_name . '__dbxref_id'] = '';
       }

+ 11 - 2
tripal_chado/includes/TripalFields/chado_linker__prop_adder.inc

@@ -23,6 +23,15 @@ class chado_linker__prop_adder extends TripalField {
   // this field.
   public static $default_storage = 'field_chado_storage';
 
+  /**
+   * @see TripalField::formatterView()
+   */
+  public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
+    // This field should never be viewed. It's to help add new properties
+    // when editing an entity.  So return nothing.
+    return '';
+  }
+
   /**
    * @see TripalField::widgetForm()
    */
@@ -74,7 +83,7 @@ class chado_linker__prop_adder extends TripalField {
   public function widgetFormValidate($entity_type, $entity, $field, $items, &$errors) {
 
   }
-  
+
   public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
 
     // Add the new field to the entity
@@ -161,5 +170,5 @@ class chado_linker__prop_adder extends TripalField {
       }
     }
   }
-  
+
 }

+ 1 - 1
tripal_chado/includes/TripalFields/chado_linker__pub.inc

@@ -161,9 +161,9 @@ class chado_linker__pub extends TripalField {
 
       // If the user removed the publication from the pub_uniquename field
       // then we want to clear out the rest of the hidden values.
+      // Leave the primary key so the record can be deleted.
       if (!$uname and $pub_id) {
         $items[$delta]['chado-' . $table_name . '__' . $fkey] = '';
-        $items[$delta]['chado-' . $table_name . '__' . $pkey] = '';
         $items[$delta]['chado-' . $table_name . '__pub_id'] = '';
       }
     }

+ 50 - 32
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -85,7 +85,7 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
 }
 
 /**
- * Write (inserts/updates) a nested array of values for a table.
+ * Write (inserts/updates/deletes) values for a Chado table.
  *
  * The $values array is of the same format used by chado_insert_record() and
  * chado_update_record().  However, both of those methods will use any nested
@@ -102,7 +102,9 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
  *   The name of the table on which the insertion/update is performed.
  * @param $values
  *   The values array for the insertion.
+ *
  * @throws Exception
+ *
  * @return
  *   The unique record ID.
  */
@@ -111,27 +113,28 @@ function tripal_chado_field_storage_write_table($table_name, $values) {
    $fkeys = $schema['foreign keys'];
    $pkey = $schema['primary key'][0];
 
-   // Before inserting or updating this table, recurse if there are any
-   // nested FK array values.
-   foreach ($values as $column => $value) {
-     // If this value is an array then it must be a FK... let's recurse.
-     if (is_array($value)) {
-
-       // Find the name of the FK table for this column.
-       $fktable_name = '';
-       foreach ($fkeys as $fktable => $details) {
-         foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
-           if ($fkey_lcolumn == $column) {
-             $fktable_name = $fktable;
-           }
-         }
-       }
 
-       // Recurse on this recod.
-       $record_id = tripal_chado_field_storage_write_table($fktable_name, $values[$column]);
-       $values[$column] = $record_id;
-     }
-   }
+//    // Before inserting or updating this table, recurse if there are any
+//    // nested FK array values.
+//    foreach ($values as $column => $value) {
+//      // If this value is an array then it must be a FK... let's recurse.
+//      if (is_array($value)) {
+
+//        // Find the name of the FK table for this column.
+//        $fktable_name = '';
+//        foreach ($fkeys as $fktable => $details) {
+//          foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
+//            if ($fkey_lcolumn == $column) {
+//              $fktable_name = $fktable;
+//            }
+//          }
+//        }
+
+//        // Recurse on this recod.
+//        $record_id = tripal_chado_field_storage_write_table($fktable_name, $values[$column]);
+//        $values[$column] = $record_id;
+//      }
+//    }
 
    // Fields with a cardinality greater than 1 will often submit an
    // empty form.  We want to remove these empty submissions.  We can detect
@@ -145,9 +148,25 @@ function tripal_chado_field_storage_write_table($table_name, $values) {
    if ($num_empty == count(array_keys($values))) {
      return '';
    }
+   // If the primary key column has a value but all other values are empty then
+   // this is a delete.
+   if (array_key_exists($pkey, $values) and $values[$pkey]) {
+     $num_vals = 0;
+     foreach ($values as $value) {
+       if ($value) {
+         $num_vals++;
+       }
+     }
+     if ($num_vals == 1) {
+       $new_vals[$pkey] = $values[$pkey];
+       if (!chado_delete_record($table_name, $new_vals)) {
+         throw new Exception('Could not delete record from table: "' . $table_name . '".');
+       }
+       return '';
+     }
+   }
 
-   // If the primary key column has a value then this will be an udpate,
-   // otherwise it's an insert.
+   // If the primary key column does not have a value then this is an insert.
    if (!array_key_exists($pkey, $values) or !$values[$pkey] or !isset($values[$pkey])) {
      // Before inserting, we want to make sure the record does not
      // already exist.  Using the unique constraint check for a matching record.
@@ -168,16 +187,15 @@ function tripal_chado_field_storage_write_table($table_name, $values) {
      }
      return $record[$pkey];
    }
-   // We have an incoming record_id so this is an update.
-   else {
-     // TODO: what if the unique constraint matches another record?  That is
-     // not being tested for here.
-     $match[$pkey] = $values[$pkey];
-     if (!chado_update_record($table_name, $match, $values)) {
-       drupal_set_message("Could not update Chado record in table: $table_name.", 'error');
-     }
-     return $values[$pkey];
+
+   // If we've made it to this point then this is an update.
+   // TODO: what if the unique constraint matches another record?  That is
+   // not being tested for here.
+   $match[$pkey] = $values[$pkey];
+   if (!chado_update_record($table_name, $match, $values)) {
+     drupal_set_message("Could not update Chado record in table: $table_name.", 'error');
    }
+   return $values[$pkey];
 }
 
 /**