Tripal v1.0 (6.x-1.0)
tripal_views_handler_filter_select_string.inc
Go to the documentation of this file.
00001 <?php
00002 
00011 class tripal_views_handler_filter_select_string extends chado_views_handler_filter_string {
00012 
00013   function options_form(&$form, &$form_state) {
00014     parent::options_form($form, $form_state);
00015 
00016     $form['values_form_type'] = array(
00017       '#type' => 'radios',
00018       '#title' => t('Filter Type'),
00019       '#options' => array(
00020         'textfield' => 'Text Field',
00021         'select' => 'Drop-Down Box',
00022       ),
00023       '#default_value' => ($this->options['values_form_type']) ? $this->options['values_form_type'] : 'select',
00024     );
00025 
00026     $form['multiple'] = array(
00027       '#type' => 'checkbox',
00028       '#title' => t('Select Multiple'),
00029       '#description' => t('Allows more then one option to be selected.'),
00030       '#default_value' => (isset($this->options['multiple'])) ? $this->options['multiple'] : FALSE,
00031     );
00032 
00033     $form['optional'] = array(
00034       '#type' => 'checkbox',
00035       '#title' => t('Optional'),
00036       '#description' => t('Adds --Any-- to the available options.'),
00037       '#default_value' => (isset($this->options['optional'])) ? $this->options['optional'] : TRUE,
00038     );
00039 
00040     $form['max_length'] = array(
00041       '#type' => 'textfield',
00042       '#title' => t('Max Width'),
00043       '#description' => t('Specify the maximum width of the select box'),
00044       '#default_value' => (isset($this->options['max_length'])) ? $this->options['max_length'] : 40,
00045 
00046     );
00047     $form['note'] = array(
00048       '#type' => 'markup',
00049       '#value' => t('<strong><font color="red">Note:</font></strong> If another filter exists for the same table then '.
00050                     'the values shown in the drop box will only include those from rows that are not filtered.'),
00051 
00052     );
00053 
00054   }
00055 
00060   function value_form(&$form, &$form_state) {
00061     parent::value_form($form, $form_state);
00062 
00063     if (preg_match('/textfield/', $this->options['values_form_type'])) {
00064       $form['value'] = array(
00065         '#type' => 'textfield',
00066         '#title' => t('%label', array('%label' => $this->options['label'])),
00067         '#default_value' => $this->value,
00068       );
00069 
00070     }
00071     else {
00072 
00073       // build a where clause that will filter the list in the drop box
00074       // using fields that are not exposed and that are for the table
00075       // from whcih the values in the drop box will be slected and
00076       // we only want to use non-exposed fields because these are not
00077       // available to the user to edit--they're fixed.
00078       $where = '';
00079       $filters = (is_array($this->view->filter)) ? $this->view->filter : array();
00080       foreach ($filters as $filter_name => $details) {
00081          // we only want to inclue non-exposed filters
00082          if ($details->options['exposed'] == FALSE) {
00083             // we only want to filter on the table we're getting the list from
00084             if (strcmp($details->table, $this->table)==0) {
00085               $where .= "$details->field $details->operator " . $details->value['value'];
00086               $where .= ' AND ';
00087             }
00088          }
00089       }
00090       if ($where) {
00091          $where = "WHERE $where";
00092          $where = substr($where, 0, -5); # remove the final ' AND '
00093       }
00094 
00095       // get the values from the table
00096       $sql = "SELECT $this->real_field FROM $this->table $where ORDER BY $this->field ASC";
00097       $results = chado_query($sql);
00098 
00099       // Build the select box options
00100       $max_length = $this->options['max_length'];
00101       if (!$max_length) {
00102         $max_length = 40;
00103       }
00104       $options = array();
00105       if ($this->options['optional']) {
00106         //$options['<select '.$this->table.'>'] = '--None--';
00107         $options['All'] = '--Any--';
00108       }
00109       while ($r = db_fetch_object($results)) {
00110         if (drupal_strlen($r->{$this->field}) > $max_length) {
00111           $options[$r->{$this->field}] = drupal_substr($r->{$this->field}, 0, $max_length) . '...';
00112         }
00113         else {
00114           $options[$r->{$this->field}] = $r->{$this->field};
00115         }
00116       }
00117 
00118       //Select List
00119       $form['value'] = array(
00120           '#type' => 'select',
00121           '#title' => t('%label', array('%label' => $this->options['label'])),
00122           '#options' => $options,
00123           '#default_value' => $this->value,
00124       );
00125 
00126       if ($this->options['multiple']) {
00127         $form['value']['#multiple'] = TRUE;
00128       }
00129     }
00130   }
00131 
00135   function exposed_form(&$form, &$form_state) {
00136     if (empty($this->options['exposed'])) {
00137       return;
00138     }
00139 
00140     $value = $this->options['expose']['identifier'];
00141     $this->value_form($form, $form_state);
00142     $form[$value] = $form['value'];
00143 
00144     if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
00145       unset($form[$value]['#title']);
00146     }
00147 
00148     $this->exposed_translate($form[$value], 'value');
00149 
00150     if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
00151       unset($form[$value]['#default_value']);
00152     }
00153 
00154     if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
00155       $form[$value]['#default_value'] = 'All';
00156     }
00157 
00158     if ($value != 'value') {
00159       unset($form['value']);
00160     }
00161 
00162   }
00163 
00167   function query() {
00168 
00169     // make optional
00170     // if it is not set or empty then don't restrict the query
00171     if (!$this->value) {
00172       return;
00173     }
00174 
00175     $this->ensure_my_table();
00176 
00177     $table = $this->query->get_table_info($this->table);
00178     if (preg_match('/aggregator/', $table['join']->definition['handler'])) {
00179       $this->aggregated = TRUE;
00180     }
00181     else {
00182       $this->aggregated = FALSE;
00183     }
00184 
00185     // filter the aggregates
00186     if ($this->options['agg']['aggregates_with']) {
00187       $this->query_restrict_curr_table_records();
00188     }
00189 
00190     // filter the base table
00191     if ($this->options['agg']['records_with']) {
00192       $this->query_restrict_base_records();
00193     }
00194 
00195 
00196 
00197 
00198   }
00199 
00206   function query_restrict_base_records() {
00207     if (!$this->aggregated) {
00208       // Not Aggregated ---------------
00209 
00210       $this->ensure_my_table();
00211       $field = "$this->table_alias.$this->real_field";
00212       $upper = $this->case_transform();
00213 
00214       if ($this->options['multiple'] AND is_array($this->value)) {
00215         // Remove any if it's there
00216         unset($this->value['All']);
00217 
00218         if (sizeof($this->value)) {
00219           $holders = array();
00220           foreach ($this->value as $v) {
00221             if (preg_match('/^[\d\.]+$/', $v)) {
00222               $holders[] = '%f';
00223             }
00224             else {
00225               $holders[] = "'%s'";
00226             }
00227           }
00228           $where = "$field IN (" . implode(", ", $holders) . ")";
00229           $this->query->add_where($this->options['group'], $where, $this->value);
00230         }
00231       }
00232       else {
00233 
00234         // Deal with All/Any as value
00235         if (preg_match('/All/', $this->value)) {
00236           // Don't do anything
00237         }
00238         else {
00239           $info = $this->operators();
00240           if (!empty($info[$this->operator]['method'])) {
00241             $this->{$info[$this->operator]['method']}($field, $upper);
00242           }
00243         }
00244       }
00245 
00246     }
00247     else {
00248       // Is Aggregated ----------------
00249 
00250       $this->ensure_my_table();
00251       $field = "$this->table_alias.$this->real_field";
00252       $upper = $this->case_transform();
00253 
00254       if ($this->options['multiple'] AND is_array($this->value)) {
00255         // Remove any if it's there
00256         unset($this->value['All']);
00257 
00258         if (sizeof($this->value) > 1) {
00259           $holders = array();
00260           foreach ($this->value as $v) {
00261             $holders[] = "'%s'";
00262           }
00263           $where = $field .' && ARRAY[' . implode(", ", $holders) . ']';
00264           $this->query->add_where($this->options['group'], $where, $this->value);
00265 
00266         }
00267         elseif (sizeof($this->value) == 1) {
00268           $where = "'%s' = ANY($field)";
00269           $this->query->add_where($this->options['group'], $where, array_pop($this->value));
00270         }
00271       }
00272       else {
00273 
00274         // Deal with All/Any as value
00275         if (preg_match('/All/', $this->value)) {
00276           // Don't do anything
00277         }
00278         else {
00279           $where = "'%s' = ANY($field)";
00280           $this->query->add_where($this->options['group'], $where, $this->value);
00281         }
00282       }
00283 
00284     }
00285   }
00286 
00294   function query_restrict_curr_table_records() {
00295 
00296     if (!$this->aggregated) {
00297       // Not Aggregated ---------------
00298       // Warn the admin/user that they have selected that the aggregates should be filtered
00299       // on a field that isn't aggregated...
00300       watchdog(
00301         'tripal_views',
00302         'You have chosen to filter the aggregates shown for %table %field
00303           in %view; however, that field is not aggregated (ie: it is part of the base table
00304           or is a 1:1 relationship to the base table)',
00305         array(
00306           '%field' => $this->field,
00307           '%table' => $this->table,
00308           '%view' => $this->view->name
00309         ),
00310         WATCHDOG_WARNING
00311       );
00312       // Do nothing!
00313     }
00314     else {
00315       // Is Aggregated ----------------
00316 
00317       $this->ensure_my_table();
00318       $field = "$this->table_alias.$this->real_field";
00319       $upper = $this->case_transform();
00320 
00321       if ($this->options['multiple'] AND is_array($this->value)) {
00322         // Remove any if it's there
00323         unset($this->value['All']);
00324 
00325         if (sizeof($this->value) > 1) {
00326           $holders = array();
00327           foreach ($this->value as $v) {
00328             $holders[] = "'%s'";
00329           }
00330           $where = $field .' IN (' . implode(", ", $holders) . ')';
00331           $where = vsprintf($where, $this->value);
00332 
00333           // Add the where to the chado aggregated join object for this table
00334           // then the views_handler_join_chado_aggregator will add this to the WHERE
00335           // clause of the sub-query generating the aggregated listing
00336           $this->query->table_queue[ $this->table ]['join']->filter[] = $where;
00337 
00338         }
00339         elseif (sizeof($this->value) == 1) {
00340           $where = "$field = '%s'";
00341           $where = vsprintf($where, $this->value);
00342 
00343           // Add the where to the chado aggregated join object for this table
00344           // then the views_handler_join_chado_aggregator will add this to the WHERE
00345           // clause of the sub-query generating the aggregated listing
00346           $this->query->table_queue[ $this->table ]['join']->filter[] = $where;
00347         }
00348       }
00349       else {
00350 
00351         // Deal with All/Any as value
00352         if (preg_match('/All/', $this->value)) {
00353           // Don't do anything
00354         }
00355         else {
00356           $where = "'%s' = ANY($field)";
00357           $this->query->add_where($this->options['group'], $where, $this->value);
00358         }
00359       }
00360 
00361     }
00362   }
00363 }
 All Classes Files Functions Variables