src/Controller/Admin/DashboardController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  9. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use App\Entity\Arrondissement;
  13. use App\Entity\Commune;
  14. use App\Entity\Departement;
  15. use App\Entity\PriseCharge;
  16. use App\Entity\Profession;
  17. use App\Entity\TypePriseCharge;
  18. use App\Entity\User;
  19. use App\Entity\ZoneSanitaire;
  20. use App\Entity\Infos;
  21. use App\Entity\SuiviFormation;
  22. use App\Form\PcFiltreType;
  23. use App\Repository\PriseChargeRepository;
  24. use App\Repository\SuiviFormationRepository;
  25. use App\Repository\ConnexionRepository;
  26. use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
  27. use Symfony\UX\Chartjs\Model\Chart;
  28. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  29. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  30. use PhpOffice\PhpSpreadsheet\IOFactory;
  31. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  32. class DashboardController extends AbstractDashboardController
  33. {
  34.     private $helper;
  35.     private $chartBuilder;
  36.     private $PriseChargeRepository;
  37.     private $ConnexionRepository;
  38.     private $SuiviFormationRepository;
  39.     public function __construct(UploaderHelper $helperConnexionRepository $ConnexionRepositoryPriseChargeRepository $PriseChargeRepositoryChartBuilderInterface $chartBuilderSuiviFormationRepository  $SuiviFormationRepository)
  40.     {
  41.         $this->helper $helper;
  42.         $this->PriseChargeRepository $PriseChargeRepository;
  43.         $this->ConnexionRepository $ConnexionRepository;
  44.         $this->chartBuilder $chartBuilder;
  45.         $this->SuiviFormationRepository $SuiviFormationRepository;
  46.     }
  47.     /**
  48.      * @Route("/admin", name="admin")
  49.      */
  50.     public function index(): Response
  51.     {
  52.         $connexions =  $this->ConnexionRepository->findBy([], ['id'=>'DESC'], 4,0);
  53.         $totalConnexions =  $this->ConnexionRepository->getTotalWeekConnexion();
  54.         if ($this->isGranted('ROLE_ADMIN')) {
  55.             $priseCharges $this->PriseChargeRepository->findBy([], ['id'=>'DESC'], 20,0);
  56.             $suiviFormations $this->SuiviFormationRepository->findBy([], ['id'=>'DESC'], 20,0);
  57.         }
  58.         else
  59.         {
  60.             $priseCharges $this->PriseChargeRepository->findBy(['createBy' => $this->getUser()], ['id'=>'DESC'], 20,0);
  61.             $suiviFormations $this->SuiviFormationRepository->findBy(['createBy' => $this->getUser()], ['id'=>'DESC'], 20,0);
  62.         }
  63.         return $this->render('admin/dashboard.html.twig', [
  64.             'chart' => $this->chart(),
  65.             'chart1' => $this->chart1(),
  66.             'chart2' => $this->chart2(),
  67.             'chart3' => $this->chart3(),
  68.             'chart4' => $this->chart4(),
  69.             'chart5' => $this->chart5(),
  70.             'priseCharges' => $priseCharges,
  71.             'suiviFormations' => $suiviFormations,
  72.             'connexions' => $connexions,
  73.             'totalConnexions' => $totalConnexions,
  74.         ]);
  75.     }
  76.      /**
  77.      * @Route("/download/pc", name="downloadCSV")
  78.      */
  79.     public function download(): Response
  80.     {
  81.         $dt = new \DateTime;
  82.         $priseCharges $this->PriseChargeRepository->toCSV();
  83.         $spreadsheet = new Spreadsheet();
  84.         $sheet $spreadsheet->getActiveSheet();
  85.         $entete = ["Numero""Age""Sexe""profession""Date""Type""Departement""Zone sanitaire""Commune""Arrondissement""Centre de sante""motif""nom""prenom"];
  86.         $sheet->fromArray($entete,NULL);
  87.         $sheet->fromArray($priseCharges,NULL,'A2');
  88.         $writer = new Xlsx($spreadsheet);
  89.         $fileName 'prise_en_charge_'.$this->getUser()->getId().$dt->format('Ymdhis').'.xlsx';
  90.         $temp_file tempnam(sys_get_temp_dir(), $fileName);
  91.         $publicDirectory $this->getParameter('kernel.project_dir') . '/downloads/';
  92.         $writer->save($temp_file);
  93.         $writer->save($publicDirectory.$fileName);
  94.         return $this->file($temp_file$fileNameResponseHeaderBag::DISPOSITION_INLINE);
  95.         //return $this->redirectToRoute('admin');
  96.     }
  97.      /**
  98.      * @Route("/download/sf", name="downloadSfCSV")
  99.      */
  100.     public function downloadSf(): Response
  101.     {
  102.         $dt = new \DateTime;
  103.         $suiviFormations $this->SuiviFormationRepository->toCSV();
  104.         $spreadsheet = new Spreadsheet();
  105.         $sheet $spreadsheet->getActiveSheet();
  106.         $entete = ["formationUtiles""partageInformations""description""integrationModule""precisionModule""conseilsInformations""precisionThematique""nom""prenom"];
  107.         $sheet->fromArray($entete,NULL);
  108.         $sheet->fromArray($suiviFormations,NULL,'A2');
  109.         $writer = new Xlsx($spreadsheet);
  110.         $fileName 'suivi_formation_'.$this->getUser()->getId().$dt->format('Ymdhis').'.xlsx';
  111.         $temp_file tempnam(sys_get_temp_dir(), $fileName);
  112.         $publicDirectory $this->getParameter('kernel.project_dir') . '/downloads/';
  113.         $writer->save($temp_file);
  114.         $writer->save($publicDirectory.$fileName);
  115.         return $this->file($temp_file$fileNameResponseHeaderBag::DISPOSITION_INLINE);
  116.         //return $this->redirectToRoute('admin');
  117.     }
  118.     public function configureDashboard(): Dashboard
  119.     {
  120.         return Dashboard::new()
  121.             ->setTitle('<img class="img-fluid" src="assets/images/ASAF-PASS.png">');
  122.     }
  123.     public function configureMenuItems(): iterable
  124.     {
  125.         yield MenuItem::linkToDashboard('Dashboard''fa fa-home');
  126.         yield MenuItem::linkToCrud('Prise en charge''fas fa-list'PriseCharge::class);
  127.         if ($this->isGranted('ROLE_ADMIN')) {
  128.             yield MenuItem::linkToCrud('Arrondissement''fas fa-list'Arrondissement::class);
  129.             yield MenuItem::linkToCrud('Commune''fas fa-list'Commune::class);
  130.             yield MenuItem::linkToCrud('Département''fas fa-list'Departement::class);
  131.             yield MenuItem::linkToCrud('Profession''fas fa-list'Profession::class);
  132.             yield MenuItem::linkToCrud('Type prise en charge''fas fa-list'TypePriseCharge::class);
  133.             yield MenuItem::linkToCrud('Utilisateur''fas fa-user'User::class);
  134.             yield MenuItem::linkToCrud('Zone sanitaire''fas fa-list'ZoneSanitaire::class);
  135.             yield MenuItem::linkToCrud('Informations générales''fas fa-info'Infos::class);
  136.             yield MenuItem::linkToCrud('Suivi Formation''fas fa-list'SuiviFormation::class);
  137.         }
  138.     }
  139.     public function configureUserMenu(UserInterface $user): UserMenu
  140.     {
  141.         return parent::configureUserMenu($user)
  142.             ->setAvatarUrl($this->helper->asset($user'imageFile'))
  143.             ->addMenuItems([
  144.                 MenuItem::linkToRoute('Changer Mot de passe''fa fa-lock''user_pass_change'),
  145.             ])
  146.             ->addMenuItems([
  147.                 MenuItem::linkToRoute('Profile''fa fa-id-card''user_profile')
  148.             ]);
  149.     }
  150.     private function chart()
  151.     {
  152.         $typePriseCharge $this->PriseChargeRepository->getTotalByTypePriseCharge();
  153.         $chart $this->chartBuilder->createChart(Chart::TYPE_DOUGHNUT);
  154.         $chart->setData([
  155.             'labels' => array_column($typePriseCharge'libelle'),
  156.             'datasets' => [
  157.                 [
  158.                     'label' => 'My First dataset',
  159.                     'backgroundColor' => [
  160.                           'rgb(255, 205, 86)',
  161.                           'rgb(188,143,143)',
  162.                           'rgb(50,205,50)',
  163.                         ],
  164.                     'data' => array_column($typePriseCharge'total'),
  165.                 ],
  166.             ],
  167.         ]);
  168.         return $chart;
  169.     }
  170.     private function chart1()
  171.     {
  172.         $sexe $this->PriseChargeRepository->getTotalBySexe();
  173.         $sexeLibelle = [];
  174.         foreach (array_column($sexe'sexe') as $key => $value) {
  175.             if ($value) {
  176.                 $sexeLibelle[$key] = "Masculin";
  177.             }
  178.             else
  179.             {
  180.                 $sexeLibelle[$key] = "Féminin";
  181.             }
  182.         }
  183.         $chart $this->chartBuilder->createChart(Chart::TYPE_PIE);
  184.         $chart->setData([
  185.             'labels' => $sexeLibelle,
  186.             'datasets' => [
  187.                 [
  188.                     'label' => 'My First dataset',
  189.                     'backgroundColor' => [
  190.                           'rgb(54, 162, 235)',
  191.                           'rgb(255, 99, 132)',
  192.                         ],
  193.                     'data' => array_column($sexe'total'),
  194.                 ],
  195.             ],
  196.         ]);
  197.         return $chart;
  198.     }
  199.     private function chart2()
  200.     {
  201.         $totalByDepartement $this->PriseChargeRepository->getTotalByDepartement();
  202.         $chart $this->chartBuilder->createChart(Chart::TYPE_BAR);
  203.         $chart->setData([
  204.             'labels' => array_column($totalByDepartement'libelle'),
  205.             'datasets' => [
  206.                 [
  207.                     'label' => 'Prise en charge par département',
  208.                     'backgroundColor' => [
  209.                           'rgb(54, 162, 235)',
  210. 'rgb(54, 162, 235)',
  211. 'rgb(54, 162, 235)',
  212. 'rgb(54, 162, 235)',
  213. 'rgb(54, 162, 235)',
  214. 'rgb(54, 162, 235)',
  215. 'rgb(54, 162, 235)',
  216. 'rgb(54, 162, 235)',
  217. 'rgb(54, 162, 235)',
  218. 'rgb(54, 162, 235)',
  219. 'rgb(54, 162, 235)',
  220. 'rgb(54, 162, 235)',
  221.                         ],
  222.                     'data' => array_column($totalByDepartement'total'),
  223.                 ],
  224.             ],
  225.         ]);
  226.         return $chart;
  227.     }
  228.     private function chart3()
  229.     {
  230.         $PriseChargePerMonth $this->PriseChargeRepository->getPriseChargePerMonth();
  231.         //dd($PriseChargePerMonth);
  232.         $chart $this->chartBuilder->createChart(Chart::TYPE_LINE);
  233.         $chart->setData([
  234.             'labels' => array_column($PriseChargePerMonth'label'),
  235.             'datasets' => [
  236.                 [
  237.                     'label' => 'Nombre de prise en charge par mois',
  238.                     'data' => array_column($PriseChargePerMonth'total'),
  239.                     'fill' => false,
  240.                     'borderColor' => '#dc3545',
  241.                     //'tension' => 0.1
  242.                 ],
  243.             ],
  244.         ]);
  245.         return $chart;
  246.     }
  247.     private function chart4()
  248.     {
  249.         $totalByAgeRange $this->PriseChargeRepository->getTotalByAgeRange();
  250.         $chart $this->chartBuilder->createChart(Chart::TYPE_BAR);
  251.         $chart->setData([
  252.             'labels' => array_column($totalByAgeRange'label'),
  253.             'datasets' => [
  254.                 [
  255.                     'label' => "Nombre de prise en charge par tranche d'âge",
  256.                     'data' => array_column($totalByAgeRange'total'),
  257.                     'backgroundColor' => [
  258.                           '#d63384',
  259. '#d63384',
  260. '#d63384',
  261. '#d63384',
  262. '#d63384',
  263. '#d63384',
  264. '#d63384',
  265. '#d63384',
  266.                         ],
  267.                 ],
  268.             ],
  269.         ]);
  270.         return $chart;
  271.     }
  272.     private function chart5()
  273.     {
  274.         $ConnexionPerMonth $this->ConnexionRepository->getConnexionPerMonth();
  275.         $chart $this->chartBuilder->createChart(Chart::TYPE_LINE);
  276.         $chart->setData([
  277.             'labels' => array_column($ConnexionPerMonth'label'),
  278.             'datasets' => [
  279.                 [
  280.                     'label' => 'Nombre de connexion par mois',
  281.                     'data' => array_column($ConnexionPerMonth'total'),
  282.                     'fill' => false,
  283.                     'borderColor' => '#fd7e14',
  284.                     //'tension' => 0.1
  285.                 ],
  286.             ],
  287.         ]);
  288.         return $chart;
  289.     }
  290. }