Browse Source

Added filter by stock external dbxref accession

laceysanderson 14 years ago
parent
commit
27b5b8ab91

+ 106 - 78
tripal_core/views/handlers/views_handler_field_stockrel_by_type.inc

@@ -18,7 +18,7 @@ class views_handler_field_stockrel_by_type extends views_handler_field_prerender
   function option_definition() {
     $options = parent::option_definition();
     $options['stockrel_type_ids'] = array('default' => array());
-		$options['stockrel_display_options'] = array('default' => array('subject','type','object'));
+    $options['stockrel_display_options'] = array('default' => array('subject','type','object'));
     return $options;
   }
 
@@ -28,27 +28,40 @@ class views_handler_field_stockrel_by_type extends views_handler_field_prerender
   function options_form(&$form, &$form_state) {
     parent::options_form($form, $form_state);
 
-		$form['stockrel_display_options'] = array(
-		  '#prefix' => '<div><div id="edit-options-display_options">',
-      '#suffix' => '</div></div>',
-			'#type' => 'checkboxes',
-			'#title' => t('Display Relationship Parts'),
-			'#description' => t('Check each part of the relationship you want displayed where the part '
-				.'of a relationship are: <Subject> <Relationship Type> <Object>. '
-				.'For example, with the relationship Sarah is the maternal parent of Fred '
-				.'if you checked only Object then "Fred" would be displayed.'),
-			'#options' => array(
-				'subject' => 'Subject',
-				'type' => 'Relationship Type',
-				'object' => 'Object',
-			),
-			'#default_value' => array($this->options['stockrel_display_options']['subject'], $this->options['stockrel_display_options']['type'], $this->options['stockrel_display_options']['object']),
-		);
-		
+    $form['stockrel_display_parts'] = array(
+      '#type' => 'checkboxes',
+      '#title' => t('Display Relationship Parts'),
+      '#description' => t('Check each part of the relationship you want displayed where the part '
+        .'of a relationship are: \<Subject\> \<Relationship Type\> \<Object\>. '
+        .'For example, with the relationship Sarah is the maternal parent of Fred '
+        .'if you checked only Object then "Fred" would be displayed.'),
+      '#options' => array(
+        'subject' => 'Subject',
+        'type' => 'Relationship Type',
+        'object' => 'Object',
+      ),
+      '#default_value' => array($this->options['stockrel_display_parts']['subject'], $this->options['stockrel_display_parts']['type'], $this->options['stockrel_display_parts']['object']),
+    );
+    
+    $form['stockrel_display_rels'] = array(
+      '#type' => 'radios',
+      '#title' => t('Display Relationships Where'),
+      '#description' => t('Only relationships where the selected criteria is met will be shown. '
+        .'The parts of a relationship are: \<Subject\> \<Relationship Type\> \<Object\>. '
+        .'For example, with the relationships Sarah is the maternal parent of Fred and '
+        .'Fred is the paternal_parent of Max where Fred is the current stock, '
+        .'if you selected "Current Stock is the Object" only Sarah is the maternal parent of Fred'
+        .' would be displayed.'),
+      '#options' => array(
+        'subject' => 'Current Stock is the Subject',
+        'object' => 'Current Stock is the Object',
+        'all' => 'Current Stock is the Subject and/or Object',
+      ),
+      '#default_value' => $this->options['stockrel_display_rels'],
+    );
+    
     $options = get_chado_cvterm_options( variable_get('chado_stock_relationship_cv', 'null') );
     $form['stockrel_type_ids'] = array(
-      '#prefix' => '<div><div id="edit-options-stockrel_type_ids">',
-      '#suffix' => '</div></div>',
       '#type' => 'checkboxes',
       '#title' => t('Relationship Types'),
       '#options' => $options,
@@ -63,74 +76,89 @@ class views_handler_field_stockrel_by_type extends views_handler_field_prerender
     $this->add_additional_fields();
   }
 
-	function pre_render($values) {
-		$this->aliases['relationships'] = 'relationships';
-		$this->aliases['stock_id'] = 'stock_id';
-		$this->field_alias = $this->aliases['stock_id'];
-		
-		//for each stock in this view page
-		foreach ($values as $result) {
-			if (!empty($result->{$this->aliases['relationships']})) {
-					
-				// all relationships including the current stock
-				$relationships = $result->{$this->aliases['relationships']};
-				foreach ($relationships as $relationship) {			
-					// perform filtering
-					if (!empty($this->options['stockrel_type_ids'])) {
-						$relationships2keep = array_filter($this->options['stockrel_type_ids']);
-						if (!in_array($relationship->type_id, $relationships2keep)) {
-							continue;
-						}
-					}
-					
-					// Add relationship to the list of items to be rendered
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['stock_id'] = $relationship->stock_id;
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['stock_relationship_id'] = $relationship->stock_relationship_id;
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['subject_id'] = $relationship->subject_id;
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['subject_name'] = $relationship->subject_name;
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['object_id'] = $relationship->object_id;
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['object_name'] = $relationship->object_name;
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['type_id'] = $relationship->type_id;
-					$this->items[$relationship->stock_id][$relationship->stock_relationship_id]['type_name'] = $relationship->type_name;
-				}
-			}
-		}
-	}
+  function pre_render($values) {
+    $this->aliases['relationships'] = 'relationships';
+    $this->aliases['stock_id'] = 'stock_id';
+    $this->field_alias = $this->aliases['stock_id'];
+    
+    //for each stock in this view page
+    foreach ($values as $result) {
+      if (!empty($result->{$this->aliases['relationships']})) {
+          
+        // all relationships including the current stock
+        $relationships = $result->{$this->aliases['relationships']};
+        foreach ($relationships as $relationship) {      
+          // perform filtering------
+          //type
+          if (!empty($this->options['stockrel_type_ids'])) {
+            $relationships2keep = array_filter($this->options['stockrel_type_ids']);
+            if (!in_array($relationship->type_id, $relationships2keep)) {
+              continue;
+            }
+          }
+          
+          //"Display Relationships Where" criteria
+          if (preg_match('/subject/', $this->options['stockrel_display_rels'])) {
+            if ($relationship->stock_id != $relationship->subject_id) {
+              continue;
+            }      
+          } elseif (preg_match('/object/', $this->options['stockrel_display_rels'])) {
+            if ($relationship->stock_id != $relationship->object_id) {
+              continue;
+            }
+          }
+          
+          // Add relationship to the list of items to be rendered
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['stock_id'] = $relationship->stock_id;
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['stock_relationship_id'] = $relationship->stock_relationship_id;
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['subject_id'] = $relationship->subject_id;
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['subject_name'] = $relationship->subject_name;
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['object_id'] = $relationship->object_id;
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['object_name'] = $relationship->object_name;
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['type_id'] = $relationship->type_id;
+          $this->items[$relationship->stock_id][$relationship->stock_relationship_id]['type_name'] = $relationship->type_name;
+        }
+      }
+    }
+  }
 
   function render_item($count, $item) {
-	  $text = array();
-		if ($this->options['stockrel_display_options']['subject']) {
-			$text[] = $item['subject_name'];
-		}
-		if ($this->options['stockrel_display_options']['type']) {
-			$text[] = $item['type_name'];
-		}
-		if ($this->options['stockrel_display_options']['object']) {
-			$text[] = $item['object_name'];
-		}
-		return implode(' ', $text);
+    $text = array();
+    
+    // Render Parts
+    if ($this->options['stockrel_display_parts']['subject']) {
+      $text[] = $item['subject_name'];
+    }
+    if ($this->options['stockrel_display_parts']['type']) {
+      $text[] = $item['type_name'];
+    }
+    if ($this->options['stockrel_display_parts']['object']) {
+      $text[] = $item['object_name'];
+    }
+    
+    return implode(' ', $text);
   }
 
   function document_self_tokens(&$tokens) {
     $tokens['[' . $this->options['id'] . '-stock_id' . ']'] = t('The Stock ID.');
-		$tokens['[' . $this->options['id'] . '-stock_relationship_id' . ']'] = t('Relationship ID');
-		$tokens['[' . $this->options['id'] . '-subject_id' . ']'] = t('Subject ID');
-		$tokens['[' . $this->options['id'] . '-subject_name' . ']'] = t('Subject Name');
-		$tokens['[' . $this->options['id'] . '-object_id' . ']'] = t('Object ID');
-		$tokens['[' . $this->options['id'] . '-object_name' . ']'] = t('Object Name');
-		$tokens['[' . $this->options['id'] . '-type_id' . ']'] = t('Type ID');
-		$tokens['[' . $this->options['id'] . '-type_name' . ']'] = t('Type Name');
+    $tokens['[' . $this->options['id'] . '-stock_relationship_id' . ']'] = t('Relationship ID');
+    $tokens['[' . $this->options['id'] . '-subject_id' . ']'] = t('Subject ID');
+    $tokens['[' . $this->options['id'] . '-subject_name' . ']'] = t('Subject Name');
+    $tokens['[' . $this->options['id'] . '-object_id' . ']'] = t('Object ID');
+    $tokens['[' . $this->options['id'] . '-object_name' . ']'] = t('Object Name');
+    $tokens['[' . $this->options['id'] . '-type_id' . ']'] = t('Type ID');
+    $tokens['[' . $this->options['id'] . '-type_name' . ']'] = t('Type Name');
   }
 
   function add_self_tokens(&$tokens, $item) {
     $tokens['[' . $this->options['id'] . '-stock_id' . ']'] = $item['stock_id'];
-		$tokens['[' . $this->options['id'] . '-stock_relationship_id' . ']'] = $item['stock_relationship_id'];
-		$tokens['[' . $this->options['id'] . '-subject_id' . ']'] = $item['subject_id'];
-		$tokens['[' . $this->options['id'] . '-subject_name' . ']'] = $item['subject_name'];
-		$tokens['[' . $this->options['id'] . '-object_id' . ']'] = $item['object_id'];
-		$tokens['[' . $this->options['id'] . '-object_name' . ']'] = $item['object_name'];
-		$tokens['[' . $this->options['id'] . '-type_id' . ']'] = $item['type_id'];
-		$tokens['[' . $this->options['id'] . '-type_name' . ']'] = $item['type_name'];
+    $tokens['[' . $this->options['id'] . '-stock_relationship_id' . ']'] = $item['stock_relationship_id'];
+    $tokens['[' . $this->options['id'] . '-subject_id' . ']'] = $item['subject_id'];
+    $tokens['[' . $this->options['id'] . '-subject_name' . ']'] = $item['subject_name'];
+    $tokens['[' . $this->options['id'] . '-object_id' . ']'] = $item['object_id'];
+    $tokens['[' . $this->options['id'] . '-object_name' . ']'] = $item['object_name'];
+    $tokens['[' . $this->options['id'] . '-type_id' . ']'] = $item['type_id'];
+    $tokens['[' . $this->options['id'] . '-type_name' . ']'] = $item['type_name'];
 
   }
 }

+ 7 - 0
tripal_core/views/handlers/views_handler_filter_stockprop_id.inc

@@ -269,6 +269,13 @@ class views_handler_filter_stockprop_id extends views_handler_filter {
         '#value' => '',
       );
     }
+    
+    $form['expose']['optional'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Optional'),
+      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
+      '#default_value' => $this->options['expose']['optional'],
+    );
   }
   
   

+ 3 - 0
tripal_stock/tripal_stock.views.inc

@@ -69,6 +69,9 @@ function tripal_stock_views_handlers() {
      'views_handler_filter_stockprop_id' => array(
      	'parent' => 'views_handler_filter',
      ),
+     'views_handler_filter_stock_dbxref_id' => array(
+     	'parent' => 'views_handler_filter',
+     ),
      'views_handler_argument_stockprop_id' => array(
      	'parent' => 'views_handler_argument_string',
      ),

+ 239 - 0
tripal_stock/views/handlers/views_handler_filter_stock_dbxref_id.inc

@@ -0,0 +1,239 @@
+<?php
+// $Id$
+
+
+class views_handler_filter_stock_dbxref_id extends views_handler_filter {
+
+  function init(&$view, $options) {
+    parent::init($view, $options);
+    $this->db = $this->options['db'];
+  }
+  
+  function options_form(&$form, &$form_state) {
+    if ($this->can_expose()) {
+      $this->show_expose_button($form, $form_state);
+    }
+
+    $form['op_val_start'] = array('#value' => '<div class="clear-block">');
+
+    $this->types_form($form, $form_state);
+
+    $this->show_operator_form($form, $form_state);
+    $form['operator']['#prefix'] = '<div class="views-right-70">';
+    $this->show_value_form($form, $form_state);
+    $form['op_val_end'] = array('#value' => '</div>');
+
+    if ($this->can_expose()) {
+      $this->show_expose_form($form, $form_state);
+    }
+
+  }
+
+	function query () {
+		$this-> db = array_filter($this->db);
+		
+		if (preg_match('/IS NOT NULL/', $this->options['operator'])) {
+			$new_where_sql = "stock.stock_id IN "
+			."(SELECT stock_dbxref.stock_id FROM stock_dbxref, dbxref WHERE stock_dbxref.dbxref_id=dbxref.dbxref_id AND dbxref.db_id IN (".implode(', ',$this->db)."))";
+			$this->query->add_where($this->options['group'], $new_where_sql);
+		} elseif (preg_match('/IS NULL/', $this->options['operator'])) {
+			$new_where_sql = "stock.stock_id NOT IN "
+			."(SELECT stock_dbxref.stock_id FROM stock_dbxref, dbxref WHERE stock_dbxref.dbxref_id=dbxref.dbxref_id AND dbxref.db_id IN (".implode(', ',$this->db)."))";
+			$this->query->add_where($this->options['group'], $new_where_sql);
+		} else {
+			if (!empty($this->value)) {
+				$new_where_sql = "stock.stock_id IN "
+					."(SELECT stock_dbxref.stock_id FROM stock_dbxref, dbxref WHERE stock_dbxref.dbxref_id=dbxref.dbxref_id "
+					."AND dbxref.db_id IN (".implode(', ',$this->db).") AND dbxref.accession".$this->operator."'".$this->value."')";
+				$this->query->add_where($this->options['group'], $new_where_sql);
+			}
+		}
+	}
+  
+  /////////// Form Parts/////////////////////////
+  function types_form(&$form, &$form_state) {
+  
+    $db_options = get_chado_db_options();
+	  $db_options[0] = 'Select a Database';
+	  ksort($db_options);
+    $form['db'] = array(
+      '#type' => 'checkboxes',
+      '#title' => t('Databases'),
+      '#options' => $db_options,
+      '#default_value' => $this->db,
+      '#prefix' => '<div class="views-left-30">',
+      '#suffix' => '</div>',
+    );
+
+  }
+
+  function value_form(&$form, &$form_state) {
+    parent::value_form(&$form, &$form_state);
+    
+		$form['value'] = array(
+  	  '#type' => 'textfield',
+    	'#title' => t('Accession'),
+      '#default_value' => $this->value,
+    );
+  }
+
+  function operator_options() {
+    return array(
+      '=' => t('Is equal to'),
+      '!=' => t('Is not equal to'),
+      '~' => t('Contains'),
+      '!~' => t('Does not contain'),
+      'IS NOT NULL' => t('Is Present (Not Empty)'),
+      'IS NULL' => t('Is Absent (Empty)'),
+    );
+  }
+
+ /**
+  * Render our chunk of the exposed filter form when selecting
+  */
+  function exposed_form(&$form, &$form_state) {
+    if (empty($this->options['exposed'])) {
+      return;
+    }
+
+    if (!empty($this->options['expose']['use_type']) && !empty($this->options['expose']['type'])) {
+      $type = $this->options['expose']['type'];
+      $form[$type] = array(
+        '#type' => 'select',
+        '#title' => t('Database References'),
+        '#options' => $this->type_options(),
+        '#default_value' => $this->type,  
+      );
+
+      if (isset($form[$type]['#title'])) {
+        unset($form[$type]['#title']);
+      }
+    }
+
+    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator'])) {
+      $operator = $this->options['expose']['operator'];
+      $this->operator_form($form, $form_state);
+      $form[$operator] = $form['operator'];
+
+      if (isset($form[$operator]['#title'])) {
+        unset($form[$operator]['#title']);
+      }
+
+      $this->exposed_translate($form[$operator], 'operator');
+
+      unset($form['operator']);
+    }
+
+    if (!empty($this->options['expose']['identifier'])) {
+      $value = $this->options['expose']['identifier'];
+      $this->value_form($form, $form_state);
+      $form[$value] = $form['value'];
+
+      if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
+        unset($form[$value]['#title']);
+      }
+
+      $this->exposed_translate($form[$value], 'value');
+
+      if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
+        unset($form[$value]['#default_value']);
+      }
+
+      if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
+        $form[$value]['#default_value'] = 'All';
+      }
+
+      if ($value != 'value') {
+        unset($form['value']);
+      }
+    }
+  }
+
+  function expose_form_left(&$form, &$form_state) {
+    $form['expose']['label'] = array(
+      '#type' => 'textfield',
+      '#default_value' => $this->options['expose']['label'],
+      '#title' => t('Label'),
+      '#size' => 40,
+    );
+
+    $form['expose']['identifier'] = array(
+      '#type' => 'textfield',
+      '#default_value' => $this->options['expose']['identifier'],
+      '#title' => t('Filter identifier'),
+      '#size' => 40,
+      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
+    );
+
+  }
+
+  function expose_form_right(&$form, &$form_state) {
+    if (!empty($form['type']['#type'])) {
+      $form['expose']['use_type'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Unlock Database Reference'),
+        '#description' => t('When checked, the Database Reference will be exposed to the user'),
+        '#default_value' => !empty($this->options['expose']['use_type']),
+      );
+      $form['expose']['type'] = array(
+        '#type' => 'textfield',
+        '#default_value' => $this->options['expose']['type'],
+        '#title' => t('Database Reference identifier'),
+        '#size' => 40,
+        '#description' => t('This will appear in the URL after the ? to identify this Database Reference.'),
+        '#process' => array('views_process_dependency'),
+        '#dependency' => array(
+          'edit-options-expose-use-type' => array(1)
+        ),
+      );
+    }
+    else {
+      $form['expose']['type'] = array(
+        '#type' => 'value',
+        '#value' => '',
+      );
+    }
+
+    $form['expose']['identifier'] = array(
+      '#type' => 'textfield',
+      '#default_value' => $this->options['expose']['identifier'],
+      '#title' => t('Filter identifier'),
+      '#size' => 40,
+      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
+    );
+
+    if (!empty($form['operator']['#type'])) {
+      $form['expose']['use_operator'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Unlock operator'),
+        '#description' => t('When checked, the operator will be exposed to the user'),
+        '#default_value' => !empty($this->options['expose']['use_operator']),
+      );
+      $form['expose']['operator'] = array(
+        '#type' => 'textfield',
+        '#default_value' => $this->options['expose']['operator'],
+        '#title' => t('Operator identifier'),
+        '#size' => 40,
+        '#description' => t('This will appear in the URL after the ? to identify this operator.'),
+        '#process' => array('views_process_dependency'),
+        '#dependency' => array(
+          'edit-options-expose-use-operator' => array(1)
+        ),
+      );
+    }
+    else {
+      $form['expose']['operator'] = array(
+        '#type' => 'value',
+        '#value' => '',
+      );
+    }
+    
+    $form['expose']['optional'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Optional'),
+      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
+      '#default_value' => $this->options['expose']['optional'],
+    );
+  }
+}
+

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

@@ -240,6 +240,11 @@ function retrieve_stock_views_data() {
       'help' => t('Display database references from a given database for the current stock.'),
       'handler' => 'views_handler_field_stock_dbxref_by_type',
     ),
+    'filter' => array(
+    	'title' => t('Database References'),
+    	'help' => t('Filter by a given database reference type and/or value.'),
+    	'handler' => 'views_handler_filter_stock_dbxref_id',
+    ),
   );
 
 	//Calculated Field: stock dbxrefs (ALL)