View source
<?php
namespace Drupal\admin_audit_trail;
class AdminAuditTrailStorage {
public static function getSearchData(array $getData, array $header, $limit = NULL) {
$db = \Drupal::database();
$query = $db
->select('admin_audit_trail', 'e');
$query
->fields('e');
$table_sort = $query
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderByHeader($header);
$pager = $table_sort
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit($limit);
if (!empty($getData['type'])) {
$query
->condition('type', $getData['type']);
if (!empty($getData['operation'])) {
$query
->condition('operation', $getData['operation']);
}
}
if (!empty($getData['id'])) {
$query
->condition('ref_numeric', $getData['id']);
}
if (!empty($getData['ip'])) {
$query
->condition('ip', $getData['ip']);
}
if (!empty($getData['name'])) {
$query
->condition('ref_char', $getData['name']);
}
if (!empty($getData['path'])) {
$query
->condition('path', '%' . \Drupal::database()
->escapeLike($getData['path']) . '%', 'LIKE');
}
if (!empty($getData['keyword'])) {
$query
->condition('description', '%' . \Drupal::database()
->escapeLike($getData['keyword']) . '%', 'LIKE');
}
if (!empty($getData['user'])) {
$query
->condition('uid', $getData['user']);
}
$result = $pager
->execute();
return $result;
}
public static function formGetOperations($type) {
$element = [
'#type' => 'select',
'#name' => 'operation',
'#title' => t('Operation'),
'#description' => t('The entity operation.'),
'#options' => [
'' => t('Choose an operation'),
],
'#prefix' => '<div id="operation-dropdown-replace">',
'#suffix' => '</div>',
];
if ($type) {
$db = \Drupal::database();
$query = $db
->select('admin_audit_trail', 'e')
->fields('e', [
'operation',
])
->condition('type', $type)
->groupBy('operation');
$query
->addExpression('COUNT(e.lid)', 'c');
$query
->distinct(TRUE);
$results = $query
->execute()
->fetchAllKeyed(0);
$operations = [];
foreach ($results as $name => $count) {
$operations[$name] = $name . ' (' . $count . ')';
}
$element['#options'] += $operations;
}
return $element;
}
}