You are here

user_prune.module in User Prune 8

Same filename and directory in other branches
  1. 7 user_prune.module

File

user_prune.module
View source
<?php

function user_prune_cron() {
  $cron_v = \Drupal::state()
    ->get('delete_users_on_cron');
  if ($cron_v) {
    $selectr = list_of_user();
    $uids = array();
    foreach ($selectr as $row) {
      $uids[] = $row->uid;

      //drupal_set_message($row->uid . " " . $row->Name);
    }
    user_delete_multiple($uids);
  }
}
function preview_button_action($form, &$form_state) {
  $selectr = list_of_user();
  $uids = array();
  foreach ($selectr as $row) {
    $uids[] = $row->uid;
    drupal_set_message($row->uid . " " . $row->Name);
  }
}
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;
}