src/Controller/Backend/DefaultController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backend;
  3. use App\Entity\Entreprise;
  4. use App\Entity\Region;
  5. use App\Repository\CotisationRepository;
  6. use App\Repository\EnterpriseRepository;
  7. use App\Repository\RegionRepository;
  8. use DateTime;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
  15. use Symfony\UX\Chartjs\Model\Chart;
  16. #[IsGranted('ROLE_USER'message"Cher utilisateur FFCB, Vous n'avez pas l'autorisation pour cette action !")]
  17. #[Route('/admin')]
  18. class DefaultController extends AbstractController
  19. {
  20.     #[Route(path'/'name'app_homepage')]
  21.     public function index(
  22.         Request $request,
  23.         EnterpriseRepository $enterpriseRepository,
  24.         CotisationRepository $cotisationRepository,
  25.         RegionRepository $regionRepository,
  26.         ChartBuilderInterface $chartBuilder
  27.     )
  28.     {
  29.         $colors = [
  30.             'rgb(255, 99, 132)',
  31.             'rgb(255, 159, 64)',
  32.             'rgb(255, 205, 86)',
  33.             'rgb(75, 192, 192)',
  34.             'rgb(54, 162, 235)',
  35.             'rgb(153, 102, 255)',
  36.             'rgb(201, 203, 207)',
  37.             'rgb(50, 59, 116)'
  38.         ];
  39.         $cotisationLimit = new DateTime((new DateTime())->format('Y').'-07-01');
  40.         $now = new DateTime();
  41.         if ($now <= $cotisationLimit) {
  42.             $nowYear = ((int) (new \DateTime())->format('Y')) - 1;
  43.         } else {
  44.             $nowYear = (int) (new \DateTime())->format('Y');
  45.         }
  46.         $nowYear = (int) (new \DateTime())->format('Y');
  47.         $mapSelectedYear $request->query->get('year'$nowYear);
  48.         $regions $regionRepository->findAll();
  49.         $memberPerRegion = [];
  50.         $count 0;
  51.         /**
  52.          * @var int $key
  53.          * @var Region $region
  54.          */
  55.         foreach ($regions as $key => $region) {
  56.             $countMembers $enterpriseRepository->countMemberPerRegion($mapSelectedYear$region->getId());
  57.             $memberPerRegion[$key] = [
  58.                 'countMember' => $countMembers !== null $countMembers[1] : 0,
  59.                 'name' => $region->getNom(),
  60.                 'id' => $region->getId()
  61.             ];
  62.             if ($countMembers !== null) {
  63.                 $count += $countMembers[1];
  64.             }
  65.         }
  66.         $membersPerYear $cotisationRepository->getMemberPerYear($nowYear);
  67.         $chartMemberPerYearLabel = [];
  68.         $chartMemberPerYearData = [];
  69.         foreach ($membersPerYear as $item) {
  70.             $chartMemberPerYearLabel[] = $item['year'];
  71.             $chartMemberPerYearData[] = $item['countMember'];
  72.         }
  73.         $chartMemberPerYear $chartBuilder->createChart(Chart::TYPE_BAR);
  74.         $chartMemberPerYear->setData([
  75.             'labels' => $chartMemberPerYearLabel,
  76.             'datasets' => [
  77.                 [
  78.                     'backgroundColor' => [
  79.                         'rgba(0, 33, 215, 0.7)',
  80.                         'rgba(127, 255, 0, 0.7)',
  81.                         'rgba(255, 92, 0, 0.7)',
  82.                     ],
  83.                     'borderColor' => [
  84.                         'rgb(0, 33, 215)',
  85.                         'rgb(127, 255, 0)',
  86.                         'rgb(255, 92, 0)',
  87.                     ],
  88.                     'data' => $chartMemberPerYearData,
  89.                 ],
  90.             ],
  91.         ]);
  92.         $chartMemberPerYear->setOptions([
  93.             'plugins' => [
  94.                 'legend' => [
  95.                     'display' => false,
  96.                     'title' => false
  97.                 ]
  98.             ]
  99.         ]);
  100.         $chartFirmByType $chartBuilder->createChart(Chart::TYPE_DOUGHNUT);
  101.         $countFirmByType $enterpriseRepository->countFirmByType();
  102.         $countFirmByTypeLabels = [];
  103.         $countFirmByTypeData = [];
  104.         foreach ($countFirmByType as $key => $item) {
  105.             $countFirmByTypeLabels[] = $item['name'];
  106.             $countFirmByTypeData[] = $item['countFirm'];
  107.         }
  108.         $chartFirmByType->setData([
  109.             'labels' => $countFirmByTypeLabels,
  110.             'datasets' => [[
  111.                 'label' => 'Nb entreprise par type',
  112.                 'data' => $countFirmByTypeData,
  113.                 'backgroundColor' => $colors,
  114.                 'hoverOffset' => 4
  115.             ]]
  116.         ]);
  117.         return $this->render('backend/default/index.html.twig', [
  118.             'menu' => 'home',
  119.             'chartMemberPerYear' => $chartMemberPerYear,
  120.             'chartFirmByType' => $chartFirmByType,
  121.             'memberPerRegion' => $memberPerRegion,
  122.             'nowYear' => $nowYear,
  123.             'mapSelectedYear' => $mapSelectedYear
  124.         ]);
  125.     }
  126.     #[Route('/region-details/{id}'name'home_region_details'methods: ['POST'])]
  127.     public function regionDetails(Region $regionRequest $requestEnterpriseRepository $enterpriseRepository): JsonResponse
  128.     {
  129.         $datas json_decode($request->getContent(), true);
  130.         $members $enterpriseRepository->findMemberPerRegion($region$datas['year']);
  131.         $results = [
  132.             'region' => $region->getNom(),
  133.             'datas' => []
  134.         ];
  135.         /** @var Entreprise $member */
  136.         foreach ($members as $member) {
  137.             $results['datas'][] = [
  138.                 'id' => $member->getId(),
  139.                 'department' => $member->getDepartement()->getNom(),
  140.                 'name' => $member->getRaisonSocial(),
  141.                 'url' => $this->generateUrl('show_entreprise', ['id' => $member->getId()])
  142.             ];
  143.         }
  144.         return new JsonResponse($results);
  145.     }
  146. }