Browse Source

Views: fixed some bugs related to options for the cvterm and string select handlers

Lacey Sanderson 11 years ago
parent
commit
7fbd129d20

+ 86 - 5
tripal_views/views/handlers/tripal_views_handler_filter_select_cvterm.inc

@@ -61,11 +61,60 @@ class tripal_views_handler_filter_select_cvterm extends tripal_views_handler_fil
 
     }
     else {
+
+      $where = '';
+      if ($this->options['exposed']) {
+        // build a where clause that will filter the list in the drop box
+        // using fields that are not exposed and that are for the table
+        // from whcih the values in the drop box will be slected and
+        // we only want to use non-exposed fields because these are not
+        // available to the user to edit--they're fixed.
+        $filters = (is_array($this->view->filter)) ? $this->view->filter : array();
+        foreach ($filters as $filter_name => $details) {
+           // we only want to inclue non-exposed filters
+           if ($details->options['exposed'] == FALSE) {
+              $value = $details->value;
+              if (is_array($details->value) AND isset($details->value['value'])) {
+                $value = $details->value['value'];
+              }
+
+              $field = $details->field;
+              if (($this->table == $this->view->base_table) AND ($details->field == 'type_id')) {
+                $field = 'cvterm_id';
+              }
+
+              if (is_array($value)) {
+                // we only want to filter on the table we're getting the list from
+                if (strcmp($details->table, $this->table)==0 AND !empty($value)) {
+                  $where .= "$field IN (" . implode(', ', $value) . ')';
+                  $where .= ' AND ';
+                }
+              }
+              else {
+                // we only want to filter on the table we're getting the list from
+                if (strcmp($details->table, 'cvterm')==0 AND !empty($value)) {
+                  $where .= "$field $details->operator " . $value;
+                  $where .= ' AND ';
+                }
+              }
+           }
+        }
+        if ($where) {
+          $where = ' AND ' . $where;
+          $where = substr($where, 0, -5); # remove the final ' AND '
+        }
+      }
+
       // @coder-ignore: non-drupal schema therefore table prefixing does not apply
       // D7 TODO: Check DBTNG changes work
-      $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cvterm_id IN (SELECT distinct(" . $this->field . ") FROM {" . $this->table . "})";
+      $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cvterm_id IN (SELECT distinct(" . $this->field . ") FROM {" . $this->table . "}) " . $where . ' ORDER BY cvterm.name ASC';
       $resource = chado_query($sql);
       $cvterms = array();
+
+      if ($this->options['select_optional']) {
+        $cvterms['All'] = '- Any -';
+      }
+
       foreach ($resource as $r) {
         $cvterms[$r->cvterm_id] = $r->name;
       }
@@ -73,12 +122,44 @@ class tripal_views_handler_filter_select_cvterm extends tripal_views_handler_fil
     //sort cvterms by name (case insensitive)
     natcasesort($cvterms);
 
-    if ($this->options['expose']['select_optional']) {
-      $options['All'] = '--Any--';
-    }
+    return $cvterms;
+
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function options_definition() {
+    return parent::option_definition();
+  }
 
-    return $options + $cvterms;
+  /**
+   * {@inheritdoc}
+   */
+  function expose_form(&$form, &$form_state) {
+    parent::expose_form($form, $form_state);
+    return $form;
+  }
 
+  /**
+   * {@inheritdoc}
+   */
+  function expose_submit($form, &$form_state) {
+    parent::expose_submit($form, $form_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function expose_options() {
+    parent::expose_options();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function value_form(&$form, &$form_state) {
+    parent::value_form($form, $form_state);
   }
 
 }

+ 95 - 41
tripal_views/views/handlers/tripal_views_handler_filter_select_string.inc

@@ -12,27 +12,59 @@
  */
 class tripal_views_handler_filter_select_string extends views_handler_filter_string {
 
+  /**
+   * {@inheritdoc}
+   */
+  function init(&$view, &$options) {
+    parent::init($view, $options);
+
+    // Backwards compatibility
+    if (isset($this->options['expose']['values_form_type'])) {
+      $this->options['values_form_type'] = $this->options['expose']['values_form_type'];
+      unset($this->options['expose']['values_form_type']);
+    }
+    if (isset($this->options['expose']['select_multiple'])) {
+      $this->options['select_multiple'] = $this->options['expose']['select_multiple'];
+      unset($this->options['expose']['select_multiple']);
+    }
+    if (isset($this->options['expose']['select_optional'])) {
+      $this->options['select_optional'] = $this->options['expose']['select_optional'];
+      unset($this->options['expose']['select_optional']);
+    }
+    if (isset($this->options['expose']['max_length'])) {
+      $this->options['max_length'] = $this->options['expose']['max_length'];
+      unset($this->options['expose']['max_length']);
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function has_extra_options() {
+    return TRUE;
+  }
+
   /**
    * {@inheritdoc}
    */
   function options_definition() {
     $options = parent::option_definition();
 
-    $options['expose']['values_form_type'] = array(
+    $options['values_form_type'] = array(
       'default' => 'textfield',
       'export' => 'export_plugin'
     );
-    $options['expose']['select_multiple'] = array(
+    $options['select_multiple'] = array(
       'default' => FALSE,
       'bool' => TRUE,
       'export' => 'export_plugin'
     );
-    $options['expose']['select_optional'] = array(
+    $options['select_optional'] = array(
       'default' => FALSE,
       'bool' => TRUE,
       'export' => 'export_plugin'
     );
-    $options['expose']['max_length'] = array(
+    $options['max_length'] = array(
       'default' => 40,
       'export' => 'export_plugin'
     );
@@ -57,15 +89,30 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
     $where = '';
     $filters = (is_array($this->view->filter)) ? $this->view->filter : array();
     foreach ($filters as $filter_name => $details) {
-       // we only want to inclue non-exposed filters
-       if ($details->options['exposed'] == FALSE) {
-          $value = (is_array($details->value)) ? $details->value['value'] : $details->value;
+      // we only want to inclue non-exposed filters
+      if ($details->options['exposed'] == FALSE) {
+
+        $value = $details->value;
+        if (is_array($details->value) AND isset($details->value['value'])) {
+         $value = $details->value['value'];
+        }
+
+        if (is_array($value)) {
           // we only want to filter on the table we're getting the list from
           if (strcmp($details->table, $this->table)==0 AND !empty($value)) {
-            $where .= "$details->field $details->operator " . $details->value;
+            $where .= "$details->field IN (" . implode(', ', $value) . ')';
             $where .= ' AND ';
           }
-       }
+
+        }
+        else {
+          // we only want to filter on the table we're getting the list from
+          if (strcmp($details->table, $this->table)==0 AND !empty($value)) {
+            $where .= "$details->field $details->operator " . $value;
+            $where .= ' AND ';
+          }
+        }
+      }
     }
     if ($where) {
        $where = "WHERE $where";
@@ -77,12 +124,12 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
     $results = chado_query($sql);
 
     // Build the select box options
-    $max_length = (isset($this->options['expose']['max_length'])) ? $this->options['expose']['max_length'] : 40;
+    $max_length = (isset($this->options['max_length'])) ? $this->options['max_length'] : 40;
     if (!$max_length) {
       $max_length = 40;
     }
     $options = array();
-    if ($this->options['expose']['select_optional']) {
+    if ($this->options['select_optional']) {
       $options['All'] = '--Any--';
     }
     foreach ($results as $r) {
@@ -100,41 +147,41 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
   /**
    * {@inheritdoc}
    */
-  function expose_form(&$form, &$form_state) {
-    parent::expose_form($form, $form_state);
+  function extra_options_form(&$form, &$form_state) {
+    parent::extra_options_form($form, $form_state);
 
-    $form['expose']['values_form_type'] = array(
+    $form['values_form_type'] = array(
       '#type' => 'radios',
       '#title' => t('Filter Type'),
       '#options' => array(
         'textfield' => 'Text Field',
         'select' => 'Drop-Down Box',
       ),
-      '#default_value' => $this->options['expose']['values_form_type'],
+      '#default_value' => $this->options['values_form_type'],
     );
 
-    $form['expose']['select_multiple'] = array(
+    $form['select_multiple'] = array(
       '#type' => 'checkbox',
       '#title' => t('Select Multiple'),
       '#description' => t('Allows more then one option to be selected.'),
-      '#default_value' => $this->options['expose']['select_multiple'],
+      '#default_value' => $this->options['select_multiple'],
     );
 
-    $form['expose']['select_optional'] = array(
+    $form['select_optional'] = array(
       '#type' => 'checkbox',
       '#title' => t('Optional'),
       '#description' => t('Adds --Any-- to the available options.'),
-      '#default_value' => $this->options['expose']['select_optional'],
+      '#default_value' => $this->options['select_optional'],
     );
 
-    $form['expose']['max_length'] = array(
+    $form['max_length'] = array(
       '#type' => 'textfield',
       '#title' => t('Max Width'),
       '#description' => t('Specify the maximum width of the select box'),
-      '#default_value' => $this->options['expose']['max_length'],
-
+      '#default_value' => $this->options['max_length'],
     );
-    $form['expose']['note'] = array(
+
+    $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.'),
@@ -148,21 +195,21 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
   /**
    * {@inheritdoc}
    */
-  function expose_submit($form, &$form_state) {
-    $this->options['expose']['values_form_type'] = $form_state['values']['options']['expose']['values_form_type'];
-    $this->options['expose']['select_multiple'] = $form_state['values']['options']['expose']['select_multiple'];
-    $this->options['expose']['select_optional'] = $form_state['values']['options']['expose']['select_optional'];
-    $this->options['expose']['max_length'] = $form_state['values']['options']['expose']['max_length'];
+  function extra_options_submit($form, &$form_state) {
+    $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['max_length'] = $form_state['values']['options']['max_length'];
   }
 
   /**
    * {@inheritdoc}
    */
-  function expose_options() {
-    $this->options['expose']['values_form_type'] = 'textfield';
-    $this->options['expose']['select_multiple'] = FALSE;
-    $this->options['expose']['select_optional'] = FALSE;
-    $this->options['expose']['max_length'] = 40;
+  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;
   }
 
   /**
@@ -171,9 +218,9 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
   function value_form(&$form, &$form_state) {
     parent::value_form($form, $form_state);
 
-    $this->options['expose']['values_form_type'] = (isset($this->options['expose']['values_form_type'])) ? $this->options['expose']['values_form_type'] : 'textfield';
+    $this->options['values_form_type'] = (isset($this->options['values_form_type'])) ? $this->options['values_form_type'] : 'textfield';
 
-    if (preg_match('/select/', $this->options['expose']['values_form_type'])) {
+    if (preg_match('/select/', $this->options['values_form_type'])) {
 
       //Select List
       $form['value'] = array(
@@ -183,7 +230,7 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
           '#default_value' => $this->value,
       );
 
-      if ($this->options['expose']['select_multiple']) {
+      if ($this->options['select_multiple']) {
         $form['value']['#multiple'] = TRUE;
       }
     }
@@ -204,9 +251,16 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
   function exposed_form(&$form, &$form_state) {
     parent::exposed_form($form, $form_state);
 
-    if (isset($this->options['expose']['select_multiple'])) {
-      if ($this->options['expose']['select_multiple']) {
-        $form[$this->options['id']]['#multiple'] = TRUE;
+    if (isset($this->options['select_multiple'])) {
+      if ($this->options['select_multiple']) {
+
+        if (isset($this->options['expose']['identifier'])) {
+          $id = $this->options['expose']['identifier'];
+        }
+        else {
+          $id = $this->options['id'];
+        }
+        $form[$id]['#multiple'] = TRUE;
       }
     }
   }
@@ -226,8 +280,8 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
     $field = $this->table_alias . "." . $this->real_field;
     $table = $this->query->get_table_info($this->table);
 
-    $this->options['expose']['values_form_type'] = (isset($this->options['expose']['values_form_type'])) ? $this->options['expose']['values_form_type'] : 'textfield';
-    if (preg_match('/select/', $this->options['expose']['values_form_type'])) {
+    $this->options['values_form_type'] = (isset($this->options['values_form_type'])) ? $this->options['values_form_type'] : 'textfield';
+    if (preg_match('/select/', $this->options['values_form_type'])) {
       if (is_array($this->value)) {
         if (isset($this->value['All'])) {
           unset($this->value['All']);