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

Definition of views_handler_filter_many_to_one.

File

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