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