src/Controller/WarrantyController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\RouterInterface;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use App\Form\WarrantyType;
  12. use App\Entity\Warranty;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. class WarrantyController extends AbstractController
  16. {
  17.     const TEMPLATE_FORMAT '%s/%s.html.twig';
  18.     const SECTION_PATIENT 'patient';
  19.     const SECTION_HCP 'hcp';
  20.     public function resourcesPageRequest $request ): Response
  21.     {
  22.         $locale $request->getLocale();
  23.         $name $request->get('_route');
  24.         $section = ( str_contains($nameSelf::SECTION_PATIENT) ) ? Self::SECTION_PATIENT Self::SECTION_HCP;
  25.         $warranty = new Warranty();
  26.         $form $this->createForm(WarrantyType::class, $warranty);
  27.         $form->handleRequest($request);
  28.         if($form->isSubmitted() && $form->isValid()) {
  29.             $entityManager $this->getDoctrine()->getManager();
  30.             $entityManager->persist($warranty);
  31.             $entityManager->flush();
  32.             $this->get('session')->getFlashBag()->add('success''form_submitted');
  33.             return $this->redirectToRoute($name, [], Response::HTTP_SEE_OTHER);            
  34.         }
  35.         return $this->render(sprintf(Self::TEMPLATE_FORMAT$locale$name), [
  36.             'section' => $section,
  37.             'form' => $form->createView()
  38.         ]);
  39.     }
  40.     /**
  41.      * @Route("/admin/warranty", name="admin_warranty_index", methods={"GET"})
  42.      * @IsGranted("ROLE_ADMIN")
  43.      */
  44.     public function index(Request $request
  45.     {
  46.         $is_ajax $request->isXmlHttpRequest();
  47.         if ( $is_ajax ) {
  48.             $entityManager $this->getDoctrine()->getManager();
  49.             $warranties $entityManager->getRepository(Warranty::class)->findAll();
  50.             $data = array();
  51.             foreach ($warranties as $warranty) {
  52.                 $data[] = array(
  53.                     'id'                        => $warranty->getId(),
  54.                     'serialNumber'              => $warranty->getSerialNumber(),
  55.                     'dateOfPurchase'            => $warranty->getDateOfPurchase()->format("Y-m-d"),
  56.                     'firstname'                 => $warranty->getFirstname(),
  57.                     'lastname'                  => $warranty->getLastname(),
  58.                     'dateOfBirth'               => $warranty->getDateOfBirth()->format("Y-m-d"),
  59.                     'inrSelfTesting'            => $warranty->getInrSelfTesting(),
  60.                     'purpose'                   => $warranty->getPurpose(),
  61.                     'healthcareProfessional'    => $warranty->getHealthcareProfessional(),
  62.                     'profession'                => $warranty->getProfession(),
  63.                     'street'                    => $warranty->getStreet(),
  64.                     'unit'                      => $warranty->getUnit(),
  65.                     'city'                      => $warranty->getCity(),
  66.                     'postalCode'                => $warranty->getPostalCode(),
  67.                     'phone'                     => $warranty->getPhone(),
  68.                     'email'                     => $warranty->getEmail(),
  69.                     'language'                  => $warranty->getLanguage(),
  70.                     'declarationPrescription'   => $warranty->getDeclarationPrescription(),
  71.                     'declarationTestLimitation' => $warranty->getDeclarationTestLimitation(),
  72.                     'declarationCorrectly'      => $warranty->getDeclarationCorrectly(),
  73.                     'declarationQuestions'      => $warranty->getDeclarationQuestions(),
  74.                     'declarationCommunications' => $warranty->getDeclarationCommunications(),
  75.                     'createdAt'                 => $warranty->getCreatedAt()->format('Y-m-d')
  76.                 );
  77.             } 
  78.             return $this->json($data);  
  79.         }
  80.         return $this->render('warranty/index.html.twig', []);
  81.     }     
  82. }