<?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\ContactType;
use App\Entity\Contact;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use App\Service\Paginator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator;
class ContactController extends AbstractController
{
const TEMPLATE_FORMAT = '%s/%s.html.twig';
const SECTION_PATIENT = 'patient';
const SECTION_HCP = 'hcp';
/**
* @Route("/patient/contact", name="patient-contact")
* @Route("/hcp/contact", name="hcp-contact")
*/
public function contactPage( Request $request, Recaptcha3Validator $recaptcha3Validator ): Response
{
$locale = $request->getLocale();
$name = $request->get('_route');
$section = ( str_contains($name, Self::SECTION_PATIENT) ) ? Self::SECTION_PATIENT : Self::SECTION_HCP;
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($contact);
$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/contact", name="admin_contact_index", methods={"GET"})
* @IsGranted("ROLE_ADMIN")
*/
public function index(Request $request, Paginator $paginator)
{
$is_ajax = $request->isXmlHttpRequest();
if ( $is_ajax ) {
$entityManager = $this->getDoctrine()->getManager();
$pagination = $paginator->getPagination( $entityManager->getRepository(Contact::class), 'c.createdAt', 'desc' );
$response['rows'] = [];
$response['total'] = $pagination->getTotalItemCount();
foreach ($pagination as $contact) {
$response['rows'][] = [
'id' => $contact->getId(),
'subject' => $contact->getSubject(),
'firstname' => $contact->getFirstname(),
'lastname' => $contact->getLastname(),
'email' => $contact->getEmail(),
'phone' => $contact->getPhone(),
'message' => $contact->getMessage(),
'productInfo' => $contact->getProductInfo(),
'feedback' => $contact->getFeedback(),
'promotion' => $contact->getPromotion(),
'createdAt' => $contact->getCreatedAt()->format('Y-m-d')
];
}
return new JsonResponse( $response );
}
return $this->render('contact/index.html.twig', []);
}
}