src/Controller/DashboardController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Beneficiary;
  4. use App\Entity\Promotion;
  5. use App\Entity\AddressRegion;
  6. use App\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Contracts\Service\Attribute\Required;
  13. class DashboardController extends AbstractController
  14. {
  15.     #[Required]
  16.     public PromotionListingController $promotionListingController;
  17.     #[Route('/admin'name'home')]
  18.     public function index(EntityManagerInterface $emRequest $request): Response
  19.     {
  20.         // Pas le plus clean, je sais, mais ça a le mérite de fonctionner
  21.         if ($this->isGranted('ROLE_BENEFICIARY')) {
  22.             return $this->redirectToRoute('beneficiary.edit', ['id' => $this->getUser()->getBeneficiary()->getId()]);
  23.         }
  24.         try {
  25.             $items $this->promotionListingController->getItemsFromRequest($request'', ['no_limit' => true], PromotionListingController::QUERY_DASHBOARD, ['no_limit' => true]);
  26.         } catch (\Exception $e) {
  27.             $this->addFlash('danger''Une erreur est survenu durant le chargement du listing : ' $e->getMessage());
  28.             return $this->redirectToRoute('home');
  29.         }
  30.         /** @var User $user */
  31.         $user $this->getUser();
  32.         $beneficiariesPerRegions $em->getRepository(Beneficiary::class)->findNbPerRegions(
  33.             $user->hasRole('ROLE_SPEAKER') || $user->hasRole('ROLE_SUPPORT') ? $user->getContact()->getStructure() : null
  34.         );
  35.         $regions $em->getRepository(AddressRegion::class)->findAllForDashboard();
  36.         $max 0;
  37.         // Fusion des régions & du nombre de bénéficiaires par région
  38.         // J'en profite pour récupérer le nombre de bénéficiaire le plus important sur une région
  39.         // Et calculer le pourcentage de bénéficiaire sur une région
  40.         foreach ($regions as $i => $region) {
  41.             foreach ($beneficiariesPerRegions as $benefRegion) {
  42.                 if ($benefRegion['code'] === $region['code']) {
  43.                     $region['nb_beneficiaries'] = $benefRegion['nb_beneficiaries'];
  44.                     $max max($max$benefRegion['nb_beneficiaries']);
  45.                 }
  46.             }
  47.             if (!isset($region['nb_beneficiaries'])) {
  48.                 $region['nb_beneficiaries'] = 0;
  49.             }
  50.             $regions[$i] = $region;
  51.         }
  52.         // InterStep permet de calculer les échelons de couleur
  53.         // Calculez sur 4 échelons, car le dernier est le maximum atteignable
  54.         $steps $grade 0;
  55.         if ($max >= 4) {
  56.             $steps 4;
  57.             $grade floor($max 4);
  58.         } elseif ($max 0) {
  59.             $steps $max;
  60.             $grade 1;
  61.         }
  62.         // Calcule pour chaque région de son échelon sur ces couleurs
  63.         foreach ($regions as $i => $region) {
  64.             $nb $region['nb_beneficiaries'];
  65.             if ($nb === 0) {
  66.                 $regions[$i]['step'] = 1;
  67.                 continue;
  68.             }
  69.             if ($max >= 4) {
  70.                 $step $grade floor($nb $grade) : 0;
  71.                 $regions[$i]['step'] = min($step 15);
  72.             } else {
  73.                 $regions[$i]['step'] = $nb 1;
  74.             }
  75.         }
  76.         $countBeneficiaries $em->getRepository(Beneficiary::class)->findNbAll(
  77.             $user->hasRole('ROLE_SPEAKER') || $user->hasRole('ROLE_SUPPORT') ? $user->getContact()->getStructure() : null
  78.         );
  79.         $nbBeneficiaryInProgress $em->getRepository(Beneficiary::class)->findNbInProgress(
  80.             $user->hasRole('ROLE_SPEAKER') || $user->hasRole('ROLE_SUPPORT') ? $user->getContact()->getStructure() : null
  81.         );
  82.         $nbPromotions $em->getRepository(Promotion::class)->findNbAll(
  83.             $user->hasRole('ROLE_SPEAKER') || $user->hasRole('ROLE_SUPPORT') ? $user->getContact()->getStructure() : null
  84.         );
  85.         return $this->render('dashboard/index.html.twig'array_merge($items, [
  86.             'user' => $this->getUser(),
  87.             'nbBeneficiaries' => $countBeneficiaries,
  88.             'nbBeneficiaryInProgress' => $nbBeneficiaryInProgress,
  89.             'nbPromotions' => $nbPromotions,
  90.             'regions' => $regions,
  91.             'steps' => $steps,
  92.             'regionsLegends' => $grade,
  93.             'items' => $this->isGranted('ROLE_SUPPORT') || $this->isGranted('ROLE_SPEAKER') ?
  94.                 $em->getRepository(Promotion::class)->findBy(['status' => Promotion::STATUS['pending'], 'structure' => $user->getContact()->getStructure()]) :
  95.                 $em->getRepository(Promotion::class)->findBy(['status' => Promotion::STATUS['pending']])
  96.             ,
  97.             'lastBeneficiaries' => $this->isGranted('ROLE_SUPPORT') || $this->isGranted('ROLE_SPEAKER') ?
  98.                 $em->getRepository(Beneficiary::class)->findByStructure($user->getContact()->getStructure(), 9) :
  99.                 $em->getRepository(Beneficiary::class)->findBy([], ['updatedDate' => 'DESC'], 9)
  100.         ]));
  101.     }
  102. }