views_handler_filter_fields_compare.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_fields_compare.
  5. */
  6. /**
  7. * A handler to filter a view using fields comparison.
  8. *
  9. * @ingroup views_filter_handlers
  10. */
  11. class views_handler_filter_fields_compare extends views_handler_filter {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function can_expose() {
  16. return FALSE;
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function option_definition() {
  22. $options = parent::option_definition();
  23. $options['left_field'] = $options['right_field'] = array('default' => '');
  24. return $options;
  25. }
  26. /**
  27. * Provide a list of all operators.
  28. */
  29. public function fields_operator_options() {
  30. return array(
  31. '<' => t('Is less than'),
  32. '<=' => t('Is less than or equal to'),
  33. '=' => t('Is equal to'),
  34. '<>' => t('Is not equal to'),
  35. '>=' => t('Is greater than or equal to'),
  36. '>' => t('Is greater than'),
  37. );
  38. }
  39. /**
  40. * Provide a list of available fields.
  41. */
  42. public function field_options() {
  43. $options = array();
  44. $field_handlers = $this->view->display_handler->get_handlers('field');
  45. foreach ($field_handlers as $field => $handler) {
  46. if ($handler->table != 'views') {
  47. $options[$field] = $handler->ui_name();
  48. }
  49. }
  50. return $options;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function options_form(&$form, &$form_state) {
  56. parent::options_form($form, $form_state);
  57. $field_options = $this->field_options();
  58. $form['left_field'] = array(
  59. '#type' => 'select',
  60. '#title' => t('Left field'),
  61. '#default_value' => $this->options['left_field'],
  62. '#options' => $field_options,
  63. '#weight' => -3,
  64. );
  65. $form['operator'] = array(
  66. '#type' => 'select',
  67. '#title' => t('Operator'),
  68. '#default_value' => $this->options['operator'],
  69. '#options' => $this->fields_operator_options(),
  70. '#weight' => -2,
  71. );
  72. $form['right_field'] = array(
  73. '#type' => 'select',
  74. '#title' => t('Right field'),
  75. '#default_value' => $this->options['right_field'],
  76. '#options' => $field_options,
  77. '#weight' => -1,
  78. );
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function query() {
  84. // Build extra condition from existing fields (from existing joins).
  85. $left = $this->options['left_field'];
  86. $right = $this->options['right_field'];
  87. // Get all existing field handlers.
  88. $field_handlers = $this->view->display_handler->get_handlers('field');
  89. // Make sure the selected fields still exist.
  90. if (!isset($field_handlers[$left], $field_handlers[$right])) {
  91. return;
  92. }
  93. // Get the left table and field.
  94. $left_handler = $field_handlers[$left];
  95. $left_handler->set_relationship();
  96. $left_table_alias = $this->query->ensure_table($left_handler->table, $left_handler->relationship);
  97. // Get the left table and field.
  98. $right_handler = $field_handlers[$right];
  99. $right_handler->set_relationship();
  100. $right_table_alias = $this->query->ensure_table($right_handler->table, $right_handler->relationship);
  101. // Build piece of SQL.
  102. $snippet = $left_table_alias . '.' . $left_handler->real_field
  103. . ' ' . $this->options['operator'] . ' '
  104. . $right_table_alias . '.' . $right_handler->real_field;
  105. $this->query->add_where_expression($this->options['group'], $snippet);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function admin_summary() {
  111. return check_plain(
  112. $this->options['left_field'] . ' '
  113. . $this->options['operator'] . ' '
  114. . $this->options['right_field']
  115. );
  116. }
  117. }