Browse Source

Fixed bug for chado_linker_prop and sbo_relationship fields

Chun-Huai Cheng 8 years ago
parent
commit
7a3d8865a0

+ 2 - 2
tripal_chado/api/tripal_chado.schema.api.inc

@@ -89,9 +89,9 @@ class chado_linker__prop extends ChadoField {
     $entity->{$field_name}['und'][0] = array(
       'value' => '',
       'chado-' . $field_table . '__' . $pkey => '',
-      'chado-' . $field_table . '__' . $fkey_lcolumn => '',
+      'chado-' . $field_table . '__' . $fkey_lcolumn => $chado_record->$fkey_lcolumn,
       'chado-' . $field_table . '__value' => '',
-      'chado-' . $field_table . '__type_id' => '',
+      'chado-' . $field_table . '__type_id' => $cvterm_id,
       'chado-' . $field_table . '__rank' => '',
     );
 

+ 14 - 7
tripal_chado/includes/TripalFields/chado_linker__prop/chado_linker__prop_widget.inc

@@ -37,13 +37,15 @@ class chado_linker__prop_widget extends ChadoFieldWidget {
 
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
-    if (count($items) > 0 and array_key_exists($delta, $items)) {
+    if (count($items) > 0) {
       // Check for element values that correspond to fields in the Chado table.
-      $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__' . $pkey, $record_id);
-      $fk_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__' . $lfkey_field, $fk_value);
-      $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__type_id', $type_id);
-      $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__value', $value);
-      $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__rank', $rank);
+      $fk_value = tripal_get_field_item_keyval($items, 0, 'chado-' . $chado_table . '__' . $lfkey_field, $fk_value);
+      $type_id = tripal_get_field_item_keyval($items, 0, 'chado-' . $chado_table . '__type_id', $type_id);      
+      if (array_key_exists($delta, $items)) {
+        $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__' . $pkey, $record_id);
+        $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__value', $value);
+        $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__rank', $rank);
+      }
     }
 
     // Check $form_state['values'] to see if an AJAX call set the values.
@@ -55,6 +57,11 @@ class chado_linker__prop_widget extends ChadoFieldWidget {
       $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__rank'];
     }
 
+    // Use default value for the field if it's not already set
+    if (!$value && isset($instance['default_value'][$delta])) {
+      $value = $instance['default_value'][$delta]['chado-' . $chado_table . '__value'];
+    }
+    
     $widget['value'] = array(
       '#type' => 'value',
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
@@ -99,7 +106,7 @@ class chado_linker__prop_widget extends ChadoFieldWidget {
 
     $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__value'];
     $form_state['values'][$field_name]['und'][$delta]['value'] = $value;
-
+    
     // If the user removed the property then we want to clear out the other
     // fields so there is no insert.
     if (!$value) {

+ 9 - 12
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc

@@ -649,21 +649,18 @@ class sbo__relationship extends ChadoField {
     $schema = chado_get_schema($field_table);
     $fkeys = $schema['foreign keys'];
 
-    // Handle special cases
+    // 'nd_reagent_relationship' and 'project_relationship' have different column names from
+    // subject_id/object_id. Do a pattern matching to get the column names.
     $subject_id_key = 'subject_id';
     $object_id_key = 'object_id';
-    if ($field_table == 'nd_reagent_relationship') {
-      $subject_id_key = 'subject_reagent_id';
-      $object_id_key = 'object_reagent_id';
-    }
-    else if ($field_table == 'project_relationship') {
-      $subject_id_key = 'subject_project_id';
-      $object_id_key = 'object_project_id';
+    foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
+      if (preg_match('/^subject_.*id/', $lcolum)) {
+        $subject_id_key = $lcolum;
+      }
+      else if (preg_match('/^object_.*id/', $lcolum)) {
+        $object_id_key = $lcolum;
+      }
     }
-//     else if ($field_table == 'organism_relationship') {
-//       $subject_id_key = 'subject_organism_id';
-//       $object_id_key = 'object_organism_id';
-//     }
 
     foreach ($items as $delta => $item) {
       $subject_id = $item['chado-' . $field_table . '__' . $subject_id_key];

+ 18 - 24
tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship_widget.inc

@@ -64,21 +64,18 @@ class sbo__relationship_widget extends ChadoFieldWidget {
     $object_uniquename = '';
     $type = '';
 
-    // Handle special cases
+    // 'nd_reagent_relationship' and 'project_relationship' have different column names from
+    // subject_id/object_id. Do a pattern matching to get the column names.
     $subject_id_key = 'subject_id';
     $object_id_key = 'object_id';
-    if ($field_table == 'nd_reagent_relationship') {
-      $subject_id_key = 'subject_reagent_id';
-      $object_id_key = 'object_reagent_id';
-    }
-    else if ($field_table == 'project_relationship') {
-      $subject_id_key = 'subject_project_id';
-      $object_id_key = 'object_project_id';
+    foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
+      if (preg_match('/^subject_.*id/', $lcolum)) {
+        $subject_id_key = $lcolum;
+      }
+      else if (preg_match('/^object_.*id/', $lcolum)) {
+        $object_id_key = $lcolum;
+      }
     }
-//     else if ($field_table == 'organism_relationship') {
-//       $subject_id_key = 'subject_organism_id';
-//       $object_id_key = 'object_organism_id';
-//     }
 
     // If the field already has a value then it will come through the $items
     // array.  This happens when editing an existing record.
@@ -346,21 +343,18 @@ class sbo__relationship_widget extends ChadoFieldWidget {
     $schema = chado_get_schema($field_table);
     $fkeys = $schema['foreign keys'];
 
-    // Handle special cases
+    // 'nd_reagent_relationship' and 'project_relationship' have different column names from
+    // subject_id/object_id. Do a pattern matching to get the column names.
     $subject_id_key = 'subject_id';
     $object_id_key = 'object_id';
-    if ($field_table == 'nd_reagent_relationship') {
-      $subject_id_key = 'subject_reagent_id';
-      $object_id_key = 'object_reagent_id';
-    }
-    else if ($field_table == 'project_relationship') {
-      $subject_id_key = 'subject_project_id';
-      $object_id_key = 'object_project_id';
+    foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
+      if (preg_match('/^subject_.*id/', $lcolum)) {
+        $subject_id_key = $lcolum;
+      }
+      else if (preg_match('/^object_.*id/', $lcolum)) {
+        $object_id_key = $lcolum;
+      }
     }
-//     else if ($field_table == 'organism_relationship') {
-//       $subject_id_key = 'subject_organism_id';
-//       $object_id_key = 'object_organism_id';
-//     }
 
     $voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
     $type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';

+ 3 - 0
tripal_chado/includes/tripal_chado.fields.inc

@@ -1619,6 +1619,9 @@ function tripal_chado_bundle_create_user_field($new_field, $bundle) {
           'base_table' => $chado_table,
           'chado_table' => $table_name,
           'chado_column' => $pkey,
+          'term_vocabulary' => $vocab->vocabulary,
+          'term_accession' => $term->accession,
+          'term_name' => $term->name
         ),
         'widget' => array(
           'type' => 'chado_linker__prop_widget',