Files
myep-team/src/Controller/Admin/Log/IndexController.php
T
2025-08-21 14:20:43 +02:00

59 lines
1.8 KiB
PHP

<?php
namespace App\Controller\Admin\Log;
use App\Entity\Log;
use App\Repository\LogRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class IndexController extends AbstractController
{
public function __construct(
private readonly LogRepository $logRepository,
private readonly PaginatorInterface $paginator,
) {
}
#[Route('/admin/log', name: 'app_admin_log_index')]
#[IsGranted('ROLE_ADMIN')]
public function index(Request $request): Response
{
$qb = $this
->logRepository
->createQueryBuilder('log')
;
$pagination = $this->paginator->paginate(
$qb,
$request->query->getInt('page', 1),
$request->query->getInt('limit', 50),
[
'defaultSortFieldName' => 'log.timestamp',
'defaultSortDirection' => 'DESC',
]
);
$entries = [];
foreach ($pagination as $entry) {
/** @var Log $entry */
$extra = $entry->getExtra();
$entries[] = [
'timestamp' => $entry->getTimestamp(),
'message' => $entry->getMessage(),
'user' => $extra['user'] ? $extra['user']['username'] ?? $extra['user']['email'] : '-',
'context' => json_encode($entry->getContext(), JSON_UNESCAPED_UNICODE),
];
}
return $this->render('admin/log/index.html.twig', [
'pagination' => $pagination,
'entries' => $entries,
]);
}
}