<?php
namespace App\Blocks\ContactForm;
use App\Exception\RedirectException;
use App\Form\Frontend\Content\Pages\ContactForm;
class ContactFormBlock extends \App\Blocks\Block
{
static function getName()
{
return "Contact Form";
}
static function getDescription()
{
return "Contact form with submissions sent via email";
}
public function run()
{
// Create form
$contactForm = $this->formFactory->create(ContactForm::class, []);
// Handle form request
$contactForm->handleRequest($this->requestStack->getCurrentRequest());
// Form submitted & valid?
if($contactForm->isSubmitted() && $contactForm->isValid())
{
// Send the email
$this->mailerHelper->send($this->getRecipient(), "Contact Form Submission", "contactForm.html.twig", [
"submitted" => [
"name" => $contactForm->get('name')->getData(),
"email" => $contactForm->get('email')->getData(),
"phone" => $contactForm->get('phone')->getData(),
"message" => $contactForm->get('message')->getData()
]
]);
throw new RedirectException(new \Symfony\Component\HttpFoundation\RedirectResponse('/contact?sent=1'));
}
$this->contactForm = $contactForm;
}
public function getDirectory()
{
return "ContactForm";
}
public function applyDataDefaults($data)
{
return array_merge(array(
"recipient" => "",
"classes" => ""
), $data);
}
public function renderFrontend()
{
return $this->twig->render('Blocks/' . $this->getDirectory() . '/Frontend/renderFrontend.html.twig', array(
'block' => array(
'identifier' => $this->block->getId(),
'block' => $this->block,
'instance' => $this
),
'data' => $this->applyDataDefaults($this->block->getData()),
'current_path' => strtok($_SERVER["REQUEST_URI"], '?'),
'contactForm' => $this->contactForm->createView(),
));
}
public function getRecipient()
{
$data = $this->block->getData();
return array_key_exists('recipient', $data) ? $data['recipient'] : null;
}
}