| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file
|
| 4 |
* Advanced user module allows you to select users based on an advanced set of
|
| 5 |
* filtering and apply actions to block, unblock, delete or email the selected
|
| 6 |
* users.
|
| 7 |
*
|
| 8 |
* $Id: advuser_filters.inc,v 1.2.2.2 2009/02/27 13:19:56 earnie Exp $
|
| 9 |
**/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Return form for advuser administration filters.
|
| 13 |
*/
|
| 14 |
function advuser_filter_form() {
|
| 15 |
$session = &$_SESSION['advuser_overview_filter'];
|
| 16 |
$session = is_array($session) ? $session : array();
|
| 17 |
$filters = advuser_filters();
|
| 18 |
|
| 19 |
$i = 0;
|
| 20 |
$form['filters'] = array(
|
| 21 |
'#type' => 'fieldset',
|
| 22 |
'#title' => t('Show only users where'),
|
| 23 |
'#theme' => 'advuser_filters',
|
| 24 |
'#collapsible' => TRUE,
|
| 25 |
);
|
| 26 |
foreach ($session as $filter) {
|
| 27 |
list($type, $value, $op, $qop) = $filter;
|
| 28 |
// Merge an array of arrays into one if necessary.
|
| 29 |
if ($filters[$type]['form_type'] == 'select') {
|
| 30 |
$options = $type == 'permission'
|
| 31 |
? call_user_func_array(
|
| 32 |
'array_merge',
|
| 33 |
$filters[$type]['options']
|
| 34 |
)
|
| 35 |
: $filters[$type]['options']
|
| 36 |
;
|
| 37 |
$params = array(
|
| 38 |
'%property' => $filters[$type]['title'],
|
| 39 |
'%value' => $options[$value]
|
| 40 |
);
|
| 41 |
} else {
|
| 42 |
$params = array(
|
| 43 |
'%property' => $filters[$type]['title'],
|
| 44 |
'%value' => $value
|
| 45 |
);
|
| 46 |
}
|
| 47 |
if ($i++ > 0) {
|
| 48 |
$form['filters']['current'][] = array(
|
| 49 |
'#value' => t('<em>'.
|
| 50 |
$op.
|
| 51 |
'</em> where <strong>%property</strong> '.
|
| 52 |
_qop($qop).
|
| 53 |
' <strong>%value</strong>',
|
| 54 |
$params
|
| 55 |
).
|
| 56 |
($i == count($session) ? ')' : ''),
|
| 57 |
);
|
| 58 |
}
|
| 59 |
else {
|
| 60 |
$form['filters']['current'][] = array(
|
| 61 |
'#value' => t('(<strong>%property</strong> '.
|
| 62 |
_qop($qop).
|
| 63 |
' <strong>%value</strong>',
|
| 64 |
$params
|
| 65 |
).
|
| 66 |
($i == count($session) ? ')' : ''),
|
| 67 |
);
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
|
| 72 |
foreach ($filters as $key => $filter) {
|
| 73 |
$names[$key] = $filter['title'];
|
| 74 |
switch ($filter['form_type']) {
|
| 75 |
case 'select': {
|
| 76 |
$form['filters']['status'][$key] = array(
|
| 77 |
'#type' => 'select',
|
| 78 |
'#options' => $filter['options'],
|
| 79 |
);
|
| 80 |
} break;
|
| 81 |
case 'date': {
|
| 82 |
$form['filters']['status'][$key] = array(
|
| 83 |
'#type' => 'textfield',
|
| 84 |
'#size' => 20,
|
| 85 |
'#maxlength' => 25,
|
| 86 |
'#default_value' => 'now',
|
| 87 |
'#description' => 'You can enter this as an actual date (e.g. "'. date('M d Y') .'") or how long ago (e.g. "1 month 4 day 13hours ago")',
|
| 88 |
);
|
| 89 |
} break;
|
| 90 |
case 'id': {
|
| 91 |
$form['filters']['status'][$key] = array(
|
| 92 |
'#type' => 'textfield',
|
| 93 |
'#size' => 5,
|
| 94 |
'#maxsize' => 10,
|
| 95 |
);
|
| 96 |
} break;
|
| 97 |
case 'textfield': {
|
| 98 |
$form['filters']['status'][$key] = array(
|
| 99 |
'#type' => 'textfield',
|
| 100 |
'#size' => 30,
|
| 101 |
);
|
| 102 |
} break;
|
| 103 |
default: {
|
| 104 |
$autocomplete = '';
|
| 105 |
if ($filter['autocomplete']) {
|
| 106 |
$autocomplete = "profile/autocomplete/". $filter['autocomplete'];
|
| 107 |
}
|
| 108 |
switch ($filter['type']) {
|
| 109 |
case 'selection': {
|
| 110 |
$form['filters']['status'][$key] = array(
|
| 111 |
'#type' => 'select',
|
| 112 |
'#options' => $filter['options'],
|
| 113 |
'#autocomplete_path' => $autocomplete,
|
| 114 |
);
|
| 115 |
} break;
|
| 116 |
case 'checkbox': {
|
| 117 |
$form['filters']['status'][$key] = array(
|
| 118 |
'#type' => 'checkbox',
|
| 119 |
);
|
| 120 |
} break;
|
| 121 |
default: {
|
| 122 |
$form['filters']['status'][$key] = array(
|
| 123 |
'#type' => $filter['type'],
|
| 124 |
'#options' => $filter['options'],
|
| 125 |
'#autocomplete_path' => $autocomplete,
|
| 126 |
'#size' => 20,
|
| 127 |
);
|
| 128 |
} break;
|
| 129 |
} //End switch ($filter['type']).
|
| 130 |
} break;
|
| 131 |
}
|
| 132 |
}
|
| 133 |
|
| 134 |
$form['filters']['filter'] = array(
|
| 135 |
'#type' => 'radios',
|
| 136 |
'#options' => $names,
|
| 137 |
);
|
| 138 |
$form['filters']['buttons']['submit'] = array(
|
| 139 |
'#type' => 'submit',
|
| 140 |
'#value' => (count($session) ? t('Refine') : t('Filter'))
|
| 141 |
);
|
| 142 |
if (count($session)) {
|
| 143 |
$form['filters']['buttons']['undo'] = array(
|
| 144 |
'#type' => 'submit',
|
| 145 |
'#value' => t('Undo')
|
| 146 |
);
|
| 147 |
$form['filters']['buttons']['reset'] = array(
|
| 148 |
'#type' => 'submit',
|
| 149 |
'#value' => t('Reset')
|
| 150 |
);
|
| 151 |
}
|
| 152 |
|
| 153 |
$form['filters']['filters_ops'] = array(
|
| 154 |
'#type' => 'select',
|
| 155 |
'#options' => array('AND' => t('and'), ') OR (' => t('or'))
|
| 156 |
);
|
| 157 |
|
| 158 |
$form['filters']['filters_qops'] = array(
|
| 159 |
'#type' => 'select',
|
| 160 |
'#options' => array(
|
| 161 |
'=' => 'EQ',
|
| 162 |
'!=' => 'NE',
|
| 163 |
'<' => 'LT',
|
| 164 |
'>' => 'GT',
|
| 165 |
'<=' => 'LE',
|
| 166 |
'>=' => 'GE',
|
| 167 |
'LIKE' => 'CO',
|
| 168 |
'NOT LIKE' => 'NC',
|
| 169 |
'BEGINS WITH' => 'BE',
|
| 170 |
'ENDS WITH' => 'EN',
|
| 171 |
),
|
| 172 |
'#attributes' => array('title' => t("
|
| 173 |
'EQ' => 'is equal to'
|
| 174 |
'NE' => 'is not equal to'
|
| 175 |
'LT' => 'is less than'
|
| 176 |
'GT' => 'is greater than'
|
| 177 |
'LE' => 'is less than or equal to'
|
| 178 |
'GE' => 'is greater than or equal to'
|
| 179 |
'CO' => 'contains'
|
| 180 |
'NC' => 'does not contain'
|
| 181 |
'BE' => 'begins with'
|
| 182 |
'EN' => 'ends with'")
|
| 183 |
),
|
| 184 |
);
|
| 185 |
|
| 186 |
return $form;
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Helper function for translating symbols to language
|
| 191 |
*/
|
| 192 |
function _qop($qop) {
|
| 193 |
static $_qop = NULL;
|
| 194 |
if (!isset($_qop)) {
|
| 195 |
$_qop = array(
|
| 196 |
'=' => t('is equal to'),
|
| 197 |
'!=' => t('is not equal to'),
|
| 198 |
'<' => t('is less than'),
|
| 199 |
'>' => t('is greater than'),
|
| 200 |
'<=' => t('is less than or equal to'),
|
| 201 |
'>=' => t('is greater than or equal to'),
|
| 202 |
'LIKE' => t('contains'),
|
| 203 |
'NOT LIKE' => t('does not contain'),
|
| 204 |
'BEGINS WITH' => t('begins with'),
|
| 205 |
'ENDS WITH' => t('ends with'),
|
| 206 |
);
|
| 207 |
}
|
| 208 |
return isset($_qop[$qop]) ? $_qop[$qop] : $qop;
|
| 209 |
};
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Theme advuser administration filter form.
|
| 213 |
*/
|
| 214 |
function theme_advuser_filter_form($form) {
|
| 215 |
$output = '<div id="user-admin-filter">';
|
| 216 |
$output .= drupal_render($form['filters']);
|
| 217 |
$output .= '</div>';
|
| 218 |
$output .= drupal_render($form);
|
| 219 |
return $output;
|
| 220 |
}
|
| 221 |
|
| 222 |
/**
|
| 223 |
* Validate values entered.
|
| 224 |
*/
|
| 225 |
function advuser_filter_form_validate($form, &$form_state) {
|
| 226 |
$ret = FALSE;
|
| 227 |
|
| 228 |
if ($form_state['#id'] == 'advuser_filter_form') {
|
| 229 |
switch ($form_state['values']['filter']) {
|
| 230 |
case 'last_access': {
|
| 231 |
switch (strtolower($form_state['values']['last_access'])) {
|
| 232 |
case 'never': {
|
| 233 |
$form_state['values']['last_access'] = 0;
|
| 234 |
$ret = TRUE;
|
| 235 |
} break;;
|
| 236 |
|
| 237 |
case '0': {
|
| 238 |
$ret = TRUE;
|
| 239 |
} break;
|
| 240 |
|
| 241 |
default: {
|
| 242 |
if (!empty($form_state['values']['last_access']) && strtotime($form_state['values']['last_access']) <= 0) {
|
| 243 |
form_set_error('date', t('You have to specify a valid date to filter by Accessed.'));
|
| 244 |
$ret = FALSE;
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
$form_state['values']['last_access'] = strtotime($form_state['values']['last_access']);
|
| 248 |
$ret = TRUE;
|
| 249 |
}
|
| 250 |
} break;
|
| 251 |
}
|
| 252 |
} break;
|
| 253 |
|
| 254 |
case 'created': {
|
| 255 |
if (!empty($form_state['values']['created']) && strtotime($form_state['values']['created']) <= 0) {
|
| 256 |
form_set_error('date', t('You have to specify a valid date to filter by Created.'));
|
| 257 |
$ret = FALSE;
|
| 258 |
}
|
| 259 |
else {
|
| 260 |
$form_state['values']['created'] = strtotime($form_state['values']['created']);
|
| 261 |
$ret = TRUE;
|
| 262 |
}
|
| 263 |
|
| 264 |
} break;
|
| 265 |
}
|
| 266 |
}
|
| 267 |
return $ret;
|
| 268 |
}
|
| 269 |
|
| 270 |
/**
|
| 271 |
* List advuser administration filters that can be applied.
|
| 272 |
*/
|
| 273 |
function advuser_filters() {
|
| 274 |
// Regular filters
|
| 275 |
$filters = array();
|
| 276 |
$options = array();
|
| 277 |
$t_module = t('module');
|
| 278 |
foreach (module_list() as $module) {
|
| 279 |
if ($permissions = module_invoke($module, 'perm')) {
|
| 280 |
asort($permissions);
|
| 281 |
foreach ($permissions as $permission) {
|
| 282 |
$options["$module $t_module"][$permission] = t($permission);
|
| 283 |
}
|
| 284 |
}
|
| 285 |
}
|
| 286 |
|
| 287 |
ksort($options);
|
| 288 |
$filters['permission'] = array(
|
| 289 |
'title' => t('Permission'),
|
| 290 |
'where' => " ((u.uid %in (SELECT ur.uid FROM {users_roles} ur WHERE ur.rid %in (SELECT p.rid FROM {permission} p WHERE p.perm %op '%s'))) %andor u.uid %eq 1)",
|
| 291 |
'options' => $options,
|
| 292 |
'form_type' => 'select',
|
| 293 |
);
|
| 294 |
|
| 295 |
$filters['status'] = array(
|
| 296 |
'title' => t('Status'),
|
| 297 |
'where' => "u.status %op '%s'",
|
| 298 |
'options' => array(1 => t('active'), 0 => t('blocked')),
|
| 299 |
'form_type' => 'select',
|
| 300 |
);
|
| 301 |
|
| 302 |
$filters['created'] = array(
|
| 303 |
'title' => t('Created'),
|
| 304 |
'where' => "u.created %op '%s'",
|
| 305 |
'form_type' => 'date',
|
| 306 |
);
|
| 307 |
|
| 308 |
$filters['last_access'] = array(
|
| 309 |
'title' => t('Accessed'),
|
| 310 |
'where' => "u.access %op '%s'",
|
| 311 |
'form_type' => 'date',
|
| 312 |
);
|
| 313 |
|
| 314 |
$filters['email'] = array(
|
| 315 |
'title' => t('Email'),
|
| 316 |
'where' => "u.mail %op '%s'",
|
| 317 |
'form_type' => 'textfield',
|
| 318 |
);
|
| 319 |
|
| 320 |
$filters['uid'] = array(
|
| 321 |
'title' => t('User Id'),
|
| 322 |
'where' => "u.uid %op %d",
|
| 323 |
'form_type' => 'id',
|
| 324 |
);
|
| 325 |
|
| 326 |
$filters['username'] = array(
|
| 327 |
'title' => t('Username'),
|
| 328 |
'where' => "u.name %op '%s'",
|
| 329 |
'form_type' => 'textfield',
|
| 330 |
);
|
| 331 |
|
| 332 |
$roles = advuser_user_roles();
|
| 333 |
if (count($roles)) {
|
| 334 |
$filters['user_roles'] = array(
|
| 335 |
'title' => t('Role'),
|
| 336 |
'where' => "ur.rid %op %d",
|
| 337 |
'form_type' => 'select',
|
| 338 |
'options' => $roles,
|
| 339 |
);
|
| 340 |
}
|
| 341 |
|
| 342 |
$profile_fields = advuser_profile_fields();
|
| 343 |
foreach ($profile_fields as $field) {
|
| 344 |
// Build array of options if they exist
|
| 345 |
$opts = NULL;
|
| 346 |
if ( ! empty($field->options) ) {
|
| 347 |
$opts = array();
|
| 348 |
foreach ( explode("\n", $field->options) as $opt ) {
|
| 349 |
$opt = trim($opt);
|
| 350 |
$opts[$opt] = $opt;
|
| 351 |
}
|
| 352 |
}
|
| 353 |
// Each user defined profile field needs a unique table identifier for the
|
| 354 |
// JOIN and WHERE clauses.
|
| 355 |
// TODO: Make sure the $field->name contains valid information for a table
|
| 356 |
// identifier. This comment is to identify the source of a problem yet
|
| 357 |
// to be discovered.
|
| 358 |
$pv = $field->name;
|
| 359 |
$filters[$field->name] = array(
|
| 360 |
'title' => check_plain($field->title),
|
| 361 |
'type' => $field->type,
|
| 362 |
'class' => $field->name,
|
| 363 |
'where' => "$pv.value %op '%s' AND $pv.uid = u.uid",
|
| 364 |
'options' => $opts,
|
| 365 |
'autocomplete' => $field->autocomplete ? $field->fid : FALSE,
|
| 366 |
);
|
| 367 |
}
|
| 368 |
return $filters;
|
| 369 |
}
|
| 370 |
|
| 371 |
/**
|
| 372 |
* Build query for advuser administration filters based on session.
|
| 373 |
*/
|
| 374 |
function advuser_build_filter_query() {
|
| 375 |
$filters = advuser_filters();
|
| 376 |
|
| 377 |
// Build query
|
| 378 |
$where = $args = $join = array();
|
| 379 |
|
| 380 |
foreach ($_SESSION['advuser_overview_filter'] as $filter) {
|
| 381 |
list($key, $value, $op, $qop) = $filter;
|
| 382 |
// This checks to see if this permission filter is an enabled permission
|
| 383 |
// for the authenticated role. If so, then all users would be listed, and
|
| 384 |
// we can skip adding it to the filter query.
|
| 385 |
switch ($key) {
|
| 386 |
case 'permission': {
|
| 387 |
$account = new stdClass();
|
| 388 |
$account->uid = 'advuser_filter';
|
| 389 |
$account->roles = array(DRUPAL_AUTHENTICATED_RID => 1);
|
| 390 |
if (user_access($value, $account)) {
|
| 391 |
continue;
|
| 392 |
}
|
| 393 |
} break;
|
| 394 |
case 'created':
|
| 395 |
case 'last_access': {
|
| 396 |
$value = strtotime($value);
|
| 397 |
} break;
|
| 398 |
}
|
| 399 |
|
| 400 |
$arg_prefix = $arg_suffix = NULL;
|
| 401 |
switch ($qop) {
|
| 402 |
case 'NOT LIKE':
|
| 403 |
case 'LIKE': {
|
| 404 |
$arg_prefix = $arg_suffix = '%';
|
| 405 |
} break;
|
| 406 |
case 'BEGINS WITH': {
|
| 407 |
$qop = 'LIKE';
|
| 408 |
$arg_suffix = '%';
|
| 409 |
} break;
|
| 410 |
case 'ENDS WITH': {
|
| 411 |
$qop = 'LIKE';
|
| 412 |
$arg_prefix = '%';
|
| 413 |
} break;
|
| 414 |
}
|
| 415 |
|
| 416 |
switch ($qop) {
|
| 417 |
case '!=':
|
| 418 |
case 'NOT LIKE': {
|
| 419 |
$in = 'NOT IN';
|
| 420 |
$eq = '!=';
|
| 421 |
$andor = 'AND';
|
| 422 |
} break;
|
| 423 |
default: {
|
| 424 |
$in = 'IN';
|
| 425 |
$eq = '=';
|
| 426 |
$andor = 'OR';
|
| 427 |
}
|
| 428 |
}
|
| 429 |
|
| 430 |
$_where = $op.' '.str_ireplace("%op", $qop, $filters[$key]['where']);
|
| 431 |
$_where = str_ireplace("%eq", $eq, $_where);
|
| 432 |
$_where = str_ireplace("%andor", $andor, $_where);
|
| 433 |
$where[] = str_ireplace("%in", $in, $_where);
|
| 434 |
$args[] = $arg_prefix . $value . $arg_suffix;
|
| 435 |
$join[] = $filters[$key]['join'];
|
| 436 |
}
|
| 437 |
|
| 438 |
$where = count($where) ? 'AND ('. implode(' ', $where) . ')' : '';
|
| 439 |
$join = count($join) ? ' '. implode(' ', array_unique($join)) : '';
|
| 440 |
|
| 441 |
return array(
|
| 442 |
'where' => $where,
|
| 443 |
'join' => $join,
|
| 444 |
'args' => $args,
|
| 445 |
);
|
| 446 |
}
|
| 447 |
|
| 448 |
/**
|
| 449 |
* Process result from user administration filter form.
|
| 450 |
*/
|
| 451 |
function advuser_filter_form_submit($form, &$form_state) {
|
| 452 |
$op = $form_state['values']['op'];
|
| 453 |
$filters = advuser_filters();
|
| 454 |
$ret = 'admin/user/user/advuser';
|
| 455 |
|
| 456 |
switch ($op) {
|
| 457 |
case t('Undo'):
|
| 458 |
array_pop($_SESSION['advuser_overview_filter']);
|
| 459 |
break;
|
| 460 |
case t('Reset'):
|
| 461 |
$_SESSION['advuser_overview_filter'] = array();
|
| 462 |
break;
|
| 463 |
case t('Update'):
|
| 464 |
$ret = NULL;
|
| 465 |
break;
|
| 466 |
case t('Filter'):
|
| 467 |
case t('Refine'):
|
| 468 |
default:
|
| 469 |
if (isset($form_state['values']['filter'])) {
|
| 470 |
$filter = $form_state['values']['filter'];
|
| 471 |
if ($filters[$filter]['form_type'] == 'select') {
|
| 472 |
// Merge an array of arrays into one if necessary.
|
| 473 |
$options = $filter == 'permission' ? call_user_func_array('array_merge', $filters[$filter]['options']) : $filters[$filter]['options'] ;
|
| 474 |
if (isset($options[$form_state['values'][$filter]])) {
|
| 475 |
$_SESSION['advuser_overview_filter'][] = array(
|
| 476 |
$filter,
|
| 477 |
$form_state['values'][$filter],
|
| 478 |
$form_state['values']['filters_ops'],
|
| 479 |
$form_state['values']['filters_qops'],
|
| 480 |
);
|
| 481 |
}
|
| 482 |
} else {
|
| 483 |
if (isset($form_state['values'][$filter])) {
|
| 484 |
$_SESSION['advuser_overview_filter'][] = array(
|
| 485 |
$filter,
|
| 486 |
$form_state['values'][$filter],
|
| 487 |
$form_state['values']['filters_ops'],
|
| 488 |
$form_state['values']['filters_qops']
|
| 489 |
);
|
| 490 |
}
|
| 491 |
}
|
| 492 |
}
|
| 493 |
break;
|
| 494 |
}
|
| 495 |
$form_state['#redirect'] = $ret;
|
| 496 |
}
|
| 497 |
|
| 498 |
/**
|
| 499 |
* Theme user administration filter selector.
|
| 500 |
*/
|
| 501 |
function theme_advuser_filters($form) {
|
| 502 |
|
| 503 |
$output = '<ul>';
|
| 504 |
if (sizeof($form['current'])) {
|
| 505 |
foreach (element_children($form['current']) as $key) {
|
| 506 |
$output .= '<li>' . drupal_render($form['current'][$key]) . '</li>';
|
| 507 |
}
|
| 508 |
}
|
| 509 |
$output .= '</ul>';
|
| 510 |
|
| 511 |
$output .= '<div class="container-inline" id="user-admin-buttons">' . drupal_render($form['buttons']) . '</div>';
|
| 512 |
$output .= '<br/><br/>';
|
| 513 |
$output .= '<table class="multiselect ">';
|
| 514 |
foreach (element_children($form['filter']) as $key) {
|
| 515 |
$output .= '<tr>';
|
| 516 |
$output .= '<td>' . (sizeof($form['current']) ? drupal_render($form['filters_ops']) : '') . '</td>';
|
| 517 |
$output .= '<td class="a">' . drupal_render($form['filter'][$key]) . '</td>';
|
| 518 |
$output .= '<td>' . $f . drupal_render($form['filters_qops']) . '</td>';
|
| 519 |
$output .= '<td class="b">' . drupal_render($form['status'][$key]) . '</td>';
|
| 520 |
$output .= '</tr>';
|
| 521 |
}
|
| 522 |
|
| 523 |
foreach (element_children($form['status']) as $key) {
|
| 524 |
$output .= drupal_render($form['status'][$key]);
|
| 525 |
}
|
| 526 |
$output .= '</td></tr>';
|
| 527 |
|
| 528 |
$output .= '</table>';
|
| 529 |
|
| 530 |
return $output;
|
| 531 |
}
|
| 532 |
|
| 533 |
// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
|