views_handler_filter_many_to_one.inc

  1. 3.x handlers/views_handler_filter_many_to_one.inc
  2. 2.x handlers/views_handler_filter_many_to_one.inc

File

handlers/views_handler_filter_many_to_one.inc
View source
  1. <?php
  2. /**
  3. * Complex filter to handle filtering for many to one relationships,
  4. * such as terms (many terms per node) or roles (many roles per user).
  5. *
  6. * The construct method needs to be overridden to provide a list of options;
  7. * alternately, the value_form and admin_summary methods need to be overriden
  8. * to provide something that isn't just a select list.
  9. */
  10. class views_handler_filter_many_to_one extends views_handler_filter_in_operator {
  11. function init(&$view, $options) {
  12. parent::init($view, $options);
  13. $this->helper = new views_many_to_one_helper($this);
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['operator']['default'] = 'or';
  18. $options['value']['default'] = array();
  19. return $options;
  20. }
  21. function operators() {
  22. $operators = array(
  23. 'or' => array(
  24. 'title' => t('Is one of'),
  25. 'short' => t('or'),
  26. 'short_single' => t('='),
  27. 'method' => 'op_helper',
  28. 'values' => 1,
  29. 'ensure_my_table' => 'helper',
  30. ),
  31. 'and' => array(
  32. 'title' => t('Is all of'),
  33. 'short' => t('and'),
  34. 'short_single' => t('='),
  35. 'method' => 'op_helper',
  36. 'values' => 1,
  37. 'ensure_my_table' => 'helper',
  38. ),
  39. 'not' => array(
  40. 'title' => t('Is none of'),
  41. 'short' => t('not'),
  42. 'short_single' => t('<>'),
  43. 'method' => 'op_helper',
  44. 'values' => 1,
  45. 'ensure_my_table' => 'helper',
  46. ),
  47. );
  48. // if the definition allows for the empty operator, add it.
  49. if (!empty($this->definition['allow empty'])) {
  50. $operators += array(
  51. 'empty' => array(
  52. 'title' => t('Is empty (NULL)'),
  53. 'method' => 'op_empty',
  54. 'short' => t('empty'),
  55. 'values' => 0,
  56. ),
  57. 'not empty' => array(
  58. 'title' => t('Is not empty (NOT NULL)'),
  59. 'method' => 'op_empty',
  60. 'short' => t('not empty'),
  61. 'values' => 0,
  62. ),
  63. );
  64. }
  65. return $operators;
  66. }
  67. var $value_form_type = 'select';
  68. function value_form(&$form, &$form_state) {
  69. parent::value_form($form, $form_state);
  70. if (empty($form_state['exposed'])) {
  71. $this->helper->options_form($form, $form_state);
  72. }
  73. }
  74. /**
  75. * Override ensure_my_table so we can control how this joins in.
  76. * The operator actually has influence over joining.
  77. */
  78. function ensure_my_table() {
  79. // Defer to helper if the operator specifies it.
  80. $info = $this->operators();
  81. if (isset($info[$this->operator]['ensure_my_table']) && $info[$this->operator]['ensure_my_table'] == 'helper') {
  82. return $this->helper->ensure_my_table();
  83. }
  84. return parent::ensure_my_table();
  85. }
  86. function op_helper() {
  87. if (empty($this->value)) {
  88. return;
  89. }
  90. $this->helper->add_filter();
  91. }
  92. }