views_handler_argument_stockprop_id.inc 2.6 KB

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