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)); } return $this->render($templatePath, [ 'ip' => 'invitado', 'disks' => [], ]); } #[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' => [], ]); 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); } } }