Browse Source

prop linker field now working in all cases

Stephen Ficklin 7 years ago
parent
commit
fd97d60d4d

+ 21 - 0
tripal_chado/includes/TripalFields/chado_linker__prop/chado_linker__prop.inc

@@ -130,6 +130,7 @@ class chado_linker__prop extends ChadoField {
   public function query($query, $condition) {
     $prop_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 = $this->field['field_name'];
@@ -145,4 +146,24 @@ class chado_linker__prop extends ChadoField {
     $query->condition("$alias.value", $condition['value'], $operator);
 
   }
+
+  /**
+   * @see ChadoField::query()
+   */
+  public function queryOrder($query, $order) {
+    $prop_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 = $this->field['field_name'];
+
+    $vocab = $this->instance['settings']['term_vocabulary'];
+    $accession = $this->instance['settings']['term_accession'];
+
+    $cvterm = tripal_get_cvterm(array('id' => $vocab . ':' . $accession));
+
+    $this->queryJoinOnce($query, $prop_linker, $alias, "base.$bpkey = $alias.$bpkey AND $alias.type_id = $cvterm->cvterm_id", "LEFT OUTER");
+    $query->orderBy("$alias.value", $order['direction']);
+  }
 }

+ 33 - 27
tripal_chado/includes/TripalFields/chado_linker__prop/chado_linker__prop_widget.inc

@@ -17,13 +17,13 @@ class chado_linker__prop_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 name of the pkey field for this property table and the name
     // of the FK field 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']);
     $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
@@ -39,22 +39,22 @@ class chado_linker__prop_widget extends ChadoFieldWidget {
     // 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);
-      $type_id = tripal_get_field_item_keyval($items, 0, 'chado-' . $chado_table . '__type_id', $type_id);
+      $fk_value = tripal_get_field_item_keyval($items, 0, 'chado-' . $field_table . '__' . $lfkey_field, $fk_value);
+      $type_id = tripal_get_field_item_keyval($items, 0, 'chado-' . $field_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);
+        $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
+        $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
+        $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
       }
     }
 
     // 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];
-      $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__type_id'];
-      $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__value'];
-      $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__rank'];
+      $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
+      $fk_value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $lfkey_field];
+      $type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
+      $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
+      $rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
     }
 
     // Use default value for the field if it's not already set
@@ -80,25 +80,25 @@ class chado_linker__prop_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' => 'hidden',
       '#default_value' => $record_id,
     );
-    $widget['chado-' . $chado_table . '__' . $lfkey_field] = array(
+    $widget['chado-' . $field_table . '__' . $lfkey_field] = array(
       '#type' => 'hidden',
       '#value' => $fk_value,
     );
-    $widget['chado-' . $chado_table . '__value'] = array(
+    $widget['chado-' . $field_table . '__value'] = array(
       '#type' => 'textarea',
       '#default_value' => $value,
       '#title' => $instance['label'],
       '#description' => $instance['description'],
     );
-    $widget['chado-' . $chado_table . '__type_id'] = array(
+    $widget['chado-' . $field_table . '__type_id'] = array(
       '#type' => 'hidden',
       '#value' => $type_id,
     );
-    $widget['chado-' . $chado_table . '__rank'] = array(
+    $widget['chado-' . $field_table . '__rank'] = array(
       '#type' => 'hidden',
       '#value' => $rank,
     );
@@ -108,26 +108,32 @@ class chado_linker__prop_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'];
-    $chado_table = $this->instance['settings']['chado_table'];
-    $schema = chado_get_schema($chado_table);
+    $field_type = $this->field['type'];
+    $base_table = $this->instance['settings']['base_table'];
+    $field_table = $this->instance['settings']['chado_table'];
+    $chado_column = $this->instance['settings']['chado_column'];
+    $instance = $this->instance;
+
+    $schema = chado_get_schema($field_table);
     $pkey = $schema['primary key'][0];
     $base_table = $this->instance['settings']['base_table'];
     $lfkey_field = key($schema['foreign keys'][$base_table]['columns']);
     $rfkey_field = $schema['foreign keys'][$base_table]['columns'][$lfkey_field];
 
-    $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__value'];
+    $value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_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.
+    // fields except the pkey value. If the pkey field is present and the value
+    // is present then the chado storage backend will delete the record.
     if (!$value) {
-      $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__' . $pkey] = '';
-      $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__' . $lfkey_field] = '';
-      $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__type_id'] = '';
-      $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__value'] = '';
-      $form_state['values'][$field_name]['und'][$delta]['chado-' . $chado_table . '__rank'] = '';
+      $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $lfkey_field] = '';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'] = '';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'] = '';
+      $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'] = '';
     }
   }
 }

+ 2 - 2
tripal_chado/includes/tripal_chado.field_storage.inc

@@ -628,8 +628,8 @@ function tripal_chado_field_storage_query($query) {
     } // end if ($sort['type'] == 'field') {
   } // end foreach ($query->order as $index => $sort) {
 
-//   dpm($cquery->__toString());
-//   dpm($cquery->getArguments());
+//    dpm($cquery->__toString());
+//    dpm($cquery->getArguments());
 
   $records = $cquery->execute();