handler_filter_entity.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 value_form(&$form, &$form_state) {
  67. parent::value_form($form, $form_state);
  68. if (!is_array($this->value)) {
  69. $this->value = $this->value ? array($this->value) : array();
  70. }
  71. // Set the correct default value in case the admin-set value is used (and a
  72. // value is present). The value is used if the form is either not exposed,
  73. // or the exposed form wasn't submitted yet. (There doesn't seem to be an
  74. // easier way to check for that.)
  75. if ($this->value && (empty($form_state['input']) || !empty($form_state['input']['live_preview']))) {
  76. $form['value']['#default_value'] = $this->ids_to_strings($this->value);
  77. }
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function value_validate($form, &$form_state) {
  83. if (!empty($form['value'])) {
  84. $value = &$form_state['values']['options']['value'];
  85. if (strlen($value)) {
  86. $values = $this->isMultiValued($form_state['values']['options']) ? drupal_explode_tags($value) : array($value);
  87. $ids = $this->validate_entity_strings($form['value'], $values);
  88. if ($ids) {
  89. $value = $ids;
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function accept_exposed_input($input) {
  98. $rc = parent::accept_exposed_input($input);
  99. if ($rc) {
  100. // If we have previously validated input, override.
  101. if ($this->validated_exposed_input) {
  102. $this->value = $this->validated_exposed_input;
  103. }
  104. }
  105. return $rc;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function exposed_validate(&$form, &$form_state) {
  111. if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
  112. return;
  113. }
  114. $this->validated_exposed_input = FALSE;
  115. $identifier = $this->options['expose']['identifier'];
  116. $input = $form_state['values'][$identifier];
  117. if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
  118. $this->operator = $this->options['group_info']['group_items'][$input]['operator'];
  119. $input = $this->options['group_info']['group_items'][$input]['value'];
  120. }
  121. if (!strlen($input)) {
  122. return;
  123. }
  124. $values = $this->isMultiValued() ? drupal_explode_tags($input) : array($input);
  125. if (!$this->options['is_grouped'] || ($this->options['is_grouped'] && ($input != 'All'))) {
  126. $this->validated_exposed_input = $this->validate_entity_strings($form[$identifier], $values);
  127. }
  128. }
  129. /**
  130. * Determines whether multiple user names can be entered into this filter.
  131. *
  132. * This is either the case if the form isn't exposed, or if the " Allow
  133. * multiple selections" option is enabled.
  134. *
  135. * @param array $options
  136. * (optional) The options array to use. If not supplied, the options set on
  137. * this filter will be used.
  138. *
  139. * @return bool
  140. * TRUE if multiple values can be entered for this filter, FALSE otherwise.
  141. */
  142. protected function isMultiValued(array $options = array()) {
  143. $options = $options ? $options : $this->options;
  144. return empty($options['exposed']) || !empty($options['expose']['multiple']);
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function admin_summary() {
  150. if (!is_array($this->value)) {
  151. $this->value = $this->value ? array($this->value) : array();
  152. }
  153. $value = $this->value;
  154. $this->value = empty($value) ? '' : $this->ids_to_strings($value);
  155. $ret = parent::admin_summary();
  156. $this->value = $value;
  157. return $ret;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function query() {
  163. if ($this->operator === 'empty') {
  164. $this->query->condition($this->real_field, NULL, '=', $this->options['group']);
  165. }
  166. elseif ($this->operator === 'not empty') {
  167. $this->query->condition($this->real_field, NULL, '<>', $this->options['group']);
  168. }
  169. elseif (is_array($this->value)) {
  170. $all_of = $this->operator === 'all of';
  171. $operator = $all_of ? '=' : $this->operator;
  172. if (count($this->value) == 1) {
  173. $this->query->condition($this->real_field, reset($this->value), $operator, $this->options['group']);
  174. }
  175. else {
  176. $filter = $this->query->createFilter($operator === '<>' || $all_of ? 'AND' : 'OR');
  177. foreach ($this->value as $value) {
  178. $filter->condition($this->real_field, $value, $operator);
  179. }
  180. $this->query->filter($filter, $this->options['group']);
  181. }
  182. }
  183. }
  184. }