<?php
namespace App\Controller\Fiche;
use App\Entity\Fiche\FicheBatiment;
use App\Form\Fiche\FicheBatimentFilterType;
use App\Repository\Fiche\FicheBatimentRepository;
use App\Service\Fiche\FicheAnalyseService;
use App\Service\Fiche\FicheExempleService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Persistence\ManagerRegistry;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
#[Route(path: '/fiche-batiment/', name: 'fiche_batiment_')]
class FicheBatimentController extends AbstractController
{
const MSG_DELETE = 'La Fiche Bâtiment a été supprimée avec succès.';
const MSG_DOUBLONS_DELETE_ALL = 'Les doublons de Fiches Bâtiments ont été supprimés avec succès.';
const NB_INDEX = 20;
const NB_DOUBLONS = 20;
#[Route(path: '', name: 'index')]
#[IsGranted('IS_AUTHENTICATED')]
public function index(
Request $theRequest,
FicheBatimentRepository $theFicheBatimentRepository,
PaginatorInterface $thePaginator
)
{
if (!$this->isGranted('ROLE_ADMIN')) {
$typeFormFilter = 'utilisateur';
$idUser = $this->getUser()->getId();
} else {
$typeFormFilter = 'admin';
$idUser = null;
}
$theFormFilter = $this->createForm(FicheBatimentFilterType::class, null, ['type' => $typeFormFilter]);
$theFormFilter->handleRequest($theRequest);
if ($theFormFilter->isSubmitted() && $theFormFilter->isValid()) {
$listFicheBatimentBuilder = $theFicheBatimentRepository->findAllIndexBuilder($theFormFilter->getData(), $idUser);
} else {
$listFicheBatimentBuilder = $theFicheBatimentRepository->findAllIndexBuilder(null, $idUser);
}
dump($listFicheBatimentBuilder);
$listFicheBatiment = $thePaginator->paginate(
$listFicheBatimentBuilder,
$theRequest->get('page', 1),
self::NB_INDEX
);
dump($listFicheBatiment);
return $this->render('fiche/fiche-batiment/index.html.twig', [
'listFicheBatiment' => $listFicheBatiment,
'theFormFilter' => $theFormFilter->createView()
]);
}
#[Route(path: 'doublons', name: 'doublons')]
#[IsGranted('ROLE_ADMIN')]
public function doublons(
FicheBatimentRepository $theFicheBatimentRepository
)
{
// Récupère tout les ID de Fiches en doublon
$doublonsIdFiches = $theFicheBatimentRepository->getDoublonsIdFiche();
// Prépare la requête qui va récupérer toutes les fiches pour chaque ID de fiche en doublon
$theQueryBuilderDoublonIdUser = $theFicheBatimentRepository->findDoublonByIdFicheUserNullBuilder();
$theQueryBuilderDoublonUserNull = $theFicheBatimentRepository->findDoublonByIdFicheUserBuilder();
$nbDoublons = count($doublonsIdFiches);
for($i = 0; $i < $nbDoublons; ++$i) {
$doublonsIdFiches[$i]['listFiche'] = new ArrayCollection();
if($doublonsIdFiches[$i]['idUser'] == null) {
$listFiche = $theQueryBuilderDoublonUserNull->setParameter('idFiche', $doublonsIdFiches[$i]['idFiche'])->getQuery()->getResult();
} else {
$listFiche = $theQueryBuilderDoublonIdUser
->setParameter('idFiche', $doublonsIdFiches[$i]['idFiche'])
->andWhere('fiche_batiment.theUser = :idUser')
->setParameter('idUser', $doublonsIdFiches[$i]['idUser'])
->getQuery()->getResult();
}
foreach($listFiche as $aFiche) {
$doublonsIdFiches[$i]['listFiche']->add($aFiche);
}
}
return $this->render('fiche/fiche-batiment/doublons.html.twig', [
'doublonsIdFiches' => $doublonsIdFiches
]);
}
#[Route(path: 'doublons/delete-all', name: 'doublons_delete_all')]
#[IsGranted('ROLE_ADMIN')]
public function doublonsDeleteAll(
FicheBatimentRepository $theFicheBatimentRepository,
ManagerRegistry $theManagerRegistry
)
{
$theEM = $theManagerRegistry->getManager();
// Récupère tout les ID de Fiches en doublon
$doublonsIdFiches = $theFicheBatimentRepository->getDoublonsIdFiche();
// Prépare la requête qui va récupérer toutes les fiches pour chaque ID de fiche en doublon
$theQueryBuilderDoublonIdUser = $theFicheBatimentRepository->findDoublonByIdFicheUserNullBuilder();
$theQueryBuilderDoublonUserNull = $theFicheBatimentRepository->findDoublonByIdFicheUserBuilder();
$nbDoublons = count($doublonsIdFiches);
for($i = 0; $i < $nbDoublons; ++$i) {
$doublonsIdFiches[$i]['listFiche'] = new ArrayCollection();
if($doublonsIdFiches[$i]['idUser'] == null) {
$listFiche = $theQueryBuilderDoublonUserNull->setParameter('idFiche', $doublonsIdFiches[$i]['idFiche'])->getQuery()->getResult();
} else {
$listFiche = $theQueryBuilderDoublonIdUser
->setParameter('idFiche', $doublonsIdFiches[$i]['idFiche'])
->andWhere('fiche_batiment.theUser = :idUser')
->setParameter('idUser', $doublonsIdFiches[$i]['idUser'])
->getQuery()->getResult();
}
// Supprime toutes les fiches en doublons sauf la 1ère
$isFirstDoublon = true;
foreach($listFiche as $aFiche) {
if ($isFirstDoublon) {
$isFirstDoublon = false;
} else {
$theEM->remove($aFiche);
$aFiche->deleteXmlFile();
}
}
}
$theEM->flush();
$this->addFlash('success', self::MSG_DOUBLONS_DELETE_ALL);
return $this->redirectToRoute('fiche_batiment_doublons');
}
#[Route(path: 'analyse/{id}', name: 'analyse', options: ['expose' => true])]
public function analyse(FicheBatimentRepository $theFicheBatimentRepository, $id) {
if ($id == 0) {
$theFicheBatiment = FicheExempleService::getTheFicheBatimentExemple();
} else {
$theFicheBatiment = $theFicheBatimentRepository->find($id);
}
$this->denyAccessUnlessGranted('fiche_batiment_analyse', $theFicheBatiment);
if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED') && !$this->getUser()->isComparaisonAll()) {
$listFicheComparaison = $theFicheBatimentRepository->findByUser($this->getUser()->getId());
} else {
$listFicheComparaison = $theFicheBatimentRepository->findAll();
}
// Calcul de la moyenne de chaque donnée
$bbioChaudAnnuelSomme = 0;
$compaciteSomme = 0;
$tauxBrassageBBIOSomme = 0;
$tauxvitrageSomme = 0;
$uwMoyenSomme = 0;
$nbre = 0;
foreach ($listFicheComparaison as $aFicheComparaison) {
$bbioChaudAnnuelSomme = $aFicheComparaison->getBbioChaudAnnuel() + $bbioChaudAnnuelSomme;
$compaciteSomme = $aFicheComparaison->getCompacite() + $compaciteSomme;
$tauxBrassageBBIOSomme = $aFicheComparaison->getTauxBrassageBBIO() + $tauxBrassageBBIOSomme;
$tauxvitrageSomme = $aFicheComparaison->getTauxVitrage() + $tauxvitrageSomme;
$uwMoyenSomme = $aFicheComparaison->getUwMoyen() + $uwMoyenSomme;
$nbre++;
}
$bbioChaudAnnuelMoyen = number_format($bbioChaudAnnuelSomme / $nbre, 2);
$compaciteMoyen = number_format($compaciteSomme / $nbre, 2);
$tauxBrassageBBIOMoyen = number_format($tauxBrassageBBIOSomme / $nbre, 2);
$tauxvitrageMoyen = number_format($tauxvitrageSomme / $nbre, 2);
$uwMoyenMoyen = number_format($uwMoyenSomme / $nbre, 2);
//fin du calcul de la moyenne
//Aggrégration dans un tableau des données comparatives :
foreach ($listFicheComparaison as $aFicheComparaison) {
if ($aFicheComparaison->getBbioChaudAnnuel() !== null) {
$bbioChaudAnnuelArray[] = $aFicheComparaison->getBbioChaudAnnuel();
}
if ($aFicheComparaison->getCompacite() <= 5) {
$compaciteArray[] = $aFicheComparaison->getCompacite();
}
if ($aFicheComparaison->getTauxBrassageBBIO() !== null) {
$brassage[] = $aFicheComparaison->getTauxBrassageBBIO();
}
if ($aFicheComparaison->getRatioEclNat() !== null) {
$RatioEclNat[] = $aFicheComparaison->getRatioEclNat();
}
if ($aFicheComparaison->getPVentSpecifiqueBatiment() !== null) {
$PVentSpecifiqueBatiment[] = $aFicheComparaison->getPVentSpecifiqueBatiment();
}
if ($aFicheComparaison->getratioPsi() !== null) {
$ratioPsi[] = $aFicheComparaison->getratioPsi();
}
$tauxvitrage[] = $aFicheComparaison->getTauxVitrage();
$uWmoyen[] = $aFicheComparaison->getUwMoyen();
$masqueMoyen[] = $aFicheComparaison->getMasqueMoyen();
$deperditionSurfacique[] = $aFicheComparaison->getDeperditionSurfacique();
}
//Calcul des percentiles par l'analyseService :
$graphCompacite = FicheAnalyseService::tableau($compaciteArray);
$graphBbio = FicheAnalyseService::tableau($bbioChaudAnnuelArray);
$graphBrassage = FicheAnalyseService::tableau($brassage);
$graphtauxvitrage = FicheAnalyseService::tableau($tauxvitrage);
$graphuWmoyen = FicheAnalyseService::tableau($uWmoyen);
$graphmasqueMoyen = FicheAnalyseService::tableau($masqueMoyen);
$graphdeperditionSurfacique = FicheAnalyseService::tableau($deperditionSurfacique);
$graphratioPsi = FicheAnalyseService::tableau($ratioPsi);
$graphRatioEclNat = FicheAnalyseService::tableau($RatioEclNat);
$graphPVentSpecifiqueBatiment = FicheAnalyseService::tableau($PVentSpecifiqueBatiment);
//Transformation en language JSON :
$graphjson = [
'graphCompacite' => $graphCompacite,
'graphBbio' => $graphBbio,
'graphBrassage' => $graphBrassage,
'graphTauxVitrage' => $graphtauxvitrage,
'graphUwMoyen' => $graphuWmoyen,
'graphmasqueMoyen' => $graphmasqueMoyen,
'graphdeperditionSurfacique' => $graphdeperditionSurfacique,
'graphratioPsi' => $graphratioPsi,
'graphRatioEclNat' => $graphRatioEclNat,
'graphPVentSpecifiqueBatiment' => $graphPVentSpecifiqueBatiment
];
$DeperditionkW = number_format($theFicheBatiment->getDeperditionBatiment() / 1000, 2);
return $this->render('fiche/fiche-batiment/analyse.html.twig', ['theFicheBatiment' => $theFicheBatiment,
'bbioChaudAnnuelMoyen' => $bbioChaudAnnuelMoyen,
'compaciteMoyen' => $compaciteMoyen,
'tauxBrassageBBIOMoyen' => $tauxBrassageBBIOMoyen,
'tauxvitrageMoyen' => $tauxvitrageMoyen,
'uwMoyenMoyen' => $uwMoyenMoyen,
'DeperditionkW' =>$DeperditionkW,
'graphjson' => $graphjson
]);
}
/**
* @param Request $theRequest
* @param FicheBatiment $theFicheBatiment
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
#[Route(path: 'delete/{id}', name: 'delete')]
#[IsGranted('IS_AUTHENTICATED')]
public function delete(Request $theRequest, FicheBatiment $theFicheBatiment, ManagerRegistry $theManagerRegistry)
{
$this->denyAccessUnlessGranted('fiche_batiment_delete', $theFicheBatiment);
$theEM = $theManagerRegistry->getManager();
$theEM->remove($theFicheBatiment);
$theFicheBatiment->moveXmlFile();
$theEM->flush();
$this->addFlash('success', self::MSG_DELETE);
if ($theRequest->query->get('from') == 'fiche' || !$theRequest->headers->has('referer')) {
$theResponse = $this->redirectToRoute('fiche_batiment_index');
} else {
$theResponse = $this->redirect($theRequest->headers->get('referer'));
}
return $theResponse;
}
}