85 lines
2.9 KiB
PHP
85 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Evento;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class EventoController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$fechaStr = $request->get('fecha', now()->toDateString());
|
|
$fechaSeleccionada = Carbon::parse($fechaStr);
|
|
|
|
// Generar rango de fechas (Ayer, Hoy, Mañana + otros)
|
|
$fechasNav = [];
|
|
for ($i = -3; $i <= 3; $i++) {
|
|
$d = now()->addDays($i);
|
|
$fechasNav[] = [
|
|
'label' => $this->getFechaLabel($d),
|
|
'fecha' => $d->toDateString(),
|
|
'active' => $d->isSameDay($fechaSeleccionada)
|
|
];
|
|
}
|
|
|
|
$eventos = Evento::with(['equipoLocal.club', 'equipoVisitante.club'])
|
|
->whereDate('fecha_evento', $fechaSeleccionada->toDateString())
|
|
->orderBy('hora_inicio', 'asc')
|
|
->get()
|
|
->map(function ($e) {
|
|
$e->estado = $this->calcularEstado($e->fecha_evento, $e->hora_inicio, $e->hora_fin);
|
|
return $e;
|
|
});
|
|
|
|
return view('eventos.index', compact('eventos', 'fechasNav', 'fechaSeleccionada'));
|
|
}
|
|
|
|
private function getFechaLabel(Carbon $date)
|
|
{
|
|
$fecha = $date->format('d/m');
|
|
if ($date->isToday()) return "HOY $fecha";
|
|
if ($date->isYesterday()) return "AYER $fecha";
|
|
if ($date->isTomorrow()) return "MAÑANA $fecha";
|
|
|
|
return $date->translatedFormat('D d/m');
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$evento = Evento::with(['equipoLocal.club', 'equipoVisitante.club', 'qrCodes'])
|
|
->findOrFail($id);
|
|
|
|
$evento->estado = $this->calcularEstado($evento->fecha_evento, $evento->hora_inicio, $evento->hora_fin);
|
|
|
|
$isAdmin = session()->has('admin_logged_in') && session('admin_logged_in');
|
|
$isUser = session()->has('user_logged_in') && session('user_logged_in');
|
|
|
|
return view('eventos.show', compact('evento', 'isAdmin', 'isUser'));
|
|
}
|
|
|
|
private function calcularEstado($fechaEvento, $horaInicio, $horaFin)
|
|
{
|
|
$tz = new \DateTimeZone(config('app.timezone', 'America/Argentina/Buenos_Aires'));
|
|
|
|
// Asegurarnos de tener strings limpios (Y-m-d y H:i:s)
|
|
$f = ($fechaEvento instanceof \Carbon\Carbon) ? $fechaEvento->format('Y-m-d') : substr($fechaEvento, 0, 10);
|
|
$h1 = ($horaInicio instanceof \Carbon\Carbon) ? $horaInicio->format('H:i:s') : $horaInicio;
|
|
$h2 = ($horaFin instanceof \Carbon\Carbon) ? $horaFin->format('H:i:s') : $horaFin;
|
|
|
|
$inicio = new \DateTime("$f $h1", $tz);
|
|
$fin = new \DateTime("$f $h2", $tz);
|
|
|
|
if ($fin <= $inicio) {
|
|
$fin->modify('+1 day');
|
|
}
|
|
|
|
$ahora = new \DateTime('now', $tz);
|
|
|
|
if ($ahora >= $inicio && $ahora <= $fin) return 'Activo';
|
|
if ($ahora < $inicio) return 'Próximo';
|
|
return 'Finalizado';
|
|
}
|
|
}
|