src/Controller/SchedulerController.php line 45

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Employee;
  4. use App\Entity\Imports;
  5. use App\Entity\Modifications;
  6. use App\Entity\PatchNote;
  7. use App\Entity\Ref\RefGroupesAD;
  8. use App\Entity\Regle;
  9. use App\Entity\User;
  10. use App\Service\RecapEmailsCounterService;
  11. use DateTime;
  12. use DateTimeZone;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Exception;
  15. use IntlDateFormatter;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\Finder\Finder;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\UX\Chartjs\Builder\ChartBuilder;
  21. use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
  22. use Symfony\UX\Chartjs\Model\Chart;
  23. class SchedulerController extends AbstractController
  24. {
  25.     private ManagerRegistry $doctrine;
  26.     private RecapEmailsCounterService $recapEmailsCounterService;
  27.     /**
  28.      * @param ManagerRegistry $doctrine
  29.      * @param RecapEmailsCounterService $recapEmailsCounterService
  30.      */
  31.     public function __construct(ManagerRegistry $doctrineRecapEmailsCounterService $recapEmailsCounterService)
  32.     {
  33.         $this->doctrine $doctrine;
  34.         $this->recapEmailsCounterService $recapEmailsCounterService;
  35.     }
  36.     /**
  37.      * @Route("/", name="home")
  38.      * @throws Exception
  39.      */
  40.     public function home(RefController $refController): Response
  41.     {
  42.         /**
  43.          * @var User $user
  44.          */
  45.         $user $this->getUser();
  46.         if ($user &&
  47.             (
  48.              !$this->isGranted('ROLE_ADMIN') &&
  49.              !$this->isGranted('ROLE_IT') &&
  50.              !$this->isGranted('ROLE_MG') &&
  51.              !$this->isGranted('ROLE_BT') &&
  52.              !$this->isGranted('ROLE_MANAGER') &&
  53.              !$this->isGranted('ROLE_RH')
  54.             )
  55.         ) {
  56.             return $this->redirectToRoute('employee', [
  57.                 'id' => $user->getEmployee()->getId(),
  58.             ]);
  59.         }
  60.         $employeeRepo $this->doctrine->getRepository(Employee::class);
  61.         $managerId NULL;
  62.         if ($this->isGranted("ROLE_MANAGER") && !$this->isGranted("ROLE_IT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_RH") && !$this->isGranted("ROLE_MG") && !$this->isGranted("ROLE_BT"))
  63.             $managerId $user->getEmployee()?->getId();
  64.         $employeesArrivee $employeeRepo->getEmployeeCountByMouvement('arrivee'$managerId);
  65.         $employeesMobilite $employeeRepo->getEmployeeCountByMouvement('mobilite'$managerId);
  66.         $employeesDepart $employeeRepo->getEmployeeCountByMouvement('depart'$managerId);
  67.         $employeesComplete $employeeRepo->getEmployeeCountByMouvement('complete'$managerId);
  68.         $employeesTotal $employeeRepo->getEmployeeCountByMouvement('all'$managerId);
  69.         /**
  70.          * @var User $user
  71.          */
  72.         $user $this->getUser();
  73.         $data_user $this->doctrine->getRepository(Employee::class)->findByEmail($user->getEmail());
  74.         if ($data_user === []) {
  75.             $data_user[0]['name'] = $user->getUsername();
  76.             $data_user[0]['Firstname'] = '';
  77.             $data_user[0]['civility'] = 'Homme';
  78.         }
  79.         $user_sended = [
  80.             'role' => $user->getRoles(),
  81.             'user' => $data_user[0],
  82.         ];
  83.         $stats = [
  84.             'Arrivées' => $employeesArrivee,
  85.             'Changements' => $employeesMobilite,
  86.             'Départs' => $employeesDepart,
  87.             'Complétés' => $employeesComplete,
  88.         ];
  89.         $patchnote $this->doctrine->getRepository(PatchNote::class)->findLast3PatchNote();
  90.         if ($patchnote === NULL) {
  91.             $patchnote = [];
  92.         }
  93.         $chartBuilder = new ChartBuilder;
  94.         $presentationChart $this->presentationDataGraph($chartBuilder);
  95.         $newCropperLogo $this->getLogoCropperImage();
  96.         $rulesNumbers $this->doctrine->getRepository(Regle::class)->findAll();
  97.         $adGroupsNumbers $this->doctrine->getRepository(RefGroupesAD::class)->findAll();
  98.         $usersNumbers $this->doctrine->getRepository(User::class)->findAll();
  99.         $exportsNumbers $this->doctrine->getRepository(Imports::class)->findAll();
  100.         $date = new DateTime('now');
  101.         $actualYear $date->format('Y');
  102.         $totalEntriesReferentials 0;
  103.         foreach($refController->referentialRotator as $referential) {
  104.             $refRepo $this->doctrine->getRepository($referential['entity']);
  105.             $totalEntriesReferentials += $refRepo->count([]);
  106.         }
  107.         $modifications $this->doctrine->getManager()->getRepository(Modifications::class)->getRecentlyActivity($user->getId());
  108. //        $modifications_array['modifications'] = $modificationController->getRealModifications($modifications);
  109.         $modifications_array['modifications'] = $modifications;
  110.         return $this-> render('scheduler/home.html.twig', [
  111.             'stats' => $stats,
  112.             'user' => $user_sended,
  113.             'patchnotes' => $patchnote,
  114.             'chart' => $presentationChart,
  115.             'newLogo' => $newCropperLogo,
  116.             'rulesNumbers' => count($rulesNumbers),
  117.             'adGroupsNumbers' => count($adGroupsNumbers),
  118.             'usersNumbers' => count($usersNumbers),
  119.             'exportsNumbers' => count($exportsNumbers),
  120.             'employeesTotal' => $employeesTotal,
  121.             'totalEntriesReferentials' => $totalEntriesReferentials,
  122.             'actualYear' => $actualYear,
  123.             'modifications' => $modifications_array['modifications']
  124.         ]);
  125.     }
  126.     /**
  127.      * @param ChartBuilderInterface $chartBuilder
  128.      * @return Chart
  129.      * @throws Exception
  130.      */
  131.     private function presentationDataGraph(ChartBuilderInterface $chartBuilder): Chart
  132.     {
  133.         $allDataGraphNewArrival $this->numberOfNewArrival();
  134.         $maxY 0;
  135.         foreach($allDataGraphNewArrival as $mouvementsArray) {
  136.             if (max($mouvementsArray) > $maxY) {
  137.                 $maxY max($mouvementsArray);
  138.             }
  139.         }
  140.         $chart $chartBuilder->createChart(Chart::TYPE_LINE);
  141.         $chart->setData([
  142.             'labels' => $this->OneYearComplete(),
  143.             'datasets' => [
  144.                 [
  145.                     'label' => 'Arrivées',
  146.                     'backgroundColor' => 'rgba(18, 139, 252, 0.3)',
  147.                     'borderColor' => 'rgb(18, 139, 252)',
  148.                     'tension' => 0,
  149.                     'data' => $allDataGraphNewArrival['arrivee'],
  150.                 ], [
  151.                     'label' => 'Changements',
  152.                     'backgroundColor' => 'rgba(94, 43, 250, 0.3)',
  153.                     'borderColor' => 'rgb(94, 43, 250)',
  154.                     'tension' => 0,
  155.                     'data' => $allDataGraphNewArrival['mobilite'],
  156.                 ], [
  157.                     'label' => 'Départs',
  158.                     'backgroundColor' => 'rgba(245, 53, 141, 0.3)',
  159.                     'borderColor' => 'rgb(245, 53, 141)',
  160.                     'tension' => 0,
  161.                     'data' => $allDataGraphNewArrival['depart'],
  162.                 ],
  163.             ],
  164.         ]);
  165.         $chart->setOptions([
  166.             'responsive' => true,
  167.             'maintainAspectRatio' =>false,
  168.             'scales' => [
  169.                 'yAxes' => [
  170.                         'ticks' => [
  171.                             'min' => 0,
  172.                             'max' => ($maxY $maxY 2),
  173.                             'stepSize' => 1
  174.                         ]
  175.                     ],
  176.                 ],
  177.             ],
  178.         );
  179.         return $chart;
  180.     }
  181.     /**
  182.      * @throws Exception
  183.      * @return array<mixed>
  184.      */
  185.     private function numberOfNewArrival(): array
  186.     {
  187.         $dateFormatterIntl = new \IntlDateFormatter(
  188.             'fr_FR',
  189.             IntlDateFormatter::FULL,
  190.             IntlDateFormatter::NONE,
  191.             'Europe/Paris',
  192.             IntlDateFormatter::GREGORIAN,
  193.             'MM/dd/yyyy' // Format Pattern : https://unicode-org.github.io/icu/userguide/format_parse/datetime/
  194.         );
  195.         $calendar $this->OneYearComplete();
  196.         $allDataGraphNewArrival = array();
  197.         $currentDate = new DateTime('now');
  198.         $currentDateYear $currentDate->format('Y');
  199.         $emailsCounter $this->recapEmailsCounterService->getRecapEmailsCounterOnCurrentYear();
  200.         $countArrivee = array();
  201.         $countMobilite = array();
  202.         $countDepart = array();
  203.         $dateFormatterIntl->setPattern('LLLL');
  204.         foreach($calendar as $month) {
  205.             $countArrivee[$month] = 0;
  206.             $countMobilite[$month] = 0;
  207.             $countDepart[$month]  = 0;
  208.             foreach($emailsCounter as $counter) {
  209.                 $yearRegisterFormat $counter->getDateTime()->format('Y');
  210.                 $monthFrenchRegisterFormat ucfirst(
  211.                     (string) datefmt_format($dateFormatterIntl$counter->getDateTime())
  212.                 );
  213.                 if ($month === $monthFrenchRegisterFormat && $yearRegisterFormat === $currentDateYear) {
  214.                     $countArrivee[$month] = $counter->getArrival();
  215.                     $countMobilite[$month] = $counter->getChangement();
  216.                     $countDepart[$month] = $counter->getDepart();
  217.                 }
  218.             }
  219.         }
  220.         $allDataGraphNewArrival['arrivee'] = $countArrivee;
  221.         $allDataGraphNewArrival['mobilite'] = $countMobilite;
  222.         $allDataGraphNewArrival['depart'] = $countDepart;
  223.         return $allDataGraphNewArrival;
  224.     }
  225.     /**
  226.      * @return array<mixed>
  227.      * @throws Exception
  228.      */
  229.     public function oneYearComplete(): array
  230.     {
  231.         $dateFormatterIntl = new \IntlDateFormatter(
  232.             'fr_FR',
  233.             IntlDateFormatter::FULL,
  234.             IntlDateFormatter::NONE,
  235.             'Europe/Paris',
  236.             IntlDateFormatter::GREGORIAN,
  237.             'MM/dd/yyyy' // Format Pattern : https://unicode-org.github.io/icu/userguide/format_parse/datetime/
  238.         );
  239.         $currentDate = new DateTime('first day of January this year', new DateTimeZone('Europe/Paris'));
  240.         $oneYear = array();
  241.         for ($i 0$i 12$i++) {
  242.             $dateFormatterIntl->setPattern('LLLL');
  243.             $monthInYearFrench ucfirst(
  244.                 (string) datefmt_format($dateFormatterIntl$currentDate)
  245.             );
  246.             $oneYear[] = $monthInYearFrench;
  247.             $monthInYear $currentDate->modify('+1 month');
  248.         }
  249.         return $oneYear;
  250.     }
  251.     /**
  252.      * @return string
  253.      */
  254.     private function getLogoCropperImage(): string
  255.     {
  256.         $finder = new Finder();
  257.         $fileNameWithExtension '';
  258.         $finder->files()->in('../public/uploads/logo');
  259.         if ($finder->hasResults()) {
  260.             foreach ($finder as $file) {
  261.                 $fileNameWithExtension $file->getRelativePathname();
  262.             }
  263.         }
  264.         return $fileNameWithExtension;
  265.     }
  266. }