views_handler_argument_stockprop_id.inc 2.7 KB

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