Index: CHANGELOG.txt
===================================================================
RCS file: CHANGELOG.txt
diff -N CHANGELOG.txt
--- CHANGELOG.txt	23 Jun 2009 15:30:25 -0000	1.1.4.3
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,5 +0,0 @@
-$Id: CHANGELOG.txt,v 1.1.4.3 2009/06/23 15:30:25 earnie Exp $
-
-Advanced User 6.x-3, 2009-06-23
------------------------------------
-#470605 Need to remember the page the action was executed from.
Index: advuser.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/advuser/advuser.module,v
retrieving revision 1.14.2.7.2.3
diff -u -p -r1.14.2.7.2.3 advuser.module
--- advuser.module	11 Mar 2010 14:22:12 -0000	1.14.2.7.2.3
+++ advuser.module	8 Apr 2010 15:28:44 -0000
@@ -11,7 +11,7 @@
 /**
  * @constants
  */
-define('ADVUSER_SUBSTITUTION_TEXT', '<strong>Substitution variables</strong> available in subject and email body<br/><em> %user_name, %user_email, %user_status, %user_created, %user_theme, %user_timezone, %user_language, %user_signature, %site, %uri, %google_user (search google for user email), %yahoo_user (search yahoo for user email)</em>');
+define('ADVUSER_SUBSTITUTION_TEXT', '<strong>Substitution variables</strong> available in subject and email body<br/><em> %user_name, %user_email, %user_status, %user_created, %user_theme, %user_timezone, %user_language, %user_signature, %site, %uri</em>');
 
 /**
  * Implementation of hook_perm().
@@ -37,310 +37,34 @@ function advuser_menu() {
     'page arguments' => array('advuser_settings'),
     'access arguments' => array('administer advuser'),
     'type' => MENU_NORMAL_ITEM,
-    'file' => 'advuser_filters.inc',
+    'file' => 'forms/advuser_settings.inc',
   );
 
   $items['admin/user/user/advuser'] = array(
     'title' => t('Advanced'),
     'description' => t('List, add, edit and email users.'),
-    'page callback' => 'advuser_admin', 
-    'page arguments' => array('list'), 
+    'page callback' => 'drupal_get_form', 
+    'page arguments' => array('advuser_admin'), 
     'access arguments' => array('access advuser'),
     'type' => MENU_LOCAL_TASK,
-    'file' => 'advuser_filters.inc',
+    'file' => 'forms/advuser_admin.inc',
   );
 
   return $items;
 }
 
-function advuser_admin($callback_arg = '') {
-  $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
-
-  switch ($op) {
-    default: {
-      if ($_POST['accounts'] && $_POST['operation'] == 'delete') {
-        $output = drupal_get_form('advuser_multiple_delete_confirm');
-      } 
-      elseif ($_POST['accounts'] && $_POST['operation'] == 'email') {
-        $output = drupal_get_form('advuser_multiple_email_confirm');
-      } 
-      else {
-        $output = drupal_get_form('advuser_filter_form');
-        $output .= drupal_get_form('advuser_admin_account');
-      }
-    } break;
-  }
-  return $output;
-}
-
-function advuser_admin_account() {
-  $filter = advuser_build_filter_query();
-  $header = array(
-    array(),
-    array('data' => t('Username'), 'field' => 'u.name'),
-    array('data' => t('Status'), 'field' => 'u.status'),
-  );
-  $roles = advuser_user_roles();
-  if (count($roles)) {
-    $header[] = t('Roles');
-  }
-  $header = array_merge($header, array(
-    array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
-    array('data' => t('Last access'), 'field' => 'u.access'),
-  ));
-
-  $query = '';
-  $ff = array();
-  $pf = array();
-  foreach (advuser_profile_fields() as $field) {
-    $ff[] = array('data'=>t($field->title), 'field'=>"$field->name.value");
-    $pf[] = "LEFT JOIN {profile_values} $field->name ON $field->name.fid = $field->fid AND $field->name.uid = u.uid";
-  }
-  $header = array_merge($header, $ff);
-  $header[] = t('Operations');
-
-  $filter['join'] .= count($pf) ? ' ' . implode(' ', array_unique($pf)) : NULL;
-
-  $sql = 'SELECT DISTINCT u.uid, u.name, u.status, u.created, u.access '.$pquery.' FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
-
-  $sql .= tablesort_sql($header);
-  $query_count = 'SELECT COUNT(DISTINCT u.uid) FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
-  $result = pager_query($sql, variable_get('advuser_listno', 50), 0, $query_count, $filter['args']);
-
-  $form['options'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Update options'),
-    '#prefix' => '<div class="container-inline">',
-    '#suffix' => '</div>',
-  );
-  $options = array();
-
-  $operations = module_invoke_all('user_operations');
-  $operations = array_merge($operations,module_invoke_all('advuser_operations'));
-  foreach ($operations as $operation => $array) {
-    $options[$operation] = $array['label'];
-  }
-  $form['options']['operation'] = array(
-    '#type' => 'select',
-    '#options' => $options,
-    '#default_value' => 'unblock',
-  );
-  $form['options']['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Update'),
-  );
-
-  $destination = drupal_get_destination();
-
-  list($junk, $go) = split('=', $destination);
-  $form['destination'] = array('#type' => 'value', '#value' => urldecode($go));
-
-  $status = array(t('blocked'), t('active'));
-  $roles = advuser_user_roles();
-
-  while ($account = db_fetch_object($result)) {
-    $accounts[$account->uid] = '';
-    $form['name'][$account->uid] = array('#value' => theme('username', $account));
-    $form['status'][$account->uid] =  array('#value' => $status[$account->status]);
-    $users_roles = array();
-    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid);
-    while ($user_role = db_fetch_object($roles_result)) {
-      $users_roles[] = $roles[$user_role->rid];
-    }
-    asort($users_roles);
-    $form['roles'][$account->uid][0] = array('#value' => theme('item_list', $users_roles));
-    $form['member_for'][$account->uid] = array('#value' => format_interval(time() - $account->created));
-    $form['last_access'][$account->uid] =  array('#value' => $account->access ? t('@time ago', array('@time' => format_interval(time() - $account->access))) : t('never'));
-    if (module_exists('profile')) {
-      profile_load_profile($account);
-    }
-    foreach(advuser_profile_fields() as $field) {
-      $form[$field->name][$account->uid] = array('#value' => profile_view_field($account, $field));
-    }
-    $fv = l(t('edit'), "user/$account->uid/edit", array('query' => $destination));
-    if ($account->uid != 1) {
-      $fv .= ' | ' . l(t('delete'), "user/$account->uid/delete", array('query' => $destination));
-    }
-    $form['operations'][$account->uid] = array('#value' => $fv);
-  }
-  $form['accounts'] = array(
-    '#type' => 'checkboxes',
-    '#options' => $accounts
-  );
-  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
-
-  return $form;
-}
-
 /**
  * hook_theme implementation  
  */
 function advuser_theme() {
   return array(
-    'advuser_admin_account' => array(
-      'arguments' => array('form' => NULL),
-    ),
-    'advuser_filters' => array(
+    'advuser_admin' => array(
       'arguments' => array('form' => NULL),
     ),
   );
 }
 
 /**
- * Theme user administration overview.
- */
-function theme_advuser_admin_account($form) {
-  static $profile_fields = array();
-  // Overview table:
-  $header = array(
-    theme('table_select_header_cell'),
-    array('data' => t('Username'), 'field' => 'u.name'),
-    array('data' => t('Status'), 'field' => 'u.status'),
-  );
-  $roles = advuser_user_roles();
-  if (count($roles)) {
-    $header[] = t('Roles');
-  }
-  $header = array_merge($header, array(
-    array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
-    array('data' => t('Last access'), 'field' => 'u.access'),
-  ));
-
-  $ff = array();
-  foreach (advuser_profile_fields() as $field) {
-    $ff[] = array('data'=>t($field->title), 'field'=>$field->name);
-  }
-  $header = array_merge($header, $ff);
-  $header[] = t('Operations');
-
-  $output = drupal_render($form['options']);
-  if (isset($form['name']) && is_array($form['name'])) {
-    foreach (element_children($form['name']) as $key) {
-      $row = array(
-        drupal_render($form['accounts'][$key]),
-        drupal_render($form['name'][$key]),
-        drupal_render($form['status'][$key]),
-      );
-      $roles = advuser_user_roles();
-      if (count($roles)) {
-        $row[] = drupal_render($form['roles'][$key]);
-      }
-      $row = array_merge($row, array(
-        drupal_render($form['member_for'][$key]),
-        drupal_render($form['last_access'][$key]),
-      ));
-
-      if (module_exists('profile')) {
-        $fields = variable_get('advuser_profile_fields', NULL);
-
-        if (is_array($fields)) {
-          foreach ( $fields as $fid => $value) {
-            if ( $value ) {
-              if (empty($profile_fields[$fid])) {
-
-                $field = db_fetch_object(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
-                $profile_fields[$fid] = $field;
-              }
-              else {
-                $field = $profile_fields[$fid];
-              }
-              $row[] = drupal_render($form[$field->name][$key]);
-            }
-          }
-        }
-      }
-      $row[] = drupal_render($form['operations'][$key]);
-      $rows[] = $row;
-    }
-  }
-  else  {
-    $rows[] = array(array('data' => t('No users available.'), 'colspan' => '7'));
-  }
-
-  $output .= theme('table', $header, $rows);
-  if ($form['pager']['#value']) {
-    $output .= drupal_render($form['pager']);
-  }
-
-  $output .= drupal_render($form);
-
-  return $output;
-}
-
-/**
- * Submit the user administration update form.
- */
-function advuser_admin_account_submit($form, &$form_state) {
-  $operations = module_invoke_all('user_operations', $form_state);
-  $operations = array_merge($operations,module_invoke_all('advuser_operations'));
-  $operation = $operations[$form_state['values']['operation']];
-  $destination = $form_state['values']['destination'];
-  // Filter out unchecked accounts.
-  $accounts = array_filter($form_state['values']['accounts']);
-  if ($function = $operation['callback']) {
-    // Add in callback arguments if present.
-    if (isset($operation['callback arguments'])) {
-      $args = array_merge(array($accounts), $operation['callback arguments']);
-    }
-    else {
-      $args = array($accounts);
-    }
-    call_user_func_array($function, $args);
-
-    cache_clear_all('*', 'cache_menu', TRUE);
-    drupal_set_message(t('The update has been performed.'));
-    extract(parse_url($destination));
-    drupal_goto($path, $query);
-  }
-}
-
-function advuser_admin_account_validate($form, &$form_state) {
-  $form_state['values']['accounts'] = array_filter($form_state['values']['accounts']);
-  if (count($form_state['values']['accounts']) == 0) {
-    form_set_error('', t('No users selected.'));
-  }
-}
-
-function advuser_multiple_delete_confirm() {
-  $edit = $_POST;
-
-  $form['accounts'] = array(
-    '#prefix' => '<ul>', 
-    '#suffix' => '</ul>', 
-    '#tree' => TRUE
-  );
-  // array_filter returns only elements with TRUE values
-  foreach (array_filter($edit['accounts']) as $uid => $value) {
-    $user = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid));
-    $form['accounts'][$uid] = array(
-      '#type' => 'hidden', 
-      '#value' => $uid, 
-      '#prefix' => '<li>', 
-      '#suffix' => check_plain($user) ."</li>\n"
-    );
-  }
-  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
-
-  return confirm_form(
-    $form,
-    t('Are you sure you want to delete these users?'),
-    'admin/user/user/advuser', t('This action cannot be undone.'),
-    t('Delete all'), 
-    t('Cancel')
-  );
-}
-
-function advuser_multiple_delete_confirm_submit($form, &$form_state) {
-  if ($form_state['values']['confirm']) {
-    foreach ($form_state['values']['accounts'] as $uid => $value) {
-      user_delete($form_state['values'], $uid);
-    }
-    drupal_set_message(t('The users have been deleted.'));
-  }
-  $form_state['redirect'] = 'admin/user/user/advuser';
-}
-
-/**
  * Email functionality
  */
 function advuser_advuser_operations() {
@@ -352,70 +76,6 @@ function advuser_advuser_operations() {
   return $operations;
 }
 
-function advuser_multiple_email_confirm() {
-  $edit = $_POST;
-
-  $form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
-  // array_filter returns only elements with TRUE values
-  foreach (array_filter($edit['accounts']) as $uid => $value) {
-    $user = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid));
-    $form['accounts'][$uid] = array(
-      '#type' => 'hidden', 
-      '#value' => $uid, 
-      '#prefix' => '<li>', 
-      '#suffix' => check_plain($user) ."</li>\n"
-    );
-  }
-  $form['operation'] = array(
-    '#type' => 'hidden', 
-    '#value' => 'email'
-  );
-
-  $form['variables'] = array(
-    '#type' => 'markup', 
-    '#prefix' => '<div class="advuser-inset-panel">',
-    '#value' => t(ADVUSER_SUBSTITUTION_TEXT),
-    '#suffix' => '</div>' 
-  );
-
-  $form['mailsubject'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Subject'),
-    '#required' => TRUE,
-  );
-
-  $form['mailbody'] = array(
-    '#type' => 'textarea', 
-    '#title' => t('Mail body'),
-    '#required' => TRUE,
-  );
-
-  return confirm_form(
-    $form,
-    t('Are you sure you want to email these users?'),
-    'admin/user/user/advuser',
-    t('This action cannot be undone.'),
-    t('Email'),
-    t('Cancel')
-  );
-}
-
-function advuser_multiple_email_confirm_submit($form, &$form_state) {
-  if ($form_state['values']['confirm']) {
-    foreach ($form_state['values']['accounts'] as $uid => $value) {
-      $account = user_load(array('uid' => $uid));
-      $from = variable_get("site_mail", "nobody@$_SERVER[SERVER_NAME]");
-      // these are invariant for all sent emails
-      $variables = _advuser_get_variables($account);
-      $mail_subject = strtr($form_state['values']['mailsubject'], $variables);
-      $mail_body = strtr($form_state['values']['mailbody'], $variables);
-      drupal_mail('advuser', 'advance-user-mail', $account->mail, user_preferred_language($account), array('subject' => $mail_subject, 'body' => $mail_body), $from, TRUE);
-    }
-    drupal_set_message(t('The users have been mailed.'));
-  }
-  $form_state['redirect'] = 'admin/user/user/advuser';
-}
-
 /**
  * Implementation of hook_mail
  */
@@ -424,122 +84,6 @@ function advuser_mail($key, &$message, $
 }
 
 /**
- * advuser settings page
- */
-function advuser_settings() {
-
-  $form['advuser_mail'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Mail notifications on user account activity.'),
-    '#collapsible' => FALSE,
-    '#collapsed' => FALSE,
-  );
-
-  $form['advuser_mail']['variables'] = array(
-    '#type' => 'markup', 
-    '#prefix' => '<div class="advuser-inset-panel">',
-    '#value' => t(ADVUSER_SUBSTITUTION_TEXT),
-    '#suffix' => '</div>',
-  );
-
-  //New User Notification
-  $form['advuser_mail']['advuser_new_notify'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Send notifications on new user registration'),
-    '#description' => t('Notify selected roles when new users register.'),
-    '#default_value' => variable_get('advuser_new_notify', FALSE),
-  );
-
-  $form['advuser_mail']['advuser_new_subject'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Mail subject'),
-    '#description' => t('The subject of the mail that is going to be sent to the user.  You may insert substitution variables within this item.'),
-    '#default_value' => variable_get('advuser_new_subject', NULL),
-  );
-
-  $form['advuser_mail']['advuser_new_mail'] = array(
-    '#type' => 'textarea',
-    '#title' => t('Mail body'),
-    '#description' => t('The mail that is going to be sent to the selected roles.  You may insert substitution variables within this item.'),
-    '#default_value' => variable_get('advuser_new_mail', NULL),
-  );
-
-   //User change notification
-   $form['advuser_mail']['advuser_modify_notify'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Send notifications on user profile updates'),
-    '#description' => t('Notify selected roles when users update their profiles.'),
-    '#default_value' => variable_get('advuser_modify_notify', FALSE),
-  );
-
-  $form['advuser_mail']['advuser_modify_subject'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Mail subject'),
-    '#description' => t('The subject of the mail that is going to be sent when a user modifies their profiles.  You may insert substitution variables within this item.'),
-    '#default_value' => variable_get('advuser_modify_subject', NULL),
-  );
-  $form['advuser_mail']['advuser_modify_mail'] = array(
-    '#type' => 'textarea',
-    '#title' => t('Mail body'),
-    '#description' => t('The mail that is going to be sent to the selected roles when a user modifies their account.  You may insert substitution variables within this item.'),
-    '#default_value' => variable_get('advuser_modify_mail', NULL),
-  );
-        
-  //Maximum rows in dataset to display
-  $form['advuser_mail']['advuser_listno'] = array(
-    '#type' => 'select',
-    '#options' => drupal_map_assoc(array(1, 10, 25, 50, 75, 100, 125, 150, 175, 200)),
-    '#title' => t('Number of users in listing'),
-    '#description' => t('Sets how many users to display in table view'),
-    '#default_value' => variable_get('advuser_listno', 50),
-  );
-    
-  $sel_roles_count = count(users_by_access('receive email advuser'));
-  if ($sel_roles_count == 0) {
-    $form['advuser_mailonnew']['no_roles_sel_warning'] = array(
-      '#type' => 'markup', 
-      '#prefix' => '<div class="advuser-settings-warning">',
-      '#value' => '<strong>WARNING: No users have "receive email advuser" ' . 
-                  l('access permissions', 'admin/user/access') . 
-                  '!</strong> - No email notifications will be sent.',
-      '#suffix' => '</div>',
-    );
-  }
-
-  if ( module_exists('profile') ) {
-    $form['advuser_profile'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Profile module special settings'),
-      '#collapsible' => TRUE,
-      '#collapsed' => TRUE,
-    );
-
-    $fields = array();
-    $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
-    while ( $row = db_fetch_object($result)) {
-      $fields[$row->fid] = $row->title;
-    }
-    
-    $values = array();
-    $options = variable_get('advuser_profile_fields', NULL);
-    foreach ( (array)$options as $opt => $v ) {
-      if ( $v > 0 ) {
-        $values[] = $v;
-      }
-    }
-    
-    $form['advuser_profile']['advuser_profile_fields'] = array(
-        '#type' => 'checkboxes',
-        '#description' => t('Profile fields to be used as filters for the users.'),
-        '#title' => t('Profile fields'),
-        '#options' => $fields,
-        '#default_value' => $values,
-    );    
-  }
-  return system_settings_form($form);
-}
-
-/**
  * Get a list of substitution variables for the user account
  * @param $user the user account
  * @return An associative array of substitution variables 
@@ -556,8 +100,6 @@ function _advuser_get_variables($user) {
     '%user_language' => empty($user->language) ? t('DEFAULT') : $user->language,
     '%user_timezone' => empty($user->timezone) ? '0' : "$user->timezone",
     '%user_signature' => $user->signature,
-    '%google_user' => "http://www.google.com/search?q=%22$user->mail%22",
-    '%yahoo_user' => "http://search.yahoo.com/search/?p=%22$user->mail%22", 
   );
 }
 
@@ -731,4 +273,185 @@ function advuser_user_roles() {
   return $roles;
 }
 
+/**
+ * Build query for advuser administration filters based on session.
+ */
+function advuser_build_filter_query(&$form_state) {
+  $filters = advuser_filters();
+
+  // Build query
+  $where = $args = $join = array();
+
+  foreach ($form_state['storage']['filters'] as $filter) {
+    list($key, $op, $qop, $value) = array_values($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,
+  );
+}
+
+/**
+ * List advuser administration filters that can be applied.
+ */
+function advuser_filters() {
+  // Regular filters
+  static $filters = array();
+
+  if (empty($filters)) {
+    $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('Last 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',
+    );
+
+    $filters['username'] = array(
+      'title' => t('Username'),
+      'where' => "u.name %op '%s'",
+      'form_type' => 'textfield',
+    );
+
+    $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;
+}
+
 // vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
Index: advuser_filters.inc
===================================================================
RCS file: advuser_filters.inc
diff -N advuser_filters.inc
--- advuser_filters.inc	23 Jun 2009 15:30:25 -0000	1.2.2.2.2.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,534 +0,0 @@
-<?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.
- *
- * $Id: advuser_filters.inc,v 1.2.2.2.2.1 2009/06/23 15:30:25 earnie Exp $
- **/
-
-/**
- * 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,
-    '#collapsed' => 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',
-          '#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")',
-        );
-      } 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("
-      'EQ' => 'is equal to'
-      'NE' => 'is not equal to'
-      'LT' => 'is less than'
-      'GT' => 'is greater than'
-      'LE' => 'is less than or equal to'
-      'GE' => 'is greater than or equal to'
-      'CO' => 'contains'
-      'NC' => 'does not contain'
-      'BE' => 'begins with'
-      '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($form, &$form_state) {
-  $ret = FALSE;
- 
-  if ($form_state['#id'] == 'advuser_filter_form') {
-    switch ($form_state['values']['filter']) {
-      case 'last_access': {
-        switch (strtolower($form_state['values']['last_access'])) {
-          case 'never': {
-            $form_state['values']['last_access'] = 0;
-            $ret = TRUE;
-          } break;;
-
-          case '0': {
-            $ret = TRUE;
-          } break;
-
-          default: {
-            if (!empty($form_state['values']['last_access']) && strtotime($form_state['values']['last_access']) <= 0) {
-              form_set_error('date', t('You have to specify a valid date to filter by Accessed.'));
-              $ret = FALSE;
-            }
-            else {
-              $form_state['values']['last_access'] = strtotime($form_state['values']['last_access']);
-              $ret = TRUE;
-            }
-          } break;
-        }
-      } break;
-
-      case 'created': {
-        if (!empty($form_state['values']['created']) && strtotime($form_state['values']['created']) <= 0) {
-          form_set_error('date', t('You have to specify a valid date to filter by Created.'));
-          $ret = FALSE;
-        }
-        else {
-          $form_state['values']['created'] = strtotime($form_state['values']['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',
-  );
-
-  $filters['username'] = array(
-    'title' => t('Username'),
-    'where' => "u.name %op '%s'",
-    'form_type' => 'textfield',
-  );
-
-  $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, &$form_state) {
-  $op = $form_state['values']['op'];
-  $filters = advuser_filters();
-  $ret = 'admin/user/user/advuser';
-
-  switch ($op) {
-    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;
-    case t('Filter'):
-    case t('Refine'):
-    default:
-      if (isset($form_state['values']['filter'])) {
-        $filter = $form_state['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_state['values'][$filter]])) {
-            $_SESSION['advuser_overview_filter'][] = array(
-              $filter, 
-              $form_state['values'][$filter], 
-              $form_state['values']['filters_ops'], 
-              $form_state['values']['filters_qops'],
-            );
-          }
-        } else {
-          if (isset($form_state['values'][$filter])) {
-            $_SESSION['advuser_overview_filter'][] = array(
-               $filter, 
-               $form_state['values'][$filter], 
-               $form_state['values']['filters_ops'], 
-               $form_state['values']['filters_qops']
-            );
-          }
-        }
-      }
-      break;
-  }
-  $form_state['#redirect'] = $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
Index: forms/advuser_admin.inc
===================================================================
RCS file: forms/advuser_admin.inc
diff -N forms/advuser_admin.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ forms/advuser_admin.inc	8 Apr 2010 15:28:44 -0000
@@ -0,0 +1,129 @@
+<?php
+
+require_once 'advuser_filter_ui.inc';
+
+function advuser_admin(&$form_state) {
+  $function = &$form_state['storage']['function'];
+
+  if ($_POST['accounts'] && $_POST['operation'] == 'delete') {
+    $function = 'advuser_multiple_delete_confirm';
+    require_once "${function}.inc";
+    return $function($form_state);
+  } 
+  elseif ($_POST['accounts'] && $_POST['operation'] == 'email') {
+    $function = 'advuser_multiple_email_confirm';
+    require_once "${function}.inc";
+    return $function($form_state);
+  } 
+  else {
+    $function = 'advuser_filter_ui';
+    $form = $function($form_state);
+    $function = 'advuser_admin_account';
+    require_once "${function}.inc";
+    $form2 = $function($form_state);
+    $function = 'advuser_admin';
+    return array_merge_recursive($form, $form2);
+  }
+
+  return array();
+}
+
+function advuser_admin_validate(&$form, &$form_state) {
+  $function = &$form_state['storage']['function'];
+  $validate = "${function}_validate";
+  if (function_exists($validate) && __FUNCTION__ != $validate) {
+    return $validate($form, $form_state);
+  }
+}
+
+function advuser_admin_submit(&$form, &$form_state) {
+  $function = &$form_state['storage']['function'];
+  $submit = "${function}_submit";
+  if (function_exists($submit) && __FUNCTION__ != $submit) {
+    return $submit($form, $form_state);
+  }
+}
+
+/**
+ * Theme user administration overview.
+ */
+function theme_advuser_admin(&$form) {
+  static $profile_fields = array();
+  // Overview table:
+  $header = array(
+    theme('table_select_header_cell'),
+    array('data' => t('Username'), 'field' => 'u.name'),
+    array('data' => t('Status'), 'field' => 'u.status'),
+  );
+  $roles = advuser_user_roles();
+  if (count($roles)) {
+    $header[] = t('Roles');
+  }
+  $header = array_merge($header, array(
+    array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
+    array('data' => t('Last access'), 'field' => 'u.access'),
+  ));
+
+  $ff = array();
+  foreach (advuser_profile_fields() as $field) {
+    $ff[] = array('data'=>t($field->title), 'field'=>$field->name);
+  }
+  $header = array_merge($header, $ff);
+  $header[] = t('Operations');
+
+  $output = drupal_render($form['filters']);
+  $output .= drupal_render($form['options']);
+  if (isset($form['name']) && is_array($form['name'])) {
+    foreach (element_children($form['name']) as $key) {
+      $row = array(
+        drupal_render($form['accounts'][$key]),
+        drupal_render($form['name'][$key]),
+        drupal_render($form['status'][$key]),
+      );
+      $roles = advuser_user_roles();
+      if (count($roles)) {
+        $row[] = drupal_render($form['roles'][$key]);
+      }
+      $row = array_merge($row, array(
+        drupal_render($form['member_for'][$key]),
+        drupal_render($form['last_access'][$key]),
+      ));
+
+      if (module_exists('profile')) {
+        $fields = variable_get('advuser_profile_fields', NULL);
+
+        if (is_array($fields)) {
+          foreach ( $fields as $fid => $value) {
+            if ( $value ) {
+              if (empty($profile_fields[$fid])) {
+
+                $field = db_fetch_object(db_query('SELECT * FROM {profile_fields} WHERE fid = %d', $fid));
+                $profile_fields[$fid] = $field;
+              }
+              else {
+                $field = $profile_fields[$fid];
+              }
+              $row[] = drupal_render($form[$field->name][$key]);
+            }
+          }
+        }
+      }
+      $row[] = drupal_render($form['operations'][$key]);
+      $rows[] = $row;
+    }
+  }
+  else  {
+    $rows[] = array(array('data' => t('No users available.'), 'colspan' => '7'));
+  }
+
+  $output .= theme('table', $header, $rows);
+  if ($form['pager']['#value']) {
+    $output .= drupal_render($form['pager']);
+  }
+
+  $output .= drupal_render($form);
+
+  return $output;
+}
+
+// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
Index: forms/advuser_admin_account.inc
===================================================================
RCS file: forms/advuser_admin_account.inc
diff -N forms/advuser_admin_account.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ forms/advuser_admin_account.inc	8 Apr 2010 15:28:44 -0000
@@ -0,0 +1,142 @@
+<?php
+
+function advuser_admin_account(&$form_state) {
+  $filter = advuser_build_filter_query($form_state);
+  $header = array(
+    array(),
+    array('data' => t('Username'), 'field' => 'u.name'),
+    array('data' => t('Status'), 'field' => 'u.status'),
+  );
+  $roles = advuser_user_roles();
+  if (count($roles)) {
+    $header[] = t('Roles');
+  }
+  $header = array_merge($header, array(
+    array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
+    array('data' => t('Last access'), 'field' => 'u.access'),
+  ));
+
+  $query = '';
+  $ff = array();
+  $pf = array();
+  foreach (advuser_profile_fields() as $field) {
+    $ff[] = array('data'=>t($field->title), 'field'=>"$field->name.value");
+    $pf[] = "LEFT JOIN {profile_values} $field->name ON $field->name.fid = $field->fid AND $field->name.uid = u.uid";
+  }
+  $header = array_merge($header, $ff);
+  $header[] = t('Operations');
+
+  $filter['join'] .= count($pf) ? ' ' . implode(' ', array_unique($pf)) : NULL;
+
+  $sql = 'SELECT DISTINCT u.uid, u.name, u.status, u.created, u.access '.$pquery.' FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
+
+  $sql .= tablesort_sql($header);
+  $query_count = 'SELECT COUNT(DISTINCT u.uid) FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
+  $result = pager_query($sql, variable_get('advuser_listno', 50), 0, $query_count, $filter['args']);
+
+  $form['options'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Update options'),
+    '#prefix' => '<div class="container-inline">',
+    '#suffix' => '</div>',
+  );
+  $options = array();
+
+  $operations = module_invoke_all('user_operations');
+  $operations = array_merge($operations,module_invoke_all('advuser_operations'));
+  foreach ($operations as $operation => $array) {
+    $options[$operation] = $array['label'];
+  }
+  $form['options']['operation'] = array(
+    '#type' => 'select',
+    '#options' => $options,
+    '#default_value' => 'unblock',
+  );
+  $form['options']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Update'),
+    '#submit' => array('advuser_admin_account_submit'),
+  );
+
+  $destination = drupal_get_destination();
+
+  list($junk, $go) = split('=', $destination);
+  $form['destination'] = array('#type' => 'value', '#value' => urldecode($go));
+
+  $status = array(t('blocked'), t('active'));
+  $roles = advuser_user_roles();
+
+  while ($account = db_fetch_object($result)) {
+    $accounts[$account->uid] = '';
+    $form['name'][$account->uid] = array('#value' => theme('username', $account));
+    $form['status'][$account->uid] =  array('#value' => $status[$account->status]);
+    $users_roles = array();
+    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid);
+    while ($user_role = db_fetch_object($roles_result)) {
+      $users_roles[] = $roles[$user_role->rid];
+    }
+    asort($users_roles);
+    $form['roles'][$account->uid][0] = array('#value' => theme('item_list', $users_roles));
+    $form['member_for'][$account->uid] = array('#value' => format_interval(time() - $account->created));
+    $form['last_access'][$account->uid] =  array('#value' => $account->access ? t('@time ago', array('@time' => format_interval(time() - $account->access))) : t('never'));
+    if (module_exists('profile')) {
+      profile_load_profile($account);
+    }
+    foreach(advuser_profile_fields() as $field) {
+      $form[$field->name][$account->uid] = array('#value' => profile_view_field($account, $field));
+    }
+    if ($account->uid) {
+      $fv = l(t('edit'), "user/$account->uid/edit", array('query' => $destination));
+      if ($account->uid != 1) {
+        $fv .= ' | ' . l(t('delete'), "user/$account->uid/delete", array('query' => $destination));
+      }
+    }
+    else {
+      $fv = NULL;
+    }
+    $form['operations'][$account->uid] = array('#value' => $fv);
+  }
+  $form['accounts'] = array(
+    '#type' => 'checkboxes',
+    '#options' => $accounts
+  );
+  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
+
+  return $form;
+}
+
+/**
+ * Submit the user administration update form.
+ */
+function advuser_admin_account_submit($form, &$form_state) {
+  $operations = module_invoke_all('user_operations', $form_state);
+  $operations = array_merge($operations,module_invoke_all('advuser_operations'));
+  $operation = $operations[$form_state['values']['operation']];
+  $destination = $form_state['values']['destination'];
+  // Filter out unchecked accounts.
+  $accounts = array_filter($form_state['values']['accounts']);
+  if ($function = $operation['callback']) {
+    // Add in callback arguments if present.
+    if (isset($operation['callback arguments'])) {
+      $args = array_merge(array($accounts), $operation['callback arguments']);
+    }
+    else {
+      $args = array($accounts);
+    }
+    call_user_func_array($function, $args);
+
+    cache_clear_all('*', 'cache_menu', TRUE);
+    drupal_set_message(t('The update has been performed.'));
+    extract(parse_url($destination));
+    drupal_goto($path, $query);
+  }
+}
+
+function advuser_admin_account_validate($form, &$form_state) {
+  $form_state['values']['accounts'] = array_filter($form_state['values']['accounts']);
+  if (count($form_state['values']['accounts']) == 0) {
+    form_set_error('', t('No users selected.'));
+  }
+}
+
+// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
Index: forms/advuser_filter_ui.inc
===================================================================
RCS file: forms/advuser_filter_ui.inc
diff -N forms/advuser_filter_ui.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ forms/advuser_filter_ui.inc	8 Apr 2010 15:28:44 -0000
@@ -0,0 +1,324 @@
+<?php
+
+function advuser_filter_ui(&$form_state) {
+  $phase = &$form_state['storage']['phase'];
+  $filters = &$form_state['storage']['filters'];
+
+  $form['filters'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Show only users where'),
+    '#theme' => 'advuser_filters',
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+
+  if (empty($phase)) {
+    $phase['id'] = 0;
+  }
+
+  if (empty($filters)) {
+    $filters = array();
+  }
+
+  $filter_fields = advuser_filters();
+  $i = 0;
+
+  foreach ($filters as $filter) {
+    list($field, $conjunction, $operations, $data) = array_values($filter);
+    $params = array(
+      '%property' => $filter_fields[$field]['title'], 
+      '%value' => _advuser_filter_ui_data_display($field, $data),
+    );
+    if ($i++ > 0) {
+      $form['filters']['current'][] = array(
+        '#prefix' => '<li>',
+        '#suffix' => '</li>',
+        '#value' => t('<em>'.
+                      $conjunction .
+                      '</em> where <strong>%property</strong> ' .
+                      _advuser_filter_ui_symbols($operations) .
+                      ' <strong>%value</strong>',
+                      $params
+                     ) .
+                    ($i == count($filters) ? ')' : ''), 
+      );
+    }
+    else {
+      $form['filters']['current'][] = array(
+        '#prefix' => '<ul><li>',
+        '#suffix' => ($i == count($filters) ? '</li></ul>' : '</li>'),
+        '#value' => t('(<strong>%property</strong> '.
+                      _advuser_filter_ui_symbols($operations).
+                      ' <strong>%value</strong>',
+                      $params
+                     ).
+                    ($i == count($filters) ? ')' : ''), 
+      );
+    }
+  }
+
+  $function = "_advuser_filter_ui_$phase[id]";
+  $function($form, $form_state);
+  return $form;
+}
+
+function advuser_filter_ui_submit(&$form, &$form_state) {
+  $phase = &$form_state['storage']['phase'];
+  $function = "_advuser_filter_ui_$phase[id]_submit";
+  return $function($form, $form_state);
+}
+
+function _advuser_filter_ui_0(&$form, &$form_state) {
+  $filters = &$form_state['storage']['filters'];
+
+  $form['filters']['conjunction'] = array(
+    '#type' => 'select',
+    '#title' => t('Filter conjunction'),
+    '#options' => array('AND' => t('and'), ') OR (' => t('or')),
+  );
+
+  if (!count($filters)) {
+    $form['filters']['conjunction']['#type'] = 'hidden';
+    $form['filters']['conjunction']['#default_value'] = 'AND';
+  }
+
+  $form['filters']['field'] = array(
+    '#type' => 'select',
+    '#title' => t('Field'),
+    '#description' => t('Select the field for this filter'),
+    '#options' => _advuser_filter_ui_fields(),
+  );
+
+  $form['filters']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Continue'),
+    '#submit' => array('_advuser_filter_ui_0_submit'),
+  );
+  if (count($filters)) {
+    $form['filters']['undo'] = array(
+      '#type' => 'submit',
+      '#value' => t('Undo'),
+      '#submit' => array('_advuser_filter_ui_0_undo'),
+    );
+    $form['filters']['reset'] = array(
+      '#type' => 'submit',
+      '#value' => t('Reset'),
+      '#submit' => array('_advuser_filter_ui_0_reset'),
+    );
+  }
+}
+
+function _advuser_filter_ui_0_submit(&$form, &$form_state) {
+  $phase = &$form_state['storage']['phase'];
+  $filters = &$form_state['storage']['filters'];
+  $phase['values']['field'] = $form_state['values']['field'];
+  if (count($filters)) {
+    $phase['values']['conjunction'] = $form_state['values']['conjunction'];
+  }
+  else {
+    $phase['values']['conjunction'] = '';
+  }
+  $phase['id'] = 1;
+}
+
+function _advuser_filter_ui_0_undo(&$form, &$form_state) {
+  $filters = &$form_state['storage']['filters'];
+  array_pop($filters);
+}
+
+function _advuser_filter_ui_0_reset(&$form, &$form_state) {
+  drupal_goto('admin/user/user/advuser');
+}
+
+function _advuser_filter_ui_1(&$form, &$form_state) {
+  $phase = &$form_state['storage']['phase'];
+  $field = &$phase['values']['field'];
+  $conjunction = &$phase['values']['conjunction'];
+  $filters = &$form_state['storage']['filters'];
+
+  $form['filters']['conjunction'] = array(
+    '#type' => 'select',
+    '#title' => t('Filter conjunction'),
+    '#options' => array('AND' => t('and'), ') OR (' => t('or')),
+    '#disabled' => TRUE,
+    '#value' => $conjunction,
+    '#default_value' => $conjunction,
+  );
+  if (!count($filters)) {
+    $form['filters']['conjunction']['#type'] = 'hidden';
+    $form['filters']['conjunction']['#default_value'] = 'AND';
+    $form['filters']['conjunction']['#value'] = 'AND';
+  }
+
+  $form['filters']['field'] = array(
+    '#type' => 'select',
+    '#title' => t('Field'),
+    '#description' => t('Select the field for this filter'),
+    '#options' => _advuser_filter_ui_fields(),
+    '#disabled' => TRUE,
+    '#value' => $field,
+    '#default_value' => $field,
+  );
+
+  $form['filters']['operations'] = array(
+    '#type' => 'select',
+    '#title' => t('Operator'),
+    '#options' => _advuser_filter_ui_operations($field),
+  );
+
+  $form['filters']['data'] = array(
+    '#type' => _advuser_filter_ui_type($field),
+    '#title' => t('Data'),
+    '#description' => _advuser_filter_ui_desc($field),
+  );
+
+  if ($form['filters']['data']['#type'] == 'select') {
+    $form['filters']['data']['#options'] = _advuser_filter_ui_options($field);
+  }
+  $form['filters']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => (count($filters) ? t('Refine') : t('Filter')),
+    '#submit' => array('_advuser_filter_ui_1_submit'),
+  );
+  $form['filters']['back'] = array(
+    '#type' => 'submit',
+    '#value' => t('Back'),
+    '#submit' => array('_advuser_filter_ui_1_back'),
+  );
+}
+
+function _advuser_filter_ui_1_submit(&$form, &$form_state) {
+  $phase = &$form_state['storage']['phase'];
+  $filters = &$form_state['storage']['filters'];
+  $phase['values']['operations'] = $form_state['values']['operations'];
+  $phase['values']['data'] = $form_state['values']['data'];
+  $filters[] = $phase['values'];
+  $phase['values'] = array();
+  $phase['id'] = 0;
+}
+
+function _advuser_filter_ui_1_back(&$form, &$form_state) {
+  $phase = &$form_state['storage']['phase'];
+  $phase['values'] = array();
+  $phase['id'] = 0;
+}
+
+function _advuser_filter_ui_operations($field) {
+  $symbols = array();
+  switch ($field) {
+    case 'permission': {
+      $symbols += array('=' => _advuser_filter_ui_symbols('='));
+      $symbols += array('!=' => _advuser_filter_ui_symbols('!='));
+    } break;
+    case 'status': {
+      $symbols += array('=' => _advuser_filter_ui_symbols('='));
+    } break;
+    case 'uid':
+    case 'last_access':
+    case 'created': {
+      $symbols += array('=' => _advuser_filter_ui_symbols('='));
+      $symbols += array('!=' => _advuser_filter_ui_symbols('!='));
+      $symbols += array('<' => _advuser_filter_ui_symbols('<'));
+      $symbols += array('<=' => _advuser_filter_ui_symbols('<='));
+      $symbols += array('>' => _advuser_filter_ui_symbols('>'));
+      $symbols += array('>=' => _advuser_filter_ui_symbols('>='));
+    } break;
+    default: {
+      $symbols = _advuser_filter_ui_symbols();
+    }
+  }
+
+  return $symbols;
+}
+
+function _advuser_filter_ui_fields() {
+  $fields = advuser_filters();
+  foreach ($fields as $key => $filter) {
+    $filter_fields[$key] = $filter['title'];
+  }
+  return $filter_fields;
+}
+
+function _advuser_filter_ui_type($field) {
+  switch ($field) {
+    case 'permission':
+    case 'status': {
+      $ret = 'select';
+    } break;
+    // Yea, these are date fields but we want textfield input so that we can
+    // use phrases like 3 weeks ago.
+    case 'created':
+    case 'last_access': {
+      $ret = 'textfield';
+    } break;
+    default: {
+      $ret = 'textfield';
+    } break;
+  }
+  return $ret;
+}
+
+function _advuser_filter_ui_desc($field) {
+  switch ($field) {
+    default: {
+      $ret = "Enter the data value for this filter.";
+    }
+  }
+  return t($ret);
+}
+
+function _advuser_filter_ui_options($field) {
+  $options = array();
+  switch ($field) {
+    case 'permission': {
+      foreach (module_list() as $module) {
+        if ($permissions = module_invoke($module, 'perm')) {
+          asort($permissions);
+          foreach ($permissions as $permission) {
+            $options["$module $t_module"][$permission] = t($permission);
+          }
+        }
+      }
+    } break;
+    case 'status': {
+      $options = array(1 => t('active'), 0 => t('blocked'));
+    } break;
+  }
+  return $options;
+}
+
+/**
+ * Helper function for translating operation symbols to language
+ */
+function _advuser_filter_ui_symbols($symbol = NULL) {
+  static $symbols = NULL;
+  if (!isset($symbols)) {
+    $symbols = 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($symbol) ? (isset($symbols[$symbol]) ? $symbols[$symbol] : $symbol) : $symbols;
+};
+
+function _advuser_filter_ui_data_display($field, $data) {
+  switch ($field) {
+    case 'status': {
+      $ret = t($data ? 'active' : 'blocked');
+    } break;
+    default: {
+      $ret = $data;
+    } break;
+  }
+  return $ret;
+}
+
+// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
Index: forms/advuser_multiple_delete_confirm.inc
===================================================================
RCS file: forms/advuser_multiple_delete_confirm.inc
diff -N forms/advuser_multiple_delete_confirm.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ forms/advuser_multiple_delete_confirm.inc	8 Apr 2010 15:28:44 -0000
@@ -0,0 +1,42 @@
+<?php
+
+function advuser_multiple_delete_confirm() {
+  $edit = $_POST;
+
+  $form['accounts'] = array(
+    '#prefix' => '<ul>', 
+    '#suffix' => '</ul>', 
+    '#tree' => TRUE
+  );
+  // array_filter returns only elements with TRUE values
+  foreach (array_filter($edit['accounts']) as $uid => $value) {
+    $user = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid));
+    $form['accounts'][$uid] = array(
+      '#type' => 'hidden', 
+      '#value' => $uid, 
+      '#prefix' => '<li>', 
+      '#suffix' => check_plain($user) ."</li>\n"
+    );
+  }
+  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
+
+  return confirm_form(
+    $form,
+    t('Are you sure you want to delete these users?'),
+    'admin/user/user/advuser', t('This action cannot be undone.'),
+    t('Delete all'), 
+    t('Cancel')
+  );
+}
+
+function advuser_multiple_delete_confirm_submit($form, &$form_state) {
+  if ($form_state['values']['confirm']) {
+    foreach ($form_state['values']['accounts'] as $uid => $value) {
+      user_delete($form_state['values'], $uid);
+    }
+    drupal_set_message(t('The users have been deleted.'));
+  }
+  $form_state['redirect'] = 'admin/user/user/advuser';
+}
+
+// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
Index: forms/advuser_multiple_email_confirm.inc
===================================================================
RCS file: forms/advuser_multiple_email_confirm.inc
diff -N forms/advuser_multiple_email_confirm.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ forms/advuser_multiple_email_confirm.inc	8 Apr 2010 15:28:44 -0000
@@ -0,0 +1,67 @@
+<?php
+
+function advuser_multiple_email_confirm() {
+  $edit = $_POST;
+
+  $form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
+  // array_filter returns only elements with TRUE values
+  foreach (array_filter($edit['accounts']) as $uid => $value) {
+    $user = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid));
+    $form['accounts'][$uid] = array(
+      '#type' => 'hidden', 
+      '#value' => $uid, 
+      '#prefix' => '<li>', 
+      '#suffix' => check_plain($user) ."</li>\n"
+    );
+  }
+  $form['operation'] = array(
+    '#type' => 'hidden', 
+    '#value' => 'email'
+  );
+
+  $form['variables'] = array(
+    '#type' => 'markup', 
+    '#prefix' => '<div class="advuser-inset-panel">',
+    '#value' => t(ADVUSER_SUBSTITUTION_TEXT),
+    '#suffix' => '</div>' 
+  );
+
+  $form['mailsubject'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Subject'),
+    '#required' => TRUE,
+  );
+
+  $form['mailbody'] = array(
+    '#type' => 'textarea', 
+    '#title' => t('Mail body'),
+    '#required' => TRUE,
+  );
+
+  return confirm_form(
+    $form,
+    t('Are you sure you want to email these users?'),
+    'admin/user/user/advuser',
+    t('This action cannot be undone.'),
+    t('Email'),
+    t('Cancel')
+  );
+}
+
+function advuser_multiple_email_confirm_submit($form, &$form_state) {
+  if ($form_state['values']['confirm']) {
+    foreach ($form_state['values']['accounts'] as $uid => $value) {
+      $account = user_load(array('uid' => $uid));
+      $from = variable_get("site_mail", "nobody@$_SERVER[SERVER_NAME]");
+      // these are invariant for all sent emails
+      $variables = _advuser_get_variables($account);
+      $mail_subject = strtr($form_state['values']['mailsubject'], $variables);
+      $mail_body = strtr($form_state['values']['mailbody'], $variables);
+      drupal_mail('advuser', 'advance-user-mail', $account->mail, user_preferred_language($account), array('subject' => $mail_subject, 'body' => $mail_body), $from, TRUE);
+    }
+    drupal_set_message(t('The users have been mailed.'));
+  }
+  $form_state['redirect'] = 'admin/user/user/advuser';
+}
+
+// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
Index: forms/advuser_settings.inc
===================================================================
RCS file: forms/advuser_settings.inc
diff -N forms/advuser_settings.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ forms/advuser_settings.inc	8 Apr 2010 15:28:44 -0000
@@ -0,0 +1,119 @@
+<?php
+
+/**
+ * advuser settings page
+ */
+function advuser_settings() {
+
+  $form['advuser_mail'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Mail notifications on user account activity.'),
+    '#collapsible' => FALSE,
+    '#collapsed' => FALSE,
+  );
+
+  $form['advuser_mail']['variables'] = array(
+    '#type' => 'markup', 
+    '#prefix' => '<div class="advuser-inset-panel">',
+    '#value' => t(ADVUSER_SUBSTITUTION_TEXT),
+    '#suffix' => '</div>',
+  );
+
+  //New User Notification
+  $form['advuser_mail']['advuser_new_notify'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Send notifications on new user registration'),
+    '#description' => t('Notify selected roles when new users register.'),
+    '#default_value' => variable_get('advuser_new_notify', FALSE),
+  );
+
+  $form['advuser_mail']['advuser_new_subject'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Mail subject'),
+    '#description' => t('The subject of the mail that is going to be sent to the user.  You may insert substitution variables within this item.'),
+    '#default_value' => variable_get('advuser_new_subject', NULL),
+  );
+
+  $form['advuser_mail']['advuser_new_mail'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Mail body'),
+    '#description' => t('The mail that is going to be sent to the selected roles.  You may insert substitution variables within this item.'),
+    '#default_value' => variable_get('advuser_new_mail', NULL),
+  );
+
+   //User change notification
+   $form['advuser_mail']['advuser_modify_notify'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Send notifications on user profile updates'),
+    '#description' => t('Notify selected roles when users update their profiles.'),
+    '#default_value' => variable_get('advuser_modify_notify', FALSE),
+  );
+
+  $form['advuser_mail']['advuser_modify_subject'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Mail subject'),
+    '#description' => t('The subject of the mail that is going to be sent when a user modifies their profiles.  You may insert substitution variables within this item.'),
+    '#default_value' => variable_get('advuser_modify_subject', NULL),
+  );
+  $form['advuser_mail']['advuser_modify_mail'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Mail body'),
+    '#description' => t('The mail that is going to be sent to the selected roles when a user modifies their account.  You may insert substitution variables within this item.'),
+    '#default_value' => variable_get('advuser_modify_mail', NULL),
+  );
+        
+  //Maximum rows in dataset to display
+  $form['advuser_mail']['advuser_listno'] = array(
+    '#type' => 'select',
+    '#options' => drupal_map_assoc(array(1, 10, 25, 50, 75, 100, 125, 150, 175, 200)),
+    '#title' => t('Number of users in listing'),
+    '#description' => t('Sets how many users to display in table view'),
+    '#default_value' => variable_get('advuser_listno', 50),
+  );
+    
+  $sel_roles_count = count(users_by_access('receive email advuser'));
+  if ($sel_roles_count == 0) {
+    $form['advuser_mailonnew']['no_roles_sel_warning'] = array(
+      '#type' => 'markup', 
+      '#prefix' => '<div class="advuser-settings-warning">',
+      '#value' => '<strong>WARNING: No users have "receive email advuser" ' . 
+                  l('access permissions', 'admin/user/access') . 
+                  '!</strong> - No email notifications will be sent.',
+      '#suffix' => '</div>',
+    );
+  }
+
+  if ( module_exists('profile') ) {
+    $form['advuser_profile'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Profile module special settings'),
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+    );
+
+    $fields = array();
+    $result = db_query('SELECT * FROM {profile_fields} ORDER BY category, weight');
+    while ( $row = db_fetch_object($result)) {
+      $fields[$row->fid] = $row->title;
+    }
+    
+    $values = array();
+    $options = variable_get('advuser_profile_fields', NULL);
+    foreach ( (array)$options as $opt => $v ) {
+      if ( $v > 0 ) {
+        $values[] = $v;
+      }
+    }
+    
+    $form['advuser_profile']['advuser_profile_fields'] = array(
+        '#type' => 'checkboxes',
+        '#description' => t('Profile fields to be used as filters for the users.'),
+        '#title' => t('Profile fields'),
+        '#options' => $fields,
+        '#default_value' => $values,
+    );    
+  }
+  return system_settings_form($form);
+}
+
+// vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
