Skip to content

Commit

Permalink
Adds ability to specify username in log view (closes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostieDE committed Aug 31, 2023
1 parent b91b43d commit ca2f1ca
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
25 changes: 21 additions & 4 deletions Controller/LogController.php
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Monolog\Level;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use SchulIT\CommonBundle\Controller\Model\LogCounter;
use SchulIT\CommonBundle\Entity\LogEntry;
use SchulIT\CommonBundle\Form\ConfirmType;
Expand All @@ -18,13 +19,14 @@
class LogController extends AbstractController {
const ITEMS_PER_PAGE = 25;

public function __construct(private readonly EntityManagerInterface $em) { }
public function __construct(private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger) { }

#[Route('/admin/logs', name: 'admin_logs')]
public function index(Request $request): Response {
$page = $request->query->get('page', 1);
$channel = $request->query->get('channel', null);
$level = $request->query->get('level', null);
$username = $request->query->get('username', null);

if(!is_numeric($page) || $page < 1) {
$page = 1;
Expand Down Expand Up @@ -62,6 +64,15 @@ public function index(Request $request): Response {
->setParameter('channel', $channel);
}

if(!empty($username)) {
if($this->em->getConfiguration()->getCustomStringFunction('JSON_VALUE') !== null) {
$queryBuilder->andWhere("JSON_VALUE(l.details, '$.username') = :username")
->setParameter('username', $username);
} else {
$this->logger->notice('Der SQL-Befehl JSON_VALUE ist nicht verfügbar in Doctrine. Bitte die Bibliothek installieren (https://github.com/ScientaNL/DoctrineJsonFunctions) und einrichten');
}
}

$paginator = new Paginator($queryBuilder->getQuery());
$count = $paginator->count();
$pages = 0;
Expand All @@ -70,7 +81,7 @@ public function index(Request $request): Response {
$pages = ceil((float)$count / static::ITEMS_PER_PAGE);
}

$counters = $this->getCounterForLevels($channel);
$counters = $this->getCounterForLevels($channel, $username);

return $this->render('@Common/logs/index.html.twig', [
'items' => $paginator,
Expand All @@ -79,11 +90,12 @@ public function index(Request $request): Response {
'level' => $level,
'channel' => $channel,
'channels' => $channels,
'counters' => $counters
'counters' => $counters,
'username' => $username
]);
}

private function getCounterForLevels($channel = null): array {
private function getCounterForLevels($channel = null, ?string $username = null): array {
$levels = [ ];

foreach(Level::cases() as $level) {
Expand All @@ -103,6 +115,11 @@ private function getCounterForLevels($channel = null): array {
->setParameter('channel', $channel);
}

if(!empty($username) && $this->em->getConfiguration()->getCustomStringFunction('JSON_VALUE') !== null) {
$qb->andWhere("JSON_VALUE(l.details, '$.username') = :username")
->setParameter('username', $username);
}

$results = $qb->getQuery()->getArrayResult();

foreach($results as $row) {
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/messages.de.yml
Expand Up @@ -43,6 +43,7 @@ logs:
message: Mitteilung
time: Zeit
filter: Filter
username: Benutzername
apply_filter: Filter übernehmen
clear_filter: Filter löschen
all_channels: Alle Bereiche
Expand Down
1 change: 1 addition & 0 deletions Resources/translations/messages.en.yml
Expand Up @@ -43,6 +43,7 @@ logs:
message: Message
time: Time
filter: Filter
username: Username
apply_filter: Apply filter
clear_filter: Clear filter
all_channels: All channels
Expand Down
9 changes: 7 additions & 2 deletions Resources/views/logs/index.html.twig
Expand Up @@ -9,7 +9,7 @@
<div class="row row-cols-4 mb-3 g-4">
{% for counter in counters %}
<div class="col">
<a class="card card-link h-100" href="{{ path('admin_logs', { level: counter.level, channel: channel }) }}">
<a class="card card-link h-100" href="{{ path('admin_logs', { level: counter.level, channel: channel, username: username }) }}">
<div class="card-body d-flex align-items-center" >
<div class="icon d-inline-block me-2">{{ counter.counter }}</div>
{{ counter.name }}
Expand Down Expand Up @@ -86,7 +86,7 @@
<input type="hidden" name="level" value="{{ level }}" />
{% endif %}

<div class="form-group">
<div class="mb-2">
<label for="channel" class="form-label">{{ 'logs.channel'|trans }}</label>
<select class="form-control" name="channel" id="channel" data-choice="true">
<option value="">{{ 'logs.all_channels'|trans }}</option>
Expand All @@ -95,6 +95,11 @@
{% endfor %}
</select>
</div>

<div>
<label for="username" class="form-label">{{ 'logs.username'|trans }}</label>
<input type="text" name="username" id="username" class="form-control" value="{{ username }}">
</div>
</div>

<div class="card-footer">
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -24,7 +24,8 @@
"knplabs/knp-time-bundle": "",
"shapecode/cron-bundle": "",
"doctrine/orm": "",
"symfony/doctrine-messenger": ""
"symfony/doctrine-messenger": "",
"scienta/doctrine-json-functions": ""
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit ca2f1ca

Please sign in to comment.