src/Controller/SchedulerController.php line 45
<?phpnamespace App\Controller;use App\Entity\Employee;use App\Entity\Imports;use App\Entity\Modifications;use App\Entity\PatchNote;use App\Entity\Ref\RefGroupesAD;use App\Entity\Regle;use App\Entity\User;use App\Service\RecapEmailsCounterService;use DateTime;use DateTimeZone;use Doctrine\Persistence\ManagerRegistry;use Exception;use IntlDateFormatter;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\Finder\Finder;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\UX\Chartjs\Builder\ChartBuilder;use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;use Symfony\UX\Chartjs\Model\Chart;class SchedulerController extends AbstractController{private ManagerRegistry $doctrine;private RecapEmailsCounterService $recapEmailsCounterService;/*** @param ManagerRegistry $doctrine* @param RecapEmailsCounterService $recapEmailsCounterService*/public function __construct(ManagerRegistry $doctrine, RecapEmailsCounterService $recapEmailsCounterService){$this->doctrine = $doctrine;$this->recapEmailsCounterService = $recapEmailsCounterService;}/*** @Route("/", name="home")* @throws Exception*/public function home(RefController $refController): Response{/*** @var User $user*/$user = $this->getUser();if ($user &&(!$this->isGranted('ROLE_ADMIN') &&!$this->isGranted('ROLE_IT') &&!$this->isGranted('ROLE_MG') &&!$this->isGranted('ROLE_BT') &&!$this->isGranted('ROLE_MANAGER') &&!$this->isGranted('ROLE_RH'))) {return $this->redirectToRoute('employee', ['id' => $user->getEmployee()->getId(),]);}$employeeRepo = $this->doctrine->getRepository(Employee::class);$managerId = NULL;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"))$managerId = $user->getEmployee()?->getId();$employeesArrivee = $employeeRepo->getEmployeeCountByMouvement('arrivee', $managerId);$employeesMobilite = $employeeRepo->getEmployeeCountByMouvement('mobilite', $managerId);$employeesDepart = $employeeRepo->getEmployeeCountByMouvement('depart', $managerId);$employeesComplete = $employeeRepo->getEmployeeCountByMouvement('complete', $managerId);$employeesTotal = $employeeRepo->getEmployeeCountByMouvement('all', $managerId);/*** @var User $user*/$user = $this->getUser();$data_user = $this->doctrine->getRepository(Employee::class)->findByEmail($user->getEmail());if ($data_user === []) {$data_user[0]['name'] = $user->getUsername();$data_user[0]['Firstname'] = '';$data_user[0]['civility'] = 'Homme';}$user_sended = ['role' => $user->getRoles(),'user' => $data_user[0],];$stats = ['Arrivées' => $employeesArrivee,'Changements' => $employeesMobilite,'Départs' => $employeesDepart,'Complétés' => $employeesComplete,];$patchnote = $this->doctrine->getRepository(PatchNote::class)->findLast3PatchNote();if ($patchnote === NULL) {$patchnote = [];}$chartBuilder = new ChartBuilder;$presentationChart = $this->presentationDataGraph($chartBuilder);$newCropperLogo = $this->getLogoCropperImage();$rulesNumbers = $this->doctrine->getRepository(Regle::class)->findAll();$adGroupsNumbers = $this->doctrine->getRepository(RefGroupesAD::class)->findAll();$usersNumbers = $this->doctrine->getRepository(User::class)->findAll();$exportsNumbers = $this->doctrine->getRepository(Imports::class)->findAll();$date = new DateTime('now');$actualYear = $date->format('Y');$totalEntriesReferentials = 0;foreach($refController->referentialRotator as $referential) {$refRepo = $this->doctrine->getRepository($referential['entity']);$totalEntriesReferentials += $refRepo->count([]);}$modifications = $this->doctrine->getManager()->getRepository(Modifications::class)->getRecentlyActivity($user->getId());// $modifications_array['modifications'] = $modificationController->getRealModifications($modifications);$modifications_array['modifications'] = $modifications;return $this-> render('scheduler/home.html.twig', ['stats' => $stats,'user' => $user_sended,'patchnotes' => $patchnote,'chart' => $presentationChart,'newLogo' => $newCropperLogo,'rulesNumbers' => count($rulesNumbers),'adGroupsNumbers' => count($adGroupsNumbers),'usersNumbers' => count($usersNumbers),'exportsNumbers' => count($exportsNumbers),'employeesTotal' => $employeesTotal,'totalEntriesReferentials' => $totalEntriesReferentials,'actualYear' => $actualYear,'modifications' => $modifications_array['modifications']]);}/*** @param ChartBuilderInterface $chartBuilder* @return Chart* @throws Exception*/private function presentationDataGraph(ChartBuilderInterface $chartBuilder): Chart{$allDataGraphNewArrival = $this->numberOfNewArrival();$maxY = 0;foreach($allDataGraphNewArrival as $mouvementsArray) {if (max($mouvementsArray) > $maxY) {$maxY = max($mouvementsArray);}}$chart = $chartBuilder->createChart(Chart::TYPE_LINE);$chart->setData(['labels' => $this->OneYearComplete(),'datasets' => [['label' => 'Arrivées','backgroundColor' => 'rgba(18, 139, 252, 0.3)','borderColor' => 'rgb(18, 139, 252)','tension' => 0,'data' => $allDataGraphNewArrival['arrivee'],], ['label' => 'Changements','backgroundColor' => 'rgba(94, 43, 250, 0.3)','borderColor' => 'rgb(94, 43, 250)','tension' => 0,'data' => $allDataGraphNewArrival['mobilite'],], ['label' => 'Départs','backgroundColor' => 'rgba(245, 53, 141, 0.3)','borderColor' => 'rgb(245, 53, 141)','tension' => 0,'data' => $allDataGraphNewArrival['depart'],],],]);$chart->setOptions(['responsive' => true,'maintainAspectRatio' =>false,'scales' => ['yAxes' => ['ticks' => ['min' => 0,'max' => ($maxY + $maxY % 2),'stepSize' => 1]],],],);return $chart;}/*** @throws Exception* @return array<mixed>*/private function numberOfNewArrival(): array{$dateFormatterIntl = new \IntlDateFormatter('fr_FR',IntlDateFormatter::FULL,IntlDateFormatter::NONE,'Europe/Paris',IntlDateFormatter::GREGORIAN,'MM/dd/yyyy' // Format Pattern : https://unicode-org.github.io/icu/userguide/format_parse/datetime/);$calendar = $this->OneYearComplete();$allDataGraphNewArrival = array();$currentDate = new DateTime('now');$currentDateYear = $currentDate->format('Y');$emailsCounter = $this->recapEmailsCounterService->getRecapEmailsCounterOnCurrentYear();$countArrivee = array();$countMobilite = array();$countDepart = array();$dateFormatterIntl->setPattern('LLLL');foreach($calendar as $month) {$countArrivee[$month] = 0;$countMobilite[$month] = 0;$countDepart[$month] = 0;foreach($emailsCounter as $counter) {$yearRegisterFormat = $counter->getDateTime()->format('Y');$monthFrenchRegisterFormat = ucfirst((string) datefmt_format($dateFormatterIntl, $counter->getDateTime()));if ($month === $monthFrenchRegisterFormat && $yearRegisterFormat === $currentDateYear) {$countArrivee[$month] = $counter->getArrival();$countMobilite[$month] = $counter->getChangement();$countDepart[$month] = $counter->getDepart();}}}$allDataGraphNewArrival['arrivee'] = $countArrivee;$allDataGraphNewArrival['mobilite'] = $countMobilite;$allDataGraphNewArrival['depart'] = $countDepart;return $allDataGraphNewArrival;}/*** @return array<mixed>* @throws Exception*/public function oneYearComplete(): array{$dateFormatterIntl = new \IntlDateFormatter('fr_FR',IntlDateFormatter::FULL,IntlDateFormatter::NONE,'Europe/Paris',IntlDateFormatter::GREGORIAN,'MM/dd/yyyy' // Format Pattern : https://unicode-org.github.io/icu/userguide/format_parse/datetime/);$currentDate = new DateTime('first day of January this year', new DateTimeZone('Europe/Paris'));$oneYear = array();for ($i = 0; $i < 12; $i++) {$dateFormatterIntl->setPattern('LLLL');$monthInYearFrench = ucfirst((string) datefmt_format($dateFormatterIntl, $currentDate));$oneYear[] = $monthInYearFrench;$monthInYear = $currentDate->modify('+1 month');}return $oneYear;}/*** @return string*/private function getLogoCropperImage(): string{$finder = new Finder();$fileNameWithExtension = '';$finder->files()->in('../public/uploads/logo');if ($finder->hasResults()) {foreach ($finder as $file) {$fileNameWithExtension = $file->getRelativePathname();}}return $fileNameWithExtension;}}