handler_filter_user.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $result = db_query("SELECT uid, name FROM {users} u WHERE uid IN (:uids)", $args);
  28. $result = $result->fetchAllKeyed();
  29. foreach ($ids as $uid) {
  30. if (!$uid) {
  31. $names[] = variable_get('anonymous', t('Anonymous'));
  32. }
  33. elseif (isset($result[$uid])) {
  34. $names[] = $result[$uid];
  35. }
  36. }
  37. return implode(', ', $names);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function validate_entity_strings(array &$form, array $values) {
  43. $uids = array();
  44. $missing = array();
  45. foreach ($values as $value) {
  46. if (drupal_strtolower($value) === drupal_strtolower(variable_get('anonymous', t('Anonymous')))) {
  47. $uids[] = 0;
  48. }
  49. else {
  50. $missing[strtolower($value)] = $value;
  51. }
  52. }
  53. if (!$missing) {
  54. return $uids;
  55. }
  56. $result = db_query("SELECT * FROM {users} WHERE name IN (:names)", array(':names' => array_values($missing)));
  57. foreach ($result as $account) {
  58. unset($missing[strtolower($account->name)]);
  59. $uids[] = $account->uid;
  60. }
  61. if ($missing) {
  62. form_error($form, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', $missing))));
  63. }
  64. return $uids;
  65. }
  66. }