Преглед на файлове

Fixed fields and Chado field storage for inserts

Stephen Ficklin преди 8 години
родител
ревизия
ef9c4fc40f

+ 2 - 2
tripal_chado/includes/fields/chado_base__dbxref_id.inc

@@ -129,7 +129,7 @@ class chado_base__dbxref_id extends TripalField {
 
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
-    if (array_key_exists($delta, $items)) {
+    if (count($items) > 0 and array_key_exists($delta, $items)) {
       $fk_val = $items[$delta][$field_table . '__' . $field_column];
       $dbxref_id = $items[$delta][$field_table . '__' . $field_column . '--dbxref_id'];
       $db_id = $items[$delta][$field_table . '__' . $field_column . '--db_id'];
@@ -180,7 +180,7 @@ class chado_base__dbxref_id extends TripalField {
 
     $widget['value'] = array(
       '#type' => 'value',
-      '#value' => $items[0]['value'],
+      '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
 
     $widget[$field_table . '__' . $field_column] = array(

+ 29 - 16
tripal_chado/includes/fields/chado_base__organism_id.inc

@@ -47,6 +47,12 @@ class chado_base__organism_id extends TripalField {
       return $field_info;
     }
 
+    $is_required = FALSE;
+    if (array_key_exists('not null', $schema['fields']['organism_id']) and
+        $schema['fields']['organism_id']['not null']) {
+      $is_required = TRUE;
+    }
+
     // There is an organism_id column so attach the field!
     $field_info = array(
       'field_name' => $table_name . '__organism_id',
@@ -54,7 +60,7 @@ class chado_base__organism_id extends TripalField {
       'widget_type' => 'chado_base__organism_id_widget',
       'description' => 'Select an organism.',
       'label' => 'Organism',
-      'is_required' => 0,
+      'is_required' => $is_required,
       'storage' => 'field_chado_storage',
       'widget_settings' => array(
         'display_label' => 1
@@ -126,9 +132,9 @@ class chado_base__organism_id extends TripalField {
     $field_table = $field['settings']['chado_table'];
     $field_column = $field['settings']['chado_column'];
 
-    $default_value = 0;
-    if (array_key_exists($field_table . '__organism_id', $items[0])) {
-      $default_value = $items[0][$field_table . '__organism_id'];
+    $organism_id = 0;
+    if (count($items) > 0 and array_key_exists($field_table . '__organism_id', $items[0])) {
+      $organism_id = $items[0][$field_table . '__organism_id'];
     }
 
     $widget['value'] = array(
@@ -141,7 +147,7 @@ class chado_base__organism_id extends TripalField {
       '#title' => $element['#title'],
       '#description' => $element['#description'],
       '#options' => $options,
-      '#default_value' => $default_value,
+      '#default_value' => $organism_id,
       '#required' => $element['#required'],
       '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
       '#delta' => $delta,
@@ -162,18 +168,25 @@ class chado_base__organism_id extends TripalField {
     $field_table = $field['settings']['chado_table'];
     $field_column = $field['settings']['chado_column'];
 
-    $organism = $record->organism_id;
-    $string = $settings['field_display_string'];
-    $value = tripal_replace_chado_tokens($string, $organism);
-
-    $entity->{$field_name}['und'][0]['value'] = $value;
-    $entity->{$field_name}['und'][0][$field_table . '__organism_id'] = $organism->organism_id;
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => '',
+      'organism__type_id' => '',
+    );
 
-    // Is there a published entity for this organism?
-    if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
-      $fk_entity_id = $entity->chado_record->$field_column->entity_id;
-      $entity->{$field_name}['und'][0]['entity_id'] = $fk_entity_id;
-      $entity->{$field_name}['und'][0]['entity_type'] = 'TripalEntity';
+    if ($record) {
+      $organism = $record->organism_id;
+      $string = $settings['field_display_string'];
+      $value = tripal_replace_chado_tokens($string, $organism);
+      $entity->{$field_name}['und'][0]['value'] = $value;
+      $entity->{$field_name}['und'][0][$field_table . '__organism_id'] = $organism->organism_id;
+
+      // Is there a published entity for this organism?
+      if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
+        $fk_entity_id = $entity->chado_record->$field_column->entity_id;
+        $entity->{$field_name}['und'][0]['entity_id'] = $fk_entity_id;
+        $entity->{$field_name}['und'][0]['entity_type'] = 'TripalEntity';
+      }
     }
   }
 

+ 11 - 2
tripal_chado/includes/fields/chado_feature__md5checksum.inc

@@ -107,13 +107,22 @@ class chado_feature__md5checksum  extends TripalField {
     $field_table = $field['settings']['chado_table'];
     $field_column = $field['settings']['chado_column'];
 
+    // Get the field defaults.
+    $md5checksum = '';
+    if (count($items) > 0 and array_key_exists('feature__md5checksum', $items[0])) {
+      $md5checksum = $items[0]['feature__md5checksum'];
+    }
+    if (array_key_exists('values', $form_state)) {
+      $md5checksum = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'feature__md5checksum');
+    }
+
     $widget['value'] = array(
       '#type' => 'value',
-      '#value' => $items[0]['value'],
+      '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
     $widget['feature__md5checksum'] = array(
       '#type' => 'value',
-      '#value' => $items[0]['value'],
+      '#value' => $md5checksum,
       '#element_validate' => array('chado_feature__md5checksum_widget_validate'),
     );
   }

+ 9 - 7
tripal_chado/includes/fields/chado_feature__residues.inc

@@ -151,23 +151,25 @@ class chado_feature__residues extends TripalField {
     $field_table = $field['settings']['chado_table'];
     $field_column = $field['settings']['chado_column'];
 
-    // This field provides several items if an alignment for the feature
-    // is present.  We don't want widgets for those.  Just for the first
-    // one which should always be the residues for the feature table.
-    if ($delta > 0) {
-      return;
+    // Get the field defaults.
+    $residues = '';
+    if (count($items) > 0 and array_key_exists('feature__residues', $items[0])) {
+      $residues = $items[0]['feature__residues'];
+    }
+    if (array_key_exists('values', $form_state)) {
+      $residues = tripal_chado_get_field_form_values($field_name, $form_state, 0, 'feature__residues');
     }
 
     $widget['value'] = array(
       '#type' => 'value',
-      '#value' => $items[$delta]['value'],
+      '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
     $widget['feature__residues'] = array(
       '#type' => 'textarea',
       '#title' => $element['#title'],
       '#description' => $element['#description'],
       '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
-      '#default_value' => $items[$delta]['feature__residues'],
+      '#default_value' => $residues,
       '#delta' => $delta,
       '#element_validate' => array('chado_feature__residues_widget_validate'),
       '#cols' => 30,

+ 1 - 1
tripal_chado/includes/fields/chado_feature__seqlen.inc

@@ -106,7 +106,7 @@ class chado_feature__seqlen extends TripalField {
 
     $widget['value'] = array(
       '#type' => 'value',
-      '#value' => $items[$delta]['value'],
+      '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
 
     $widget['feature__seqlen'] =  array(

+ 8 - 0
tripal_chado/includes/fields/chado_gene__transcripts.inc

@@ -120,6 +120,9 @@ class chado_gene__transcripts extends TripalField {
     $rows = array();
     foreach ($items as $delta => $item) {
 
+      if (!$item['value']) {
+        continue;
+      }
       $transcript = $item['value'];
 
       // Get the field values
@@ -165,6 +168,11 @@ class chado_gene__transcripts extends TripalField {
 
     $field_name = $field['field_name'];
 
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => array(),
+    );
+
     // TODO: If the tripal_get_feature_relationships() slows this down then
     // we may need to write a custom function to get the data.
     $rels = tripal_get_feature_relationships($record);

+ 9 - 0
tripal_chado/includes/fields/chado_linker__featureloc.inc

@@ -120,6 +120,10 @@ class chado_linker__featureloc extends TripalField {
     $headers = array('Aligned Feature' ,'Feature Type', 'Alignment Location');
 
     foreach ($items as $delta => $item) {
+      if (!$item['value']) {
+        continue;
+      }
+
       $alignment = $item['value'];
       $feature_name = $alignment['name'];
       $feature_loc = '';
@@ -189,6 +193,11 @@ class chado_linker__featureloc extends TripalField {
     $field_name = $field['field_name'];
     $field_type = $field['type'];
 
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => array(),
+    );
+
     $options = array(
       'return_array' => TRUE,
       'include_fk' => array(

+ 18 - 9
tripal_chado/includes/fields/chado_organism__type_id.inc

@@ -96,24 +96,27 @@ class chado_organism__type_id extends TripalField {
     $field_table = $field['settings']['chado_table'];
     $field_column = $field['settings']['chado_column'];
 
-    $default_value = 0;
-    if (array_key_exists('organism__type_id', $items[0])) {
-      $default_value = $items[0]['organism__type_id'];
+    $type_id = 0;
+    if (count($items) > 0 and array_key_exists('organism__type_id', $items[0])) {
+      $type_id = $items[0]['organism__type_id'];
     }
 
     $form['value'] = array(
       '#type' => 'value',
-      '#value' => $items[$delta]['value'],
+      '#value' =>  array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
 
     $cv = tripal_get_default_cv($field_table, $field_column);
-    $options = tripal_get_cvterm_select_options($cv->cv_id);
+    $options = array();
+    if ($cv) {
+      $options = tripal_get_cvterm_select_options($cv->cv_id);
+    }
     $widget['organism__type_id'] = array(
       '#type' => 'select',
       '#title' => $element['#title'],
       '#description' => $element['#description'],
       '#options' => $options,
-      '#default_value' => $default_value,
+      '#default_value' => $type_id,
       '#required' => $element['#required'],
       '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
       '#delta' => $delta,
@@ -134,10 +137,16 @@ class chado_organism__type_id extends TripalField {
     $field_table = $field['settings']['chado_table'];
     $field_column = $field['settings']['chado_column'];
 
-    $organism = $record->organism_id;
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => '',
+      'organism__type_id' => '',
+    );
 
-    $entity->{$field_name}['und'][0]['value'] = $organism->type_id->name;
-    $entity->{$field_name}['und'][0]['organism__type_id'] = $organism->type_id->cvterm_id;
+    if ($record->type_id) {
+      $entity->{$field_name}['und'][0]['value'] = $record->type_id->name;
+      $entity->{$field_name}['und'][0]['organism__type_id'] = $record->type_id->cvterm_id;
+    }
   }
 
 }

+ 10 - 6
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -27,6 +27,7 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
   $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
   $term = entity_load('TripalTerm', array('id' => $entity->term_id));
   $term = reset($term);
+  $cvterm = $term->details['cvterm'];
 
   // Get the base table, type field and record_id from the entity.
   $base_table = $entity->chado_table;
@@ -39,12 +40,15 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
   // Convert the fields into a key/value list of fields and their values.
   $field_vals = tripal_chado_field_storage_merge_fields($fields, $entity_type, $entity);
 
-  // Write the record for the base table.  If this is an update then we'll have
-  // the record_id and we need to add that to our values array.
+  // Write the record for the base table.  First get the values for this table
+  // and set the record_id (update) or the type_id (insert)
   $values = $field_vals[$base_table][0];
   if ($record_id) {
     $values[$base_pkey] = $record_id;
   }
+  else {
+    $values[$type_field] = $cvterm->cvterm_id;
+  }
   $base_record_id = tripal_chado_field_storage_write_table($base_table, $values);
 
   // If this is an insert then add the chado_entity record.
@@ -53,7 +57,7 @@ function tripal_chado_field_storage_write($entity_type, $entity, $op, $fields) {
     // fields can be pulled from Chado when loaded the next time.
     $record = array(
       'entity_id' => $entity->id,
-      'record_id' => $record_id,
+      'record_id' => $base_record_id,
       'data_table' => $base_table,
       'type_table' => $base_table,
       'field' => $type_field,
@@ -140,14 +144,14 @@ function tripal_chado_field_storage_write_table($table_name, $values) {
 
    // If the primary key column has a value then this will be an udpate,
    // otherwise it's an insert.
-   if (!$values[$pkey]) {
+   if (!array_key_exists($pkey, $values) or !$values[$pkey]) {
      // Before inserting, we want to make sure the record does not
      // already exist.  Using the unique constraint check for a matching record.
      $options = array('is_duplicate' => TRUE);
      $is_duplicate = chado_select_record($table_name, array('*'), $values, $options);
      if($is_duplicate) {
        $record = chado_select_record($table_name, array('*'), $values);
-       return $record[0]->$pkey_field;
+       return $record[0]->$pkey;
      }
 
      // Insert the values array as a new record in the table.
@@ -155,7 +159,7 @@ function tripal_chado_field_storage_write_table($table_name, $values) {
      if ($record === FALSE) {
        throw new Exception('Could not insert Chado record into table: "' . $tablename . '".');
      }
-     return $record_id = $record[$pkey_field];
+     return $record[$pkey];
    }
    // We have an incoming record_id so this is an update.
    else {