src/Controller/ContactController.php line 32

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\ContactType;
  12. use App\Entity\Contact;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use App\Service\Paginator;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3Validator;
  18. class ContactController extends AbstractController
  19. {
  20.     const TEMPLATE_FORMAT '%s/%s.html.twig';
  21.     const SECTION_PATIENT 'patient';
  22.     const SECTION_HCP 'hcp';
  23.     /**
  24.      * @Route("/patient/contact", name="patient-contact")
  25.      * @Route("/hcp/contact", name="hcp-contact")
  26.      */
  27.     public function contactPageRequest $requestRecaptcha3Validator $recaptcha3Validator ): Response
  28.     {
  29.         $locale $request->getLocale();
  30.         $name $request->get('_route');
  31.         $section = ( str_contains($nameSelf::SECTION_PATIENT) ) ? Self::SECTION_PATIENT Self::SECTION_HCP;
  32.         $contact = new Contact();
  33.         $form $this->createForm(ContactType::class, $contact);
  34.         $form->handleRequest($request);
  35.         if($form->isSubmitted() && $form->isValid()) {
  36.             $entityManager $this->getDoctrine()->getManager();
  37.             $entityManager->persist($contact);
  38.             $entityManager->flush();
  39.             $this->get('session')->getFlashBag()->add('success''form_submitted');
  40.             return $this->redirectToRoute($name, [], Response::HTTP_SEE_OTHER);
  41.         }
  42.         return $this->render(sprintf(Self::TEMPLATE_FORMAT$locale$name), [
  43.             'section' => $section,
  44.             'form' => $form->createView()
  45.         ]);
  46.     }    
  47.     /**
  48.      * @Route("/admin/contact", name="admin_contact_index", methods={"GET"})
  49.      * @IsGranted("ROLE_ADMIN")
  50.      */
  51.     public function index(Request $requestPaginator $paginator
  52.     {
  53.         $is_ajax $request->isXmlHttpRequest();
  54.         if ( $is_ajax ) {
  55.             $entityManager $this->getDoctrine()->getManager();
  56.             $pagination $paginator->getPagination$entityManager->getRepository(Contact::class), 'c.createdAt''desc' );
  57.             $response['rows'] = [];
  58.             $response['total'] = $pagination->getTotalItemCount(); 
  59.             foreach ($pagination as $contact) {
  60.                 $response['rows'][] = [
  61.                     'id'            => $contact->getId(),
  62.                     'subject'       => $contact->getSubject(),
  63.                     'firstname'     => $contact->getFirstname(),
  64.                     'lastname'      => $contact->getLastname(),
  65.                     'email'         => $contact->getEmail(),
  66.                     'phone'         => $contact->getPhone(),
  67.                     'message'       => $contact->getMessage(),
  68.                     'productInfo'   => $contact->getProductInfo(),
  69.                     'feedback'      => $contact->getFeedback(),
  70.                     'promotion'     => $contact->getPromotion(),
  71.                     'createdAt'     => $contact->getCreatedAt()->format('Y-m-d')
  72.                 ];
  73.             }
  74.             return new JsonResponse$response );  
  75.         }
  76.         return $this->render('contact/index.html.twig', []);
  77.     }
  78.  
  79. }