src/Controller/VoucherController.php line 230

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Voucher;
  4. use App\Entity\VoucherTracker;
  5. use App\Form\VoucherType;
  6. use App\Helper\FileCrypt;
  7. use App\Helper\Tools;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/voucher")
  16.  */
  17. class VoucherController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="app_voucher_index", methods={"GET"})
  21.      */
  22.     public function index(EntityManagerInterface $entityManager): Response
  23.     {
  24.         $vouchers $entityManager
  25.             ->getRepository(Voucher::class)
  26.             ->findBy(['status' => [Voucher::STATUS_NEWVoucher::STATUS_NOT_EMPTY]]);
  27.         $allvouchers $entityManager
  28.             ->getRepository(Voucher::class)
  29.             ->findAll();
  30.         return $this->render('voucher/index.html.twig', [
  31.             'vouchers' => $vouchers,
  32.             'allvouchers' => $allvouchers,
  33.             'voucherstrack' => $entityManager->getRepository(VoucherTracker::class)->findAll(),
  34.             'all' => false,
  35.             'pageParams' => [
  36.                 'title' => 'Bons d\'achat actifs',
  37.                 'domain' => 'voucher',
  38.                 'breadcrumbs' => []
  39.             ],
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route("/all", name="app_voucher_all", methods={"GET"})
  44.      */
  45.     public function all(EntityManagerInterface $entityManager): Response
  46.     {
  47.         $vouchers $entityManager->getRepository(Voucher::class)->findAll();
  48.         return $this->render('voucher/index.html.twig', [
  49.             'vouchers' => $vouchers,
  50.             'voucherstrack' => $entityManager->getRepository(VoucherTracker::class)->findAll(),
  51.             'all' => true,
  52.             'pageParams' => [
  53.                 'title' => 'Tous les bons d\'achat',
  54.                 'domain' => 'voucher',
  55.                 'breadcrumbs' => []
  56.             ],
  57.         ]);
  58.     }
  59.     /**
  60.      * @Route("/new", name="app_voucher_new", methods={"GET", "POST"})
  61.      */
  62.     public function new(Request $requestEntityManagerInterface $entityManagerFileCrypt $fileCrypt): Response
  63.     {
  64.         $voucher = new Voucher();
  65.         $plus6 = new \DateTime();
  66.         $plus6->modify('+6 months');
  67.         $voucher->setEndDate($plus6);
  68.         $form $this->createForm(VoucherType::class, $voucher);
  69.         $form->handleRequest($request);
  70.         if ($form->isSubmitted() && $form->isValid()) {
  71.             $uploadedFile $form['onefile']->getData();
  72.             if ($uploadedFile) {
  73.                 // crypt and move the file
  74.                 $key $this->getParameter('voucher_files_key');
  75.                 $fileName md5(uniqid()).'.'.$uploadedFile->guessExtension();
  76.                 $documentsDir $this->getParameter('voucher_files_directory');
  77.                 if (!$fileCrypt->encrypt($uploadedFile->getPathname(), $documentsDir$fileName$key)) {
  78.                     $this->get('session')->getFlashBag()->add('error''basic.error');
  79.                 }
  80.                 $voucher->setFilename($fileName);
  81.             }
  82.             $voucher->setAddDate(new \DateTime());
  83.             $voucher->setStatus(Voucher::STATUS_NEW);
  84.             $entityManager->persist($voucher);
  85.             $entityManager->flush();
  86.             return $this->redirectToRoute('app_voucher_index', [], Response::HTTP_SEE_OTHER);
  87.         }
  88.         return $this->renderForm('voucher/new.html.twig', [
  89.             'voucher' => $voucher,
  90.             'form' => $form,
  91.             'pageParams' => [
  92.                 'title' => 'Nouveau bon d\'achat',
  93.                 'domain' => 'voucher',
  94.                 'breadcrumbs' => ['Bons d\'achat' => 'app_voucher_index']
  95.             ],
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/{id}", name="app_voucher_show", methods={"GET"})
  100.      */
  101.     public function show(Voucher $voucher): Response
  102.     {
  103.         return $this->render('voucher/show.html.twig', [
  104.             'voucher' => $voucher,
  105.             'pageParams' => [
  106.                 'title' => 'Bon d\'achat '.$voucher->getBrand(),
  107.                 'domain' => 'voucher',
  108.                 'breadcrumbs' => ['Bons d\'achat' => 'app_voucher_index']
  109.             ],
  110.         ]);
  111.     }
  112.     /**
  113.      * @Route("/activshare/{id}", name="app_voucher_activshare", methods={"GET"})
  114.      */
  115.     public function activshare(Voucher $voucherEntityManagerInterface $entityManager): Response
  116.     {
  117.         $voucher->setShareSlug(Tools::generateStrongPassword(64false'ld'));
  118.         $entityManager->persist($voucher);
  119.         $entityManager->flush();
  120.         $this->get('session')->getFlashBag()->add('info''Bon d\'achat <b>'.$voucher->getBrand().' / '.$voucher->getVoucher().'</b> est maintenant partageable');
  121.         return $this->redirectToRoute('app_voucher_index', [], Response::HTTP_SEE_OTHER);
  122.     }
  123.     /**
  124.      * @Route("/unactivshare/{id}", name="app_voucher_unactivshare", methods={"GET"})
  125.      */
  126.     public function unactivshare(Voucher $voucherEntityManagerInterface $entityManager): Response
  127.     {
  128.         $voucher->setShareSlug(null);
  129.         $entityManager->persist($voucher);
  130.         $entityManager->flush();
  131.         $this->get('session')->getFlashBag()->add('info''Bon d\'achat <b>'.$voucher->getBrand().' / '.$voucher->getVoucher().'</b> n\'est plus partageable');
  132.         return $this->redirectToRoute('app_voucher_index', [], Response::HTTP_SEE_OTHER);
  133.     }
  134.     /**
  135.      * @Route("/{id}/edit", name="app_voucher_edit", methods={"GET", "POST"})
  136.      */
  137.     public function edit(Request $requestVoucher $voucherEntityManagerInterface $entityManagerFileCrypt $fileCrypt): Response
  138.     {
  139.         $form $this->createForm(VoucherType::class, $voucher, ['edit' => true]);
  140.         $form->handleRequest($request);
  141.         if ($form->isSubmitted() && $form->isValid()) {
  142.             if (isset($form['onefile'])) {
  143.                 $uploadedFile $form['onefile']->getData();
  144.                 if ($uploadedFile) {
  145.                     // crypt and move the file
  146.                     $key $this->getParameter('voucher_files_key');
  147.                     $fileName md5(uniqid()).'.'.$uploadedFile->guessExtension();
  148.                     $documentsDir $this->getParameter('voucher_files_directory');
  149.                     if (!$fileCrypt->encrypt($uploadedFile->getPathname(), $documentsDir$fileName$key)) {
  150.                         $this->get('session')->getFlashBag()->add('error''basic.error');
  151.                     }
  152.                     $voucher->setFilename($fileName);
  153.                 }
  154.             }
  155.             if ($voucher->getUsed() > && $voucher->getUsed() < $voucher -> getMontant()) {
  156.                 $voucher->setStatus(Voucher::STATUS_NOT_EMPTY);
  157.                 $entityManager->persist($voucher);
  158.             }
  159.             $entityManager->flush();
  160.             $this->get('session')->getFlashBag()->add('info''Bon d\'achat <b>'.$voucher->getBrand().' / '.$voucher->getVoucher().'</b> a été mis à jour');
  161.             return $this->redirectToRoute('app_voucher_index', [], Response::HTTP_SEE_OTHER);
  162.         }
  163.         return $this->renderForm('voucher/edit.html.twig', [
  164.             'voucher' => $voucher,
  165.             'form' => $form,
  166.             'pageParams' => [
  167.                 'title' => 'Modifier bon d\'achat '.$voucher->getBrand(),
  168.                 'domain' => 'voucher',
  169.                 'breadcrumbs' => ['Bons d\'achat' => 'app_voucher_index']
  170.             ],
  171.         ]);
  172.     }
  173.     /**
  174.      * @Route("/archive/{id}", name="app_voucher_close", methods={"GET"})
  175.      */
  176.     public function close(Voucher $voucherEntityManagerInterface $entityManager)
  177.     {
  178.         $voucher->setShareSlug(null);
  179.         $voucher->setStatus(Voucher::STATUS_EMPTY);
  180.         $entityManager->persist($voucher);
  181.         $entityManager->flush();
  182.         $this->get('session')->getFlashBag()->add('info''Bon d\'achat <b>'.$voucher->getBrand().' / '.$voucher->getVoucher().'</b> a été archivé');
  183.         return $this->redirectToRoute('app_voucher_index', [], Response::HTTP_SEE_OTHER);
  184.     }
  185.     /**
  186.      * @Route("/{id}", name="app_voucher_delete", methods={"POST"})
  187.      */
  188.     public function delete(Request $requestVoucher $voucherEntityManagerInterface $entityManager): Response
  189.     {
  190.         if ($this->isCsrfTokenValid('delete'.$voucher->getId(), $request->request->get('_token'))) {
  191.             $entityManager->remove($voucher);
  192.             $entityManager->flush();
  193.             $this->get('session')->getFlashBag()->add('info''Bon d\'achat <b>'.$voucher->getBrand().' / '.$voucher->getVoucher().'</b> a été supprimé');
  194.         }
  195.         return $this->redirectToRoute('app_voucher_index', [], Response::HTTP_SEE_OTHER);
  196.     }
  197.     /**
  198.      * @Route("/download_file/{id}/{action?download}", name="app_voucher_download_file", methods={"GET"})
  199.      */
  200.     public function downloadFile(Voucher $voucherstring $action 'download'EntityManagerInterface $entityManagerFileCrypt $fileCrypt)
  201.     {
  202.         $key $this->getParameter('voucher_files_key');
  203.         $documentsDir $this->getParameter('voucher_files_directory');
  204.         $filePath $documentsDir.'/'.$voucher->getFilename();
  205.         $fileContent $fileCrypt->encodeThis(file_get_contents($filePath), 'd'$key);
  206.         $response = new Response($fileContent);
  207.         if ($action === 'download') {
  208.             $response->headers->set('Content-Type''application/octet-stream');
  209.             $response->headers->set('Content-Disposition''attachment; filename="' $voucher->getBrand().'-'.$voucher->getMontant().'.pdf"');
  210.         } else {
  211.             $response->headers->set('Content-Type''application/pdf');
  212.             $response->headers->set('Content-Disposition''inline; filename="' $voucher->getBrand().'-'.$voucher->getMontant().'.pdf"');
  213.         }
  214.         return $response;
  215.     }
  216.     /**
  217.      * @Route("/ignoretoggle/{id}/{ignore}", name="app_voucher_ignore")
  218.      */
  219.     public function ignoreAlert(Voucher $voucher$ignoreEntityManagerInterface $entityManager)
  220.     {
  221.         if($ignore == 1) {
  222.             $this->get('session')->getFlashBag()->add('info'$voucher->getBrand().' '.$voucher->getMontant().'€ <b>alerte ignorée</b>');
  223.             $voucher->setIgnored(1);
  224.         } else {
  225.             $this->get('session')->getFlashBag()->add('info'$voucher->getBrand().' '.$voucher->getMontant().'€ <b>alerte plus ignorée</b>');
  226.             $voucher->setIgnored(0);
  227.         }
  228.         $entityManager->persist($voucher);
  229.         $entityManager->flush();
  230.         return $this->redirectToRoute('app_voucher_index');
  231.     }
  232. }