Browse Source

Fixing bugs in contact linker field

Stephen Ficklin 7 years ago
parent
commit
b4d5fa6ba0

+ 1 - 1
tripal/includes/TripalFields/TripalField.inc

@@ -654,7 +654,7 @@ class TripalField {
   }
 
   /**
-   * Afte a field instance is created the following function is run.
+   * After a field instance is created the following function is run.
    *
    * This function is equivalent to the hook_field_create_field() hook of
    * the Drupal Field API. This function is invoked after a new field

+ 1 - 1
tripal/includes/TripalFields/TripalFieldWidget.inc

@@ -113,7 +113,7 @@ class TripalFieldWidget {
   }
 
   /**
-   * Performs validation of the widgetForm.
+   * Performs validation of the widget form.
    *
    * Use this validate to ensure that form values are entered correctly.
    * The 'value' key of this field must be set in the $form_state['values']

+ 3 - 1
tripal/includes/tripal.fields.inc

@@ -694,9 +694,11 @@ function tripal_field_instance_settings_form_submit($form, &$form_state) {
 function tripal_field_widget_form_validate($element, &$form_state, $form) {
   $field = $element['#field'];
   $instance = $element['#instance'];
-  $widget_class = $element['#field_name'] . '_widget';
+
   $langcode = $element['#language'];
   $delta = $element['#delta'];
+
+  $widget_class = $instance['widget']['type'];
   tripal_load_include_field_class($widget_class);
   if (class_exists($widget_class)) {
     $widget = new $widget_class($field, $instance);

+ 9 - 4
tripal/tripal_views_query.inc

@@ -137,10 +137,12 @@ class tripal_views_query extends views_plugin_query {
       if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
          $field_name = $matches[1];
          $element_name = $matches[2];
-         if (tripal_load_include_field_class($field_name)) {
-           $field = field_info_field($field_name);
-           $instance = field_info_instance('TripalEntity', $field_name, $this->query->entityConditions['bundle']['value']);
-           $field_obj = new $field_name($field, $instance);
+         $field = field_info_field($field_name);
+         $instance = field_info_instance('TripalEntity', $field_name, $this->query->entityConditions['bundle']['value']);
+         $field_class = $field['type'];
+         if (tripal_load_include_field_class($field_class)) {
+
+           $field_obj = new $field_class($field, $instance);
            $element_name = $field_obj->getFieldTermID() . ',' . $element_name;
            // Replace periods with commas.
            $element_name = preg_replace('/\./', ',', $element_name);
@@ -252,6 +254,9 @@ class tripal_views_query extends views_plugin_query {
         $this->pager->pre_execute($query);
         $num_items_per_page = $this->pager->get_items_per_page();
         $offset = $this->pager->get_current_page() * $num_items_per_page;
+        // I'm not sure why an offset would come back as -1 but it has happened
+        // with Drupal Views.  This is a quick band-aid fix.
+        $offset = ($offset < 0) ? 0 : $offset;
         $query->range($offset, $num_items_per_page);
 
         // Get the IDs

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

@@ -1670,7 +1670,7 @@ function chado_query($sql, $args = array()) {
           chado_set_active($previous_db);
         }
         catch (Exception $e2) {
-          throw Exception($e->getMessage() . $e2->getMessage());
+          throw new Exception($e->getMessage() . $e2->getMessage());
         }
       }
     }

+ 76 - 14
tripal_chado/includes/TripalFields/chado_linker__contact/chado_linker__contact.inc

@@ -61,8 +61,8 @@ class chado_linker__contact extends ChadoField {
    * @see TripalField::elements()
    */
   public function elementInfo() {
-    $field_term = $this->getFieldTermID();
 
+    $field_term = $this->getFieldTermID();
     $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
     $name_term = tripal_get_chado_semweb_term('contact', 'name');
     $description_term = tripal_get_chado_semweb_term('contact', 'description');
@@ -96,7 +96,7 @@ class chado_linker__contact extends ChadoField {
             'label' => 'Contact Description',
             'help' => 'A descriptoin of the contact.',
             'operations' => array('contains'),
-            'sortable' => FALSE,
+            'sortable' => TRUE,
           ),
           'entity' => array(
             'searchable' => FALSE,
@@ -116,6 +116,7 @@ class chado_linker__contact extends ChadoField {
     $field_type = $this->field['type'];
     $field_table = $this->instance['settings']['chado_table'];
     $field_column = $this->instance['settings']['chado_column'];
+    $base_table = $this->instance['settings']['base_table'];
 
     $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
     $name_term = tripal_get_chado_semweb_term('contact', 'name');
@@ -123,7 +124,6 @@ class chado_linker__contact extends ChadoField {
 
     // Get the FK that links to the base record.
     $schema = chado_get_schema($field_table);
-    $base_table = $entity->chado_table;
     $pkey = $schema['primary key'][0];
     if (!isset($schema['foreign keys'][$base_table]['columns'])) {
       return;
@@ -184,28 +184,90 @@ class chado_linker__contact extends ChadoField {
    * @see ChadoField::query()
    */
   public function query($query, $condition) {
-    $contact_linker = $this->instance['settings']['chado_table'];
-    $base_table = $this->instance['settings']['base_table'];
-    $bschema = chado_get_schema($base_table);
-    $bpkey = $bschema['primary key'][0];
-    $alias = 'contact_linker';
+    $alias = $this->field['field_name'];
     $operator = $condition['operator'];
 
-    $type_term = tripal_get_chado_semweb_term('contact', 'type');
+    $field_term_id = $this->getFieldTermID();
+    $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
     $name_term = tripal_get_chado_semweb_term('contact', 'name');
     $description_term = tripal_get_chado_semweb_term('contact', 'description');
 
-    if ($condition['column'] == $name_term) {
-      $this->queryJoinOnce($query, $contact_linker, $alias, "base.$bpkey = $alias.$bpkey");
-      $this->queryJoinOnce($query, 'contact', 'C', "C.contact_id = $alias.contact_id");
-      $query->condition("C.name", $condition['value'], $operator);
+    $contact_linker = $this->instance['settings']['chado_table'];
+    $base_table = $this->instance['settings']['base_table'];
+
+    // Get the FK that links to the base record.
+    $schema = chado_get_schema($contact_linker);
+    $pkey = $schema['primary key'][0];
+    $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
+
+    // Join the contact linker table and then join the contact table.
+    $calias = $contact_linker . '_contact';
+    $this->queryJoinOnce($query, $contact_linker, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
+    $this->queryJoinOnce($query, 'contact', $calias, "$calias.contact_id = $alias.contact_id");
+
+    // Search by the contact name
+    if ($condition['column'] == $field_term_id or
+        $condition['column'] == $field_term_id . ',' . $name_term) {
+      $query->condition("$calias.name", $condition['value'], $operator);
+    }
+    // Search on the contact description.
+    if ($condition['column'] == $field_term_id . ',' . $description_term) {
+      $query->condition("$calias.description", $condition['value'], $operator);
     }
-    if ($condition['column'] == 'contact.type') {
+    // Search on the contact type.
+    if ($condition['column'] == $field_term_id . ',' . $type_term) {
+      $talias = $contact_linker . '_contact_type';
+      $this->queryJoinOnce($query, 'cvterm', $talias, "$calias.type_id = $talias.cvterm_id");
+      $query->condition("$talias.name", $condition['value'], $operator);
+    }
+  }
 
+  /**
+   * @see ChadoField::queryOrder()
+   */
+  public function queryOrder($query, $order) {
+    $alias = $this->field['field_name'];
+    $field_term_id = $this->getFieldTermID();
+    $type_term = tripal_get_chado_semweb_term('contact', 'type_id');
+    $name_term = tripal_get_chado_semweb_term('contact', 'name');
+    $description_term = tripal_get_chado_semweb_term('contact', 'description');
+
+    $contact_linker = $this->instance['settings']['chado_table'];
+    $base_table = $this->instance['settings']['base_table'];
+
+    // Get the FK that links to the base record.
+    $schema = chado_get_schema($contact_linker);
+    $base_table = $entity->chado_table;
+    $pkey = $schema['primary key'][0];
+    $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
+
+    // Join the contact linker table and then join the contact table.
+    $calias = $contact_linker . '_contact';
+    $this->queryJoinOnce($query, $contact_linker, $alias, "base.$fkey_rcolumn = $alias.$fkey_lcolumn");
+    $this->queryJoinOnce($query, 'contact', $calias, "$calias_contact.contact_id = $alias.contact_id");
+
+    // Search by the contact name
+    if ($condition['column'] == $field_term_id or
+        $condition['column'] == $field_term_id . ',' . $name_term) {
+      $query->orderBy("$calias.name", $order['direction']);
+    }
+    // Search on the contact description.
+    if ($condition['column'] == $field_term_id . ',' . $description_term) {
+      $query->condition("$calias.description", $condition['value'], $operator);
+    }
+    // Search on the contact type.
+    if ($condition['column'] == $field_term_id . ',' . $type_term) {
+      $talias = $contact_linker . '_contact_type';
+      $this->queryJoinOnce($query, 'cvterm', $talias, "$calias_contact.type_id = $talias.cvterm_id");
+      $query->condition("$talias.name", $condition['value'], $operator);
     }
   }
 }
 
+
+
 /**
  * An Ajax callback for the pub widget.
  */

+ 29 - 32
tripal_chado/includes/TripalFields/chado_linker__contact/chado_linker__contact_widget.inc

@@ -17,39 +17,37 @@ class chado_linker__contact_widget extends ChadoFieldWidget {
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
     $base_table = $this->instance['settings']['base_table'];
-    $chado_table = $this->instance['settings']['chado_table'];
+    $field_table = $this->instance['settings']['chado_table'];
     $chado_column = $this->instance['settings']['chado_column'];
     $instance = $this->instance;
 
     // Get the FK column that links to the base table.
-    $schema = chado_get_schema($chado_table);
+    $schema = chado_get_schema($field_table);
     $pkey = $schema['primary key'][0];
-    $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
+    $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
 
     // Get the field defaults.
     $record_id = '';
-    $fk_value = array_key_exists('#entity', $element) and $element['#entity'] ? $element['#entity']->chado_record_id : NULL;
+    $fk_value = (array_key_exists('#entity', $element) and $element['#entity']) ? $element['#entity']->chado_record_id : NULL;
     $contact_id = '';
     $name = '';
     $value = '';
 
+    $name_term = tripal_get_chado_semweb_term('contact', 'name');
 
     // 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) {
-      // Check for element values that correspond to fields in the Chado table.
-      $fk_value = tripal_get_field_item_keyval($items, 0, 'chado-' . $chado_table . '__' . $lfkey_field, $fk_value);
-      if (array_key_exists($delta, $items)) {
-        $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__' . $pkey, $record_id);
-        $contact_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $chado_table . '__contact_id', $contact_id);
-      }
+    if (count($items) > 0 and array_key_exists($delta, $items)) {
+      $name = array_key_exists($name_term, $items[$delta]['value']) ? $items[$delta]['value'][$name_term] : $name;
+      $fk_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $fkey_lcolumn, $fk_value);
+      $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
+      $contact_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__contact_id', $contact_id);
     }
 
     // Check $form_state['values'] to see if an AJAX call set the values.
     if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
-      $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__' . $pkey];
-      $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__' . $lfkey_field];
-      $contact_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__contact_id'];
+      $name = $form_state['values'][$field_name]['und'][$delta]['name'];
     }
 
 
@@ -60,15 +58,15 @@ class chado_linker__contact_widget extends ChadoFieldWidget {
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
 
-    $widget['chado-' . $chado_table . '__' . $pkey] = array(
+    $widget['chado-' . $field_table . '__' . $pkey] = array(
       '#type' => 'value',
       '#default_value' => $record_id,
     );
-    $widget['chado-' . $chado_table . '__' . $lfkey_field] = array(
+    $widget['chado-' . $field_table . '__' . $fkey_lcolumn] = array(
       '#type' => 'value',
       '#default_value' => $fk_value,
     );
-    $widget['chado-' . $chado_table . '__contact_id'] = array(
+    $widget['chado-' . $field_table . '__contact_id'] = array(
       '#type' => 'value',
       '#default_value' => $contact_id,
     );
@@ -78,12 +76,6 @@ class chado_linker__contact_widget extends ChadoFieldWidget {
       '#title' => t('Contact'),
       '#default_value' => $name,
       '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/contact',
-      '#ajax' => array(
-        'callback' => "chado_linker__contact_widget_form_ajax_callback",
-        'wrapper' => "$chado_table-$delta",
-        'effect' => 'fade',
-        'method' => 'replace'
-      ),
       '#maxlength' => 100000,
     );
   }
@@ -93,30 +85,35 @@ class chado_linker__contact_widget extends ChadoFieldWidget {
    *
    * @see TripalFieldWidget::submit()
    */
-  public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
+  public function validate($element, $form, &$form_state, $langcode, $delta) {
     $field_name = $this->field['field_name'];
     $field_type = $this->field['type'];
     $base_table = $this->instance['settings']['base_table'];
-    $chado_table = $this->instance['settings']['chado_table'];
+    $field_table = $this->instance['settings']['chado_table'];
     $chado_column = $this->instance['settings']['chado_column'];
     $instance = $this->instance;
-    $schema = chado_get_schema($chado_table);
 
+    // Get information about this contact linke rtable.
+    $schema = chado_get_schema($field_table);
     $pkey = $schema['primary key'][0];
     $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
 
-    // Get the field values.
-    $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__' . $pkey];
-    $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__' . $lfkey_field];
-    $contact_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__contact_id'];
+    // Get the name from the form state.
     $name = $form_state['values'][$field_name]['und'][$delta]['name'];
 
     // If the user provided a name then we want to set the foreign key
     // value to be the chado_record_id
     if ($name) {
       $contact = chado_generate_var('contact', array('name' => $name));
-      $form_state['values'][$field_name][$langcode][$delta]['chado-' . $chado_table . '__contact_id'] = $contact->contact_id;
-      $form_state['values'][$field_name][$langcode][$delta]['value'] = $name;
+      if ($contact) {
+        $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__contact_id'] = $contact->contact_id;
+        $form_state['values'][$field_name]['und'][$delta]['value'] = $name;
+      }
+    }
+    // If no name is provided then we want to set the field for deletion.
+    else {
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $lfkey_field] = '';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__contact_id'] = '';
     }
   }
 }

+ 1 - 8
tripal_chado/includes/TripalFields/obi__organism/obi__organism.inc

@@ -84,14 +84,7 @@ class obi__organism extends ChadoField {
 
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
-      'value' => array(
-        /*
-        // Types of elements that will appear in the value array.
-        $label_term => '',
-        $genus_term => '',
-        $species_term => '',
-        */
-      ),
+      'value' => array(),
     );
 
     if ($record) {

+ 0 - 7
tripal_chado/includes/TripalFields/sio__annotation/sio__annotation_formatter.inc

@@ -29,13 +29,6 @@ class sio__annotation_formatter extends ChadoFieldFormatter {
           $accession = l($accession, tripal_get_dbxref_url($dbxref), array('attributes' => array('target' => '_blank')));
         }
 
-        // Build the publication reference.
-        $pub_ref = '';
-        $pub_id = $item['chado-' . $chado_table . '__pub_id'];
-        if ($pub_id) {
-          $pub = chado_generate_var('pub', array('pub_id' => $pub_id));
-          $pub_ref = $pub->title;
-        }
         $rows[] = array(
           $accession,
           $cvterm->definition,

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

@@ -43,6 +43,7 @@ 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_write_merge_fields($fields, $entity_type, $entity);
+  //dpm($field_vals);
 
   // First, write the record for the base table.  If we have a record id then
   // this is an update and we need to set the primary key.  If not, then this