<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Form\ContactType;
use App\Repository\ContactRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
#[Route('/contact')]
class ContactController extends AbstractController
{
private function getClientIp(): string {
$ip = '';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
#[Route('/all', name: 'app_contact_index', methods: ['GET'])]
public function index(ContactRepository $contactRepository): Response
{
return $this->render('contact/index.html.twig', [
'contacts' => $contactRepository->findAll(),
]);
}
#[Route('/', name: 'app_contact_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager, MailerInterface $mailer): Response
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
$ip = $this->getClientIp();
$htmlSanitizer = new HtmlSanitizer(
(new HtmlSanitizerConfig())->allowSafeElements()
);
$contact->setIpAddress($ip);
if ($form->isSubmitted() && $form->isValid()) {
$message = $htmlSanitizer->sanitize($form['message']->getData());
$contact->setMessage($message);
$entityManager->persist($contact);
$entityManager->flush();
$name = $htmlSanitizer->sanitize($form['name']->getData());
$surname = $htmlSanitizer->sanitize($form['surname']->getData());
$email = $htmlSanitizer->sanitize($form['email']->getData());
$email = (new Email())
->from('mailtrap@demomailtrap.com')
->to('bydunok.za.zugarok@gmail.com') // Primary recipient
->subject('в Арці ворота')
->text("$name $surname пише: $message. Зворотній email: $email. IP адреса: $ip");
try {
$mailer->send($email);
} catch (TransportExceptionInterface $e) {
// some error prevented the email sending; display an
// error message or try to resend the message
var_dump("error", $e);
die();
}
return $this->renderForm('default/contact_received.html.twig');
}
return $this->renderForm('contact/new.html.twig', [
'contact' => $contact,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_contact_show', methods: ['GET'])]
public function show(Contact $contact): Response
{
return $this->render('contact/show.html.twig', [
'contact' => $contact,
]);
}
#[Route('/{id}/edit', name: 'app_contact_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Contact $contact, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_contact_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('contact/edit.html.twig', [
'contact' => $contact,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_contact_delete', methods: ['POST'])]
public function delete(Request $request, Contact $contact, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$contact->getId(), $request->request->get('_token'))) {
$entityManager->remove($contact);
$entityManager->flush();
}
return $this->redirectToRoute('app_contact_index', [], Response::HTTP_SEE_OTHER);
}
}