Tripal v1.0 (6.x-1.0)
views_handler_filter_chado_select_cvterm_name.inc
Go to the documentation of this file.
00001 <?php
00002 
00013 class views_handler_filter_chado_select_cvterm_name extends views_handler_filter_string {
00014 
00019   function init(&$view, $options) {
00020     parent::init($view, $options);
00021 
00022     if ($this->options['show_all']) {
00023       $cv_id = variable_get('chado_' . $this->view->base_table . '_cv', NULL);
00024       if ($cv_id) {
00025         $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
00026         if (empty($results)) {
00027           $results = array();
00028         }
00029         foreach ($results as $c) {
00030           $cvterms[$c->cvterm_id] = $c->name;
00031         }
00032       }
00033       else {
00034         //get a list of cvs currently used
00035         if ($this->view->base_table == 'cvterm') {
00036           $sql = 'SELECT distinct(cv.cv_id) FROM ' . $this->view->base_table
00037             .' LEFT JOIN cv cv ON cv.cv_id=cvterm.cv_id';
00038         }
00039         else {
00040           $sql = 'SELECT distinct(cv.cv_id) FROM ' . $this->view->base_table
00041             .' LEFT JOIN cvterm cvterm ON cvterm.cvterm_id=' . $this->view->base_table . '.type_id '
00042             .'LEFT JOIN cv cv ON cv.cv_id=cvterm.cv_id';
00043         }
00044         $resource = chado_query($sql);
00045         $cvterms = array();
00046         while ( $r = db_fetch_object($resource) ) {
00047           $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $r->cv_id));
00048           if (empty($results)) {
00049             $results = array();
00050           }
00051           foreach ($results as $c) {
00052             $cvterms[$c->cvterm_id] = $c->name;
00053           }
00054         }
00055       }// end of if variable not defined
00056 
00057     }
00058     else {
00059       // @coder-ignore: non-drupal schema therefore table prefixing does not apply
00060       $sql = "SELECT cvterm_id, name FROM cvterm WHERE cvterm_id IN (SELECT distinct(type_id) FROM %s)";
00061       $resource = chado_query($sql, $this->table);
00062       $cvterms = array();
00063       while ( $r = db_fetch_object($resource) ) {
00064         $cvterms[$r->cvterm_id] = $r->name;
00065       }
00066     }
00067     //sort cvterms by name (case insensitive)
00068     natcasesort($cvterms);
00069 
00070     //add to this handler
00071     $this->cvterm_options = $cvterms;
00072   }
00073 
00077   function options_form(&$form, &$form_state) {
00078     parent::options_form($form, $form_state);
00079 
00080     $form['values_form_type'] = array(
00081       '#type' => 'radios',
00082       '#title' => t('Filter Type'),
00083       '#options' => array(
00084         'textfield' => 'Text Field',
00085         'select' => 'Drop-Down Box',
00086       ),
00087       '#default_value' => ($this->options['values_form_type']) ? $this->options['values_form_type'] : 'select',
00088     );
00089 
00090     $form['multiple'] = array(
00091       '#type' => 'checkbox',
00092       '#title' => t('Select Multiple'),
00093       '#description' => t('Allows more then one option to be selected.'),
00094       '#default_value' => (isset($this->options['multiple'])) ? $this->options['multiple'] : FALSE,
00095     );
00096 
00097     $form['optional'] = array(
00098       '#type' => 'checkbox',
00099       '#title' => t('Optional'),
00100       '#description' => t('Adds --Any-- to the available options.'),
00101       '#default_value' => (isset($this->options['optional'])) ? $this->options['optional'] : TRUE,
00102     );
00103 
00104     $form['show_all'] = array(
00105       '#type' => 'checkbox',
00106       '#title' => t('Show All Terms'),
00107       '#description' => 'Otherwise only cvterms used in the base table will be used'
00108     );
00109   }
00110 
00114   function query() {
00115     $this->ensure_table;
00116 
00117     if ($this->options['multiple']) {
00118       // Remove any if it's there
00119       unset($this->value['All']);
00120 
00121       if (sizeof($this->value)) {
00122         $holders = array();
00123         foreach ($this->value as $v) {
00124           if (preg_match('/^[\d\.]+$/', $v)) {
00125             $holders[] = '%d';
00126           }
00127           else {
00128             $holders[] = "'%s'";
00129           }
00130         }
00131         $where = "cvterm.cvterm_id IN (" . implode(", ", $holders) . ")";
00132       }
00133     }
00134     elseif ($this->value != 'All') {
00135       if (preg_match('/^\d+$/', $this->value)) {
00136         $where = 'cvterm.cvterm_id=%d';
00137       }
00138       else {
00139         $where = "cvterm.name" . $this->operator . "'%s'";
00140       }
00141     }
00142 
00143     if ($where) {
00144       $this->query->add_where($this->options['group'], $where, $this->value);
00145     }
00146   }
00147 
00152   function value_form(&$form, &$form_state) {
00153     parent::value_form($form, $form_state);
00154 
00155     if (preg_match('/select/', $this->options['values_form_type'])) {
00156       // Get Options
00157       if ($this->options['optional']) {
00158         $options['<select ' . $this->table . '>'] = '--None--';
00159         $options['All'] = '--Any--';
00160       }
00161       $max_length = 40;
00162       foreach ($this->cvterm_options as $cvterm_id => $cvterm_name) {
00163         if (drupal_strlen($cvterm_name) > $max_length) {
00164           $options[$cvterm_id] = drupal_substr($cvterm_name, 0, $max_length) . '...';
00165         }
00166         else {
00167           $options[$cvterm_id] = $cvterm_name;
00168         }
00169       }
00170 
00171       if (empty($options)) {
00172         $options[0] = '';
00173       }
00174 
00175       //Select List
00176       $form['value'] = array(
00177           '#type' => 'select',
00178           '#title' => t('%label', array('%label' => $this->options['label'])),
00179           '#options' => $options,
00180           '#default_value' => $this->value,
00181       );
00182 
00183       if ($this->options['multiple']) {
00184         $form['value']['#multiple'] = TRUE;
00185       }
00186 
00187     }
00188     else {
00189       $form['value'] = array(
00190         '#type' => 'textfield',
00191         '#title' => t('%label', array('%label' => $this->options['label'])),
00192         '#default_value' => $this->value,
00193       );
00194     }
00195   }
00196 
00200   function exposed_form(&$form, &$form_state) {
00201     if (empty($this->options['exposed'])) {
00202       return;
00203     }
00204 
00205     $value = $this->options['expose']['identifier'];
00206     $this->value_form($form, $form_state);
00207     $form[$value] = $form['value'];
00208 
00209     if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
00210       unset($form[$value]['#title']);
00211     }
00212 
00213     $this->exposed_translate($form[$value], 'value');
00214 
00215     if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
00216       unset($form[$value]['#default_value']);
00217     }
00218 
00219     if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
00220       $form[$value]['#default_value'] = 'All';
00221     }
00222 
00223     if ($value != 'value') {
00224       unset($form['value']);
00225     }
00226 
00227   }
00228 
00234   function operators() {
00235     $operators = array(
00236       '=' => array(
00237         'title' => t('Is equal to'),
00238         'short' => t('='),
00239         'method' => 'op_equal',
00240         'values' => 1,
00241       ),
00242       '!=' => array(
00243         'title' => t('Is not equal to'),
00244         'short' => t('!='),
00245         'method' => 'op_equal',
00246         'values' => 1,
00247       ),
00248       '~' => array(
00249         'title' => t('Contains'),
00250         'short' => t('contains'),
00251         'method' => 'op_contains',
00252         'values' => 1,
00253       ),
00254     );
00255 
00256     return $operators;
00257   }
00258 }
 All Classes Files Functions Variables