src/Controller/HomeController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Address;
  4. use App\Entity\Category;
  5. use App\Entity\ContactUs;
  6. use App\Form\AddressType;
  7. use App\Form\ContactType;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Email;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18. class HomeController extends AbstractController
  19. {
  20. /**
  21. * @Route("/", name="home")
  22. */
  23. public function index(Request $request, ManagerRegistry $registry): Response
  24. {
  25. $categories = $registry->getRepository(Category::class);
  26. return $this->render('home/index.html.twig', [
  27. 'controller_name' => 'HomeController',
  28. 'categories' => $categories->findAll(),
  29. ]);
  30. }
  31. /**
  32. * @Route("/contact", name="contact_request" , methods="POST")
  33. */
  34. public function contact_request(Request $request , ValidatorInterface $validator,
  35. MailerInterface $mailer, ManagerRegistry $registry): JsonResponse
  36. {
  37. $contactUs = new ContactUs();
  38. $data = $request->request->all();
  39. $contactUs->setName($data['name'])
  40. ->setEmail($data['email'])
  41. ->setPhone($data['mobile'])
  42. ->setSubject($data['subject'])
  43. ->setMessage($data['message']);
  44. $error = $validator->validate($contactUs);
  45. if (count($error) > 0) {
  46. return $this->json([
  47. 'error' => $error,
  48. ], 400, []);
  49. } else {
  50. $em = $registry->getManager();
  51. $email = (new Email())
  52. ->from('info@sharmadesignhouse.com')
  53. ->to('info@sharmadesignhouse.com')
  54. ->priority(Email::PRIORITY_HIGH)
  55. ->subject("{$data['subject']} {$data['email']}")
  56. ->text($data['message']);
  57. try {
  58. $mailer->send($email);
  59. $em->persist($contactUs);
  60. $em->flush();
  61. } catch (TransportExceptionInterface $e) {
  62. return $this->json(['message' => $e->getMessage()], $e->getCode());
  63. }
  64. }
  65. return $this->json(['message' => 'your message has been sent successfully.']);
  66. }
  67. /**
  68. * @Route("/get-option" , name="app_city_option", methods={"GET"})
  69. */
  70. public function getOptions(Request $request): Response
  71. {
  72. $state = $request->query->get('state');
  73. $address = new Address();
  74. $address->setState($state);
  75. $form = $this->createForm(AddressType::class, $address);
  76. return $this->render('partials/_city-options.html.twig', [
  77. 'controller_name' => 'DashboardController',
  78. 'form' => $form->createView(),
  79. 'address' => $address,
  80. ]);
  81. }
  82. }