views_handler_filter_many_to_one.inc 3.5 KB

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