Browse Source

Added support to chado views fk filter to allow user to select which fields to display to the user.

Lacey Sanderson 9 years ago
parent
commit
44ae61c43c
1 changed files with 40 additions and 18 deletions
  1. 40 18
      tripal_chado/includes/chado_views_handler_filter.inc

+ 40 - 18
tripal_chado/includes/chado_views_handler_filter.inc

@@ -171,7 +171,12 @@ class chado_views_handler_filter_fk extends views_handler_filter {
   
   function get_select_options() {
 
-    $name_field = 'common_name';
+    if (isset($this->options['display_column'])) {
+      $name_field = $this->options['display_column'];
+    } 
+    else {
+      $name_field = 'name';
+    }
         
     // Using a "Loose Index Scan" to get a list of all the unique values for
     // the name in the table referenced by the foreign key constraint.
@@ -236,7 +241,7 @@ class chado_views_handler_filter_fk extends views_handler_filter {
       ),
       '#default_value' => $this->options['values_form_type'],
     );
-
+    
     // @todo: implement.
     //$form['show_all'] = array(
       //'#type' => 'checkbox',
@@ -260,6 +265,23 @@ class chado_views_handler_filter_fk extends views_handler_filter {
       '#default_value' => $this->options['select_optional'],
     );
 
+    // Allow the user to select which column to display to the user.
+    // For example, if the foreign key points to the organism table you might want to
+    // display the abbreviation or common name to the user.
+    $table_defn = chado_get_schema($this->definition['foreign_key']['right_table']);
+    $options = array();
+    foreach ($table_defn['fields'] as $name => $details) {
+      $options[$name] = $name;
+    }
+    $form['display_column'] = array(
+      '#type' => 'select',
+      '#title' => 'Display Column',
+      '#description' => t('Specify which column from the parent table you would like to
+        display to the user.'),
+      '#options' => $options,
+      '#default_value' => $this->options['display_column'],
+    );
+    
     $form['max_length'] = array(
       '#type' => 'textfield',
       '#title' => t('Max Width'),
@@ -267,11 +289,6 @@ class chado_views_handler_filter_fk extends views_handler_filter {
       '#default_value' => $this->options['max_length'],
     );
 
-    $form['note'] = array(
-      '#type' => 'markup',
-      '#value' => t('<strong><font color="red">Note:</font></strong> If another filter exists for the same table then ' . 'the values shown in the drop box will only include those from rows that are not filtered.'),
-    );
-
     return $form;
 
   }
@@ -283,21 +300,11 @@ class chado_views_handler_filter_fk extends views_handler_filter {
     $this->options['values_form_type'] = $form_state['values']['options']['values_form_type'];
     $this->options['select_multiple'] = $form_state['values']['options']['select_multiple'];
     $this->options['select_optional'] = $form_state['values']['options']['select_optional'];
+    $this->options['display_column'] = $form_state['values']['options']['display_column'];
     $this->options['max_length'] = $form_state['values']['options']['max_length'];
     $this->options['show_all'] = $form_state['values']['options']['show_all'];
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  function extra_options_options() {
-    $this->options['values_form_type'] = 'textfield';
-    $this->options['select_multiple'] = FALSE;
-    $this->options['select_optional'] = FALSE;
-    $this->options['max_length'] = 40;
-    $this->options['show_all'] = FALSE;
-  }
-
   /**
    * {@inheritdoc}
    */
@@ -323,6 +330,21 @@ class chado_views_handler_filter_fk extends views_handler_filter {
       'bool' => TRUE,
       'export' => TRUE,
     );
+    
+    // display column.
+    $table_defn = chado_get_schema($this->definition['foreign_key']['right_table']);
+    $fields = array_keys($table_defn['fields']);
+    $name_fields = preg_grep('/name/', $fields);
+    if ($name_fields) {
+      $default = array_pop($name_fields);
+    }
+    else {
+      $default = array_pop($fields);
+    }
+    $options['display_column'] = array(
+      'export' => TRUE,
+      'default' => $default,
+    );
     $options['max_length'] = array(
       'default' => 40,
       'export' => TRUE,