handler_filter.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerFilter.
  5. */
  6. /**
  7. * Views filter handler base class for handling all "normal" cases.
  8. */
  9. class SearchApiViewsHandlerFilter extends views_handler_filter {
  10. /**
  11. * The value to filter for.
  12. *
  13. * @var mixed
  14. */
  15. public $value;
  16. /**
  17. * The operator used for filtering.
  18. *
  19. * @var string
  20. */
  21. public $operator;
  22. /**
  23. * The associated views query object.
  24. *
  25. * @var SearchApiViewsQuery
  26. */
  27. public $query;
  28. /**
  29. * Provide a list of options for the operator form.
  30. */
  31. public function operator_options() {
  32. return array(
  33. '<' => t('Is less than'),
  34. '<=' => t('Is less than or equal to'),
  35. '=' => t('Is equal to'),
  36. '<>' => t('Is not equal to'),
  37. '>=' => t('Is greater than or equal to'),
  38. '>' => t('Is greater than'),
  39. 'empty' => t('Is empty'),
  40. 'not empty' => t('Is not empty'),
  41. );
  42. }
  43. /**
  44. * Provide a form for setting the filter value.
  45. */
  46. public function value_form(&$form, &$form_state) {
  47. while (is_array($this->value) && count($this->value) < 2) {
  48. $this->value = $this->value ? reset($this->value) : NULL;
  49. }
  50. $form['value'] = array(
  51. '#type' => 'textfield',
  52. '#title' => empty($form_state['exposed']) ? t('Value') : '',
  53. '#size' => 30,
  54. '#default_value' => isset($this->value) ? $this->value : '',
  55. );
  56. // Hide the value box if the operator is 'empty' or 'not empty'.
  57. // Radios share the same selector so we have to add some dummy selector.
  58. if (empty($form_state['exposed'])) {
  59. $form['value']['#states']['visible'] = array(
  60. ':input[name="options[operator]"],dummy-empty' => array('!value' => 'empty'),
  61. ':input[name="options[operator]"],dummy-not-empty' => array('!value' => 'not empty'),
  62. );
  63. }
  64. elseif (!empty($this->options['expose']['use_operator'])) {
  65. $name = $this->options['expose']['operator_id'];
  66. $form['value']['#states']['visible'] = array(
  67. ':input[name="' . $name . '"],dummy-empty' => array('!value' => 'empty'),
  68. ':input[name="' . $name . '"],dummy-not-empty' => array('!value' => 'not empty'),
  69. );
  70. }
  71. }
  72. /**
  73. * Display the filter on the administrative summary
  74. */
  75. function admin_summary() {
  76. if (!empty($this->options['exposed'])) {
  77. return t('exposed');
  78. }
  79. if ($this->operator === 'empty') {
  80. return t('is empty');
  81. }
  82. if ($this->operator === 'not empty') {
  83. return t('is not empty');
  84. }
  85. return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
  86. }
  87. /**
  88. * Add this filter to the query.
  89. */
  90. public function query() {
  91. if ($this->operator === 'empty') {
  92. $this->query->condition($this->real_field, NULL, '=', $this->options['group']);
  93. }
  94. elseif ($this->operator === 'not empty') {
  95. $this->query->condition($this->real_field, NULL, '<>', $this->options['group']);
  96. }
  97. else {
  98. while (is_array($this->value)) {
  99. $this->value = $this->value ? reset($this->value) : NULL;
  100. }
  101. if (strlen($this->value) > 0) {
  102. $this->query->condition($this->real_field, $this->value, $this->operator, $this->options['group']);
  103. }
  104. }
  105. }
  106. }