<?php
namespace App\Controller;
use App\Entity\Address;
use App\Entity\Category;
use App\Entity\ContactUs;
use App\Form\AddressType;
use App\Form\ContactType;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function index(Request $request, ManagerRegistry $registry): Response
{
$categories = $registry->getRepository(Category::class);
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
'categories' => $categories->findAll(),
]);
}
/**
* @Route("/contact", name="contact_request" , methods="POST")
*/
public function contact_request(Request $request , ValidatorInterface $validator,
MailerInterface $mailer, ManagerRegistry $registry): JsonResponse
{
$contactUs = new ContactUs();
$data = $request->request->all();
$contactUs->setName($data['name'])
->setEmail($data['email'])
->setPhone($data['mobile'])
->setSubject($data['subject'])
->setMessage($data['message']);
$error = $validator->validate($contactUs);
if (count($error) > 0) {
return $this->json([
'error' => $error,
], 400, []);
} else {
$em = $registry->getManager();
$email = (new Email())
->from('info@sharmadesignhouse.com')
->to('info@sharmadesignhouse.com')
->priority(Email::PRIORITY_HIGH)
->subject("{$data['subject']} {$data['email']}")
->text($data['message']);
try {
$mailer->send($email);
$em->persist($contactUs);
$em->flush();
} catch (TransportExceptionInterface $e) {
return $this->json(['message' => $e->getMessage()], $e->getCode());
}
}
return $this->json(['message' => 'your message has been sent successfully.']);
}
/**
* @Route("/get-option" , name="app_city_option", methods={"GET"})
*/
public function getOptions(Request $request): Response
{
$state = $request->query->get('state');
$address = new Address();
$address->setState($state);
$form = $this->createForm(AddressType::class, $address);
return $this->render('partials/_city-options.html.twig', [
'controller_name' => 'DashboardController',
'form' => $form->createView(),
'address' => $address,
]);
}
}