Project

General

Profile

Paste
Download (4.75 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / modules / user / views_handler_filter_user_name.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_user_name.
6
 */
7

    
8
/**
9
 * Filter handler for usernames.
10
 *
11
 * @ingroup views_filter_handlers
12
 */
13
class views_handler_filter_user_name extends views_handler_filter_in_operator {
14

    
15
  /**
16
   *
17
   */
18
  public $always_multiple = TRUE;
19

    
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function value_form(&$form, &$form_state) {
24
    $values = array();
25
    if ($this->value) {
26
      $result = db_query("SELECT * FROM {users} u WHERE uid IN (:uids)", array(':uids' => $this->value));
27
      foreach ($result as $account) {
28
        if ($account->uid) {
29
          $values[] = $account->name;
30
        }
31
        else {
32
          // Intentionally NOT translated.
33
          $values[] = 'Anonymous';
34
        }
35
      }
36
    }
37

    
38
    sort($values);
39
    $default_value = implode(', ', $values);
40
    $form['value'] = array(
41
      '#type' => 'textfield',
42
      '#title' => t('Usernames'),
43
      '#description' => t('Enter a comma separated list of user names.'),
44
      '#default_value' => $default_value,
45
      '#autocomplete_path' => 'admin/views/ajax/autocomplete/user',
46
    );
47

    
48
    if (!empty($form_state['exposed']) && !isset($form_state['input'][$this->options['expose']['identifier']])) {
49
      $form_state['input'][$this->options['expose']['identifier']] = $default_value;
50
    }
51
  }
52

    
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public function value_validate($form, &$form_state) {
57
    $values = drupal_explode_tags($form_state['values']['options']['value']);
58
    $uids = $this->validate_user_strings($form['value'], $values);
59

    
60
    if ($uids) {
61
      $form_state['values']['options']['value'] = $uids;
62
    }
63
  }
64

    
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function accept_exposed_input($input) {
69
    $rc = parent::accept_exposed_input($input);
70

    
71
    if ($rc) {
72
      // If we have previously validated input, override.
73
      if (isset($this->validated_exposed_input)) {
74
        $this->value = $this->validated_exposed_input;
75
      }
76
    }
77

    
78
    return $rc;
79
  }
80

    
81
  /**
82
   * {@inheritdoc}
83
   */
84
  public function exposed_validate(&$form, &$form_state) {
85
    if (empty($this->options['exposed'])) {
86
      return;
87
    }
88

    
89
    if (empty($this->options['expose']['identifier'])) {
90
      return;
91
    }
92

    
93
    $identifier = $this->options['expose']['identifier'];
94
    $input = $form_state['values'][$identifier];
95

    
96
    if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
97
      $this->operator = $this->options['group_info']['group_items'][$input]['operator'];
98
      $input = $this->options['group_info']['group_items'][$input]['value'];
99
    }
100

    
101
    $values = drupal_explode_tags($input);
102

    
103
    if (!$this->options['is_grouped'] || ($this->options['is_grouped'] && ($input != 'All'))) {
104
      $uids = $this->validate_user_strings($form[$identifier], $values);
105
    }
106
    else {
107
      $uids = FALSE;
108
    }
109

    
110
    if ($uids) {
111
      $this->validated_exposed_input = $uids;
112
    }
113
  }
114

    
115
  /**
116
   * Validate the user string.
117
   *
118
   * Since this can come from either the form or the exposed filter, this is
119
   * abstracted out a bit so it can handle the multiple input sources.
120
   */
121
  public function validate_user_strings(&$form, $values) {
122
    $uids = array();
123
    $placeholders = array();
124
    $args = array();
125
    $results = array();
126
    foreach ($values as $value) {
127
      if (strtolower($value) == 'anonymous') {
128
        $uids[] = 0;
129
      }
130
      else {
131
        $missing[strtolower($value)] = TRUE;
132
        $args[] = $value;
133
        $placeholders[] = "'%s'";
134
      }
135
    }
136

    
137
    if (!$args) {
138
      return $uids;
139
    }
140

    
141
    $result = db_query("SELECT * FROM {users} WHERE name IN (:names)", array(':names' => $args));
142
    foreach ($result as $account) {
143
      unset($missing[strtolower($account->name)]);
144
      $uids[] = $account->uid;
145
    }
146

    
147
    if ($missing) {
148
      form_error($form, format_plural(count($missing), 'Unable to find user: @users', 'Unable to find users: @users', array('@users' => implode(', ', array_keys($missing)))));
149
    }
150

    
151
    return $uids;
152
  }
153

    
154
  /**
155
   * {@inheritdoc}
156
   */
157
  public function value_submit($form, &$form_state) {
158
    // Prevent array filter from removing our anonymous user.
159
  }
160

    
161
  /**
162
   * {@inheritdoc}
163
   */
164
  public function get_value_options() {
165
  }
166

    
167
  /**
168
   * {@inheritdoc}
169
   */
170
  public function admin_summary() {
171
    // Set up $this->value_options for the parent summary.
172
    $this->value_options = array();
173

    
174
    if ($this->value) {
175
      $result = db_query("SELECT * FROM {users} u WHERE uid IN (:uids)", array(':uids' => $this->value));
176

    
177
      foreach ($result as $account) {
178
        if ($account->uid) {
179
          $this->value_options[$account->uid] = $account->name;
180
        }
181
        else {
182
          // Intentionally NOT translated.
183
          $this->value_options[$account->uid] = 'Anonymous';
184
        }
185
      }
186
    }
187

    
188
    return parent::admin_summary();
189
  }
190

    
191
}