views_handler_filter_user_name.inc

  1. 3.x modules/user/views_handler_filter_user_name.inc
  2. 2.x modules/user/views_handler_filter_user_name.inc

File

modules/user/views_handler_filter_user_name.inc
View source
  1. <?php
  2. /**
  3. * Filter handler for usernames
  4. */
  5. class views_handler_filter_user_name extends views_handler_filter_in_operator {
  6. var $no_single = TRUE;
  7. function value_form(&$form, &$form_state) {
  8. $values = array();
  9. if ($this->value) {
  10. $result = db_query("SELECT * FROM {users} u WHERE uid IN (" . implode(', ', $this->value) . ")");
  11. while ($account = db_fetch_object($result)) {
  12. if ($account->uid) {
  13. $values[] = $account->name;
  14. }
  15. else {
  16. $values[] = 'Anonymous'; // Intentionally NOT translated.
  17. }
  18. }
  19. }
  20. sort($values);
  21. $default_value = implode(', ', $values);
  22. $form['value'] = array(
  23. '#type' => 'textfield',
  24. '#title' => t('Usernames'),
  25. '#description' => t('Enter a comma separated list of user names.'),
  26. '#default_value' => $default_value,
  27. '#autocomplete_path' => 'admin/views/ajax/autocomplete/user',
  28. );
  29. if (!empty($form_state['exposed']) && !isset($form_state['input'][$this->options['expose']['identifier']])) {
  30. $form_state['input'][$this->options['expose']['identifier']] = $default_value;
  31. }
  32. }
  33. function value_validate(&$form, &$form_state) {
  34. $values = drupal_explode_tags($form_state['values']['options']['value']);
  35. $uids = $this->validate_user_strings($form['value'], $values);
  36. if ($uids) {
  37. $form_state['values']['options']['value'] = $uids;
  38. }
  39. }
  40. function accept_exposed_input($input) {
  41. $rc = parent::accept_exposed_input($input);
  42. if ($rc) {
  43. // If we have previously validated input, override.
  44. if (isset($this->validated_exposed_input)) {
  45. $this->value = $this->validated_exposed_input;
  46. }
  47. }
  48. return $rc;
  49. }
  50. function exposed_validate(&$form, &$form_state) {
  51. if (empty($this->options['exposed'])) {
  52. return;
  53. }
  54. if (empty($this->options['expose']['identifier'])) {
  55. return;
  56. }
  57. $identifier = $this->options['expose']['identifier'];
  58. $values = drupal_explode_tags($form_state['values'][$identifier]);
  59. $uids = $this->validate_user_strings($form[$identifier], $values);
  60. if ($uids) {
  61. $this->validated_exposed_input = $uids;
  62. }
  63. }
  64. /**
  65. * Validate the user string. Since this can come from either the form
  66. * or the exposed filter, this is abstracted out a bit so it can
  67. * handle the multiple input sources.
  68. */
  69. function validate_user_strings(&$form, $values) {
  70. $uids = array();
  71. $placeholders = array();
  72. $args = array();
  73. $results = array();
  74. foreach ($values as $value) {
  75. if (strtolower($value) == 'anonymous') {
  76. $uids[] = 0;
  77. }
  78. else {
  79. $missing[strtolower($value)] = TRUE;
  80. $args[] = $value;
  81. $placeholders[] = "'%s'";
  82. }
  83. }
  84. if (!$args) {
  85. return $uids;
  86. }
  87. $result = db_query("SELECT * FROM {users} WHERE name IN (" . implode(', ', $placeholders) . ")", $args);
  88. while ($account = db_fetch_object($result)) {
  89. unset($missing[strtolower($account->name)]);
  90. $uids[] = $account->uid;
  91. }
  92. if ($missing) {
  93. form_error($form, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', array_keys($missing)))));
  94. }
  95. return $uids;
  96. }
  97. function value_submit() {
  98. // prevent array filter from removing our anonymous user.
  99. }
  100. // Override to do nothing.
  101. function get_value_options() { }
  102. function admin_summary() {
  103. // set up $this->value_options for the parent summary
  104. $this->value_options = array();
  105. if ($this->value) {
  106. $result = db_query("SELECT * FROM {users} u WHERE uid IN (" . implode(', ', $this->value) . ")");
  107. while ($account = db_fetch_object($result)) {
  108. if ($account->uid) {
  109. $this->value_options[$account->uid] = $account->name;
  110. }
  111. else {
  112. $this->value_options[$account->uid] = 'Anonymous'; // Intentionally NOT translated.
  113. }
  114. }
  115. }
  116. return parent::admin_summary();
  117. }
  118. }