Browse Source

Added filter by stock property views functionality

laceysanderson 14 years ago
parent
commit
ffa8a1798c

+ 45 - 40
tripal_core/views/handlers/views_handler_filter_stockprop_id.inc

@@ -52,39 +52,17 @@ class views_handler_filter_stockprop_id extends views_handler_filter {
     }
   }
 
-  /**
-   * Add this filter to the query.
-   */
-//  function query() {
-//    if (!empty($this->value) || preg_match('/NULL/', $this->operator)) {
-//      if (preg_match('/stockprop/', $this->query->table_queue['stockprop']['table'])) {
-//        //already one stockprop join thus create another one with a different alias
-//        // to ensure no collisions
-//        $original_join = $this->query->table_queue['stockprop'];
-//
-//        $this->query->table_queue['stockprop_'.$this->type] = $original_join;
-//        $this->query->table_queue['stockprop_'.$this->type]['alias'] = 'stockprop_'.$this->type;
-//        $this->query->tables['stockprop']['count']++;
-//        $this->table_alias = 'stockprop_'.$this->type;
-//      } else {
-//        // Add stockprop join to query if it's not already there
-//        $this->ensure_my_table();
-//      }
-//
-//      if (!empty($this->type)) {
-//        $this->query->add_where($this->options['group'], $this->table_alias. '.type_id = '.$this->type);
-//      }
-//
-//      if (preg_match('/NULL/', $this->operator)) {
-//        $this->query->add_where($this->options['group'], $this->table_alias. '.value ' . $this->operator);
-//      } else {
-//        if (!empty($this->value)) {
-//          $this->query->add_where($this->options['group'], $this->table_alias. '.value ' . $this->operator . " '%s'", $this->value);
-//        }
-//      }
-//    }
-//  }
-
+	function query () {
+		if (preg_match('/IS NOT NULL/', $this->options['operator'])) {
+			$new_where_sql = "stock_id IN (SELECT stock_id FROM stockprop WHERE type_id=".$this->type.")";
+		} elseif (preg_match('/IS NULL/', $this->options['operator'])) {
+			$new_where_sql = "stock_id NOT IN (SELECT stock_id FROM stockprop WHERE type_id=".$this->type.")";
+		} else {
+			$new_where_sql = "stock_id IN (SELECT stock_id FROM stockprop WHERE type_id=".$this->type." AND value".$this->operator."'".$this->value."')";
+		}
+		$this->query->add_where($this->options['group'], $new_where_sql);
+	}
+	
   function types_form(&$form, &$form_state) {
     $previous_db = db_set_active('chado');
     $result = db_query("SELECT cvt.cvterm_id as type_id, cvt.name FROM cvterm cvt WHERE cvt.cvterm_id IN (SELECT type_id FROM stockprop)");
@@ -106,12 +84,27 @@ class views_handler_filter_stockprop_id extends views_handler_filter {
 
   function value_form(&$form, &$form_state) {
     parent::value_form(&$form, &$form_state);
-
-    $form['value'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Value'),
-      '#default_value' => $this->value,
-    );
+	
+		if ($this->options['expose']['display_type'] == 'select') {
+			$previous_db = db_set_active('chado');
+			$resource = db_query("SELECT value FROM stockprop WHERE type_id=".$this->type." ORDER BY value");
+			db_set_active($previous_db);
+			while ($r = db_fetch_object($resource)) {
+				$options[$r->value] = $r->value;
+			}
+			$form['value'] = array(
+        '#type' => 'select',
+        '#title' => $this->options['label'],
+        '#options' => $options,
+        '#default_value' => $this->value,
+      );
+		} else {
+			$form['value'] = array(
+  	    '#type' => 'textfield',
+    	  '#title' => t('Value'),
+      	'#default_value' => $this->value,
+    	);
+    }
   }
 
   function operator_options() {
@@ -201,6 +194,17 @@ class views_handler_filter_stockprop_id extends views_handler_filter {
       '#size' => 40,
       '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
     );
+    
+    $form['expose']['display_type'] = array(
+    	'#type' => 'radios',
+    	'#default_value' => $this->options['expose']['display_type'],
+    	'#title' => t('Display Type'),
+    	'#description' => t('This will change the form item type of the exposed value form. ie: it can be used to let the user select the property value from a select box rather than a textfield.'),
+			'#options' => array(
+				'textfield' => 'Text Field',
+				'select' => 'Drop Down',
+			),
+    );
   }
 
   function expose_form_right(&$form, &$form_state) {
@@ -264,5 +268,6 @@ class views_handler_filter_stockprop_id extends views_handler_filter {
       );
     }
   }
-
+  
+  
 }

+ 14 - 4
tripal_stock/tripal_stock-api_functions.inc

@@ -42,10 +42,11 @@ function get_all_chado_stocks() {
  * @return an array of matching stock objects (produced using node_load)
  *   matching the given criteria
  */
-function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
+function get_chado_stocks($criteria, $match_type, $organism_id = NULL, $error_checking = FALSE) {
 	
 	//Deal with unknown column----------------------------
 	if (!empty($criteria['unknown'])) {
+		if ($error_checking) { drupal_set_message('Uknown provided','warning');}
 		$unknown_provided = TRUE;
 		$new_criteria = array();
 		foreach ($criteria['unknown']['columns'] as $column_name) {
@@ -57,7 +58,9 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 				$new_criteria[$column_name] = array('type'=>'STRING','value' => $criteria['unknown']['value'], 'regex'=>TRUE);
 			}
 		}
-		$unknown_stocks = get_chado_stocks($new_criteria, 'ANY', $organism_id);
+		if ($error_checking) { drupal_set_message('Re-calling get_chado_stocks(<pre>'.print_r($new_criteria,TRUE).'</pre>, ANY, '.$organism_id.')','warning'); }
+		$unknown_stocks = get_chado_stocks($new_criteria, 'ANY', $organism_id, $error_checking);
+		if ($error_checking) { drupal_set_message(sizeof($unknown_stocks).' Unknown Stocks', 'warning'); }
 	}
 	unset($criteria['unknown']); //so it's not mistaken as a column in the stock table
 	
@@ -94,6 +97,11 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 		}
 	}
 	
+	if ($error_checking) { 
+		drupal_set_message('Where Conditions: <pre>'.print_r($where,TRUE).'</pre>', 'warning'); 
+		drupal_set_message('Left Joins: <pre>'.print_r($joins,TRUE).'</pre>', 'warning');
+	}
+	
 	//Build query-----------------------------------------
 	if (preg_match('/ANY/', $match_type)) {
 		$where_string = implode(' OR ',$where);
@@ -118,19 +126,21 @@ function get_chado_stocks($criteria, $match_type, $organism_id = NULL) {
 		$execute_query = FALSE;
 	}
 	
+	if ($error_checking) { drupal_set_message('Query: '.$sql_query, 'warning'); }
+	
 	if ($execute_query) {
 		//Get stock_ids---------------------------------------
 		$previous_db = tripal_db_set_active('chado');
 		$resource = db_query($sql_query);
 		tripal_db_set_active($previous_db);
 	
-		//drupal_set_message($sql_query);
-	
 		$stock_ids = array();	
 		while ($r = db_fetch_object($resource)) {
 			$stock_ids[] = $r->stock_id;
 		}
 		$stock_ids = array_unique($stock_ids);
+		
+		if ($error_checking) { drupal_set_message('Stock IDs: <pre>'.print_r($stock_ids,TRUE).'</pre>', 'warning'); }
 	
 		//Get Stocks------------------------------------------
 		if (!empty($stock_ids)) {

+ 3 - 0
tripal_stock/tripal_stock.views.inc

@@ -64,6 +64,9 @@ function tripal_stock_views_handlers() {
 		'views_handler_field_stock_dbxref_all' => array(
        'parent' => 'views_handler_field_prerender_list',
      ),
+     'views_handler_filter_stockprop_id' => array(
+     	'parent' => 'views_handler_filter',
+     ),
    ),
  );
 }

+ 5 - 0
tripal_stock/views/stock.views.inc

@@ -159,6 +159,11 @@ function retrieve_stock_views_data() {
       'help' => t('Display a given type of properties associated with a stock.'),
       'handler' => 'views_handler_field_stockprop_by_type',
     ),
+    'filter' => array(
+    	'title' => t('Properties'),
+    	'help' => t('Filter by a given property type or value.'),
+    	'handler' => 'views_handler_filter_stockprop_id',
+    ),
   );
 
 	//Calculated Field: stock properties (ALL)