View source
<?php
namespace Drupal\user_expire\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Datetime\TimeInterface;
class UserExpireReport extends ControllerBase {
protected $database;
protected $dateFormatter;
protected $time;
public function __construct(DateFormatterInterface $date_formatter, Connection $database, TimeInterface $time) {
$this->dateFormatter = $date_formatter;
$this->database = $database;
$this->time = $time;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('database'), $container
->get('datetime.time'));
}
public function listOfUsers() {
$header = [
'username' => [
'data' => $this
->t('Username'),
'field' => 'u.name',
],
'access' => [
'data' => $this
->t('Last access'),
'field' => 'u.access',
],
'expiration' => [
'data' => $this
->t('Expiration'),
'field' => 'expiration',
'sort' => 'asc',
],
];
$rows = [];
$query = $this->database
->select('user_expire', 'ue');
$query
->join('users_field_data', 'u', 'ue.uid = u.uid');
$query = $query
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
$query
->fields('u', [
'uid',
'name',
'access',
])
->fields('ue', [
'expiration',
])
->limit(50)
->orderByHeader($header);
$accounts = $query
->execute();
foreach ($accounts as $account) {
$username = [
'#theme' => 'username',
'#account' => $this
->entityTypeManager()
->getStorage('user')
->load($account->uid),
];
$rows[$account->uid] = [
'username' => render($username),
'access' => $account->access ? $this
->t('@time ago', [
'@time' => $this->dateFormatter
->formatInterval($this->time
->getRequestTime() - $account->access),
]) : $this
->t('never'),
'expiration' => $this
->t('@time from now', [
'@time' => $this->dateFormatter
->formatInterval($account->expiration - $this->time
->getRequestTime()),
]),
];
}
$table = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $table;
}
}