src/Controller/Fiche/FicheBatimentController.php line 150

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Fiche;
  3. use App\Entity\Fiche\FicheBatiment;
  4. use App\Form\Fiche\FicheBatimentFilterType;
  5. use App\Repository\Fiche\FicheBatimentRepository;
  6. use App\Service\Fiche\FicheAnalyseService;
  7. use App\Service\Fiche\FicheExempleService;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. #[Route(path'/fiche-batiment/'name'fiche_batiment_')]
  16. class FicheBatimentController extends AbstractController
  17. {
  18.     const MSG_DELETE 'La Fiche Bâtiment a été supprimée avec succès.';
  19.     const MSG_DOUBLONS_DELETE_ALL 'Les doublons de Fiches Bâtiments ont été supprimés avec succès.';
  20.     const NB_INDEX 20;
  21.     const NB_DOUBLONS 20;
  22.     /**
  23.      * @IsGranted("IS_AUTHENTICATED")
  24.      */
  25.     #[Route(path''name'index')]
  26.     public function index(
  27.         Request $theRequest,
  28.         FicheBatimentRepository $theFicheBatimentRepository,
  29.         PaginatorInterface $thePaginator
  30.     )
  31.     {
  32.         if (!$this->isGranted('ROLE_ADMIN')) {
  33.             $typeFormFilter 'utilisateur';
  34.             $idUser $this->getUser()->getId();
  35.         } else {
  36.             $typeFormFilter 'admin';
  37.             $idUser null;
  38.         }
  39.         $theFormFilter $this->createForm(FicheBatimentFilterType::class, null, ['type' => $typeFormFilter]);
  40.         $theFormFilter->handleRequest($theRequest);
  41.         if ($theFormFilter->isSubmitted() && $theFormFilter->isValid()) {
  42.             $listFicheBatimentBuilder $theFicheBatimentRepository->findAllIndexBuilder($theFormFilter->getData(), $idUser);
  43.         } else {
  44.             $listFicheBatimentBuilder $theFicheBatimentRepository->findAllIndexBuilder(null$idUser);
  45.         }
  46.         
  47.         dump($listFicheBatimentBuilder);
  48.         $listFicheBatiment $thePaginator->paginate(
  49.             $listFicheBatimentBuilder,
  50.             $theRequest->get('page'1),
  51.             self::NB_INDEX
  52.         );
  53.         
  54.         dump($listFicheBatiment);
  55.         return $this->render('fiche/fiche-batiment/index.html.twig', [
  56.             'listFicheBatiment' => $listFicheBatiment,
  57.             'theFormFilter' => $theFormFilter->createView()
  58.         ]);
  59.     }
  60.     /**
  61.      * @IsGranted("ROLE_ADMIN")
  62.      */
  63.     #[Route(path'doublons'name'doublons')]
  64.     public function doublons(
  65.         FicheBatimentRepository $theFicheBatimentRepository
  66.     )
  67.     {
  68.         // Récupère tout les ID de Fiches en doublon
  69.         $doublonsIdFiches $theFicheBatimentRepository->getDoublonsIdFiche();
  70.         // Prépare la requête qui va récupérer toutes les fiches pour chaque ID de fiche en doublon
  71.         $theQueryBuilderDoublonIdUser $theFicheBatimentRepository->findDoublonByIdFicheUserNullBuilder();
  72.         $theQueryBuilderDoublonUserNull $theFicheBatimentRepository->findDoublonByIdFicheUserBuilder();
  73.         $nbDoublons count($doublonsIdFiches);
  74.         for($i 0$i $nbDoublons; ++$i) {
  75.             $doublonsIdFiches[$i]['listFiche'] = new ArrayCollection();
  76.              if($doublonsIdFiches[$i]['idUser'] == null) {
  77.                  $listFiche $theQueryBuilderDoublonUserNull->setParameter('idFiche'$doublonsIdFiches[$i]['idFiche'])->getQuery()->getResult();
  78.              } else {
  79.                  $listFiche $theQueryBuilderDoublonIdUser
  80.                     ->setParameter('idFiche'$doublonsIdFiches[$i]['idFiche'])
  81.                     ->andWhere('fiche_batiment.theUser = :idUser')
  82.                     ->setParameter('idUser'$doublonsIdFiches[$i]['idUser'])
  83.                     ->getQuery()->getResult();
  84.              }
  85.             foreach($listFiche as $aFiche) {
  86.                 $doublonsIdFiches[$i]['listFiche']->add($aFiche);
  87.             }
  88.         }
  89.         return $this->render('fiche/fiche-batiment/doublons.html.twig', [
  90.             'doublonsIdFiches' => $doublonsIdFiches
  91.         ]);
  92.     }
  93.     /**
  94.      * @IsGranted("ROLE_ADMIN")
  95.      */
  96.     #[Route(path'doublons/delete-all'name'doublons_delete_all')]
  97.     public function doublonsDeleteAll(
  98.         FicheBatimentRepository $theFicheBatimentRepository,
  99.         ManagerRegistry $theManagerRegistry
  100.     )
  101.     {
  102.         $theEM $theManagerRegistry->getManager();
  103.         // Récupère tout les ID de Fiches en doublon
  104.         $doublonsIdFiches $theFicheBatimentRepository->getDoublonsIdFiche();
  105.         // Prépare la requête qui va récupérer toutes les fiches pour chaque ID de fiche en doublon
  106.         $theQueryBuilderDoublonIdUser $theFicheBatimentRepository->findDoublonByIdFicheUserNullBuilder();
  107.         $theQueryBuilderDoublonUserNull $theFicheBatimentRepository->findDoublonByIdFicheUserBuilder();
  108.         $nbDoublons count($doublonsIdFiches);
  109.         for($i 0$i $nbDoublons; ++$i) {
  110.             $doublonsIdFiches[$i]['listFiche'] = new ArrayCollection();
  111.             if($doublonsIdFiches[$i]['idUser'] == null) {
  112.                 $listFiche $theQueryBuilderDoublonUserNull->setParameter('idFiche'$doublonsIdFiches[$i]['idFiche'])->getQuery()->getResult();
  113.             } else {
  114.                 $listFiche $theQueryBuilderDoublonIdUser
  115.                     ->setParameter('idFiche'$doublonsIdFiches[$i]['idFiche'])
  116.                     ->andWhere('fiche_batiment.theUser = :idUser')
  117.                     ->setParameter('idUser'$doublonsIdFiches[$i]['idUser'])
  118.                     ->getQuery()->getResult();
  119.             }
  120.             // Supprime toutes les fiches en doublons sauf la 1ère
  121.             $isFirstDoublon true;
  122.             foreach($listFiche as $aFiche) {
  123.                 if ($isFirstDoublon) {
  124.                     $isFirstDoublon false;
  125.                 } else {
  126.                     $theEM->remove($aFiche);
  127.                     $aFiche->deleteXmlFile();
  128.                 }
  129.             }
  130.         }
  131.         $theEM->flush();
  132.         $this->addFlash('success'self::MSG_DOUBLONS_DELETE_ALL);
  133.         return $this->redirectToRoute('fiche_batiment_doublons');
  134.     }
  135.     #[Route(path'analyse/{id}'name'analyse'options: ['expose' => true])]
  136.     public function analyse(FicheBatimentRepository $theFicheBatimentRepository$id) {
  137.         if ($id == 0) {
  138.             $theFicheBatiment FicheExempleService::getTheFicheBatimentExemple();
  139.         } else {
  140.             $theFicheBatiment $theFicheBatimentRepository->find($id);
  141.         }
  142.         $this->denyAccessUnlessGranted('fiche_batiment_analyse'$theFicheBatiment);
  143.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED') && !$this->getUser()->isComparaisonAll()) {
  144.             $listFicheComparaison $theFicheBatimentRepository->findByUser($this->getUser()->getId());
  145.         } else {
  146.             $listFicheComparaison $theFicheBatimentRepository->findAll();
  147.         }
  148.         
  149.         
  150. // Calcul de la moyenne de chaque donnée
  151.         $bbioChaudAnnuelSomme 0;
  152.         $compaciteSomme 0;
  153.         $tauxBrassageBBIOSomme 0;
  154.         $tauxvitrageSomme 0;
  155.         $uwMoyenSomme 0;
  156.         $nbre 0;
  157.       
  158.         foreach ($listFicheComparaison as $aFicheComparaison) {
  159.             $bbioChaudAnnuelSomme $aFicheComparaison->getBbioChaudAnnuel() + $bbioChaudAnnuelSomme;
  160.             $compaciteSomme $aFicheComparaison->getCompacite() + $compaciteSomme;
  161.             $tauxBrassageBBIOSomme $aFicheComparaison->getTauxBrassageBBIO() + $tauxBrassageBBIOSomme;
  162.             $tauxvitrageSomme $aFicheComparaison->getTauxVitrage() + $tauxvitrageSomme;
  163.             $uwMoyenSomme $aFicheComparaison->getUwMoyen() + $uwMoyenSomme;
  164.             $nbre++;
  165.         }
  166.         $bbioChaudAnnuelMoyen number_format($bbioChaudAnnuelSomme $nbre2);
  167.         $compaciteMoyen number_format($compaciteSomme $nbre2);
  168.         $tauxBrassageBBIOMoyen number_format($tauxBrassageBBIOSomme $nbre2);
  169.         $tauxvitrageMoyen number_format($tauxvitrageSomme $nbre2);
  170.         $uwMoyenMoyen number_format($uwMoyenSomme $nbre2);
  171.         //fin du calcul de la moyenne
  172.         
  173.         //Aggrégration dans un tableau des données comparatives :
  174.         foreach ($listFicheComparaison as $aFicheComparaison) {
  175.             if ($aFicheComparaison->getBbioChaudAnnuel() !== null) {
  176.                 $bbioChaudAnnuelArray[] = $aFicheComparaison->getBbioChaudAnnuel();
  177.             }
  178.             if ($aFicheComparaison->getCompacite() <= 5) {
  179.                 $compaciteArray[] = $aFicheComparaison->getCompacite();
  180.             }
  181.             if ($aFicheComparaison->getTauxBrassageBBIO() !== null) {
  182.                 $brassage[] = $aFicheComparaison->getTauxBrassageBBIO();
  183.             }
  184.             if ($aFicheComparaison->getRatioEclNat() !== null) {
  185.                 $RatioEclNat[] = $aFicheComparaison->getRatioEclNat();
  186.             }
  187.             if ($aFicheComparaison->getPVentSpecifiqueBatiment() !== null) {
  188.                 $PVentSpecifiqueBatiment[] = $aFicheComparaison->getPVentSpecifiqueBatiment();
  189.             }
  190.             if ($aFicheComparaison->getratioPsi() !== null) {
  191.                 $ratioPsi[] = $aFicheComparaison->getratioPsi();
  192.             }
  193.             $tauxvitrage[] = $aFicheComparaison->getTauxVitrage();
  194.             $uWmoyen[] = $aFicheComparaison->getUwMoyen();
  195.             $masqueMoyen[] = $aFicheComparaison->getMasqueMoyen();
  196.             $deperditionSurfacique[] = $aFicheComparaison->getDeperditionSurfacique();
  197.            
  198.         }
  199.         //Calcul des percentiles par l'analyseService :
  200.         $graphCompacite FicheAnalyseService::tableau($compaciteArray);
  201.         $graphBbio FicheAnalyseService::tableau($bbioChaudAnnuelArray);
  202.         $graphBrassage FicheAnalyseService::tableau($brassage);
  203.         $graphtauxvitrage FicheAnalyseService::tableau($tauxvitrage);
  204.         $graphuWmoyen FicheAnalyseService::tableau($uWmoyen);
  205.         $graphmasqueMoyen FicheAnalyseService::tableau($masqueMoyen);
  206.         $graphdeperditionSurfacique FicheAnalyseService::tableau($deperditionSurfacique);
  207.         $graphratioPsi FicheAnalyseService::tableau($ratioPsi);
  208.         $graphRatioEclNat FicheAnalyseService::tableau($RatioEclNat);
  209.         $graphPVentSpecifiqueBatiment FicheAnalyseService::tableau($PVentSpecifiqueBatiment);
  210.         //Transformation en language JSON :
  211.         $graphjson = [
  212.             'graphCompacite' => $graphCompacite,
  213.             'graphBbio' => $graphBbio,
  214.             'graphBrassage' => $graphBrassage,
  215.             'graphTauxVitrage' => $graphtauxvitrage,
  216.             'graphUwMoyen' => $graphuWmoyen,
  217.             'graphmasqueMoyen' => $graphmasqueMoyen,
  218.             'graphdeperditionSurfacique' => $graphdeperditionSurfacique,
  219.             'graphratioPsi' => $graphratioPsi,
  220.             'graphRatioEclNat' => $graphRatioEclNat,
  221.             'graphPVentSpecifiqueBatiment' => $graphPVentSpecifiqueBatiment
  222.         ];
  223.         $DeperditionkW number_format($theFicheBatiment->getDeperditionBatiment() / 10002);
  224.         return $this->render('fiche/fiche-batiment/analyse.html.twig', ['theFicheBatiment' => $theFicheBatiment,
  225.             'bbioChaudAnnuelMoyen' => $bbioChaudAnnuelMoyen,
  226.             'compaciteMoyen' => $compaciteMoyen,
  227.             'tauxBrassageBBIOMoyen' => $tauxBrassageBBIOMoyen,
  228.             'tauxvitrageMoyen' => $tauxvitrageMoyen,
  229.             'uwMoyenMoyen' => $uwMoyenMoyen,
  230.             'DeperditionkW' =>$DeperditionkW,
  231.             'graphjson' => $graphjson
  232.                 
  233.         ]);
  234.     }
  235.     /**
  236.      * @param Request $theRequest
  237.      * @param FicheBatiment $theFicheBatiment
  238.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  239.      *
  240.      * @IsGranted("IS_AUTHENTICATED")
  241.      */
  242.     #[Route(path'delete/{id}'name'delete')]
  243.     public function delete(Request $theRequestFicheBatiment $theFicheBatimentManagerRegistry $theManagerRegistry)
  244.     {
  245.         $this->denyAccessUnlessGranted('fiche_batiment_delete'$theFicheBatiment);
  246.         $theEM $theManagerRegistry->getManager();
  247.         $theEM->remove($theFicheBatiment);
  248.         $theFicheBatiment->moveXmlFile();
  249.         $theEM->flush();
  250.         $this->addFlash('success'self::MSG_DELETE);
  251.         if ($theRequest->query->get('from') == 'fiche' || !$theRequest->headers->has('referer')) {
  252.             $theResponse $this->redirectToRoute('fiche_batiment_index');
  253.         } else {
  254.             $theResponse $this->redirect($theRequest->headers->get('referer'));
  255.         }
  256.         return $theResponse;
  257.     }
  258. }