handler_filter_entity.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerFilterEntity.
  5. */
  6. /**
  7. * Views filter handler class for entities.
  8. *
  9. * Should be extended for specific entity types, such as
  10. * SearchApiViewsHandlerFilterUser and SearchApiViewsHandlerFilterTaxonomyTerm.
  11. *
  12. * Based on views_handler_filter_term_node_tid.
  13. */
  14. abstract class SearchApiViewsHandlerFilterEntity extends SearchApiViewsHandlerFilter {
  15. /**
  16. * If exposed form input was successfully validated, the entered entity IDs.
  17. *
  18. * @var array
  19. */
  20. protected $validated_exposed_input;
  21. /**
  22. * Validates entered entity labels and converts them to entity IDs.
  23. *
  24. * Since this can come from either the form or the exposed filter, this is
  25. * abstracted out a bit so it can handle the multiple input sources.
  26. *
  27. * @param array $form
  28. * The form or form element for which any errors should be set.
  29. * @param array $values
  30. * The entered user names to validate.
  31. *
  32. * @return array
  33. * The entity IDs corresponding to all entities that could be found.
  34. */
  35. abstract protected function validate_entity_strings(array &$form, array $values);
  36. /**
  37. * Transforms an array of entity IDs into a comma-separated list of labels.
  38. *
  39. * @param array $ids
  40. * The entity IDs to transform.
  41. *
  42. * @return string
  43. * A string containing the labels corresponding to the IDs, separated by
  44. * commas.
  45. */
  46. abstract protected function ids_to_strings(array $ids);
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function operator_options() {
  51. $operators = array(
  52. '=' => $this->isMultiValued() ? t('Is one of') : t('Is'),
  53. 'all of' => t('Is all of'),
  54. '<>' => $this->isMultiValued() ? t('Is not one of') : t('Is not'),
  55. 'empty' => t('Is empty'),
  56. 'not empty' => t('Is not empty'),
  57. );
  58. if (!$this->isMultiValued()) {
  59. unset($operators['all of']);
  60. }
  61. return $operators;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function option_definition() {
  67. $options = parent::option_definition();
  68. $options['expose']['multiple']['default'] = TRUE;
  69. return $options;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function value_form(&$form, &$form_state) {
  75. parent::value_form($form, $form_state);
  76. if (!is_array($this->value)) {
  77. $this->value = $this->value ? array($this->value) : array();
  78. }
  79. // Set the correct default value in case the admin-set value is used (and a
  80. // value is present). The value is used if the form is either not exposed,
  81. // or the exposed form wasn't submitted yet (there is
  82. if ($this->value && (empty($form_state['input']) || !empty($form_state['input']['live_preview']))) {
  83. $form['value']['#default_value'] = $this->ids_to_strings($this->value);
  84. }
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function value_validate($form, &$form_state) {
  90. if (!empty($form['value'])) {
  91. $value = &$form_state['values']['options']['value'];
  92. $values = $this->isMultiValued($form_state['values']['options']) ? drupal_explode_tags($value) : array($value);
  93. $ids = $this->validate_entity_strings($form['value'], $values);
  94. if ($ids) {
  95. $value = $ids;
  96. }
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function accept_exposed_input($input) {
  103. $rc = parent::accept_exposed_input($input);
  104. if ($rc) {
  105. // If we have previously validated input, override.
  106. if ($this->validated_exposed_input) {
  107. $this->value = $this->validated_exposed_input;
  108. }
  109. }
  110. return $rc;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function exposed_validate(&$form, &$form_state) {
  116. if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
  117. return;
  118. }
  119. $identifier = $this->options['expose']['identifier'];
  120. $input = $form_state['values'][$identifier];
  121. if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
  122. $this->operator = $this->options['group_info']['group_items'][$input]['operator'];
  123. $input = $this->options['group_info']['group_items'][$input]['value'];
  124. }
  125. $values = $this->isMultiValued() ? drupal_explode_tags($input) : array($input);
  126. if (!$this->options['is_grouped'] || ($this->options['is_grouped'] && ($input != 'All'))) {
  127. $this->validated_exposed_input = $this->validate_entity_strings($form[$identifier], $values);
  128. }
  129. else {
  130. $this->validated_exposed_input = FALSE;
  131. }
  132. }
  133. /**
  134. * Determines whether multiple user names can be entered into this filter.
  135. *
  136. * This is either the case if the form isn't exposed, or if the " Allow
  137. * multiple selections" option is enabled.
  138. *
  139. * @param array $options
  140. * (optional) The options array to use. If not supplied, the options set on
  141. * this filter will be used.
  142. *
  143. * @return bool
  144. * TRUE if multiple values can be entered for this filter, FALSE otherwise.
  145. */
  146. protected function isMultiValued(array $options = array()) {
  147. $options = $options ? $options : $this->options;
  148. return empty($options['exposed']) || !empty($options['expose']['multiple']);
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function admin_summary() {
  154. $value = $this->value;
  155. $this->value = empty($value) ? '' : $this->ids_to_strings($value);
  156. $ret = parent::admin_summary();
  157. $this->value = $value;
  158. return $ret;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function query() {
  164. if ($this->operator === 'empty') {
  165. $this->query->condition($this->real_field, NULL, '=', $this->options['group']);
  166. }
  167. elseif ($this->operator === 'not empty') {
  168. $this->query->condition($this->real_field, NULL, '<>', $this->options['group']);
  169. }
  170. elseif (is_array($this->value)) {
  171. $all_of = $this->operator === 'all of';
  172. $operator = $all_of ? '=' : $this->operator;
  173. if (count($this->value) == 1) {
  174. $this->query->condition($this->real_field, reset($this->value), $operator, $this->options['group']);
  175. }
  176. else {
  177. $filter = $this->query->createFilter($operator === '<>' || $all_of ? 'AND' : 'OR');
  178. foreach ($this->value as $value) {
  179. $filter->condition($this->real_field, $value, $operator);
  180. }
  181. $this->query->filter($filter, $this->options['group']);
  182. }
  183. }
  184. }
  185. }