handler_filter_user.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerFilterUser.
  5. */
  6. /**
  7. * Views filter handler class for handling user entities.
  8. *
  9. * Based on views_handler_filter_user_name.
  10. */
  11. class SearchApiViewsHandlerFilterUser extends SearchApiViewsHandlerFilterEntity {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function value_form(&$form, &$form_state) {
  16. parent::value_form($form, $form_state);
  17. // Set autocompletion.
  18. $path = $this->isMultiValued() ? 'admin/views/ajax/autocomplete/user' : 'user/autocomplete';
  19. $form['value']['#autocomplete_path'] = $path;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function ids_to_strings(array $ids) {
  25. $names = array();
  26. $args[':uids'] = array_filter($ids);
  27. if ($args[':uids']) {
  28. $result = db_query('SELECT uid, name FROM {users} u WHERE uid IN (:uids)', $args);
  29. $result = $result->fetchAllKeyed();
  30. }
  31. foreach ($ids as $uid) {
  32. if (!$uid) {
  33. $names[] = variable_get('anonymous', t('Anonymous'));
  34. }
  35. elseif (isset($result[$uid])) {
  36. $names[] = $result[$uid];
  37. }
  38. }
  39. return implode(', ', $names);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function validate_entity_strings(array &$form, array $values) {
  45. $uids = array();
  46. $missing = array();
  47. foreach ($values as $value) {
  48. if (drupal_strtolower($value) === drupal_strtolower(variable_get('anonymous', t('Anonymous')))) {
  49. $uids[] = 0;
  50. }
  51. else {
  52. $missing[strtolower($value)] = $value;
  53. }
  54. }
  55. if (!$missing) {
  56. return $uids;
  57. }
  58. $result = db_query("SELECT * FROM {users} WHERE name IN (:names)", array(':names' => array_values($missing)));
  59. foreach ($result as $account) {
  60. unset($missing[strtolower($account->name)]);
  61. $uids[] = $account->uid;
  62. }
  63. if ($missing) {
  64. form_error($form, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', $missing))));
  65. }
  66. return $uids;
  67. }
  68. }