Tripal v1.0 (6.x-1.0)
views_handler_filter_chado_select_string.inc
Go to the documentation of this file.
00001 <?php
00002 
00011 class views_handler_filter_chado_select_string extends 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['max_length'] = 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--their fixed.
00078       $where = '';
00079       $filters = $this->view->filter;
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       if ($this->options['optional']) {
00105         //$options['<select '.$this->table.'>'] = '--None--';
00106         $options['All'] = '--Any--';
00107       }
00108       while ($r = db_fetch_object($results)) {
00109         if (drupal_strlen($r->{$this->field}) > $max_length) {
00110           $options[$r->{$this->field}] = drupal_substr($r->{$this->field}, 0, $max_length) . '...';
00111         }
00112         else {
00113           $options[$r->{$this->field}] = $r->{$this->field};
00114         }
00115       }
00116 
00117       //Select List
00118       $form['value'] = array(
00119           '#type' => 'select',
00120           '#title' => t('%label', array('%label' => $this->options['label'])),
00121           '#options' => $options,
00122           '#default_value' => $this->value,
00123       );
00124 
00125       if ($this->options['multiple']) {
00126         $form['value']['#multiple'] = TRUE;
00127       }
00128     }
00129   }
00130 
00134   function exposed_form(&$form, &$form_state) {
00135     if (empty($this->options['exposed'])) {
00136       return;
00137     }
00138 
00139     $value = $this->options['expose']['identifier'];
00140     $this->value_form($form, $form_state);
00141     $form[$value] = $form['value'];
00142 
00143     if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
00144       unset($form[$value]['#title']);
00145     }
00146 
00147     $this->exposed_translate($form[$value], 'value');
00148 
00149     if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
00150       unset($form[$value]['#default_value']);
00151     }
00152 
00153     if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
00154       $form[$value]['#default_value'] = 'All';
00155     }
00156 
00157     if ($value != 'value') {
00158       unset($form['value']);
00159     }
00160 
00161   }
00162 
00166   function query() {
00167 
00168     $this->ensure_my_table();
00169     $field = "$this->table_alias.$this->real_field";
00170     $upper = $this->case_transform();
00171 
00172     if ($this->options['multiple']) {
00173       // Remove any if it's there
00174       unset($this->value['All']);
00175 
00176       if (sizeof($this->value)) {
00177         $holders = array();
00178         foreach ($this->value as $v) {
00179           if (preg_match('/^[\d\.]+$/', $v)) {
00180             $holders[] = '%d';
00181           }
00182           else {
00183             $holders[] = "'%s'";
00184           }
00185         }
00186         $where = "$field IN (" . implode(", ", $holders) . ")";
00187         $this->query->add_where($this->options['group'], $where, $this->value);
00188       }
00189     }
00190     else {
00191 
00192       // Deal with All/Any as value
00193       if (preg_match('/All/', $this->value)) {
00194         // Don't do anything
00195       }
00196       else {
00197         $info = $this->operators();
00198         if (!empty($info[$this->operator]['method'])) {
00199           $this->{$info[$this->operator]['method']}($field, $upper);
00200         }
00201       }
00202     }
00203   }
00204 }
 All Classes Files Functions Variables