<?php
namespace App\Controller;
use App\Entity\Category;
use App\Entity\Competition;
use App\Entity\Event;
use App\Entity\Result;
use App\Services\CSVPersonServices;
use App\Services\PodiumServices;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpClient\CurlHttpClient;
use function PHPUnit\Framework\isNull;
class AppController extends AbstractController
{
public function __construct( EntityManagerInterface $em)
{
$this->em =$em;
}
/**
* @Route("/resultats", name="resultats_ffme")
*/
public function resultats_ffme(): Response
{
return $this->render('app/resultats_ffme.html.twig');
}
/**
* @Route("/api/ffme", name="api_ffme")
*/
public function apiresultat(): Response
{
$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/../../.env');
$this->dirCacheJson = '../var/cache/' . $_ENV['APP_ENV'] . '/ffme';
$this->fileCacheJson = $this->dirCacheJson . '/config_resultat_ffme.json';
$jsonString = file_get_contents($this->fileCacheJson);
$config = json_decode($jsonString, true);
$date = new \DateTimeImmutable();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config["url"].$config["id"]."?z=".uniqid());
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//recuperer les information url
$response = curl_exec($ch);
$arrResultat = json_decode($response);
$Res=$config;
$Res["name"]=$arrResultat->nomCompetition;
$Res["lieu"]=$arrResultat->lieu;
$Res["timestamp"]=$arrResultat->timestamp;
// $Res["groupes"]=$arrResultat->groupes;
foreach ( $arrResultat->groupes as $vague){
// $Res["vague"][$vague->sequence][$vague->categorie][$vague->sexe]["etapes"] = $vague->etapes;
if(count($vague->resultats) > 0){
$Res["VAGUES"][$vague->categorie."_".$vague->sexe][$vague->sequence] = $vague->resultats;
$Res["VAGUES"][$vague->categorie."_".$vague->sexe]["CAT"]=$vague->categorie;
$Res["VAGUES"][$vague->categorie."_".$vague->sexe]["GENRE"]=$vague->sexe;
if(!isset($config["VAGUES"][$vague->categorie."_".$vague->sexe])){
$Res["VAGUES"][$vague->categorie."_".$vague->sexe]["mode"] ='INVISIBLE';
}
}
}
foreach ( $Res["VAGUES"] as $key=> $vague){
if($vague["mode"]=="INVISIBLE"){
unset( $Res["VAGUES"][$key]);
}
}
// $Res["states"] =['INVISIBLE',"QUALIF_IN_PROGRESS","QUALIF_RESULT","FINAL"];
//recuperation des donné save pour chaque cles "U18_HOMME" ...etc
// $Res["VAGUES"]["U18_HOMME"]["mode"] ='QUALIF_IN_PROGRESS';
// $Res["VAGUES"]["U16_FEMME"]["mode"] ='FINAL';
ksort($Res["VAGUES"]);
// $arrResultat->update=date("d/m/Y h:i",substr($arrResultat->timestamp,0,-3));
// $arrResultat->read=date("d/m/Y h:i",$date->getTimestamp());
// $arrResultat->urlFfme="demo.escalade.online";
// $arrResultat->idFfme=555;
// $arrResultat->numCompetition=10;
//$json = $client->request('GET', "https://demo.escalade.online/ws/app/resultatJson/555", ['headers' => ['accept' => 'application/json']])->toArray();
return $this->json($Res);
}
/**
* @Route("/", name="home")
*/
public function index(): Response
{
return $this->redirectToRoute("app_login");
}
/**
* @Route("/test", name="test_podium")
*/
public function test_podium(): Response
{
$podium = new PodiumServices($this->em);
$test = $podium->getTestPodium(["cid"=>4]);
dd( $test);
$competitions = $this->em->getRepository(Competition::class)->findBy([],["id"=>"ASC"]);
return $this->render('app/index.html.twig', [
'controller_name' => 'AppController',
'competitions'=>$competitions
]);
}
/**
* @Route("/live", name="lives_podium")
*/
public function lives_podium(): Response
{
$events = $this->em->getRepository(Event::class)->findBy([],["date_start"=>"ASC"]);
dd($events);
return $this->render('podium/events.html.twig', [
'controller_name' => 'AppController',
'event'=>$events
]);
}
/**
* @Route("/live/{id}/podium", name="live_podium")
*/
public function live_podium(Event $event): Response
{
// $competitions = $this->em->getRepository(Competition::class)->findBy([],["date_start"=>"ASC"]);
return $this->render('podium/index.html.twig', [
'controller_name' => 'AppController',
'competitions'=>$event->getCompetitions()
]);
}
/**
* @Route("/api/podium/vague", name="api_json_podium")
*/
public function api_podium(Request $request): Response
{
$datasForm = $request->getContent();
$datasForm = json_decode($datasForm, true);
if(!isset($datasForm["cid"])){
return [];
}
$competition = $this->em->getRepository(Competition::class)->find($datasForm["cid"]);
$podium = new PodiumServices($this->em);
[$competitors,$BlocsInfo] = $podium->getPodium($datasForm);
$event =[];
$event["name"]=$competition->getEvent()->getName();
$event["competition"]=$competition->getName();
foreach ($competition->getTrials() as $trial){
$bloc = $trial->getBloc();
if(!in_array($bloc->getName(),array_keys($BlocsInfo))){
$event["blocs"][$bloc->getName()]["name"]=$bloc->getName();
$event["blocs"][$bloc->getName()]["pts_total_zone"]=$bloc->getPtsDefault()/2;
$event["blocs"][$bloc->getName()]["pts_total_top"]=$bloc->getPtsDefault()/2;
}else{
$event["blocs"][$bloc->getName()]=$BlocsInfo[$bloc->getName()];
}
$event["blocs"][$bloc->getName()]["id"]=$bloc->getId();
$event["blocs"][$bloc->getName()]["pts_default_zone"]=$bloc->getPtsDefault()/2;
$event["blocs"][$bloc->getName()]["pts_default_top"]=$bloc->getPtsDefault()/2;
}
return $this->json(["competitors"=>$competitors,"event"=>$event]);
}
}