src/Controller/Admin/DashboardController.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Controller\Admin\DemandeurCrudController;
  4. use App\Entity\Demande;
  5. use App\Entity\Entreprise;
  6. use App\Entity\Form;
  7. use App\Entity\FormUser;
  8. use App\Entity\Meeting;
  9. use App\Entity\NoteToDo;
  10. use App\Entity\Partenaire;
  11. use App\Entity\Particulier;
  12. use App\Entity\Reclamation;
  13. use App\Entity\Setting;
  14. use App\Entity\User;
  15. use App\Form\MeetingType;
  16. use App\Form\NoteToDoType;
  17. use App\Form\ReclamationType;
  18. use App\Entity\ReclamationReponse;
  19. use App\Form\ReclamationReponseType;
  20. use App\Form\SettingType;
  21. use App\Repository\FormUserRepository;
  22. use App\Repository\MeetingRepository;
  23. use App\Repository\SettingRepository;
  24. use App\Service\ExportExlService;
  25. use App\Service\NotificationManager;
  26. use Doctrine\ORM\EntityManager;
  27. use Doctrine\ORM\EntityManagerInterface;
  28. use App\Controller\Admin\RDCrudController;
  29. use App\Controller\Admin\UserCrudController;
  30. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  31. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  32. use EasyCorp\Bundle\EasyAdminBundle\Config\Asset;
  33. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  34. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  35. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  36. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  37. use Symfony\Component\HttpFoundation\JsonResponse;
  38. use Symfony\Component\HttpFoundation\Request;
  39. use Symfony\Component\HttpFoundation\Response;
  40. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  41. use Symfony\Component\Mime\Address;
  42. use Symfony\Component\Routing\Annotation\Route;
  43. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  44. use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
  45. use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
  46. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  47. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  48. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  49. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
  50. use App\Controller\Admin\FormCrudController;
  51. use Symfony\Component\Routing\Generator\UrlGenerator;
  52. use App\Security\EmailVerifier;
  53. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  54. use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet;
  55. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  56. #[Route('/')]
  57. class DashboardController extends AbstractDashboardController
  58. {
  59.     private $adminUrlGenerator;
  60.     private $entityManager;
  61.     private $emailVerifier;
  62.     public function __constructAdminUrlGenerator $adminUrlGeneratorEntityManagerInterface $entityManagerEmailVerifier $emailVerifierSettingRepository $settingRepository)
  63.     {
  64.         $this->adminUrlGenerator $adminUrlGenerator;
  65.         $this->entityManager $entityManager;
  66.         $this->emailVerifier $emailVerifier;
  67.         $this->settingRepository $settingRepository;
  68.     }
  69.     #[Route('/admin'name'admin_dashboard')]
  70.     public function index(): Response
  71.     {
  72.         $forms $this->entityManager->getRepository(Form::class)->findAll();
  73.         // statistiques pour admin
  74.         $formUserEtat $this->entityManager->getRepository(FormUser::class)->countWithStatus();
  75.         $demandesEtat $this->entityManager->getRepository(Demande::class)->countWithStatus();
  76.         $demandesEtatMerged $this->mergeArraysByColumnValue$formUserEtat$demandesEtat );
  77.         $formUserVille $this->entityManager->getRepository(FormUser::class)->countWithVille();
  78.         $demandesVille $this->entityManager->getRepository(Demande::class)->countWithVille();
  79.         $demandesVilleMerged $this->mergeArraysByColumnValue$formUserVille$demandesVille );
  80.         $reclamationsEtatForDemande $this->entityManager->getRepository(Reclamation::class)->countWithStatusForDemande();
  81.         $reclamationsEtatForFormuser $this->entityManager->getRepository(Reclamation::class)->countWithStatusForFormuser();
  82.         $reclamationsEtatMerged $this->mergeArraysByColumnValue$reclamationsEtatForDemande$reclamationsEtatForFormuser );
  83.         $demandeursVilleForDemande $this->entityManager->getRepository(User::class)->countWithVilleForDemande();
  84.         $demandeursVilleForFormuser $this->entityManager->getRepository(User::class)->countWithVilleForFormuser();
  85.         $reclamationsVilleMerged $this->mergeArraysByColumnValue$demandeursVilleForDemande$demandeursVilleForFormuser );
  86.         $countDemandeurs $this->entityManager->getRepository(User::class)->countByRole('ROLE_USER');
  87.         $countResponsables $this->entityManager->getRepository(User::class)->countByRole('ROLE_RD');
  88.         // statistiques pour responsable dossier (Rd)
  89.         $formUserEtatRd $this->entityManager->getRepository(FormUser::class)->countWithStatusRd$this->getUser()->getId() );
  90.         $demandesEtatRd $this->entityManager->getRepository(Demande::class)->countWithStatusRd$this->getUser()->getId() );
  91.         $demandesEtatRdMerged $this->mergeArraysByColumnValue$formUserEtatRd$demandesEtatRd );
  92.         $reclamationsEtatRdForDemande $this->entityManager->getRepository(Reclamation::class)->countWithStatusRdForDemande$this->getUser()->getId() );
  93.         $reclamationsEtatRdForFormuser $this->entityManager->getRepository(Reclamation::class)->countWithStatusRdForFormuser$this->getUser()->getId() );
  94.         $reclamationsEtatRdMerged $this->mergeArraysByColumnValue$reclamationsEtatRdForDemande$reclamationsEtatRdForFormuser );
  95.         $user $this->getUser();
  96.         if ($this->isGranted("ROLE_ADMIN")) {
  97.             $meeting $this->entityManager->getRepository(Meeting::class)->findAll();
  98.         } else {
  99.             $meeting_demande_rd $this->entityManager->getRepository(Meeting::class)->getMyMeetingsForDemandeRD($user);
  100.             $meeting_formUser_rd $this->entityManager->getRepository(Meeting::class)->getMyMeetingsForFormUserRD($user);
  101.             $meeting array_merge($meeting_demande_rd$meeting_formUser_rd);
  102.         }
  103.         $date_meeting = [];
  104.         foreach ( $meeting as $value ) {
  105.             $date_meeting []= $value->getMeetingDate()->format("j-n-Y");
  106.         }
  107.         $new_meeting = new Meeting();
  108.         $form_meeting $this->createForm(MeetingType::class, $new_meeting);
  109.         $new_note = new NoteToDo();
  110.         $form_note $this->createForm(NoteToDoType::class, $new_note);
  111.         $noteToDo $this->entityManager->getRepository(NoteToDo::class)->getMyNotesForUser($user);
  112.       /*  if ($this->isGranted("ROLE_ADMIN")) {
  113.             $noteToDo = $this->entityManager->getRepository(NoteToDo::class)->findAll();
  114.         } else {
  115.             $noteToDo = $this->entityManager->getRepository(NoteToDo::class)->findAll();
  116.         }*/
  117.         return $this->render('admin/admin_dashboard.html.twig', [
  118.             'forms' => $forms,
  119.             'demandesEtat' => $demandesEtat,
  120.             'formUserEtat' => $this->mergeArraysOfArrayByColumn($formUserEtat'fid'),
  121.             'demandesVille' => $demandesVille,
  122.             'formUserVille' => $this->mergeArraysOfArrayByColumn($formUserVille'fid'),
  123.             'demandeursVilleForDemande' => $demandeursVilleForDemande,
  124.             'demandeursVilleForFormuser' => $this->mergeArraysOfArrayByColumn($demandeursVilleForFormuser'fid'),
  125.             'reclamationsEtatDemande' => $reclamationsEtatForDemande,
  126.             'reclamationsEtatFormUser' => $this->mergeArraysOfArrayByColumn($reclamationsEtatForFormuser'fid'),
  127.             'demandesEtatRd' => $demandesEtatRd,
  128.             'formUserEtatRd' => $this->mergeArraysOfArrayByColumn($formUserEtatRd'fid'),
  129. //            'demandesEtatRd' => $demandesEtatRdMerged,
  130.             'reclamationsEtatRdDemande' => $reclamationsEtatRdForDemande,
  131.             'reclamationsEtatRdFormUser' => $this->mergeArraysOfArrayByColumn($reclamationsEtatRdForFormuser'fid'),
  132.             'countDemandeurs' => $countDemandeurs,
  133.             'countResponsables' => $countResponsables,
  134. //            'countDemande' => array_sum( array_column($demandesEtatMerged, 'count') ),
  135.             'countDemande' => array_sumarray_column($demandesEtat'count') ),
  136.             'countFormUser' => array_sumarray_column($formUserEtat'count') ),
  137.             'countReclamation' => array_sumarray_column($reclamationsEtatMerged'count') ),
  138.             'countDemandeRd' => array_sumarray_column($demandesEtatRd'count') ),
  139.             'countFormUserRd' => array_sumarray_column($formUserEtatRd'count') ),
  140.             'countReclamationRd' => array_sumarray_column($reclamationsEtatRdMerged'count') ),
  141.             'meeting' => $meeting,
  142.             'date_meeting'=> json_encode($date_meeting),
  143.             'form_meeting' => $form_meeting->createView(),
  144.             'noteToDo' => $noteToDo,
  145.             'form_noteToDo' => $form_note->createView(),
  146.         ]);
  147.     }
  148.     #[Route('/chart/export/excel'name'chart_export_excel')]
  149.     public function excelExportDemandeEtat(Request $requestExportExlService $exportExlService)
  150.     {
  151.         $file_full_name $exportExlService->xlsExportDatajson_decode$request->query->get('chart_data') ) );
  152.         return $this->file($file_full_name);
  153.     }
  154.     #[Route('/redirect/email/{type}/{id}'name'app_redirect_email')]
  155.     public function redirectEmail$type 'demande'$id ): Response
  156.     {
  157.         if ( $type == 'demande' ){
  158.             $edit_demande_url $this->adminUrlGenerator
  159.                 ->setController(DemandeCrudController::class)
  160.                 ->setAction(Action::EDIT)
  161.                 ->setEntityId($id)
  162.                 ->generateUrl();
  163.         }
  164.         if ( $type == 'formuser' ){
  165.             $edit_demande_url $this->adminUrlGenerator
  166.                 ->setController(FormUserCrudController::class)
  167.                 ->setAction(Action::EDIT)
  168.                 ->setEntityId($id)
  169.                 ->generateUrl();
  170.         }
  171.         if ( $type == 'demandeur' ){
  172.             $edit_demande_url $this->adminUrlGenerator
  173.                 ->setController(DemandeurCrudController::class)
  174.                 ->setAction(Action::EDIT)
  175.                 ->setEntityId($id)
  176.                 ->generateUrl();
  177.         }
  178.         if ( $type == 'reclamation' ){
  179.             $edit_demande_url $this->adminUrlGenerator
  180.                 ->setController(ReclamationCrudController::class)
  181.                 ->setAction(Action::EDIT)
  182.                 ->setEntityId($id)
  183.                 ->generateUrl();
  184.         }
  185.         return $this->redirect($edit_demande_url);
  186.     }
  187.     #[Route('/Demandeurs'name'Demandeurs')]
  188.     public function Demandeurs(): Response
  189.     {
  190.         $redirection $this->VerificationAccount();
  191.         if ($redirection) {
  192.             return $redirection;
  193.         }
  194.         $url $this->adminUrlGenerator->setController(DemandeurCrudController::class)->generateUrl();
  195.         return $this->redirect($url);
  196.     }
  197.     #[Route('/Responsable_dossier'name'Responsable_dossier')]
  198.     public function Responsable_dossier(): Response
  199.     {
  200.         $redirection $this->VerificationAccount();
  201.         if ($redirection) {
  202.             return $redirection;
  203.         }
  204.         $url $this->adminUrlGenerator->setController(RDCrudController::class)->generateUrl();
  205.         return $this->redirect($url);
  206.     }
  207.     #[Route('/Reclamation'name'Reclamation')]
  208.     public function Reclamation(Request $requestEntityManagerInterface $entityManager): Response
  209.     {
  210.         $redirection $this->VerificationAccount();
  211.         if ($redirection) {
  212.             return $redirection;
  213.         }
  214.         $url $this->adminUrlGenerator->setController(ReclamationCrudController::class)->generateUrl();
  215.         return $this->redirect($url);
  216.     }
  217.     #[Route('/Reclamation/{id}/Response'name'ReclamationResponse')]
  218.     public function ResponseNew($idRequest $requestEntityManagerInterface $entityManager): Response
  219.     {
  220.         $redirection $this->VerificationAccount();
  221.         if ($redirection) {
  222.             return $redirection;
  223.         }
  224.         $reclamation $this->getDoctrine()->getRepository(Reclamation::class)->find($id);
  225.         if (($this->isGranted('ROLE_ADMIN') || $this->getUser() == $reclamation->getUser()) == false)
  226.             return $this->redirectToRoute('Reclamation');
  227.         $reponse = new ReclamationReponse();
  228.         $form $this->createForm(ReclamationReponseType::class, $reponse);
  229.         $form->handleRequest($request);
  230.         if ($form->isSubmitted() && $form->isValid()) {
  231.             $reponse->setUser($this->getUser());
  232.             $reponse->setReclamation($reclamation);
  233.             $entityManager->persist($reponse);
  234.             $entityManager->flush();
  235.             return $this->redirectToRoute('ReclamationResponse', ['id' => $id]);
  236.         }
  237.         return $this->renderForm('reclamation/ReclamationResponse.html.twig', [
  238.             'form' => $form,
  239.             'reclamation' => $reclamation
  240.         ]);
  241.     }
  242.     #[Route('/form/reponses'name'FormReponses')]
  243.     public function FormReponses(): Response
  244.     {
  245.         $redirection $this->VerificationAccount();
  246.         if ($redirection) {
  247.             return $redirection;
  248.         }
  249.         $url $this->adminUrlGenerator->setController(FormUserCrudController::class)->generateUrl();
  250.         return $this->redirect($url);
  251.     }
  252.     #[Route('/audit/list'name'admin_audit_list')]
  253.     public function adminAuditList()
  254.     {
  255.         $redirection $this->VerificationAccount();
  256.         if ($redirection) {
  257.             return $redirection;
  258.         }
  259.         return $this->renderForm('audit/audit_list.html.twig', [
  260.         ]);
  261.     }
  262.     #[Route('/audit/history/{entity}'name'admin_audit_history')]
  263.     public function adminAuditHistory$entity )
  264.     {
  265.         $redirection $this->VerificationAccount();
  266.         if ($redirection) {
  267.             return $redirection;
  268.         }
  269.         return $this->renderForm('audit/audit_history.html.twig', [
  270.             'entity' => $entity
  271.         ]);
  272.     }
  273.     #[Route('/audit/history/entry/{entity}/{id}'name'admin_audit_history_entry')]
  274.     public function adminAuditHistoryEntry$entity$id )
  275.     {
  276.         $redirection $this->VerificationAccount();
  277.         if ($redirection) {
  278.             return $redirection;
  279.         }
  280.         return $this->renderForm('audit/audit_history_entity.html.twig', [
  281.             'entity' => $entity,
  282.             'id' => $id
  283.         ]);
  284.     }
  285.     public function configureDashboard(  ): Dashboard
  286.     {
  287.         $logo_path '/images/logo/igppp-logo.png';
  288.         $app_logo $this->settingRepository->findOneBy(['paramName' => 'logo_app']);
  289.         if ($app_logo){
  290.             $logo $app_logo->getParamValue();
  291.             $logo_path "/uploads/setting/".$logo;
  292.             //$logo_path = $logo;
  293.         }
  294.         return Dashboard::new()
  295.             // you can include HTML contents too (e.g. to link to an image)
  296.             ->setTitle('<img src="' $logo_path '" alt="IGPPP" width="150" height="90">')
  297.             // the path defined in this method is passed to the Twig asset() function
  298.             ->setFaviconPath('favicon.svg')
  299.             // the domain used by default is 'messages'
  300.             //->setTranslationDomain('my-custom-domain')
  301.             // there's no need to define the "text direction" explicitly because
  302.             // its default value is inferred dynamically from the user locale
  303.             ->setTextDirection('ltr')
  304.             // set this option if you prefer the page content to span the entire
  305.             // browser width, instead of the default design which sets a max width
  306.             ->renderContentMaximized()
  307.             // set this option if you prefer the sidebar (which contains the main menu)
  308.             // to be displayed as a narrow column instead of the default expanded design
  309.             ->renderSidebarMinimized()
  310.             // by default, users can select between a "light" and "dark" mode for the
  311.             // backend interface. Call this method if you prefer to disable the "dark"
  312.             // mode for any reason (e.g. if your interface customizations are not ready for it)
  313.             ->disableDarkMode()
  314.             // by default, all backend URLs are generated as absolute URLs. If you
  315.             // need to generate relative URLs instead, call this method
  316.             ->generateRelativeUrls();
  317.     }
  318.     public function configureAssets(): Assets
  319.     {
  320.         return Assets::new()
  321.             ->addHtmlContentToHead('<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">')
  322.             ->addJsFile('js/plugin/jquery-3.6.0.min.js')
  323.             ->addJsFile('js/plugin/jquery-ui-1.11.4.min.js')
  324.             ->addJsFile('js/alerte.js')
  325.             ->addJsFile('//cdn.jsdelivr.net/npm/sweetalert2@11')
  326.             ->addJsFile('js/custom/widget.js')
  327.             ->addCssFile('css/custom/style-dasboard.css')
  328.             ->addCssFile('css/custom/widget.css')
  329.         ;
  330.     }
  331.     public function configureCrud(): Crud
  332.     {
  333.         return Crud::new()->showEntityActionsInlined();
  334.     }
  335.     public function configureActions(): Actions
  336.     {
  337.         return Actions::new()
  338.             ->addBatchAction(Action::BATCH_DELETE)
  339.             ->add(Crud::PAGE_INDEX,  Action::NEW)
  340.             ->add(Crud::PAGE_INDEXAction::EDIT)
  341.             ->add(Crud::PAGE_INDEXAction::DELETE)
  342.             ->add(Crud::PAGE_DETAILAction::EDIT)
  343.             ->add(Crud::PAGE_DETAILAction::INDEX)
  344.             ->add(Crud::PAGE_DETAILAction::DELETE)
  345.             ->add(Crud::PAGE_EDITAction::SAVE_AND_RETURN)
  346.             ->add(Crud::PAGE_EDITAction::SAVE_AND_CONTINUE)
  347.             ->add(Crud::PAGE_NEWAction::SAVE_AND_RETURN)
  348.             ->add(Crud::PAGE_NEWAction::SAVE_AND_ADD_ANOTHER)
  349.             ->update(Crud::PAGE_INDEXAction::NEW, function (Action $action) {
  350.                 return $action->setIcon('fa fa-add')->setLabel(false);
  351.             })
  352.             
  353.             ->update(Crud::PAGE_INDEXAction::EDIT, function (Action $action) {
  354.                 return $action->setIcon('fa fa-pen')->setLabel(false)->setHtmlAttributes(['title'=>'Modifier']);
  355.             })
  356.             ->update(Crud::PAGE_INDEXAction::DELETE, function (Action $action) {
  357.                 return $action->setIcon('fa fa-trash')->setLabel(false);
  358.             })
  359.             ;
  360.     }
  361.     public function configureMenuItems(): iterable
  362.     {
  363.         yield MenuItem::linkToDashboard('acceuil''/images/home.png');
  364.         yield MenuItem::linkToRoute('gestion_demandeur''/images/profile.png''Demandeurs')->setPermission('ROLE_ADMIN');
  365.         yield MenuItem::linkToRoute('gestion_responsable_dossier''/images/user.png''Responsable_dossier')->setPermission('ROLE_ADMIN');
  366.         yield MenuItem::linkToRoute('gestion_reclamation''/images/warning.png''Reclamation');
  367.         // yield MenuItem::subMenu('Gestion des demandes', '/images/question.png')->setSubItems([
  368.         //     MenuItem::linkToCrud('Demandes du formulaire principale', '/images/home.png', Demande::class),
  369.         //     MenuItem::linkToRoute('Demandes des autres formulaires', '/images/home.png', 'FormReponses'),
  370.         // ]);
  371.         yield MenuItem::linkToCrud('suivi_demande_form_principl''/images/pass.png'Demande::class);
  372.         yield MenuItem::linkToRoute('suivi_demande_autre_formulaire''/images/document.png''FormReponses');
  373.         yield MenuItem::linkToCrud('generateur_formulaire''/images/new-document.png'Form::class)->setPermission('ROLE_ADMIN');
  374.         yield MenuItem::linkToRoute('historique_action''/images/history.png''admin_audit_list')->setPermission('ROLE_ADMIN');
  375.     }
  376.     public function VerificationAccount()
  377.     {
  378.         if ($this->getUser()) {
  379.             if (!$this->getUser()->isVerified()) {
  380.                 $this->addFlash('VerifAccount''Vous devez vérifier votre compte !');
  381.                 return $this->redirectToRoute('app_logout');
  382.             }
  383.             if (!$this->getUser()->getCompleted()) {
  384.                 $this->addFlash('VerifAccount''Vous devez compléter votre profil !');
  385.                 return $this->redirectToRoute('app_profil');
  386.             }
  387.             if (!$this->getUser()->isActive()) {
  388.                 $this->addFlash('VerifAccount''Votre compte est désactivé !');
  389.                 return $this->redirectToRoute('app_logout');
  390.             }
  391.             return null;
  392.         } else {
  393.             return $this->redirectToRoute('login');
  394.         }
  395.     }
  396.     private function mergeArraysByColumnValue$array1$array2 ){
  397.         $result = [];
  398.         foreach(array_merge($array1,$array2) as $array){
  399.             if(isset($result[$array['label']])){
  400.                 $result[$array['label']]['count'] += $array['count'];
  401.             }else{
  402.                 $result[$array['label']] = $array;
  403.             }
  404.         }
  405.         return array_values($result);
  406.     }
  407.     private function mergeArraysOfArrayByColumn$array$column ){
  408.         $result = [];
  409.         foreach ( $array as $array_to_merge ){
  410.             $key $array_to_merge[$column];
  411.             unset($array_to_merge[$column]);
  412.             $result$key ][] = $array_to_merge;
  413.         }
  414.         return $result;
  415.     }
  416. }