src/Controller/Traitement/DemandeController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Traitement;
  3. use App\Entity\PMessengerStatut;
  4. use App\Controller\ApiController;
  5. use App\Service\DataTableService;
  6. use App\Entity\PGenerationFichier;
  7. use App\Service\CalculPaieService;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Contracts\HttpClient\HttpClientInterface;
  14. use Symfony\Component\HttpFoundation\StreamedResponse;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. class DemandeController extends AbstractController
  17. {
  18.     private $em;
  19.     private $client;
  20.     private $api;
  21.     private $calculPaieService;
  22.     private $dataTableService;
  23.     public function __construct(ManagerRegistry $doctrineDataTableService $dataTableServiceCalculPaieService $calculPaieServiceApiController $apiHttpClientInterface $client)
  24.     {
  25.         $this->em $doctrine->getManager();
  26.         $this->api $api;
  27.         $this->client $client;
  28.         $this->calculPaieService $calculPaieService;
  29.         $this->dataTableService $dataTableService;
  30.     }
  31.     #[Route('/traitement/demande'name'app_traitement_demande')]
  32.     public function index(Request $request): Response
  33.     {
  34.         $operations $this->api->check($this->getUser(), 'app_traitement_demande'$this->em$request);
  35.         if (!is_array($operations)) {
  36.             return $this->redirectToRoute('app_site');
  37.         } elseif (count($operations) == 0) {
  38.             return $this->render('includes/404.html.twig');
  39.         }
  40.         $oneHourAgo = new \DateTime();
  41.         $oneHourAgo->modify('-1 hour');
  42.         $messages $this->em->createQueryBuilder()
  43.             ->select('m')
  44.             ->from(PMessengerStatut::class, 'm')
  45.             ->where('m.created <= :oneHourAgo')
  46.             ->andWhere('m.statut IN (:statut)')
  47.             ->setParameter('oneHourAgo'$oneHourAgo)
  48.             ->setParameter('statut', ['en cours','demarer'])
  49.             ->getQuery()
  50.             ->getResult()
  51.         ;
  52. //        dd($messages);
  53.         foreach ($messages as $message) {
  54.             $message->setStatut('error');
  55.             $message->setMessage('Something went wrong');
  56.         }
  57.         $this->em->flush();
  58.         return $this->render('traitement/demande/index.html.twig', [
  59.             'operations' => $operations,
  60.             'periode' => $this->calculPaieService->getPeriode()
  61.         ]);
  62.     }
  63.     #[Route('/app_traitement_demande_list'name'app_traitement_demande_list'options: ['expose' => true])]
  64.     public function app_traitement_demande_list(Request $request): Response
  65.     {
  66.         $draw $request->query->get('draw');
  67.         $columns $request->get('columns');
  68.         $start $request->query->get('start') ?? 0;
  69.         $length $request->query->get('length') ?? 10;
  70.         $dossier $request->getSession()->get('dossier');
  71.         $search $request->query->all('search')["value"];
  72.         $orderColumnIndex $request->query->all('order')[0]['column'];
  73.         $orderColumn $request->query->all("columns")[$orderColumnIndex]['name'];
  74.         $orderDir $request->query->all('order')[0]['dir'] ?? 'asc';
  75.         $initialQueryBuilder $this->em->createQueryBuilder()
  76.             ->from(PMessengerStatut::class, 'm')
  77.             ->innerJoin('m.user''u')
  78.             ->leftJoin('m.generationFichier''generationFichier')
  79.         ;
  80.         if($this->getUser()->getUserIdentifier() != 'admin') {
  81.             $initialQueryBuilder->andWhere('u.username = :username')
  82.                 ->setParameter('username'$this->getUser()->getUserIdentifier());
  83.         }
  84.         $queryBuilder = clone $initialQueryBuilder;
  85.         $selectColumns $this->dataTableService->selectColumns($columns);
  86.         $queryBuilder->select(implode(', '$selectColumns));
  87.         $queryBuilder->addSelect('generationFichier.token');
  88.         if (!empty($search)) {
  89.             $conditions $this->dataTableService->search($columns);
  90.             $queryBuilder->andWhere('(' implode(' OR '$conditions) . ')')
  91.                 ->setParameter('search'"%$search%");
  92.         }
  93.         $filteredRecords count($queryBuilder->getQuery()->getResult());
  94.         // Paginate results
  95.         $queryBuilder->setFirstResult($start)
  96.             ->setMaxResults($length)
  97.             ->orderBy("m.id","DESC");
  98.         $results $queryBuilder->getQuery()->getResult();
  99.         $totalRecords = clone $initialQueryBuilder;
  100.         $totalRecords->select('COUNT(m.id)');
  101.         $totalRecords $totalRecords->getQuery()
  102.             ->getSingleScalarResult();
  103.         return new JsonResponse([
  104.             'draw' => $draw,
  105.             'recordsTotal' => $totalRecords,
  106.             'recordsFiltered' => $filteredRecords,
  107.             'data' => $results,
  108.         ]);
  109.     }
  110.     #[Route('/download/{token}'name'app_traitement_demande_download'options: ['expose' => true])]
  111.     public function app_traitement_demande_download($token): Response
  112.     {
  113.         /** @var PGenerationFichier */
  114.         $generationFichier $this->em->getRepository(PGenerationFichier::class)->findOneBy(['token' => $token]);
  115.         $zipFilePath tempnam(sys_get_temp_dir(), 'zip_') . '.zip';
  116.         // Create a new instance of ZipArchive
  117.         $zip = new \ZipArchive();
  118.         // Open the ZIP file for writing
  119.         if ($zip->open($zipFilePath\ZipArchive::CREATE) !== TRUE) {
  120.             throw new \Exception('Cannot open the ZIP file for writing.');
  121.         }
  122.         $files = [];
  123.         $name '';
  124.         if($generationFichier->getTxtFileName()) {
  125.             $name pathinfo($this->getParameter('tempfiles').$generationFichier->getTxtFileName(), PATHINFO_FILENAME);
  126.             array_push($files$this->getParameter('tempfiles').$generationFichier->getTxtFileName());
  127.         }
  128.         if($generationFichier->getExcelFileName()) {
  129.             $name pathinfo($this->getParameter('tempfiles').$generationFichier->getExcelFileName(), PATHINFO_FILENAME);
  130.             array_push($files$this->getParameter('tempfiles').$generationFichier->getExcelFileName());
  131.         }
  132.         // Add files to the ZIP
  133.         foreach ($files as $file) {
  134.             if (file_exists($file)) {
  135.                 // Get the file's name
  136.                 $fileName basename($file);
  137.                 // Add the file to the ZIP archive
  138.                 $zip->addFile($file$fileName);
  139.             } else {
  140.                 throw new \Exception("File does not exist: $file");
  141.             }
  142.         }
  143.         // Close the ZIP file
  144.         $zip->close();
  145.         // Create a response to download the ZIP file
  146.         $response = new StreamedResponse(function () use ($zipFilePath) {
  147.             readfile($zipFilePath);
  148.             // Remove the temporary ZIP file after sending it to the user
  149.             unlink($zipFilePath);
  150.         });
  151.         // Set headers to download the file
  152.         $customFileName $name.'.zip';
  153.         $response->headers->set('Content-Type''application/zip');
  154.         $response->headers->set('Content-Disposition''attachment; filename="'.$customFileName.'"');
  155.         $response->headers->set('Content-Length'filesize($zipFilePath));
  156.         return $response;
  157.         
  158.     }
  159. }