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

Definition of views_handler_filter_user_name.

File

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