You are here

function list_of_user in User Prune 8

2 calls to list_of_user()
preview_button_action in ./user_prune.module
user_prune_cron in ./user_prune.module

File

./user_prune.module, line 29

Code

function list_of_user() {
  $values['user_prune_time_year'] = \Drupal::state()
    ->get('year_select');
  $values['user_prune_time_month'] = \Drupal::state()
    ->get('month_select');
  $values['user_prune_time_day'] = \Drupal::state()
    ->get('day_select');
  $values['user_never_logged_in'] = \Drupal::state()
    ->get('user_never_logged_in');
  $values['user_status'] = \Drupal::state()
    ->get('user_status');
  $values['roles'] = \Drupal::state()
    ->get('roles');

  // Filter on user created time.
  $logged_in_limit = time() - $values['user_prune_time_year'] - $values['user_prune_time_month'] - $values['user_prune_time_day'];
  $sql = 'SELECT u.uid AS uid, name AS Name, created AS Created, access AS Lastaccess, login AS Lastlogin, status AS Status FROM {users_field_data} u WHERE created < ' . $logged_in_limit;

  // Make sure not to include power users.
  $sql .= ' AND uid <> 0 AND uid <> 1';

  // Option to filter only never logged in users.
  if ($values['user_never_logged_in']) {
    $sql .= ' AND access = 0';
    $sql .= ' AND login  = 0';
  }
  else {
    $sql .= ' AND access < ' . $logged_in_limit;
    $sql .= ' AND login < ' . $logged_in_limit;
  }

  // Limit based on users active or blocked.
  switch ($values['user_status']) {
    case 'blocked':
      $sql .= ' AND status = 0';
      break;
    case 'active':
      $sql .= ' AND status = 1';
      break;
  }

  /*  // Filter based on user roles.
      $system_roles = user_roles(true);
      foreach($system_roles as $system_role){
        $role_id = $system_role->id();
        if (null !==\Drupal::state()->get($role_id)){
          if (\Drupal::state()->get($role_id) == 0) {
            $sql .= ' AND u.uid NOT IN (SELECT r.revision_id FROM {user__roles} r WHERE roles_target_id = "' . $role_id . '")';
                    drupal_set_message($role_id . " " . \Drupal::state()->get($role_id));
          }
        }
      }*/

  // Limit batches.
  $sql .= ' ORDER BY uid limit ' . \Drupal::state()
    ->get('user_number_select');

  //drupal_set_message($sql);
  $connection = \Drupal::database();
  $query = $connection
    ->query($sql);
  $selectr = $query
    ->fetchAll();
  return $selectr;
}