Procházet zdrojové kódy

Fixed dbxref field/widget insert/update issues

Stephen Ficklin před 9 roky
rodič
revize
0f342ae57c

+ 1 - 7
tripal_core/api/tripal_core.chado_query.api.inc

@@ -433,16 +433,14 @@ function chado_insert_record($table, $values, $options = array()) {
   $ifields = array();       // contains the names of the fields
   $itypes  = array();       // contains placeholders for the sql query
   $ivalues = array();       // contains the values of the fields
-  $i = 1;
   foreach ($insert_values as $field => $value) {
     $ifields[] = $field;
-    $ivalues[":$field"] = $value;
-    $i++;
     if (strcmp($value, '__NULL__')==0) {
       $itypes[] = "NULL";
     }
     else {
       $itypes[] = ":$field";
+      $ivalues[":$field"] = $value;
     }
   }
 
@@ -1029,7 +1027,6 @@ function chado_delete_record($table, $match, $options = NULL) {
  * @ingroup tripal_chado_query_api
  */
 function chado_select_record($table, $columns, $values, $options = NULL) {
-
   // Set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements.
   if (!is_array($options)) {
@@ -1094,7 +1091,6 @@ function chado_select_record($table, $columns, $values, $options = NULL) {
   $where = array();
   $args = array();
 
-  // if the 'is_duplicate' option is turned on then we want to remove all but unique keys
   if ($options['is_duplicate'] and array_key_exists('unique keys', $table_desc)) {
     $ukeys = $table_desc['unique keys'];
     $has_results = 0;
@@ -1111,7 +1107,6 @@ function chado_select_record($table, $columns, $values, $options = NULL) {
       $uq_sname = "uq_" . $table . "_";
       $has_pkey = 0;
 
-
       // include the primary key in the results returned
       if (array_key_exists('primary key', $table_desc)) {
         $has_pkey = 1;
@@ -1161,7 +1156,6 @@ function chado_select_record($table, $columns, $values, $options = NULL) {
           return FALSE;
         }
       }
-
       $results = chado_select_record($table, $new_columns, $new_values, $new_options);
       // if we have a duplicate record then return the results
       if (count($results) > 0) {

+ 12 - 7
tripal_entities/includes/TripalEntityUIController.inc

@@ -289,7 +289,8 @@ function tripal_entity_form_ajax_callback($form, $form_state) {
  */
 function tripal_entity_form_validate($form, &$form_state) {
 
-  if ($form_state['clicked_button']['#name'] == 'add_data') {
+  if (array_key_exists('clicked_button', $form_state) and
+      $form_state['clicked_button']['#name'] == 'add_data') {
     $tripal_entity = (object) $form_state['values'];
     $entity_type = $form_state['values']['type'];
     field_attach_form_validate($entity_type, $tripal_entity, $form, $form_state);
@@ -382,15 +383,19 @@ function tripal_entity_delete_form_submit($form, &$form_state) {
  * @see hook_entity_view_alter()
  */
 function tripal_entity_view($entity, $view_mode = 'full') {
-  $controller = entity_get_controller($entity->type);
-  $content = $controller->view(array($entity->id => $entity));
-  drupal_set_title($entity->title);
-  return $content;
+  if ($entity) {
+    $controller = entity_get_controller($entity->type);
+    $content = $controller->view(array($entity->id => $entity));
+    drupal_set_title($entity->title);
+    return $content;
+  }
 }
 
 /**
  * Menu title callback for showing individual entities
  */
-function tripal_entity_title(TripalEntity $entity){
-  return $entity->title;
+function tripal_entity_title($entity){
+  if ($entity) {
+    return $entity->title;
+  }
 }

+ 0 - 1
tripal_entities/includes/tripal_entities.chado_entity.inc

@@ -85,7 +85,6 @@ function tripal_entities_field_widget_form_alter(&$element, &$form_state, $conte
         $schema['fields'][$colname]['type'] == 'datetime') {
         $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s");
         $element['#date_items']['value'] = $element['#default_value']['value'];
-        $form_state['rebuild'] = TRUE;
       }
     }
   }

+ 1 - 1
tripal_entities/includes/tripal_entities.field_storage.inc

@@ -115,7 +115,7 @@ function tripal_entities_field_storage_write_recursive($entity_type, $entity,
       // exists in the $field_vals;
       $fk_val = tripal_entities_field_storage_write_recursive($entity_type,
         $entity, $op, $field_vals, $fk_table, NULL, $fk_val, $depth + 1);
-      if (isset($fk_val) and $fk_val != '') {
+      if (isset($fk_val) and $fk_val != '' and $fk_val != 0) {
         $values[$local_id] = $fk_val;
       }
     }

+ 24 - 5
tripal_entities/includes/tripal_entities.fields.inc

@@ -222,7 +222,6 @@ function tripal_entities_field_widget_form(&$form, &$form_state, $field,
       if (count($temp) > 0) {
         $db_id = $temp[0];
       }
-      drupal_debug(array($field_name, $temp, $db_id));
 
       $schema = chado_get_schema('dbxref');
       $options = tripal_get_db_select_options();
@@ -260,6 +259,12 @@ function tripal_entities_field_widget_form(&$form, &$form_state, $field,
             '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
             '#size' => 15,
             '#autocomplete_path' => "admin/tripal/chado/tripal_db/dbxref/auto_name/$db_id",
+            '#ajax' => array(
+              'callback' => "tripal_entities_primary_dbxref_widget_form_ajax_callback",
+              'wrapper' => "$field_name-dbxref--db-id",
+              'effect' => 'fade',
+              'method' => 'replace'
+            )
           ),
           'dbxref__version' => array(
             '#type' => 'textfield',
@@ -441,6 +446,9 @@ function theme_tripal_entities_primary_dbxref_widget($variables) {
  */
 function tripal_entities_get_field_form_values($field_name, $form_state, $child = NULL) {
   $values = array();
+  if (!array_key_exists('values', $form_state)) {
+    return $values;
+  }
   if (array_key_exists($field_name, $form_state['values'])) {
     foreach ($form_state['values'][$field_name] as $langcode => $items) {
       foreach ($items as $delta => $value) {
@@ -478,8 +486,19 @@ function tripal_entities_set_field_form_values($field_name, &$form_state, $newva
  * An Ajax callback for the tripal_entities_admin_publish_form..
  */
 function tripal_entities_primary_dbxref_widget_form_ajax_callback($form, $form_state) {
-  // return the form so Drupal can update the content on the page
-//  return $form['][', $element ['#parents']) . '][0][dbxref__accession',
-drupal_debug($form_state['triggering_element']);
-  return $form[$form_state['triggering_element']['#parents'][0]];
+  $field_name = $form_state['triggering_element']['#parents'][0];
+  $db_id = tripal_entities_get_field_form_values($field_name, $form_state, 'dbxref__db_id');
+  $accession = tripal_entities_get_field_form_values($field_name, $form_state, 'dbxref__accession');
+  if (count($db_id) > 0 and count($accession) > 0) {
+    $values = array(
+      'db_id' => $db_id[0],
+      'accession' => $accession[0],
+    );
+    $options = array('is_duplicate' => TRUE);
+    $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
+    if (!$has_duplicate) {
+      drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
+    }
+  }
+  return $form[$field_name];
 }

+ 6 - 3
tripal_entities/tripal_entities.module

@@ -183,7 +183,7 @@ function tripal_entities_entity_info() {
       'access callback' => 'tripal_entity_access',
 
       // FALSE disables caching. Caching functionality is handled by Drupal core.
-      'static cache' => FALSE,
+      //'static cache' => FALSE,
 
       // Bundles are added in the hook_entities_info_alter() function.
       'bundles' => array (),
@@ -367,8 +367,11 @@ function tripal_entity_load($id, $reset = FALSE) {
     ->fetchField();
 
   // Load the entity.
-  $entity = entity_load($entity_type, array($id), array(), $reset);
-  return reset($entity);
+  if ($entity_type) {
+    $entity = entity_load($entity_type, array($id), array(), $reset);
+    return reset($entity);
+  }
+  return FALSE;
 }
 
 /**