Sfoglia il codice sorgente

Views: Added a generic select list by ID filter handler which works on any foreign key as long as the referenced table has a name field

Lacey Sanderson 11 anni fa
parent
commit
fa1525e439

+ 1 - 0
tripal_views/tripal_views.info

@@ -12,6 +12,7 @@ files[] = views/handlers/tripal_views_handler_field_sequence.inc
 files[] = views/handlers/tripal_views_handler_filter_no_results.inc
 files[] = views/handlers/tripal_views_handler_filter_sequence.inc
 files[] = views/handlers/tripal_views_handler_filter_select_cvterm.inc
+files[] = views/handlers/tripal_views_handler_filter_select_id.inc
 files[] = views/handlers/tripal_views_handler_filter_select_string.inc
 files[] = views/handlers/tripal_views_handler_field_aggregate.inc
 

+ 3 - 0
tripal_views/tripal_views.views.inc

@@ -45,6 +45,9 @@ function tripal_views_views_handlers() {
       'tripal_views_handler_filter_select_cvterm' => array(
         'parent' => 'tripal_views_handler_filter_select_string',
       ),
+      'tripal_views_handler_filter_select_id' => array(
+        'parent' => 'tripal_views_handler_filter_select_string',
+      ),
       'tripal_views_handler_filter_select_string' => array(
         'parent' => 'views_handler_filter_string',
       ),

+ 49 - 43
tripal_views/views/handlers/tripal_views_handler_filter_select_cvterm.inc

@@ -45,7 +45,6 @@ class tripal_views_handler_filter_select_cvterm extends tripal_views_handler_fil
             .' LEFT JOIN chado.cvterm cvterm ON cvterm.cvterm_id=' . $this->view->base_table . '.type_id '
             .'LEFT JOIN chado.cv cv ON cv.cv_id=cvterm.cv_id';
         }
-        // D7 TODO: Check DBTNG changes work
         $resource = chado_query($sql);
         $cvterms = array();
         foreach ($resource as $r) {
@@ -62,51 +61,12 @@ class tripal_views_handler_filter_select_cvterm extends tripal_views_handler_fil
     }
     else {
 
+      $where_clauses = $this->get_select_option_where();
       $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 '
-        }
+      if (!empty($where_clauses)) {
+        $where = ' AND ' . implode(' AND ', $where_clauses);
       }
 
-      // @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 . "}) " . $where . ' ORDER BY cvterm.name ASC';
       $resource = chado_query($sql);
       $cvterms = array();
@@ -126,6 +86,52 @@ class tripal_views_handler_filter_select_cvterm extends tripal_views_handler_fil
 
   }
 
+  /**
+   * For the SQL generating the options, determine the WHERE clauses
+   *
+   * @return
+   *   An array of full qualified where clauses (ie: table.myfield = 'fred')
+   */
+  function get_select_option_where() {
+    $where = array();
+
+    // 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) . ')';
+            }
+          }
+          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;
+            }
+          }
+       }
+    }
+
+    return $where;
+  }
+
   /**
    * {@inheritdoc}
    */

+ 155 - 0
tripal_views/views/handlers/tripal_views_handler_filter_select_id.inc

@@ -0,0 +1,155 @@
+<?php
+/**
+ * @file
+ * Contains tripal_views_handler_filter_select_cvterm
+ */
+
+/**
+ * This Handler provides a select list for the type field
+ *
+ *  NOTE: This handler only works when applied to the type_id field in the base_table of
+ *  this view.
+ *
+ * @ingroup tripal_views
+ */
+class tripal_views_handler_filter_select_id extends tripal_views_handler_filter_select_string {
+
+  /**
+   * {@inheritdoc}
+   */
+  function init(&$view, &$options) {
+    parent::init($view, $options);
+
+    if (preg_match('/(\w+)_id/',$this->field,$matches)) {
+      $this->parent_table = $matches[1];
+    }
+  }
+
+  /**
+   * Provide the options used in the select list.
+   * Override this function in extended handlers to easily change option list.
+   *
+   * @return
+   *   An array of options where the key is the value of this field in the database
+   */
+  function get_select_options() {
+
+    if (isset($this->options['show_all'])) {
+      $cv_id = variable_get('chado_' . $this->view->base_table . '_cv', NULL);
+      if ($cv_id) {
+        $results = chado_select_record('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
+        if (empty($results)) {
+          $results = array();
+        }
+        foreach ($results as $c) {
+          $cvterms[$c->cvterm_id] = $c->name;
+        }
+      }
+      else {
+        //get a list of cvs currently used
+        if ($this->view->base_table == 'cvterm') {
+          $sql = 'SELECT distinct(cv.cv_id) FROM chado.' . $this->view->base_table
+            .' LEFT JOIN chado.cv cv ON cv.cv_id=cvterm.cv_id';
+        }
+        else {
+          $sql = 'SELECT distinct(cv.cv_id) FROM chado.' . $this->view->base_table
+            .' LEFT JOIN chado.cvterm cvterm ON cvterm.cvterm_id=' . $this->view->base_table . '.type_id '
+            .'LEFT JOIN chado.cv cv ON cv.cv_id=cvterm.cv_id';
+        }
+        // D7 TODO: Check DBTNG changes work
+        $resource = chado_query($sql);
+        $cvterms = array();
+        foreach ($resource as $r) {
+          $results = chado_select_record('cvterm', array('cvterm_id', 'name'), array('cv_id' => $r->cv_id));
+          if (empty($results)) {
+            $results = array();
+          }
+          foreach ($results as $c) {
+            $cvterms[$c->cvterm_id] = $c->name;
+          }
+        }
+      }// end of if variable not defined
+
+    }
+    else {
+
+      $where_clauses = $this->get_select_option_where();
+      $where = '';
+      if (!empty($where_clauses)) {
+        $where = ' AND ' . implode(' AND ', $where_clauses);
+      }
+
+      $sql = "SELECT PARENT.%field as id, PARENT.name as name
+              FROM {%parent_table} PARENT
+                LEFT JOIN {%table} CHILD ON CHILD.%field = PARENT.%field
+              WHERE CHILD.%field IS NOT NULL $where
+              GROUP BY PARENT.%field
+              ORDER BY name";
+      $sql = str_replace(array('%field', '%table','%parent_table'),array($this->field, $this->table, $this->parent_table), $sql);
+      $resource = chado_query($sql);
+      $options = array();
+
+      if ($this->options['select_optional']) {
+        $options['All'] = '- Any -';
+      }
+
+      foreach ($resource as $r) {
+        $options[$r->id] = $r->name;
+      }
+    }
+    //sort options by name (case insensitive)
+    natcasesort($options);
+
+    return $options;
+
+  }
+
+  /**
+   * For the SQL generating the options, determine the WHERE clauses
+   *
+   * @return
+   *   An array of full qualified where clauses (ie: table.myfield = 'fred')
+   */
+  function get_select_option_where() {
+    $where = parent::get_select_option_where();
+
+    return $where;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  function option_definition() {
+    return parent::option_definition();
+  }
+
+  /**
+   * {@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);
+  }
+
+}

+ 44 - 32
tripal_views/views/handlers/tripal_views_handler_filter_select_string.inc

@@ -81,12 +81,52 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
    */
   function get_select_options() {
 
+    $where_clauses = $this->get_select_option_where();
+    $where = '';
+    if (!empty($where_clauses)) {
+      $where = ' WHERE ' . implode(' AND ', $where_clauses);
+    }
+
+    // get the values from the table
+    $sql = 'SELECT ' . $this->real_field . ' FROM {' . $this->table . '} ' . $where . ' ORDER BY ' . $this->field . ' ASC';
+    $results = chado_query($sql);
+
+    // Build the select box options
+    $max_length = (isset($this->options['max_length'])) ? $this->options['max_length'] : 40;
+    if (!$max_length) {
+      $max_length = 40;
+    }
+    $options = array();
+    if ($this->options['select_optional']) {
+      $options['All'] = '--Any--';
+    }
+    foreach ($results as $r) {
+      if (drupal_strlen($r->{$this->field}) > $max_length) {
+        $options[$r->{$this->field}] = drupal_substr($r->{$this->field}, 0, $max_length) . '...';
+      }
+      else {
+        $options[$r->{$this->field}] = $r->{$this->field};
+      }
+    }
+
+    return $options;
+  }
+
+  /**
+   * For the SQL generating the options, determine the WHERE clauses
+   *
+   * @return
+   *   An array of full qualified where clauses (ie: table.myfield = 'fred')
+   */
+  function get_select_option_where() {
+    $where = array();
+
     // 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.
-    $where = '';
+    $where = array();
     $filters = (is_array($this->view->filter)) ? $this->view->filter : array();
     foreach ($filters as $filter_name => $details) {
       // we only want to inclue non-exposed filters
@@ -100,48 +140,20 @@ class tripal_views_handler_filter_select_string extends views_handler_filter_str
         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 IN (" . implode(', ', $value) . ')';
-            $where .= ' AND ';
+            $where[] = "$details->field IN (" . implode(', ', $value) . ')';
           }
 
         }
         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 ';
+            $where[] = "$details->field $details->operator " . $value;
           }
         }
       }
     }
-    if ($where) {
-       $where = "WHERE $where";
-       $where = substr($where, 0, -5); # remove the final ' AND '
-    }
 
-    // get the values from the table
-    $sql = 'SELECT ' . $this->real_field . ' FROM {' . $this->table . '} ' . $where . ' ORDER BY ' . $this->field . ' ASC';
-    $results = chado_query($sql);
-
-    // Build the select box options
-    $max_length = (isset($this->options['max_length'])) ? $this->options['max_length'] : 40;
-    if (!$max_length) {
-      $max_length = 40;
-    }
-    $options = array();
-    if ($this->options['select_optional']) {
-      $options['All'] = '--Any--';
-    }
-    foreach ($results as $r) {
-      if (drupal_strlen($r->{$this->field}) > $max_length) {
-        $options[$r->{$this->field}] = drupal_substr($r->{$this->field}, 0, $max_length) . '...';
-      }
-      else {
-        $options[$r->{$this->field}] = $r->{$this->field};
-      }
-    }
-
-    return $options;
+    return $where;
   }
 
   /**