<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Form\WarrantyType;
use App\Entity\Warranty;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
class WarrantyController extends AbstractController
{
const TEMPLATE_FORMAT = '%s/%s.html.twig';
const SECTION_PATIENT = 'patient';
const SECTION_HCP = 'hcp';
public function resourcesPage( Request $request ): Response
{
$locale = $request->getLocale();
$name = $request->get('_route');
$section = ( str_contains($name, Self::SECTION_PATIENT) ) ? Self::SECTION_PATIENT : Self::SECTION_HCP;
$warranty = new Warranty();
$form = $this->createForm(WarrantyType::class, $warranty);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($warranty);
$entityManager->flush();
$this->get('session')->getFlashBag()->add('success', 'form_submitted');
return $this->redirectToRoute($name, [], Response::HTTP_SEE_OTHER);
}
return $this->render(sprintf(Self::TEMPLATE_FORMAT, $locale, $name), [
'section' => $section,
'form' => $form->createView()
]);
}
/**
* @Route("/admin/warranty", name="admin_warranty_index", methods={"GET"})
* @IsGranted("ROLE_ADMIN")
*/
public function index(Request $request)
{
$is_ajax = $request->isXmlHttpRequest();
if ( $is_ajax ) {
$entityManager = $this->getDoctrine()->getManager();
$warranties = $entityManager->getRepository(Warranty::class)->findAll();
$data = array();
foreach ($warranties as $warranty) {
$data[] = array(
'id' => $warranty->getId(),
'serialNumber' => $warranty->getSerialNumber(),
'dateOfPurchase' => $warranty->getDateOfPurchase()->format("Y-m-d"),
'firstname' => $warranty->getFirstname(),
'lastname' => $warranty->getLastname(),
'dateOfBirth' => $warranty->getDateOfBirth()->format("Y-m-d"),
'inrSelfTesting' => $warranty->getInrSelfTesting(),
'purpose' => $warranty->getPurpose(),
'healthcareProfessional' => $warranty->getHealthcareProfessional(),
'profession' => $warranty->getProfession(),
'street' => $warranty->getStreet(),
'unit' => $warranty->getUnit(),
'city' => $warranty->getCity(),
'postalCode' => $warranty->getPostalCode(),
'phone' => $warranty->getPhone(),
'email' => $warranty->getEmail(),
'language' => $warranty->getLanguage(),
'declarationPrescription' => $warranty->getDeclarationPrescription(),
'declarationTestLimitation' => $warranty->getDeclarationTestLimitation(),
'declarationCorrectly' => $warranty->getDeclarationCorrectly(),
'declarationQuestions' => $warranty->getDeclarationQuestions(),
'declarationCommunications' => $warranty->getDeclarationCommunications(),
'createdAt' => $warranty->getCreatedAt()->format('Y-m-d')
);
}
return $this->json($data);
}
return $this->render('warranty/index.html.twig', []);
}
}