Tripal v1.0 (6.x-1.0)
chado_views_handler_filter_string.inc
Go to the documentation of this file.
00001 <?php
00002 
00011 class chado_views_handler_filter_string extends views_handler_filter_string {
00012 
00016   function options_form(&$form, &$form_state) {
00017     $form['msg'] = array(
00018       '#type' => 'item',
00019       '#value' => '<b>If this filter applies to a table that is aggregated, additionally options may be ignored.</b>'
00020     );
00021 
00022     parent::options_form($form, $form_state);
00023 
00024     $form['agg'] = array(
00025       '#type' => 'fieldset',
00026       '#title' => 'Apply to fields that are aggregated'
00027     );
00028 
00029     $form['agg']['records_with'] = array(
00030       '#type' => 'checkbox',
00031       '#title' => t('Filter base table records'),
00032       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
00033       '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
00034     );
00035 
00036     $form['agg']['aggregates_with'] = array(
00037       '#type' => 'checkbox',
00038       '#title' => t('Filter aggregates displayed'),
00039       '#description' => t('Filters the aggregates shown based on the value. Doesn\'t affect the number of %base_table records.', array('%base_table' => $this->view->base_table)),
00040       '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
00041     );
00042 
00043   }
00044 
00049   function query() {
00050 
00051     // make optional
00052     // if it is not set or empty then don't restrict the query
00053     if (!$this->value) {
00054       return;
00055     }
00056 
00057     $this->ensure_my_table();
00058 
00059     $table = $this->query->get_table_info($this->table);
00060     if (preg_match('/aggregator/', $table['join']->definition['handler'])) {
00061       $this->aggregated = TRUE;
00062     }
00063     else {
00064       $this->aggregated = FALSE;
00065     }
00066 
00067     if (!$this->aggregated) {
00068       parent::query();
00069     }
00070     else {
00071 
00072       $this->ensure_my_table();
00073       $field = "$this->table_alias.$this->real_field";
00074 
00075       // Want to filter both the base table and the aggregated field
00076       if ($this->options['agg']['records_with'] && $this->options['agg']['aggregates_with']) {
00077         $where = "$field IS NOT NULL";
00078         $this->query->add_where($this->options['group'], $where);
00079         $table['join']->filter[] = $field . " " . $this->operator . " '" . $this->value . "'";
00080       }
00081       // Only base records with value in the aggregated field
00082       // This doesn't restrict the items in the aggregate field
00083       elseif ($this->options['agg']['records_with']) {
00084         if ($this->operator == '~') {
00085           // Currently postgresql doesn't support 'somevalue' ~ ANY(field) as a means of
00086           // selecting all records where the field matches somevalue. Instead what this notation means
00087           // is return TRUE if one of the values of field matches 'somevalue'
00088           // Instead add in a subclause
00089           $where = "array_to_string($field,'###') ~ '%s'";
00090           $this->query->add_where($this->options['group'], $where, $this->value);
00091         }
00092         else {
00093           $where = "'%s' " . $this->operator . " ANY($field)";
00094           $this->query->add_where($this->options['group'], $where, $this->value);
00095         }
00096 
00097       }
00098       // To restrict the items in the aggregate...
00099       // Tell the join handler about the filter
00100       // so it can be done in the join query
00101       if ($this->options['agg']['aggregates_with']) {
00102         $table['join']->filter[] = $field . " " . $this->operator . " '" . $this->value . "'";
00103       }
00104     }
00105 
00106   }
00107 
00113   function operators() {
00114     $operators = array(
00115       '=' => array(
00116         'title' => t('Is equal to'),
00117         'short' => t('='),
00118         'method' => 'op_equal',
00119         'values' => 1,
00120       ),
00121       '!=' => array(
00122         'title' => t('Is not equal to'),
00123         'short' => t('!='),
00124         'method' => 'op_equal',
00125         'values' => 1,
00126       ),
00127       '~' => array(
00128         'title' => t('Contains'),
00129         'short' => t('contains'),
00130         'method' => 'op_contains',
00131         'values' => 1,
00132       ),
00133     );
00134 
00135     return $operators;
00136   }
00137 
00138 }
 All Classes Files Functions Variables