src/Controller/DashboardController.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Company;
  4. use App\Repository\CountryRepository;
  5. use App\Repository\RegionRepository;
  6. use App\Repository\PrefectureRepository;
  7. use App\Repository\JobOfferRepository;
  8. use App\Repository\JobAppliedRepository;
  9. use App\Repository\SchoolRepository;
  10. use App\Services\DashboardService;
  11. use App\Services\CompanyService;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. #[Route('/dashboard')]
  23. #[Security("is_granted('ROLE_ADMIN')
  24.  or is_granted('ROLE_ADMIN_PAYS')
  25.  or is_granted('ROLE_ADMIN_REGIONS')
  26.  or is_granted('ROLE_ADMIN_VILLES')
  27.  or is_granted('ROLE_LEGISLATEUR')
  28.  or is_granted('ROLE_DIRECTEUR')
  29.  or is_granted('ROLE_ETABLISSEMENT')
  30.  or is_granted('ROLE_PRINCIPAL')
  31.  or is_granted('ROLE_ENTREPRISE')
  32.  or is_granted('ROLE_DIPLOME')
  33.  ")]
  34. class DashboardController extends AbstractController {
  35.     private EntityManagerInterface $em;
  36.     private CountryRepository $countryRepository;
  37.     private RegionRepository $regionRepository;
  38.     private PrefectureRepository $prefectureRepository;
  39.     private JobOfferRepository $jobOfferRepository;
  40.     private SchoolRepository $schoolRepository;
  41.     private JobAppliedRepository $jobAppliedRepository;
  42.     private DashboardService $dashboardService;
  43.     private CompanyService $companyService;
  44.     private TranslatorInterface $translator;
  45.     public function __construct(
  46.         EntityManagerInterface $em,
  47.         CountryRepository      $countryRepository,
  48.         RegionRepository       $regionRepository,
  49.         PrefectureRepository   $prefectureRepository,
  50.         JobOfferRepository     $jobOfferRepository,
  51.         SchoolRepository    $schoolRepository,
  52.         JobAppliedRepository    $jobAppliedRepository,
  53.         DashboardService       $dashboardService,
  54.         CompanyService         $companyService,
  55.         TranslatorInterface $translator
  56.     ) {
  57.         $this->em $em;
  58.         $this->countryRepository $countryRepository;
  59.         $this->regionRepository $regionRepository;
  60.         $this->prefectureRepository $prefectureRepository;
  61.         $this->jobOfferRepository $jobOfferRepository;
  62.         $this->jobAppliedRepository $jobAppliedRepository;
  63.         $this->schoolRepository $schoolRepository;
  64.         $this->dashboardService $dashboardService;
  65.         $this->companyService $companyService;
  66.         $this->translator $translator;
  67.     }
  68.     #[Route(path'/'name'dashboard_index'methods: ['GET''POST'])]
  69.     public function indexAction(Request $request): Response {
  70.         $this->cleanJobApplied();
  71.         if ($this->getUser()->getCompany()) {
  72.             if(!$this->getUser()->getCompany()->getAgreeRgpd())
  73.                 return $this->redirectToRoute('front_company_edit');
  74.             if(!$this->companyService->checkSatisfaction($this->getUser()->getCompany()))
  75.                 return $this->redirectToRoute('front_company_satisfactioncompany_new');
  76.         }
  77.         return $this->dashboardService->checkAccountBefore(function () use ($request) {
  78.             $validCountries $this->countryRepository->findBy(['valid' => 'true']);
  79.             if (!$validCountries) {
  80.                 $validCountries $this->countryRepository->findAll();
  81.             } else {
  82.                 $userCountry $this->getUser()->getCountry();
  83.                 if ($userCountry)
  84.                     $validCountries $this->countryRepository->findById($userCountry);
  85.             }
  86.             $validRegions = [];
  87.             foreach ($validCountries as $validCountry) {
  88.                 $regions $this->regionRepository->findBy(['country' => $validCountry]);
  89.                 $validRegions array_merge($validRegions$regions);
  90.             }
  91.             $validPrefectures = [];
  92.             foreach ($validRegions as $validRegion) {
  93.                 $prefectures $this->prefectureRepository->findBy(['region' => $validRegion]);
  94.                 $validPrefectures array_merge($validPrefectures$prefectures);
  95.             }
  96.             // Récupération de la date de référence pour l'intervalle de temps, par défaut = date courante
  97.             $referenceDateTxt =  $request->request->get('referenceDate');
  98.             $session $request->getSession();
  99.             if (!$referenceDateTxt && $session->has('referenceDate')) {
  100.                 $referenceDateTxt $session->get('referenceDate');
  101.             }
  102.             if (!$referenceDateTxt) {
  103.                 $referenceDateTxt = (new DateTime())->format('Y-m-d');
  104.             }
  105.             $session->set('referenceDate'$referenceDateTxt);
  106.             $referenceDate = (new \DateTime($referenceDateTxt))->format('Y-m-d');
  107.             // Récupération de la date de début pour l'intervalle de temps, par défaut = date courante
  108.             $startDateTxt =  $request->request->get('beginDate');
  109.             if (!$startDateTxt && $session->has('beginDate')) {
  110.                 $startDateTxt $session->get('beginDate');
  111.             }
  112.             if (!$startDateTxt) {
  113.                 $startDateTxt = (new DateTime())->sub(new \DateInterval('P3Y'))->format('Y-m-d');
  114.             }
  115.             $session->set('beginDate'$startDateTxt);
  116.             $startDate = (new \DateTime($startDateTxt))->format('Y-m-d');
  117.             // Récupération de l'intervalle de temps sélectionné par le formulaire et renvoie la date du premier jour  */
  118.             $selectedDuration $request->request->get('selectDuration');
  119.             if ($selectedDuration) {
  120.                 $session->set('selectDuration'$selectedDuration);
  121.             }
  122.             // Sinon récupération par la variable de session, sinon paramétrage à 3 mois
  123.             if (!$selectedDuration) {
  124.                 $selectedDuration = ($session->has('selectDuration')) ? $session->get('selectDuration') : '3 ans';
  125.             }
  126.             $idSelectCountry $request->request->get('selectCountry');
  127.             if ($idSelectCountry) {
  128.                 $session->set('pays'$idSelectCountry);
  129.             } else {
  130.                 if ($session->has('pays')) {
  131.                     $idSelectCountry $session->get('pays');
  132.                 }
  133.             }
  134.             if (!$idSelectCountry) {
  135.                 foreach ($validCountries as $country) {
  136.                     $idSelectCountry $country->getId();
  137.                 }
  138.             }
  139.             $idSelectRegion $request->request->get('selectRegion');
  140.             if ($idSelectRegion) {
  141.                 $session->set('region'$idSelectRegion);
  142.             }
  143.             $idSelectPrefecture $request->request->get('selectPrefecture');
  144.             if ($idSelectPrefecture) {
  145.                 $session->set('prefecture'$idSelectPrefecture);
  146.             }
  147.             // adaptation aux multi administrateurs
  148.             if ($this->getUser()->hasRole('ROLE_ADMIN') or
  149.                 $this->getUser()->hasRole('ROLE_DIRECTEUR')) {
  150.                 //nothing to do, validRegions all regions
  151.             } else if ($this->getUser()->hasRole('ROLE_ADMIN_PAYS') or
  152.                 $this->getUser()->hasRole('ROLE_LEGISLATEUR')) {
  153.                 $validRegions $this->getUser()->getCountry()->getRegions();
  154.             } else if ($this->getUser()->hasRole('ROLE_ADMIN_REGIONS')) {
  155.                 $validRegions =  $this->getUser()->getAdminRegions();
  156.             } else if ($this->getUser()->hasRole('ROLE_ADMIN_VILLES')) {
  157.                 $validRegions = [];
  158.                 $selectedCities =  $this->getUser()->getAdminCities();
  159.                 foreach ($selectedCities as $selectedCity){
  160.                     if($selectedCity) {
  161.                         $regionExist false;
  162.                         foreach ($validRegions as $validRegion) {
  163.                             if ($validRegion->getId() == $selectedCity->getRegion()->getId()) {
  164.                                 $regionExist true;
  165.                             }
  166.                         }
  167.                         if (!$regionExist) {
  168.                             $validRegions[] = $selectedCity->getRegion();
  169.                         }
  170.                     }
  171.                 }
  172.             } else {
  173.                 // adpatation for Actors
  174.                 if($_ENV['STRUCT_PROVINCE_COUNTRY_CITY'] == 'true') {
  175.                     $validRegions = [];
  176.                     if($this->getUser()->getRegion())
  177.                         $validRegions[] = $this->getUser()->getRegion();
  178.                 }
  179.             }
  180.             $userSchool =  $this->getUser()->getSchool() ;
  181.             // only for Principal Role
  182.             if($this->getUser()->getPrincipalSchool()) {
  183.                 $userSchool =  $this->schoolRepository->find($this->getUser()->getPrincipalSchool());
  184.                 if($_ENV['STRUCT_PROVINCE_COUNTRY_CITY'] == 'true') {
  185.                     if($userSchool->getCity()) {
  186.                         if(!$validRegions)
  187.                             $validRegions[] = $userSchool->getCity()->getRegion();
  188.                         $idSelectRegion $userSchool->getCity()->getRegion()->getId();
  189.                     }
  190.                 }
  191.             }
  192.             return $this->render('Dashboard/index.html.twig', [
  193.                 'countries' => $validCountries,
  194.                 'regions' => $validRegions,
  195.                 'prefectures' => $validPrefectures,
  196.                 'idSelectedCountry' => $idSelectCountry,
  197.                 'idSelectedRegion' => $idSelectRegion,
  198.                 'idSelectedPrefecture' => $idSelectPrefecture,
  199.                 'selectedDuration'  => $selectedDuration,
  200.                 'startDate' => $startDate,
  201.                 'referenceDate' => $referenceDate,
  202.                 'userSchool' => $userSchool,
  203.             ]);
  204.         });
  205.     }
  206.     #[Route(path'/export_pdf'name'export_pdf_index'methods: ['GET'])]
  207.     public function exportPdfAction(): Response {
  208.         return $this->redirectToRoute('dashboard_index');
  209.     }
  210.     /**
  211.      * clean the table of job Applied when appliedDate is too old
  212.      * @return void
  213.      */
  214.     private function cleanJobApplied (): void {
  215.         $dateStr date('Y'strtotime("-3 year")) . "-01-01";
  216.         $date =  date_create_from_format('Y-m-d'$dateStr);
  217.         $jobsApplied $this->jobAppliedRepository->findBeforeYear($date);
  218.         foreach ($jobsApplied as $jobApplied) {
  219.             $this->em->remove($jobApplied);
  220.             $this->em->flush();
  221.         }
  222.         if($jobsApplied) {
  223.             $this->addFlash('warning'$this->translator->trans('flashbag.number_removed_from_jobs_applied', ['{number}' => date('Y'strtotime("-3 year"))]));
  224.         }
  225.     }
  226. }