← Back
Editing: ExportController.php
<?php namespace App\Controller; use App\Entity\User; use App\Repository\AdresseRepository; use App\Repository\AgencyPrestataireRepository; use App\Repository\ControleRepository; use App\Repository\HistoriqueAlerteRepository; use App\Repository\NoteRepository; use App\Repository\PrestataireRepository; use App\Repository\UserRepository; use App\Service\AgentService; use App\Service\ControleService; use App\Service\PdfService; use App\Service\SolutionService; use App\Service\UserService; use App\Session\Filter\Role\ManagerSessionFilters; use Dompdf\Dompdf; use Dompdf\Options; use HeadlessChromium\Exception\CommunicationException; use HeadlessChromium\Exception\NoResponseAvailable; use HeadlessChromium\Exception\OperationTimedOut; use PhpOffice\PhpSpreadsheet\Document\Properties; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Exception; use PhpOffice\PhpSpreadsheet\Writer\IWriter; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\Routing\Annotation\Route; use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; #[Route(path: '/export', defaults: ['_format' => 'xls'])] class ExportController extends AbstractController { private ControleService $controleService; public function __construct(ControleService $controleService) { $this->controleService = $controleService; } /** * @throws CommunicationException * @throws LoaderError * @throws NoResponseAvailable * @throws OperationTimedOut * @throws RuntimeError * @throws SyntaxError */ #[Route(path: '/export-pdf', name: 'export_pdf', methods: ['post'])] public function exportPdf(Request $request, PdfService $pdfService): Response { $content = $request->getContent(); $decodedContent = json_decode($content); $base64Pdf = $pdfService->export( $decodedContent->html, $decodedContent->title ?? null ); $response = new Response(base64_decode($base64Pdf)); $response->headers->set('Content-Description:', 'File Transfer'); $response->headers->set('Content-Type', 'application/pdf'); $response->headers->set('Content-Disposition', 'inline; filename=fichier.pdf'); $response->headers->set('Content-Transfer-Encoding', 'binary'); $response->headers->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0'); return $response; } /** * @throws Exception */ #[Route(path: '/prestataire/alerts.{_format}', name: 'export_historique_alertes_presta')] public function exportHistoriqueAlertePresta( Request $request, AgencyPrestataireRepository $agencyPrestataireRepository, ControleRepository $controleRepository ): BinaryFileResponse { $prestaId = $request->query->get('presta'); $agencyPrestataire = $agencyPrestataireRepository->find($prestaId); $prestataire = $agencyPrestataire->getPrestataire(); $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Alertes prestataires'); $controles = $controleRepository->findPrestaAlerts($prestataire); $startDate = new \DateTime('midnight first day of this month'); $startDate->sub(new \DateInterval('P1Y')); $endDate = new \DateTime('last day of this month'); $formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::TRADITIONAL); $formatter->setPattern('MMMM yy'); $current = $startDate; $alpha = 'A'; while ($startDate < $endDate) { $filtered = array_filter( $controles, function ($controle) use ($current) { $tmp = clone $current; $start = clone $tmp; $interval = new \DateInterval('P1M'); $tmp->add($interval); $end = $tmp; return $controle->getDateRealisation() >= $start && $controle->getDateRealisation() < $end; } ); $stat = count($filtered); $sheet->setCellValue($alpha.'1', $formatter->format($current).' '.$current->format('Y')); $sheet->setCellValue($alpha.'2', $stat); $interval = new \DateInterval('P1M'); $current->add($interval); ++$alpha; } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'alertes-presta.'.$request->get('_format'); $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @throws Exception */ #[Route(path: '/historique.{_format}', name: 'export_historique')] public function exportHistorique( Request $request, ManagerSessionFilters $sessionFilters ): BinaryFileResponse { $spreadsheet = new Spreadsheet(); $historic = $this->controleService->groupControlByMonth( $sessionFilters->getDateRange()->getDateStart(), $sessionFilters->getDateRange()->getDateEnd() ); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Historique controles'); $alpha = 'A'; foreach ($historic as $month => $stat) { $extractedMonth = explode(' ', $month)[0]; $year = explode(' ', $month)[1]; switch (strtolower($extractedMonth)) { case 'january': $monthFr = 'Janvier '.$year; break; case 'february': $monthFr = 'Février '.$year; break; case 'march': $monthFr = 'Mars '.$year; break; case 'april': $monthFr = 'Avril '.$year; break; case 'may': $monthFr = 'Mai '.$year; break; case 'june': $monthFr = 'Juin '.$year; break; case 'july': $monthFr = 'Juillet '.$year; break; case 'august': $monthFr = 'Août '.$year; break; case 'september': $monthFr = 'Septembre '.$year; break; case 'october': $monthFr = 'Octobre '.$year; break; case 'november': $monthFr = 'Novembre '.$year; break; case 'decembre': $monthFr = 'Décembre '.$year; break; default: $monthFr = $month; break; } $sheet->setCellValue($alpha.'1', $monthFr); $sheet->setCellValue($alpha.'2', $stat); ++$alpha; } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'historique.'.$request->get('_format'); $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @throws \Exception */ #[Route(path: '/historique_by_adresse.{_format}', name: 'export_historique_by_adresse')] public function exportHistoriqueByAdresse(Request $request, NoteRepository $noteRepository): BinaryFileResponse { $spreadsheet = new Spreadsheet(); $selectedVille = $request->get('selectedVille'); $selectedRue = $request->get('selectedRue'); $selectedResidence = $request->get('selectedResidence'); $selectedBatiment = $request->get('selectedBatiment'); $selectedNumero = $request->get('selectedNumero'); $results = $noteRepository->averageNoteByManagerByFullAdresse( $this->getUser(), $selectedVille, $selectedRue, $selectedResidence, $selectedBatiment, $selectedNumero ); for ($i = 0; $i < count($results); ++$i) { $myDate = $results[$i]['date']; $results[$i]['date'] = $myDate->format('F Y'); } $listMonth = []; for ($i = 0; $i < 12; ++$i) { $date = date('F Y', strtotime("last day of -$i month")); $listMonth[] = $date; } $listMonth = array_reverse($listMonth); foreach ($listMonth as $month) { $moisCible = $month; $historic[$month] = 0; $nbNotes[$moisCible] = 0; $sommeNote[$moisCible] = 0; foreach ($results as $noteMois) { if ($noteMois['date'] == $moisCible) { ++$nbNotes[$moisCible]; $sommeNote[$moisCible] += $noteMois['note']; $historic[$month] = round(($nbNotes[$moisCible] / $sommeNote[$moisCible]) * 10, 1); } } } $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Historique des notes moyennes'); $alpha = 'A'; foreach ($historic as $month => $stat) { $extractedMonth = explode(' ', $month)[0]; $year = explode(' ', $month)[1]; switch (strtolower($extractedMonth)) { case 'january': $monthFr = 'Janvier '.$year; break; case 'february': $monthFr = 'Février '.$year; break; case 'march': $monthFr = 'Mars '.$year; break; case 'april': $monthFr = 'Avril '.$year; break; case 'may': $monthFr = 'Mai '.$year; break; case 'june': $monthFr = 'Juin '.$year; break; case 'july': $monthFr = 'Juillet '.$year; break; case 'august': $monthFr = 'Août '.$year; break; case 'september': $monthFr = 'Septembre '.$year; break; case 'october': $monthFr = 'Octobre '.$year; break; case 'november': $monthFr = 'Novembre '.$year; break; case 'decembre': $monthFr = 'Décembre '.$year; break; default: $monthFr = $month; break; } $sheet->setCellValue($alpha.'1', $monthFr); $sheet->setCellValue($alpha.'2', $stat); ++$alpha; } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'historique-'.$selectedVille; if (null != $selectedRue && '' != $selectedRue) { $fileName = $fileName.'-'.$selectedRue; } if (null != $selectedResidence && '' != $selectedResidence) { $fileName = $fileName.'-'.$selectedResidence; } $fileName = $fileName.'.'.$request->get('_format'); $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @throws Exception */ #[Route(path: '/historique_alerte_by_adresse.{_format}', name: 'export_historique_alerte_by_adresse')] public function exportHistoriqueAlerteByAdresse( Request $request, AgencyPrestataireRepository $agencyPrestataireRepository, HistoriqueAlerteRepository $historiqueAlerteRepository ): BinaryFileResponse { $agency = $this->getUser()->getAgency(); $prestataire = $agencyPrestataireRepository->find($request->get('prestataire')); $spreadsheet = new Spreadsheet(); $alertes = $historiqueAlerteRepository->getByAgencyByPrestataire($agency, $prestataire); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Historique alertes prestataire'); $result = []; $listMonth = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'Decembre', ]; foreach ($listMonth as $month) { $nbalerte = 0; foreach ($alertes as $alerte) { $alerteMonth = $alerte->getDate()->format('F'); if ($month == $alerteMonth) { ++$nbalerte; } } $result[$month] = $nbalerte; } $alpha = 'A'; foreach ($result as $month => $stat) { switch (strtolower($month)) { case 'january': $monthFr = 'Janvier'; break; case 'february': $monthFr = 'Février'; break; case 'march': $monthFr = 'Mars'; break; case 'april': $monthFr = 'Avril'; break; case 'may': $monthFr = 'Mai'; break; case 'june': $monthFr = 'Juin'; break; case 'july': $monthFr = 'Juillet'; break; case 'august': $monthFr = 'Août'; break; case 'september': $monthFr = 'Septembre'; break; case 'october': $monthFr = 'Octobre'; break; case 'november': $monthFr = 'Novembre'; break; case 'decembre': $monthFr = 'Décembre'; break; default: $monthFr = $month; break; } $sheet->setCellValue($alpha.'1', $monthFr); $sheet->setCellValue($alpha.'2', $stat); ++$alpha; } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'historique-alerte-'.$prestataire->getPrestataire()->getNom().'-'.date('Y').'.'.$request->get( '_format' ); $temp_file = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($temp_file); // Return the excel file as an attachment return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @throws Exception * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/user/index.{_format}', name: 'export_user_index', methods: ['GET'])] public function exportUserIndex( Request $request, UserRepository $userRepository, AgentService $agentService, UserService $userService ): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $agents = $userRepository->exportAgentsAdmin(); } else { $agents = $userService->getAgentsForCurrentSolution(); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Agents'); $sheet->setCellValue('A1', 'Nom'); $sheet->setCellValue('B1', 'Prénom'); $sheet->setCellValue('C1', 'Email'); $sheet->setCellValue('D1', 'Activation'); $sheet->setCellValue('E1', 'Fréquence de controle en jours ouvrés'); $sheet->setCellValue('F1', 'Rôles'); $sheet->setCellValue('G1', 'Objectifs atteints'); $i = 0; foreach ($agents as $agent) { $agentService->setAgent($agent); $sheet->setCellValue('A'.($i + 2), $agent->getNom()); $sheet->setCellValue('B'.($i + 2), $agent->getPrenom()); $sheet->setCellValue('C'.($i + 2), $agent->getEmail()); $sheet->setCellValue('D'.($i + 2), $agent->isEnabled() ? 'Oui' : 'Non'); $sheet->setCellValue('E'.($i + 2), $agent->getFrequenceControle()); $sheet->setCellValue('G'.($i + 2), $agentService->getPercentDone().'%'); $role = ''; foreach ($agent->getRoles() as $r) { switch ($r) { case 'ROLE_SUPER_ADMIN': $role .= 'Super administrateur;'; break; case 'ROLE_ADMIN': $role .= 'Administrateur;'; break; case 'ROLE_MANAGER': $role .= 'Manager +1;'; break; case 'ROLE_AGENT': $role .= 'Agent;'; break; case 'ROLE_USER': break; default: $role .= $r; break; } } $sheet->setCellValue('F'.($i + 2), $role); ++$i; } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'utilisateurs.'.$request->get('_format'); $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/user/pdf/index', name: 'export_user_pdf_index', methods: ['GET'])] public function exportUserPdfIndex(Request $request, UserRepository $userRepository): Response { /** @var User $user */ $user = $this->getUser(); if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $userList = $userRepository->findAll(); } elseif ($this->container->get('security.authorization_checker')->isGranted('ROLE_MANAGER')) { $userList = $userRepository->findAgentsByAgency($request, $user->getAgency()->getId()); } $roles = []; for ($i = 0; $i < count($userList); ++$i) { $role = ''; foreach ($userList[$i]->getRoles() as $key => $r) { switch ($r) { case 'ROLE_SUPER_ADMIN': $role .= 'Super administrateur;'; break; case 'ROLE_ADMIN': $role .= 'Administrateur;'; break; case 'ROLE_MANAGER': $role .= 'Manager +1;'; break; case 'ROLE_AGENT': $role .= 'Agent;'; break; case 'ROLE_USER': break; default: $role .= $r; break; } } $roles[] = $role; } // Configure Dompdf according to your needs $pdfOptions = new Options(); $pdfOptions->set('defaultFont', 'Arial'); // Instantiate Dompdf with our options $dompdf = new Dompdf($pdfOptions); $dompdf->set_option('isHtml5ParserEnabled', true); $title = 'Welcome to our PDF Test'; // Retrieve the HTML generated in our twig file $html = $this->renderView( 'export/pdf_user.html.twig', [ 'userList' => $userList, 'roles' => $roles, 'user' => $this->getUser(), ] ); // Load HTML to Dompdf $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation 'portrait' or 'landscape' $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser (force download) $dompdf->stream( 'utilisateurs.pdf', [ 'Attachment' => true, ] ); return new Response(); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ #[Route(path: '/objectifs.{_format}', name: 'export_objectifs')] public function exportObjectifs( Request $request, UserRepository $userRepository, AgentService $agentService, UserService $userService ): BinaryFileResponse { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $agents = $userRepository->exportAgentsAdmin(); } else { $agents = $userService->getAgentsForCurrentSolution(); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Objectifs'); $sheet->setCellValue('A1', 'Agent'); $sheet->setCellValue('B1', 'Total controles'); $sheet->setCellValue('C1', 'Objectifs atteints'); $i = 0; foreach ($agents as $agent) { $agentService->setAgent($agent); $sheet->setCellValue('A'.($i + 2), $agent->getNom().' '.$agent->getPrenom()); $sheet->setCellValue('B'.($i + 2), $agentService->getTotalControles()); $sheet->setCellValue('C'.($i + 2), $agentService->getPercentDone().'%'); ++$i; } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'objectifs.'.$request->get('_format'); $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @throws Exception */ #[Route(path: '/ascenseurs_export.{_format}', name: 'export_ascenseurs')] public function exportAscenseurs( Request $request, AdresseRepository $adresseRepository, ControleRepository $controleRepository ): BinaryFileResponse { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Ascenseurs'); $sheet->setCellValue('A1', 'Ville'); $sheet->setCellValue('B1', 'Adresse'); $sheet->setCellValue('C1', 'Résidence'); $sheet->setCellValue('D1', 'Ascenseur'); $sheet->setCellValue('E1', 'A l\'arrêt depuis le'); $sheet->setCellValue('F1', 'Prestataire alerté le'); $sheet->setCellValue('G1', 'Nombres de pannes déclarées'); $sheet->setCellValue('H1', 'Dernier controle effectué le'); $adresses = $adresseRepository->getByManager($this->getUser()); foreach ($adresses as $i => $adresse) { $sheet->setCellValue('A'.($i + 2), $adresse[0]->getVille().' '.$adresse[0]->getCodePostal()); $sheet->setCellValue( 'B'.($i + 2), $adresse[0]->getNumero().(null != $adresse[0]->getRepetition() ? ' '.$adresse[0]->getRepetition( ) : '').' '.$adresse[0]->getVoie() ); $sheet->setCellValue('C'.($i + 2), $adresse[0]->getResidence()); $sheet->setCellValue('D'.($i + 2), $adresse[0]->getBatiment()); $sheet->setCellValue('E'.($i + 2), $adresse[0]->getStartDateInactive()); $lastControl = $controleRepository->getLastControlDoneByAdresse($adresse[0]); if ($lastControl && !$lastControl[0]->getValide()) { $controlDateRealisation = $lastControl[0]->getDateRealisation(); if ($lastControl[0]->getAlertePrestataire()) { $sheet->setCellValue('F'.($i + 2), date_format($controlDateRealisation, 'd/m/Y')); } else { $sheet->setCellValue('F'.($i + 2), ''); } } else { $sheet->setCellValue('F'.($i + 2), ''); } } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'ascenseurs.'.$request->get('_format'); $temp_file = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($temp_file); // Return the excel file as an attachment return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @return IWriter|Xlsx|null * * @throws Exception */ private function getWriter(Spreadsheet $spreadsheet, string $format = 'xls') { if ('xls' === $format) { return new Xlsx($spreadsheet); } if ('pdf' === $format) { return IOFactory::createWriter($spreadsheet, 'Dompdf'); } return null; } /** * @throws Exception * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/export-adresse.{_format}', name: 'export_adresse')] public function exportAdresse( Request $request, SolutionService $solutionService, AdresseRepository $adresseRepository ): BinaryFileResponse { $spreadsheet = new Spreadsheet(); $properties = new Properties(); $properties->setCreator($solutionService->getCurrent()->getName()); $properties->setTitle('Adresses'); $spreadsheet->setProperties($properties); if ($this->isGranted('ROLE_ADMIN')) { $adresseList = $adresseRepository->getAll(); } elseif ($this->isGranted('ROLE_ORGANIZATION')) { $adresseList = $adresseRepository->getByOrganization($this->getUser()->getOrganization()); } elseif ($this->isGranted('ROLE_SUPERVISOR')) { $adresseList = $adresseRepository->getByAgency($this->getUser()->getAgency()); } else { $adresseList = $adresseRepository->getByManager($this->getUser()); } $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Adresse'); $letter = 'A'; foreach ($this->getAddressExportColumns($solutionService) as $column) { $sheet->setCellValue($letter.'1', $column); ++$letter; } for ($i = 0; $i < count($adresseList); ++$i) { $letter = 'A'; $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getVoie()); ++$letter; $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getNumero()); ++$letter; $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getRepetition()); ++$letter; $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getBatiment()); ++$letter; $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getResidence()); ++$letter; if ('lift-manager' === $solutionService->getCurrent()->getSlug()) { $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getNumeroAscenseur()); ++$letter; } $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getCodePostal()); ++$letter; $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getVille()); ++$letter; $agent = $adresseList[$i][0]->getMainAgent() ? $adresseList[$i][0]->getMainAgent()->getPrenom( ).' '.$adresseList[$i][0]->getMainAgent()->getNom() : ''; $sheet->setCellValue($letter.($i + 2), $agent); ++$letter; $sheet->setCellValue($letter.($i + 2), round($adresseList[$i]['noteMoyenne'], 1)); ++$letter; $sheet->setCellValue($letter.($i + 2), $adresseList[$i][0]->getValidControls()->count()); } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'adresses.'.$request->get('_format'); $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } protected function getAddressExportColumns(SolutionService $solutionService): array { $cmItems = [ 'Voie', 'Numéro', 'Répetition', 'Batiment', 'Résidence', 'Code postal', 'Ville', 'Agent', 'Note Moyenne', 'Contrôles effectués', ]; $lmItems = [ 'Voie', 'Numéro', 'Répetition', 'Batiment', 'Résidence', 'Numéro Ascenseur', 'Code postal', 'Ville', 'Agent', 'Note Moyenne', 'Contrôles effectués', ]; if ('clean-manager' === $solutionService->getCurrent()->getSlug()) { return $cmItems; } else { return $lmItems; } } /** * @throws Exception */ #[Route(path: '/prestataires/index.{_format}', name: 'prestataire_export_index', methods: ['GET'])] public function prestataireExportIndex( Request $request, PrestataireRepository $prestataireRepository, AgencyPrestataireRepository $agencyPrestataireRepository ): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $prestataireList = $prestataireRepository->findAll(); } else { $prestataireList = $agencyPrestataireRepository->getPrestatairesByManager( $this->getUser() ); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Prestataire'); $sheet->setCellValue('A1', 'Nom'); $sheet->setCellValue('B1', 'Email'); $sheet->setCellValue('C1', 'Description'); $sheet->setCellValue('D1', 'Téléphone fixe'); $sheet->setCellValue('E1', 'Numéro Contrat'); for ($i = 0; $i < count($prestataireList); ++$i) { $sheet->setCellValue('A'.($i + 2), $prestataireList[$i]->getNomContact()); $sheet->setCellValue('B'.($i + 2), $prestataireList[$i]->getMailContact()); $sheet->setCellValue('C'.($i + 2), $prestataireList[$i]->getPrestataire()->getShortDesc()); $sheet->setCellValue('D'.($i + 2), $prestataireList[$i]->getTelephone()); $sheet->setCellValue('E'.($i + 2), $prestataireList[$i]->getNumeroContrat()); } $writer = $this->getWriter($spreadsheet, $request->get('_format')); $fileName = 'prestataires.'.$request->get('_format'); $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } }
Save File
Cancel