views_handler_argument_stockprop_id.inc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. class views_handler_argument_stockprop_id extends views_handler_argument_string {
  3. function options_form(&$form, &$form_state) {
  4. parent::options_form($form, $form_state);
  5. unset($form['glossary']);
  6. unset($form['limit']);
  7. unset($form['add_table']);
  8. unset($form['require_value']);
  9. //get options & display as options
  10. $previous_db = tripal_db_set_active('chado');
  11. $result = db_query("SELECT cvt.cvterm_id as type_id, cvt.name FROM cvterm cvt WHERE cvt.cvterm_id IN (SELECT type_id FROM stockprop)");
  12. tripal_db_set_active($previous_db);
  13. $types = array();
  14. while ($r = db_fetch_object($result)) { $types[$r->type_id] = $r->name; }
  15. $form['stockprop_type_id'] = array(
  16. '#type' => 'radios',
  17. '#title' => t('Property Types'),
  18. '#options' => $types,
  19. '#default_value' => $this->options['stockprop_type_id'],
  20. '#required' => TRUE,
  21. '#description' => t('Select the type of property represented by this argument.'),
  22. );
  23. $operators = array(
  24. '=' => t('Is equal to'),
  25. '!=' => t('Is not equal to'),
  26. '~' => t('Contains'),
  27. '!~' => t('Does not contain'),
  28. 'IS NOT NULL' => t('Is Present (Not Empty)'),
  29. 'IS NULL' => t('Is Absent (Empty)'),
  30. );
  31. $form['operator'] = array(
  32. '#type' => 'radios',
  33. '#title' => 'Operator',
  34. '#description' => t('Specify how to compare the argument with the property values.'),
  35. '#options' => $operators,
  36. '#default_value' => $this->options['operator'],
  37. '#required' => TRUE,
  38. );
  39. }
  40. /**
  41. * Build the query based upon the formula
  42. */
  43. function query() {
  44. $argument = $this->argument;
  45. if (!empty($this->options['transform_dash'])) {
  46. $argument = strtr($argument, '-', ' ');
  47. }
  48. if (preg_match('/IS NOT NULL/', $this->options['operator'])) {
  49. $new_where_sql = "stock.stock_id IN (SELECT stockprop.stock_id FROM stockprop WHERE stockprop.type_id=".$this->options['stockprop_type_id'].")";
  50. } elseif (preg_match('/IS NULL/', $this->options['operator'])) {
  51. $new_where_sql = "stock.stock_id NOT IN (SELECT stockprop.stock_id FROM stockprop WHERE stockprop.type_id=".$this->options['stockprop_type_id'].")";
  52. } else {
  53. $new_where_sql = "stock.stock_id IN (SELECT stockprop.stock_id FROM stockprop "
  54. . "WHERE stockprop.type_id=".$this->options['stockprop_type_id']." AND stockprop.value".$this->options['operator']."'".$argument."')";
  55. }
  56. $this->query->add_where($this->options['group'], $new_where_sql);
  57. }
  58. }