handler_argument.inc 3.8 KB

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