You are here

function advuser_admin_account in Advanced User 7.3

Same name and namespace in other branches
  1. 5.2 advuser.module \advuser_admin_account()
  2. 6.3 forms/advuser_admin_account.inc \advuser_admin_account()
  3. 6.2 advuser.module \advuser_admin_account()

Provide the list of filtered users.

1 string reference to 'advuser_admin_account'
advuser_admin in forms/advuser_admin.inc
Callback form controller.

File

forms/advuser_admin_account.inc, line 14
This is the part of the form that provides the actions and the list based on the selected filters.

Code

function advuser_admin_account($form, &$form_state) {
  $advuser_accounts =& $_SESSION['advuser']['accounts'];
  $selectall =& $_SESSION['advuser']['selectall'];

  // Clear the warnings.
  drupal_get_messages('warning');
  if (empty($advuser_accounts) && !$selectall) {
    drupal_set_message(t('No selections have been applied.'), 'warning', FALSE);
  }

  // Get the filters for the user data.
  $u_sel = db_select('users', 'u');
  advuser_build_filter_query($u_sel);

  // Get the list of user roles to be displayed.
  $roles = advuser_user_roles();

  // Array used for user profile title and value display in list.
  $ff = array();
  foreach (advuser_profile_fields() as $field) {
    if (isset($field->name)) {
      $ff[$field->name] = array(
        'data' => t($field->title),
        'field' => "{$field->name}.value",
      );
    }
  }
  $header = array(
    'name' => array(
      'data' => t('User name'),
      'field' => 'u.name',
    ),
    'mail' => array(
      'data' => t('Mail'),
      'field' => 'u.mail',
    ),
    'status' => array(
      'data' => t('Status'),
      'field' => 'u.status',
    ),
  );
  if (count($roles)) {
    $header['roles'] = array(
      'data' => t('Roles'),
    );
  }
  $header = array_merge($header, array(
    'member_for' => array(
      'data' => t('Member for'),
      'field' => 'u.created',
      'sort' => 'desc',
    ),
    'last_access' => array(
      'data' => t('Last access'),
      'field' => 'u.access',
    ),
  ));
  $header = array_merge($header, $ff);
  $header['operations'] = array(
    'data' => t('Operations'),
  );
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Action options'),
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
    '#collapsible' => FALSE,
  );
  $options = array();

  // We don't want to call the user_operations here because we can't control
  // the functioning of the callbacks to the saved accounts in the session.
  // The callbacks only operate on the current screenfull of data and know
  // nothing about the Select All Users mode.
  $operations = module_invoke_all('advuser_operations');
  $operations += module_invoke_all('user_operations');
  foreach ($operations as $operation => $array) {
    $options[$operation] = $array['label'];
  }
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => 'saveselect',
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Apply action'),
  );
  $destination = drupal_get_destination();
  $go = urldecode($destination['destination']);
  $form['destination'] = array(
    '#type' => 'value',
    '#value' => $go,
  );
  $status = array(
    t('blocked'),
    t('active'),
  );
  $sql = advuser_build_query();
  $sql = $sql
    ->extend('TableSort');
  $limit = variable_get('advuser_listno', 50);
  $query_count = advuser_build_query('count');
  if ($limit !== 'unlimited') {
    $sql = $sql
      ->extend('PagerDefault');
    $sql
      ->setCountQuery($query_count);
    $sql
      ->limit($limit);
  }
  $sql
    ->orderByHeader($header);
  $result = $sql
    ->execute();
  $accounts = array();
  $inputs = array();
  foreach ($result as $account) {
    $users_roles = array();
    $roles_result = db_select('users_roles', 'ur')
      ->fields('ur', array(
      'rid',
    ))
      ->condition('ur.uid', $account->uid, '=')
      ->execute();
    foreach ($roles_result as $user_role) {
      $users_roles[] = $roles[$user_role->rid];
    }
    asort($users_roles);
    $accounts[$account->uid] = array(
      'name' => theme('username', array(
        'account' => $account,
      )),
      'mail' => $account->mail,
      'status' => $status[$account->status],
      'roles' => theme('item_list', array(
        'items' => $users_roles,
      )),
      'member_for' => format_interval(REQUEST_TIME - $account->created),
      'last_access' => $account->access ? t('@time ago', array(
        '@time' => format_interval(REQUEST_TIME - $account->access),
      )) : t('never'),
    );
    if (!empty($advuser_accounts[$account->uid]) || $selectall) {
      $inputs[$account->uid] = $account->uid;
    }
    if (module_exists('profile')) {
      profile_user_load(array(
        $account->uid => $account,
      ));
    }
    foreach (advuser_profile_fields() as $field) {
      $accounts[$account->uid][$field->name] = profile_view_field($account, $field);
    }
    if ($account->uid) {
      $accounts[$account->uid]['operations'] = array(
        'data' => array(
          'edit' => array(
            '#type' => 'link',
            '#title' => t('edit'),
            '#href' => "user/{$account->uid}/edit",
            '#options' => array(
              'query' => $destination,
            ),
          ),
          array(
            '#markup' => ' | ',
          ),
          'delete' => array(
            '#type' => 'link',
            '#title' => t('delete'),
            '#href' => "user/{$account->uid}/cancel",
            '#options' => array(
              'query' => $destination,
            ),
          ),
        ),
      );
    }
  }
  $query_count = $query_count
    ->execute()
    ->fetchField();
  if (!empty($query_count)) {
    $query_count = t('Filtered %count @user.', array(
      '%count' => $query_count,
      '@user' => format_plural($query_count, 'user', 'users'),
    ));
  }
  else {
    $query_count = t('No users returned.');
  }
  $form['accounts'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $accounts,
    '#empty' => t('No people available.'),
    '#value' => $inputs,
    '#prefix' => !empty($query_count) ? '<p>' . $query_count . '</p>' : '',
  );
  $form['pager'] = array(
    '#markup' => theme('pager'),
  );
  return $form;
}