limpieza
This commit is contained in:
@@ -1,129 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\QrCode;
|
||||
use Illuminate\Http\Request;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
|
||||
class QrDownloadController extends Controller
|
||||
{
|
||||
public function download($id)
|
||||
{
|
||||
$qr = QrCode::with(['evento.equipoLocal.club', 'evento.equipoVisitante.club', 'jugador.clubActual', 'aficionado'])
|
||||
->where('id_qr', $id)
|
||||
->firstOrFail();
|
||||
|
||||
// Verificar que el usuario tenga permiso para descargar este QR
|
||||
$isUser = session()->has('user_logged_in') && session('user_logged_in');
|
||||
$isAdmin = session()->has('admin_logged_in') && session('admin_logged_in');
|
||||
|
||||
if ($isUser) {
|
||||
$userId = session('user_id');
|
||||
$userTipo = session('user_tipo');
|
||||
if ($userTipo === 'jugador' && $qr->id_jugador != $userId) abort(403);
|
||||
if ($userTipo === 'aficionado' && $qr->id_aficionado != $userId) abort(403);
|
||||
} elseif (!$isAdmin) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$user = $qr->jugador ?: $qr->aficionado;
|
||||
$userTipo = $qr->id_jugador ? 'jugador' : 'aficionado';
|
||||
|
||||
// Convertir imágenes a Base64 para DomPDF (más fiable que URLs remotas/locales)
|
||||
$qrImageBase64 = '';
|
||||
try {
|
||||
$qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=" . urlencode($qr->id_qr);
|
||||
$qrContent = @file_get_contents($qrUrl);
|
||||
if ($qrContent) {
|
||||
$qrImageBase64 = 'data:image/png;base64,' . base64_encode($qrContent);
|
||||
}
|
||||
} catch (\Exception $e) { }
|
||||
|
||||
$club = ($qr->evento && $qr->evento->equipoLocal && $qr->evento->equipoLocal->club) ? $qr->evento->equipoLocal->club : null;
|
||||
|
||||
$backgroundBase64 = null;
|
||||
if ($club && $club->qr_background) {
|
||||
// 1. Intentar en storage/app/public (ruta interna Laravel)
|
||||
$pathInStorage = str_replace(['storage/', 'storage\\'], '', $club->qr_background);
|
||||
$bgPath = storage_path('app/public/' . $pathInStorage);
|
||||
|
||||
// 2. Intentar en public_path (desarrollo local o symlink funcional)
|
||||
if (!file_exists($bgPath)) {
|
||||
$bgPath = public_path($club->qr_background);
|
||||
}
|
||||
|
||||
// 3. Intentar estructura Hostinger (public_html es hermano de la carpeta laravel)
|
||||
if (!file_exists($bgPath)) {
|
||||
$bgPath = base_path('../public_html/' . $club->qr_background);
|
||||
}
|
||||
|
||||
if (file_exists($bgPath)) {
|
||||
$bgContent = @file_get_contents($bgPath);
|
||||
if ($bgContent) {
|
||||
$ext = pathinfo($bgPath, PATHINFO_EXTENSION);
|
||||
$backgroundBase64 = 'data:image/' . $ext . ';base64,' . base64_encode($bgContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$logoBase64 = null;
|
||||
$logoPathFinal = null;
|
||||
|
||||
// Logo del club del jugador (no del local) si corresponde:
|
||||
// jugador → su club actual; aficionado/sin club → club local; fallback → logo OnAPB.
|
||||
$logoClub = null;
|
||||
if ($userTipo === 'jugador' && $qr->jugador && $qr->jugador->clubActual) {
|
||||
$logoClub = $qr->jugador->clubActual;
|
||||
} elseif ($club) {
|
||||
$logoClub = $club;
|
||||
}
|
||||
|
||||
if ($logoClub && $logoClub->imagen) {
|
||||
$logoInStorage = str_replace(['storage/', 'storage\\'], '', $logoClub->imagen);
|
||||
$lPath = storage_path('app/public/' . $logoInStorage);
|
||||
|
||||
if (!file_exists($lPath)) {
|
||||
$lPath = public_path($logoClub->imagen);
|
||||
}
|
||||
|
||||
if (!file_exists($lPath)) {
|
||||
$lPath = base_path('../public_html/' . $logoClub->imagen);
|
||||
}
|
||||
|
||||
if (file_exists($lPath)) {
|
||||
$logoPathFinal = $lPath;
|
||||
}
|
||||
}
|
||||
|
||||
// Si no hay logo, usar logo general
|
||||
if (!$logoPathFinal) {
|
||||
if (file_exists(public_path('logo.png'))) {
|
||||
$logoPathFinal = public_path('logo.png');
|
||||
}
|
||||
}
|
||||
|
||||
if ($logoPathFinal) {
|
||||
$logoContent = @file_get_contents($logoPathFinal);
|
||||
if ($logoContent) {
|
||||
$ext = pathinfo($logoPathFinal, PATHINFO_EXTENSION);
|
||||
$logoBase64 = 'data:image/' . $ext . ';base64,' . base64_encode($logoContent);
|
||||
}
|
||||
}
|
||||
|
||||
$data = [
|
||||
'qr' => $qr,
|
||||
'user' => $user,
|
||||
'userTipo' => $userTipo,
|
||||
'club' => $club,
|
||||
'qrImageBase64' => $qrImageBase64,
|
||||
'backgroundBase64' => $backgroundBase64,
|
||||
'logoBase64' => $logoBase64,
|
||||
];
|
||||
|
||||
$pdf = Pdf::loadView('pdf.qr_ticket', $data)
|
||||
->setPaper([0, 0, 320, 500], 'portrait');
|
||||
|
||||
return $pdf->download("onapb_qr_{$qr->id_qr}.pdf");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user