handler_argument.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * Determine if the argument can generate a breadcrumb
  14. *
  15. * @return boolean
  16. */
  17. // @todo Change and implement set_breadcrumb()?
  18. public function uses_breadcrumb() {
  19. return FALSE;
  20. }
  21. /**
  22. * Provide a list of default behaviors for this argument if the argument
  23. * is not present.
  24. *
  25. * Override this method to provide additional (or fewer) default behaviors.
  26. */
  27. public function default_actions($which = NULL) {
  28. $defaults = array(
  29. 'ignore' => array(
  30. 'title' => t('Display all values'),
  31. 'method' => 'default_ignore',
  32. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  33. ),
  34. 'not found' => array(
  35. 'title' => t('Hide view / Page not found (404)'),
  36. 'method' => 'default_not_found',
  37. 'hard fail' => TRUE, // This is a hard fail condition
  38. ),
  39. 'empty' => array(
  40. 'title' => t('Display empty text'),
  41. 'method' => 'default_empty',
  42. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  43. ),
  44. 'default' => array(
  45. 'title' => t('Provide default argument'),
  46. 'method' => 'default_default',
  47. 'form method' => 'default_argument_form',
  48. 'has default argument' => TRUE,
  49. 'default only' => TRUE, // this can only be used for missing argument, not validation failure
  50. ),
  51. );
  52. if ($which) {
  53. return isset($defaults[$which]) ? $defaults[$which] : NULL;
  54. }
  55. return $defaults;
  56. }
  57. public function option_definition() {
  58. $options = parent::option_definition();
  59. $options['break_phrase'] = array('default' => FALSE);
  60. return $options;
  61. }
  62. public function options_form(&$form, &$form_state) {
  63. parent::options_form($form, $form_state);
  64. // Allow passing multiple values.
  65. $form['break_phrase'] = array(
  66. '#type' => 'checkbox',
  67. '#title' => t('Allow multiple values'),
  68. '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
  69. '#default_value' => $this->options['break_phrase'],
  70. '#fieldset' => 'more',
  71. );
  72. }
  73. /**
  74. * Set up the query for this argument.
  75. *
  76. * The argument sent may be found at $this->argument.
  77. */
  78. // @todo Provide options to select the operator, instead of always using '='?
  79. public function query($group_by = FALSE) {
  80. if (!empty($this->options['break_phrase'])) {
  81. views_break_phrase($this->argument, $this);
  82. }
  83. else {
  84. $this->value = array($this->argument);
  85. }
  86. if (count($this->value) > 1) {
  87. $filter = $this->query->createFilter(drupal_strtoupper($this->operator));
  88. // $filter will be NULL if there were errors in the query.
  89. if ($filter) {
  90. foreach ($this->value as $value) {
  91. $filter->condition($this->real_field, $value, '=');
  92. }
  93. $this->query->filter($filter);
  94. }
  95. }
  96. else {
  97. $this->query->condition($this->real_field, reset($this->value));
  98. }
  99. }
  100. /**
  101. * Get the title this argument will assign the view, given the argument.
  102. *
  103. * This usually needs to be overridden to provide a proper title.
  104. */
  105. public function title() {
  106. return t('Search @field for "@arg"', array('@field' => $this->definition['title'], '@arg' => $this->argument));
  107. }
  108. }