Files
OnAPB-Carrere_Demartin/app/Http/Controllers/HomeController.php
T
2026-06-04 15:10:18 -03:00

87 lines
2.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Noticia;
use App\Models\Torneo;
use App\Models\Evento;
use App\Models\Promocion;
use App\Models\CarouselItem;
use App\Services\TournamentService;
use Carbon\Carbon;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
$eventos = $this->getEventos();
$promociones = Promocion::whereNotNull('descripcion')
->where('descripcion', '!=', '')
->orderBy('id', 'desc')
->get();
$carouselItems = CarouselItem::where('activo', true)
->orderBy('orden', 'asc')
->get();
$noticias = Noticia::orderBy('id', 'desc')
->take(3)
->get();
$torneos = Torneo::orderBy('id', 'desc')
->get();
$tournamentService = new TournamentService();
$standingsData = [];
foreach ($torneos as $t) {
$standingsData[$t->id] = $tournamentService->getStandings($t->id);
}
return view('welcome', compact('eventos', 'promociones', 'carouselItems', 'noticias', 'torneos', 'standingsData'));
}
private function getEventos()
{
$eventos = Evento::with(['equipoLocal.club', 'equipoVisitante.club'])
->orderBy('fecha_evento', 'asc')
->orderBy('hora_inicio', 'asc')
->get()
->map(function ($e) {
$estado = $this->calcularEstado($e->fecha_evento, $e->hora_inicio, $e->hora_fin);
if (in_array($estado, ['Activo', 'Próximo'])) {
$e->estado = $estado;
return $e;
}
return null;
})
->filter()
->values();
return $eventos;
}
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';
}
}