149 lines
5.5 KiB
PHP
149 lines
5.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Client;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Twig\Environment;
|
|
|
|
class MenuBrowserController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
protected readonly EntityManagerInterface $entityManager,
|
|
private Environment $twig,
|
|
)
|
|
{
|
|
}
|
|
|
|
#[Route('/menu-browser')]
|
|
public function index(Request $request): Response
|
|
{
|
|
$host = $request->getClientIp();
|
|
|
|
$partitions = [];
|
|
|
|
$client = $this->entityManager->getRepository(Client::class)->findOneBy(['ip' => $host]);
|
|
|
|
$disks = []; // Array nuevo estructurado
|
|
|
|
if ($client) {
|
|
$rawPartitions = $client->getPartitions()->toArray();
|
|
|
|
foreach ($rawPartitions as $part) {
|
|
$diskNum = $part->getDiskNumber();
|
|
$partNum = $part->getPartitionNumber();
|
|
|
|
// Inicializamos el array del disco si no existe
|
|
if (!isset($disks[$diskNum])) {
|
|
$disks[$diskNum] = [
|
|
'info' => null, // Aquí guardaremos la partición 0 (tamaño total)
|
|
'parts' => [] // Aquí las particiones reales
|
|
];
|
|
}
|
|
|
|
if ($partNum == 0) {
|
|
// Es la info del disco completo
|
|
$disks[$diskNum]['info'] = $part;
|
|
} else {
|
|
// Es una partición normal
|
|
$disks[$diskNum]['parts'][] = $part;
|
|
}
|
|
}
|
|
|
|
// Ordenamos los discos (1, 2, 3...)
|
|
ksort($disks);
|
|
|
|
// Ordenamos las particiones dentro de cada disco (1, 2, 3...)
|
|
foreach ($disks as &$diskData) {
|
|
usort($diskData['parts'], function($a, $b) {
|
|
return $a->getPartitionNumber() <=> $b->getPartitionNumber();
|
|
});
|
|
}
|
|
}
|
|
|
|
$menuName = $client->getMenu()->getPublicUrl();
|
|
|
|
return $this->render('browser/' . $menuName . '.html.twig', [
|
|
'ip' => $host,
|
|
'mac' => $client ? $client->getMac() : null,
|
|
'disks' => $disks,
|
|
]);
|
|
}
|
|
|
|
#[Route('/menu/{templateName}', name: 'render_menu_template', requirements: ['templateName' => '.+'])]
|
|
public function renderMenu(string $templateName, Request $request): Response
|
|
{
|
|
$templatePath = 'browser/' . $templateName . '.html.twig';
|
|
|
|
if (!$this->twig->getLoader()->exists($templatePath)) {
|
|
throw $this->createNotFoundException(sprintf('La plantilla "%s" no existe.', $templateName));
|
|
}
|
|
|
|
$html = $this->renderView($templatePath, [
|
|
'ip' => 'invitado',
|
|
'disks' => [],
|
|
]);
|
|
|
|
if ($request->query->get('gui')) {
|
|
$base = rtrim($request->getSchemeAndHttpHost() . $request->getBasePath(), '/');
|
|
// Rewrite both assets/ and /assets/ to the absolute API URL
|
|
$html = preg_replace('#([\'"(])/?assets/#i', '$1' . $base . '/assets/', $html);
|
|
}
|
|
|
|
return new Response($html);
|
|
}
|
|
|
|
#[Route('/menu-raw/{templateName}', name: 'get_raw_menu_template', requirements: ['templateName' => '.+'], methods: ['GET'])]
|
|
public function getRawMenu(string $templateName, Request $request): Response
|
|
{
|
|
$templatePath = 'browser/' . $templateName . '.html.twig';
|
|
|
|
if (!$this->twig->getLoader()->exists($templatePath)) {
|
|
throw $this->createNotFoundException(sprintf('La plantilla "%s" no existe.', $templateName));
|
|
}
|
|
|
|
$content = $this->twig->getLoader()->getSourceContext($templatePath)->getCode();
|
|
|
|
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'text/plain; charset=utf-8']);
|
|
}
|
|
|
|
#[Route('/menu-preview', name: 'preview_menu_template', methods: ['POST'])]
|
|
public function previewMenu(Request $request): Response
|
|
{
|
|
$data = json_decode($request->getContent(), true);
|
|
$base64Template = $data['templateBase64'] ?? '';
|
|
|
|
if (empty($base64Template)) {
|
|
return new JsonResponse(['message' => 'Falta el contenido de la plantilla.'], Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$content = base64_decode($base64Template);
|
|
if ($content === false) {
|
|
return new JsonResponse(['message' => 'El contenido Base64 es inválido.'], Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
try {
|
|
$template = $this->twig->createTemplate($content);
|
|
$rendered = $template->render([
|
|
'ip' => '127.0.0.1 (Previsualización)',
|
|
'mac' => '00:00:00:00:00:00',
|
|
'disks' => [],
|
|
]);
|
|
// Rewrite both assets/ and /assets/ to the absolute API URL
|
|
$base = rtrim($data['baseUrl'] ?? ($request->getSchemeAndHttpHost() . $request->getBasePath()), '/');
|
|
$rendered = preg_replace('#([\'"(])/?assets/#i', '$1' . $base . '/assets/', $rendered);
|
|
|
|
return new Response($rendered, Response::HTTP_OK, ['Content-Type' => 'text/html; charset=utf-8']);
|
|
} catch (\Exception $e) {
|
|
return new JsonResponse(['message' => 'Error al compilar la plantilla Twig: ' . $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
}
|