getUserRole(); // Segmentar contenido $filteredMd = $this->segmentMarkdown($md, $role); $parsedown = new \Parsedown(); $content = $parsedown->text($filteredMd); } return view('documentacion.index', compact('content')); } /** * Detecta el rol del usuario basado en las variables de sesión */ private function getUserRole() { if (session()->get('admin_logged_in')) { $adminRole = session()->get('admin_role'); return ($adminRole == 1) ? 'superadmin' : 'admin_club'; } if (session()->get('user_logged_in')) { return session()->get('user_tipo'); // 'jugador' o 'aficionado' } return 'visitante'; } /** * Segmenta y filtra el Markdown según el rol */ private function segmentMarkdown($md, $role) { $markers = [ 'cap1' => '', 'cap2' => '', 'cap3' => '', 'cap4' => '', 'cap5' => '', 'faq' => '## ❓ Preguntas Frecuentes' ]; // Encontrar posiciones de los marcadores $positions = []; foreach ($markers as $key => $marker) { $pos = strpos($md, $marker); if ($pos !== false) { $positions[$key] = $pos; } } asort($positions); $keys = array_keys($positions); $sections = []; // Intro (antes del primer capítulo) $sections['intro'] = substr($md, 0, $positions[$keys[0]]); // Capítulos y FAQ for ($i = 0; $i < count($keys); $i++) { $start = $positions[$keys[$i]]; $end = isset($keys[$i+1]) ? $positions[$keys[$i+1]] : strlen($md); $sections[$keys[$i]] = substr($md, $start, $end - $start); } // Determinar qué capítulos mostrar $allowedChapters = [1]; // Todos ven el Cap 1 $showSections = ['intro', 'cap1', 'faq']; switch ($role) { case 'superadmin': $allowedChapters = [1, 2, 3, 4, 5]; $showSections = ['intro', 'cap1', 'cap2', 'cap3', 'cap4', 'cap5', 'faq']; break; case 'admin_club': $allowedChapters = [1, 4]; $showSections = ['intro', 'cap1', 'cap4', 'faq']; break; case 'jugador': $allowedChapters = [1, 2]; $showSections = ['intro', 'cap1', 'cap2', 'faq']; break; case 'aficionado': $allowedChapters = [1, 3]; $showSections = ['intro', 'cap1', 'cap3', 'faq']; break; default: // visitante $allowedChapters = [1]; $showSections = ['intro', 'cap1', 'faq']; break; } // Filtrar Tabla de Contenidos en la Intro $sections['intro'] = $this->filterTOC($sections['intro'], $allowedChapters); // Unir secciones seleccionadas $finalMd = ""; foreach ($showSections as $s) { if (isset($sections[$s])) { $finalMd .= $sections[$s] . "\n\n---\n\n"; } } return $finalMd; } /** * Filtra la tabla de contenidos para mostrar solo los capítulos permitidos */ private function filterTOC($intro, $allowedChapters) { $lines = explode("\n", $intro); $filteredLines = []; $inTable = false; foreach ($lines as $line) { if (strpos($line, '| Capítulo | Perfil |') !== false) { $inTable = true; $filteredLines[] = $line; continue; } if ($inTable) { if (trim($line) === '' || (strpos($line, '|') === false && trim($line) !== '')) { $inTable = false; $filteredLines[] = $line; continue; } if (strpos($line, '|---|') !== false) { $filteredLines[] = $line; continue; } // Filtrar fila de la tabla $matched = false; foreach ($allowedChapters as $cap) { if (strpos($line, "[Capítulo $cap]") !== false) { $matched = true; break; } } if ($matched) { $filteredLines[] = $line; } } else { $filteredLines[] = $line; } } return implode("\n", $filteredLines); } public function download() { $path = base_path('misc/MANUAL_USUARIO.md'); if (!File::exists($path)) { abort(404, 'No se pudo generar el manual porque el archivo base no existe.'); } $md = File::get($path); // Detectar rol de la sesión $role = $this->getUserRole(); // Segmentar contenido $filteredMd = $this->segmentMarkdown($md, $role); // Convertir Markdown a HTML $parsedown = new \Parsedown(); $htmlContent = $parsedown->text($filteredMd); // Preparar Logo en Base64 para el PDF $logoBase64 = null; $logoPath = public_path('logo.png'); if (File::exists($logoPath)) { $type = pathinfo($logoPath, PATHINFO_EXTENSION); $data = File::get($logoPath); $logoBase64 = 'data:image/' . $type . ';base64,' . base64_encode($data); } // Depuración temporal: Descomenta si querés ver qué rol detecta antes de generar el PDF // dd('Rol detectado: ' . $role); // Generar PDF con dompdf $pdf = Pdf::loadView('documentacion.pdf', [ 'content' => $htmlContent, 'logo' => $logoBase64 ]); // Ajustar papel y orientación $pdf->setPaper('A4', 'portrait'); // Nombre de archivo único para evitar caché de navegador $filename = 'Manual_Segmentado_' . $role . '_' . time() . '.pdf'; return $pdf->download($filename); } }