views_handler_argument_stockprop_id.inc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  24. $form['stockprop_type_id'] = array(
  25. '#type' => 'radios',
  26. '#title' => t('Property Types'),
  27. '#options' => $types,
  28. '#default_value' => $this->options['stockprop_type_id'],
  29. '#required' => TRUE,
  30. '#description' => t('Select the type of property represented by this argument.'),
  31. );
  32. $operators = array(
  33. '=' => t('Is equal to'),
  34. '!=' => t('Is not equal to'),
  35. '~' => t('Contains'),
  36. '!~' => t('Does not contain'),
  37. 'IS NOT NULL' => t('Is Present (Not Empty)'),
  38. 'IS NULL' => t('Is Absent (Empty)'),
  39. );
  40. $form['operator'] = array(
  41. '#type' => 'radios',
  42. '#title' => 'Operator',
  43. '#description' => t('Specify how to compare the argument with the property values.'),
  44. '#options' => $operators,
  45. '#default_value' => $this->options['operator'],
  46. '#required' => TRUE,
  47. );
  48. }
  49. /**
  50. * Build the query based upon the formula
  51. */
  52. function query() {
  53. $argument = $this->argument;
  54. if (!empty($this->options['transform_dash'])) {
  55. $argument = strtr($argument, '-', ' ');
  56. }
  57. if (preg_match('/IS NOT NULL/', $this->options['operator'])) {
  58. $new_where_sql = "stock.stock_id IN (SELECT stockprop.stock_id FROM stockprop WHERE stockprop.type_id=" . $this->options['stockprop_type_id'] . ")";
  59. }
  60. elseif (preg_match('/IS NULL/', $this->options['operator'])) {
  61. $new_where_sql = "stock.stock_id NOT IN (SELECT stockprop.stock_id FROM stockprop WHERE stockprop.type_id=" . $this->options['stockprop_type_id'] . ")";
  62. }
  63. else {
  64. $new_where_sql = "stock.stock_id IN (SELECT stockprop.stock_id FROM stockprop "
  65. . "WHERE stockprop.type_id=" . $this->options['stockprop_type_id'] . " AND stockprop.value" . $this->options['operator'] . "'" . $argument . "')";
  66. }
  67. $this->query->add_where($this->options['group'], $new_where_sql);
  68. }
  69. }