handler_argument.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Views argument handler class for handling all non-fulltext types.
  4. */
  5. class SearchApiViewsHandlerArgument extends views_handler_argument {
  6. /**
  7. * The associated views query object.
  8. *
  9. * @var SearchApiViewsQuery
  10. */
  11. public $query;
  12. /**
  13. * The operator to use for multiple arguments.
  14. *
  15. * Either "and" or "or".
  16. *
  17. * @var string
  18. *
  19. * @see views_break_phrase
  20. */
  21. public $operator;
  22. /**
  23. * Determine if the argument can generate a breadcrumb
  24. *
  25. * @return boolean
  26. */
  27. // @todo Change and implement set_breadcrumb()?
  28. public function uses_breadcrumb() {
  29. return FALSE;
  30. }
  31. /**
  32. * Provide a list of default behaviors for this argument if the argument
  33. * is not present.
  34. *
  35. * Override this method to provide additional (or fewer) default behaviors.
  36. */
  37. public function default_actions($which = NULL) {
  38. $defaults = array(
  39. 'ignore' => array(
  40. 'title' => t('Display all values'),
  41. 'method' => 'default_ignore',
  42. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  43. ),
  44. 'not found' => array(
  45. 'title' => t('Hide view / Page not found (404)'),
  46. 'method' => 'default_not_found',
  47. 'hard fail' => TRUE, // This is a hard fail condition
  48. ),
  49. 'empty' => array(
  50. 'title' => t('Display empty text'),
  51. 'method' => 'default_empty',
  52. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  53. ),
  54. 'default' => array(
  55. 'title' => t('Provide default argument'),
  56. 'method' => 'default_default',
  57. 'form method' => 'default_argument_form',
  58. 'has default argument' => TRUE,
  59. 'default only' => TRUE, // this can only be used for missing argument, not validation failure
  60. ),
  61. );
  62. if ($which) {
  63. return isset($defaults[$which]) ? $defaults[$which] : NULL;
  64. }
  65. return $defaults;
  66. }
  67. public function option_definition() {
  68. $options = parent::option_definition();
  69. $options['break_phrase'] = array('default' => FALSE);
  70. $options['not'] = array('default' => FALSE);
  71. return $options;
  72. }
  73. public function options_form(&$form, &$form_state) {
  74. parent::options_form($form, $form_state);
  75. // Allow passing multiple values.
  76. $form['break_phrase'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Allow multiple values'),
  79. '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
  80. '#default_value' => $this->options['break_phrase'],
  81. '#fieldset' => 'more',
  82. );
  83. $form['not'] = array(
  84. '#type' => 'checkbox',
  85. '#title' => t('Exclude'),
  86. '#description' => t('If selected, the numbers entered for the filter will be excluded rather than limiting the view.'),
  87. '#default_value' => !empty($this->options['not']),
  88. '#fieldset' => 'more',
  89. );
  90. }
  91. /**
  92. * Set up the query for this argument.
  93. *
  94. * The argument sent may be found at $this->argument.
  95. */
  96. public function query($group_by = FALSE) {
  97. if (empty($this->value)) {
  98. if (!empty($this->options['break_phrase'])) {
  99. views_break_phrase($this->argument, $this);
  100. }
  101. else {
  102. $this->value = array($this->argument);
  103. }
  104. }
  105. $operator = empty($this->options['not']) ? '=' : '<>';
  106. if (count($this->value) > 1) {
  107. $filter = $this->query->createFilter(drupal_strtoupper($this->operator));
  108. // $filter will be NULL if there were errors in the query.
  109. if ($filter) {
  110. foreach ($this->value as $value) {
  111. $filter->condition($this->real_field, $value, $operator);
  112. }
  113. $this->query->filter($filter);
  114. }
  115. }
  116. else {
  117. $this->query->condition($this->real_field, reset($this->value), $operator);
  118. }
  119. }
  120. }