Tripal v1.0 (6.x-1.0)
tripal_views_handler_filter_select_cvterm.inc
Go to the documentation of this file.
00001 <?php
00002 
00013 class tripal_views_handler_filter_select_cvterm extends views_handler_filter_string {
00014 
00019   function init(&$view, $options) {
00020 
00021     include_once('chado_wrapper_functions.inc');
00022 
00023     parent::init($view, $options);
00024 
00025     if ($this->options['show_all']) {
00026       $cv_id = variable_get('chado_' . $this->view->base_table . '_cv', NULL);
00027       if ($cv_id) {
00028         $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
00029         if (empty($results)) {
00030           $results = array();
00031         }
00032         foreach ($results as $c) {
00033           $cvterms[$c->cvterm_id] = $c->name;
00034         }
00035       }
00036       else {
00037         //get a list of cvs currently used
00038         if ($this->view->base_table == 'cvterm') {
00039           $sql = 'SELECT distinct(cv.cv_id) FROM ' . $this->view->base_table
00040             .' LEFT JOIN cv cv ON cv.cv_id=cvterm.cv_id';
00041         }
00042         else {
00043           $sql = 'SELECT distinct(cv.cv_id) FROM ' . $this->view->base_table
00044             .' LEFT JOIN cvterm cvterm ON cvterm.cvterm_id=' . $this->view->base_table . '.type_id '
00045             .'LEFT JOIN cv cv ON cv.cv_id=cvterm.cv_id';
00046         }
00047         $resource = chado_query($sql);
00048         $cvterms = array();
00049         while ( $r = db_fetch_object($resource) ) {
00050           $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $r->cv_id));
00051           if (empty($results)) {
00052             $results = array();
00053           }
00054           foreach ($results as $c) {
00055             $cvterms[$c->cvterm_id] = $c->name;
00056           }
00057         }
00058       }// end of if variable not defined
00059 
00060     }
00061     else {
00062       // @coder-ignore: non-drupal schema therefore table prefixing does not apply
00063       $sql = "SELECT cvterm_id, name FROM cvterm WHERE cvterm_id IN (SELECT distinct(%s) FROM %s)";
00064       $resource = chado_query($sql, $this->field, $this->table);
00065       $cvterms = array();
00066       while ( $r = db_fetch_object($resource) ) {
00067         $cvterms[$r->cvterm_id] = $r->name;
00068       }
00069     }
00070     //sort cvterms by name (case insensitive)
00071     natcasesort($cvterms);
00072 
00073     //add to this handler
00074     $this->cvterm_options = $cvterms;
00075 
00076   }
00077 
00081   function options_form(&$form, &$form_state) {
00082     $form['msg'] = array(
00083       '#type' => 'item',
00084       '#value' => '<b>If this filter applies to a table that is aggregated, additionally options may be ignored.</b>'
00085     );
00086 
00087     parent::options_form($form, $form_state);
00088 
00089     $form['values_form_type'] = array(
00090       '#type' => 'radios',
00091       '#title' => t('Filter Type'),
00092       '#options' => array(
00093         'textfield' => 'Text Field',
00094         'select' => 'Drop-Down Box',
00095       ),
00096       '#default_value' => ($this->options['values_form_type']) ? $this->options['values_form_type'] : 'select',
00097     );
00098 
00099     $form['multiple'] = array(
00100       '#type' => 'checkbox',
00101       '#title' => t('Select Multiple'),
00102       '#description' => t('Allows more then one option to be selected.'),
00103       '#default_value' => (isset($this->options['multiple'])) ? $this->options['multiple'] : FALSE,
00104     );
00105 
00106     $form['optional'] = array(
00107       '#type' => 'checkbox',
00108       '#title' => t('Optional'),
00109       '#description' => t('Adds --Any-- to the available options.'),
00110       '#default_value' => (isset($this->options['optional'])) ? $this->options['optional'] : TRUE,
00111     );
00112 
00113     $form['show_all'] = array(
00114       '#type' => 'checkbox',
00115       '#title' => t('Show All Terms'),
00116       '#description' => 'Otherwise only cvterms used in the base table will be used'
00117     );
00118 
00119     $form['agg'] = array(
00120       '#type' => 'fieldset',
00121       '#title' => 'Apply to fields that are aggregated'
00122     );
00123 
00124     $form['agg']['records_with'] = array(
00125       '#type' => 'checkbox',
00126       '#title' => t('Filter base table records'),
00127       '#description' => t('Filters %base_table to only those with the value in the aggregate array.', array('%base_table' => $this->view->base_table)),
00128       '#default_value' => (isset($this->options['agg']['records_with'])) ? $this->options['agg']['records_with'] : TRUE,
00129     );
00130 
00131     $form['agg']['aggregates_with'] = array(
00132       '#type' => 'checkbox',
00133       '#title' => t('Filter aggregates displayed'),
00134       '#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)),
00135       '#default_value' => (isset($this->options['agg']['aggregates_with'])) ? $this->options['agg']['aggregates_with'] : TRUE,
00136     );
00137 
00138   }
00139 
00144   function query() {
00145 
00146     // make optional
00147     // if it is not set or empty then don't restrict the query
00148     if (!$this->value) {
00149       return;
00150     }
00151 
00152     $this->ensure_my_table();
00153     $field = "$this->table_alias.$this->real_field";
00154 
00155     $table = $this->query->get_table_info($this->table);
00156     if (preg_match('/aggregator/', $table['join']->definition['handler'])) {
00157       $this->aggregated = TRUE;
00158     }
00159     else {
00160       $this->aggregated = FALSE;
00161     }
00162 
00163     if (!$this->aggregated) {
00164 
00165       if ($this->options['multiple']) {
00166         // Remove any if it's there
00167         unset($this->value['All']);
00168 
00169         if (sizeof($this->value)) {
00170           $holders = array();
00171           foreach ($this->value as $v) {
00172             if (preg_match('/^[\d\.]+$/', $v)) {
00173               $holders[] = '%d';
00174             }
00175             else {
00176               $holders[] = "'%s'";
00177             }
00178           }
00179           $where = $field . " IN (" . implode(", ", $holders) . ")";
00180         }
00181       }
00182       elseif ($this->value != 'All') {
00183         if (preg_match('/^\d+$/', $this->value)) {
00184           $where = $field . ' = %d';
00185         }
00186         else {
00187           $where = $field . " " . $this->operator . " '%s'";
00188         }
00189       }
00190 
00191       if ($where) {
00192         $this->query->add_where($this->options['group'], $where, $this->value);
00193       }
00194     }
00195     else {
00196 
00197       // Only base records with value in the aggregated field
00198       // This doesn't restrict the items in the aggregate field
00199       $this->ensure_my_table();
00200       if ($this->options['agg']['records_with']) {
00201         $where = "'%s' = ANY($field)";
00202         $this->query->add_where($this->options['group'], $where, $this->value);
00203       }
00204 
00205       // To restrict the items in the aggregate...
00206       // Tell the join handler about the filter
00207       // so it can be done in the join query
00208       if ($this->options['agg']['aggregates_with']) {
00209 
00210         if (sizeof($this->value) == 1 && is_array($this->value)) {
00211           $table['join']->filter[] = $field . " " . $this->operator . " '" . array_pop($this->value) . "'";
00212         }
00213         elseif (sizeof($this->value) == 1 && is_string($this->value)) {
00214           $table['join']->filter[] = $field . " " . $this->operator . " '" . $this->value . "'";
00215         }
00216         elseif (sizeof($this->value) > 1 && is_array($this->value)) {
00217           $table['join']->filter[] = $field . " IN (" . implode(',',$this->value) . ")";
00218         }
00219       }
00220     }
00221 
00222   }
00223 
00228   function value_form(&$form, &$form_state) {
00229     parent::value_form($form, $form_state);
00230 
00231     if (preg_match('/select/', $this->options['values_form_type'])) {
00232       // Get Options
00233       if ($this->options['optional']) {
00234         $options['<select ' . $this->table . '>'] = '--None--';
00235         $options['All'] = '--Any--';
00236       }
00237       $max_length = 40;
00238       foreach ($this->cvterm_options as $cvterm_id => $cvterm_name) {
00239         if (drupal_strlen($cvterm_name) > $max_length) {
00240           $options[$cvterm_id] = drupal_substr($cvterm_name, 0, $max_length) . '...';
00241         }
00242         else {
00243           $options[$cvterm_id] = $cvterm_name;
00244         }
00245       }
00246 
00247       if (empty($options)) {
00248         $options[0] = '';
00249       }
00250 
00251       //Select List
00252       $form['value'] = array(
00253           '#type' => 'select',
00254           '#title' => t('%label', array('%label' => $this->options['label'])),
00255           '#options' => $options,
00256           '#default_value' => $this->value,
00257       );
00258 
00259       if ($this->options['multiple']) {
00260         $form['value']['#multiple'] = TRUE;
00261       }
00262 
00263     }
00264     else {
00265       $form['value'] = array(
00266         '#type' => 'textfield',
00267         '#title' => t('%label', array('%label' => $this->options['label'])),
00268         '#default_value' => $this->value,
00269       );
00270     }
00271   }
00272 
00276   function exposed_form(&$form, &$form_state) {
00277     if (empty($this->options['exposed'])) {
00278       return;
00279     }
00280 
00281     $value = $this->options['expose']['identifier'];
00282     $this->value_form($form, $form_state);
00283     $form[$value] = $form['value'];
00284 
00285     if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
00286       unset($form[$value]['#title']);
00287     }
00288 
00289     $this->exposed_translate($form[$value], 'value');
00290 
00291     if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
00292       unset($form[$value]['#default_value']);
00293     }
00294 
00295     if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
00296       $form[$value]['#default_value'] = 'All';
00297     }
00298 
00299     if ($value != 'value') {
00300       unset($form['value']);
00301     }
00302 
00303   }
00304 
00310   function operators() {
00311     $operators = array(
00312       '=' => array(
00313         'title' => t('Is equal to'),
00314         'short' => t('='),
00315         'method' => 'op_equal',
00316         'values' => 1,
00317       ),
00318       '!=' => array(
00319         'title' => t('Is not equal to'),
00320         'short' => t('!='),
00321         'method' => 'op_equal',
00322         'values' => 1,
00323       ),
00324       '~' => array(
00325         'title' => t('Contains'),
00326         'short' => t('contains'),
00327         'method' => 'op_contains',
00328         'values' => 1,
00329       ),
00330     );
00331 
00332     return $operators;
00333   }
00334 }
 All Classes Files Functions Variables