views_handler_argument_stockprop_id.inc 2.7 KB

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