View source
<?php
namespace Drupal\tacjs\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\JsonResponse;
class LogController extends ControllerBase {
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public function log($service_key) {
try {
$this->connection
->insert('tacjslog')
->fields([
'timestamp' => \Drupal::time()
->getCurrentTime(),
'ip_address' => \Drupal::request()
->getClientIp(),
'services_allowed' => $service_key,
])
->execute();
} catch (\Exception $e) {
throw $e;
}
return new JsonResponse([
'timestamp' => \Drupal::time()
->getCurrentTime(),
'ip_address' => \Drupal::request()
->getClientIp(),
'services_allowed' => $service_key,
]);
}
public function overview() {
$rows = [];
$header = [
[
'data' => $this
->t('Timestamp'),
'field' => 'log.timestamp',
'sort' => 'desc',
],
[
'data' => $this
->t('IP address'),
'field' => 'log.ip_address',
],
[
'data' => $this
->t('Services allowed'),
'field' => 'log.services_allowed',
],
];
try {
$sth = $this->connection
->select('tacjslog', 'log')
->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender')
->fields('log', [
'timestamp',
'ip_address',
'services_allowed',
]);
$data = $sth
->limit(50)
->orderByHeader($header)
->execute();
$results = $data
->fetchAll(\PDO::FETCH_OBJ);
} catch (\Exception $e) {
throw $e;
}
foreach ($results as $result) {
$rows[] = [
'data' => [
\Drupal::service('date.formatter')
->format($result->timestamp, 'short'),
$result->ip_address,
$result->services_allowed,
],
];
}
$build['tacjslog_table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t('No log available.'),
];
$build['tacjslog_pager'] = [
'#type' => 'pager',
];
return $build;
}
}