advuser_filters.inc in Advanced User 5.2
Same filename and directory in other branches
Advanced user module allows you to select users based on an advanced set of filtering and apply actions to block, unblock, delete or email the selected users.
File
advuser_filters.incView source
<?php
/**
* @file
* Advanced user module allows you to select users based on an advanced set of
* filtering and apply actions to block, unblock, delete or email the selected
* users.
*
**/
/**
* Return form for advuser administration filters.
*/
function advuser_filter_form() {
$session =& $_SESSION['advuser_overview_filter'];
$session = is_array($session) ? $session : array();
$filters = advuser_filters();
$i = 0;
$form['filters'] = array(
'#type' => 'fieldset',
'#title' => t('Show only users where'),
'#theme' => 'advuser_filters',
'#collapsible' => TRUE,
);
foreach ($session as $filter) {
list($type, $value, $op, $qop) = $filter;
// Merge an array of arrays into one if necessary.
if ($filters[$type]['form_type'] == 'select') {
$options = $type == 'permission' ? call_user_func_array('array_merge', $filters[$type]['options']) : $filters[$type]['options'];
$params = array(
'%property' => $filters[$type]['title'],
'%value' => $options[$value],
);
}
else {
$params = array(
'%property' => $filters[$type]['title'],
'%value' => $value,
);
}
if ($i++ > 0) {
$form['filters']['current'][] = array(
'#value' => t('<em>' . $op . '</em> where <strong>%property</strong> ' . _qop($qop) . ' <strong>%value</strong>', $params) . ($i == count($session) ? ')' : ''),
);
}
else {
$form['filters']['current'][] = array(
'#value' => t('(<strong>%property</strong> ' . _qop($qop) . ' <strong>%value</strong>', $params) . ($i == count($session) ? ')' : ''),
);
}
}
foreach ($filters as $key => $filter) {
$names[$key] = $filter['title'];
switch ($filter['form_type']) {
case 'select':
$form['filters']['status'][$key] = array(
'#type' => 'select',
'#options' => $filter['options'],
);
break;
case 'date':
$form['filters']['status'][$key] = array(
'#type' => 'textfield',
'#size' => 20,
'#maxlength' => 25,
'#default_value' => 'now',
);
break;
case 'id':
$form['filters']['status'][$key] = array(
'#type' => 'textfield',
'#size' => 5,
'#maxsize' => 10,
);
break;
case 'textfield':
$form['filters']['status'][$key] = array(
'#type' => 'textfield',
'#size' => 30,
);
break;
default:
$autocomplete = '';
if ($filter['autocomplete']) {
$autocomplete = "profile/autocomplete/" . $filter['autocomplete'];
}
switch ($filter['type']) {
case 'selection':
$form['filters']['status'][$key] = array(
'#type' => 'select',
'#options' => $filter['options'],
'#autocomplete_path' => $autocomplete,
);
break;
case 'checkbox':
$form['filters']['status'][$key] = array(
'#type' => 'checkbox',
);
break;
default:
$form['filters']['status'][$key] = array(
'#type' => $filter['type'],
'#options' => $filter['options'],
'#autocomplete_path' => $autocomplete,
'#size' => 20,
);
break;
}
//End switch ($filter['type']).
break;
}
}
$form['filters']['filter'] = array(
'#type' => 'radios',
'#options' => $names,
);
$form['filters']['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => count($session) ? t('Refine') : t('Filter'),
);
if (count($session)) {
$form['filters']['buttons']['undo'] = array(
'#type' => 'submit',
'#value' => t('Undo'),
);
$form['filters']['buttons']['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset'),
);
}
$form['filters']['filters_ops'] = array(
'#type' => 'select',
'#options' => array(
'AND' => t('and'),
') OR (' => t('or'),
),
);
$form['filters']['filters_qops'] = array(
'#type' => 'select',
'#options' => array(
'=' => 'EQ',
'!=' => 'NE',
'<' => 'LT',
'>' => 'GT',
'<=' => 'LE',
'>=' => 'GE',
'LIKE' => 'CO',
'NOT LIKE' => 'NC',
'BEGINS WITH' => 'BE',
'ENDS WITH' => 'EN',
),
'#attributes' => array(
'title' => t("\n 'EQ' => 'is equal to'\n 'NE' => 'is not equal to'\n 'LT' => 'is less than'\n 'GT' => 'is greater than'\n 'LE' => 'is less than or equal to'\n 'GE' => 'is greater than or equal to'\n 'CO' => 'contains'\n 'NC' => 'does not contain'\n 'BE' => 'begins with'\n 'EN' => 'ends with'"),
),
);
return $form;
}
/**
* Helper function for translating symbols to language
*/
function _qop($qop) {
static $_qop = NULL;
if (!isset($_qop)) {
$_qop = array(
'=' => t('is equal to'),
'!=' => t('is not equal to'),
'<' => t('is less than'),
'>' => t('is greater than'),
'<=' => t('is less than or equal to'),
'>=' => t('is greater than or equal to'),
'LIKE' => t('contains'),
'NOT LIKE' => t('does not contain'),
'BEGINS WITH' => t('begins with'),
'ENDS WITH' => t('ends with'),
);
}
return isset($_qop[$qop]) ? $_qop[$qop] : $qop;
}
/**
* Theme advuser administration filter form.
*/
function theme_advuser_filter_form($form) {
$output = '<div id="user-admin-filter">';
$output .= drupal_render($form['filters']);
$output .= '</div>';
$output .= drupal_render($form);
return $output;
}
/**
* Validate values entered.
*/
function advuser_filter_form_validate($fid, &$fval) {
$ret = FALSE;
if ($fid == 'advuser_filter_form') {
switch ($fval['filter']) {
case 'last_access':
switch (strtolower($fval['last_access'])) {
case 'never':
$fval['last_access'] = 0;
$ret = TRUE;
break;
case '0':
$ret = TRUE;
break;
default:
if (!empty($fval['last_access']) && strtotime($fval['last_access']) <= 0) {
form_set_error('date', t('You have to specify a valid date to filter by Accessed.'));
$ret = FALSE;
}
else {
$fval['last_access'] = strtotime($fval['last_access']);
$ret = TRUE;
}
break;
}
break;
case 'created':
if (!empty($fval['created']) && strtotime($fval['created']) <= 0) {
form_set_error('date', t('You have to specify a valid date to filter by Created.'));
$ret = FALSE;
}
else {
$fval['created'] = strtotime($fval['created']);
$ret = TRUE;
}
break;
}
}
return $ret;
}
/**
* List advuser administration filters that can be applied.
*/
function advuser_filters() {
// Regular filters
$filters = array();
$options = array();
$t_module = t('module');
foreach (module_list() as $module) {
if ($permissions = module_invoke($module, 'perm')) {
asort($permissions);
foreach ($permissions as $permission) {
$options["{$module} {$t_module}"][$permission] = t($permission);
}
}
}
ksort($options);
$filters['permission'] = array(
'title' => t('Permission'),
'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)",
'options' => $options,
'form_type' => 'select',
);
$filters['status'] = array(
'title' => t('Status'),
'where' => "u.status %op '%s'",
'options' => array(
1 => t('active'),
0 => t('blocked'),
),
'form_type' => 'select',
);
$filters['created'] = array(
'title' => t('Created'),
'where' => "u.created %op '%s'",
'form_type' => 'date',
);
$filters['last_access'] = array(
'title' => t('Accessed'),
'where' => "u.access %op '%s'",
'form_type' => 'date',
);
$filters['email'] = array(
'title' => t('Email'),
'where' => "u.mail %op '%s'",
'form_type' => 'textfield',
);
$filters['uid'] = array(
'title' => t('User Id'),
'where' => "u.uid %op %d",
'form_type' => 'id',
);
$roles = advuser_user_roles();
if (count($roles)) {
$filters['user_roles'] = array(
'title' => t('Role'),
'where' => "ur.rid %op %d",
'form_type' => 'select',
'options' => $roles,
);
}
$profile_fields = advuser_profile_fields();
foreach ($profile_fields as $field) {
// Build array of options if they exist
$opts = NULL;
if (!empty($field->options)) {
$opts = array();
foreach (explode("\n", $field->options) as $opt) {
$opt = trim($opt);
$opts[$opt] = $opt;
}
}
// Each user defined profile field needs a unique table identifier for the
// JOIN and WHERE clauses.
// TODO: Make sure the $field->name contains valid information for a table
// identifier. This comment is to identify the source of a problem yet
// to be discovered.
$pv = $field->name;
$filters[$field->name] = array(
'title' => check_plain($field->title),
'type' => $field->type,
'class' => $field->name,
'where' => "{$pv}.value %op '%s' AND {$pv}.uid = u.uid",
'options' => $opts,
'autocomplete' => $field->autocomplete ? $field->fid : FALSE,
);
}
return $filters;
}
/**
* Build query for advuser administration filters based on session.
*/
function advuser_build_filter_query() {
$filters = advuser_filters();
// Build query
$where = $args = $join = array();
foreach ($_SESSION['advuser_overview_filter'] as $filter) {
list($key, $value, $op, $qop) = $filter;
// This checks to see if this permission filter is an enabled permission
// for the authenticated role. If so, then all users would be listed, and
// we can skip adding it to the filter query.
switch ($key) {
case 'permission':
$account = new stdClass();
$account->uid = 'advuser_filter';
$account->roles = array(
DRUPAL_AUTHENTICATED_RID => 1,
);
if (user_access($value, $account)) {
continue;
}
break;
case 'created':
case 'last_access':
$value = strtotime($value);
break;
}
$arg_prefix = $arg_suffix = NULL;
switch ($qop) {
case 'NOT LIKE':
case 'LIKE':
$arg_prefix = $arg_suffix = '%';
break;
case 'BEGINS WITH':
$qop = 'LIKE';
$arg_suffix = '%';
break;
case 'ENDS WITH':
$qop = 'LIKE';
$arg_prefix = '%';
break;
}
switch ($qop) {
case '!=':
case 'NOT LIKE':
$in = 'NOT IN';
$eq = '!=';
$andor = 'AND';
break;
default:
$in = 'IN';
$eq = '=';
$andor = 'OR';
}
$_where = $op . ' ' . str_ireplace("%op", $qop, $filters[$key]['where']);
$_where = str_ireplace("%eq", $eq, $_where);
$_where = str_ireplace("%andor", $andor, $_where);
$where[] = str_ireplace("%in", $in, $_where);
$args[] = $arg_prefix . $value . $arg_suffix;
$join[] = $filters[$key]['join'];
}
$where = count($where) ? 'AND (' . implode(' ', $where) . ')' : '';
$join = count($join) ? ' ' . implode(' ', array_unique($join)) : '';
return array(
'where' => $where,
'join' => $join,
'args' => $args,
);
}
/**
* Process result from user administration filter form.
*/
function advuser_filter_form_submit($form_id, $form_values) {
$op = $form_values['op'];
$filters = advuser_filters();
$ret = 'admin/user/user/advuser';
switch ($op) {
case t('Filter'):
case t('Refine'):
if (isset($form_values['filter'])) {
$filter = $form_values['filter'];
if ($filters[$filter]['form_type'] == 'select') {
// Merge an array of arrays into one if necessary.
$options = $filter == 'permission' ? call_user_func_array('array_merge', $filters[$filter]['options']) : $filters[$filter]['options'];
if (isset($options[$form_values[$filter]])) {
$_SESSION['advuser_overview_filter'][] = array(
$filter,
$form_values[$filter],
$form_values['filters_ops'],
$form_values['filters_qops'],
);
}
}
else {
if (isset($form_values[$filter])) {
$_SESSION['advuser_overview_filter'][] = array(
$filter,
$form_values[$filter],
$form_values['filters_ops'],
$form_values['filters_qops'],
);
}
}
}
break;
case t('Undo'):
array_pop($_SESSION['advuser_overview_filter']);
break;
case t('Reset'):
$_SESSION['advuser_overview_filter'] = array();
break;
case t('Update'):
$ret = NULL;
break;
}
return $ret;
}
/**
* Theme user administration filter selector.
*/
function theme_advuser_filters($form) {
$output = '<ul>';
if (sizeof($form['current'])) {
foreach (element_children($form['current']) as $key) {
$output .= '<li>' . drupal_render($form['current'][$key]) . '</li>';
}
}
$output .= '</ul>';
$output .= '<div class="container-inline" id="user-admin-buttons">' . drupal_render($form['buttons']) . '</div>';
$output .= '<br/><br/>';
$output .= '<table class="multiselect ">';
foreach (element_children($form['filter']) as $key) {
$output .= '<tr>';
$output .= '<td>' . (sizeof($form['current']) ? drupal_render($form['filters_ops']) : '') . '</td>';
$output .= '<td class="a">' . drupal_render($form['filter'][$key]) . '</td>';
$output .= '<td>' . $f . drupal_render($form['filters_qops']) . '</td>';
$output .= '<td class="b">' . drupal_render($form['status'][$key]) . '</td>';
$output .= '</tr>';
}
foreach (element_children($form['status']) as $key) {
$output .= drupal_render($form['status'][$key]);
}
$output .= '</td></tr>';
$output .= '</table>';
return $output;
}
// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
Functions
Name | Description |
---|---|
advuser_build_filter_query | Build query for advuser administration filters based on session. |
advuser_filters | List advuser administration filters that can be applied. |
advuser_filter_form | Return form for advuser administration filters. |
advuser_filter_form_submit | Process result from user administration filter form. |
advuser_filter_form_validate | Validate values entered. |
theme_advuser_filters | Theme user administration filter selector. |
theme_advuser_filter_form | Theme advuser administration filter form. |
_qop | Helper function for translating symbols to language |