src/Controller/Fiche/FicheBatimentController.php line 144

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