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;
}
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');
$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;
$sql .= ' AND uid <> 0 AND uid <> 1';
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;
}
switch ($values['user_status']) {
case 'blocked':
$sql .= ' AND status = 0';
break;
case 'active':
$sql .= ' AND status = 1';
break;
}
$sql .= ' ORDER BY uid limit ' . \Drupal::state()
->get('user_number_select');
$connection = \Drupal::database();
$query = $connection
->query($sql);
$selectr = $query
->fetchAll();
return $selectr;
}