handler_filter_entity.inc 6.4 KB

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