You are here

LogController.php in TacJS 8.5

File

src/Controller/LogController.php
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 {

  /**
   * The database connection object.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a DbLog object.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection object.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * Store proof of consent.
   *
   * @param int $service_key
   *   Unique ID of the service.
   *
   * @return array
   *   Empty Array.
   *
   * @throws \Exception
   */
  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,
    ]);
  }

  /**
   * Displays a listing of database logs.
   *
   * @return array
   *   A render array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   *
   * @throws \Exception
   */
  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',
      ]);

      // Execute the statement
      $data = $sth
        ->limit(50)
        ->orderByHeader($header)
        ->execute();

      // Get all the results
      $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;
  }

}

Classes

Namesort descending Description
LogController