handler_filter.inc 2.7 KB

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