<?php
namespace App\Blocks\ValuationForm;
use App\Exception\RedirectException;
use App\Form\Frontend\Content\Pages\ValuationForm;
class ValuationFormBlock extends \App\Blocks\Block
{
static function getName()
{
return "Valuation Form";
}
static function getDescription()
{
return "Valuation form with submissions sent via email";
}
public function run()
{
// Create form
$valuationForm = $this->formFactory->create(ValuationForm::class, []);
// Handle form request
$valuationForm->handleRequest($this->requestStack->getCurrentRequest());
// Form submitted & valid?
if($valuationForm->isSubmitted() && $valuationForm->isValid())
{
// Send the email
$this->mailerHelper->send($this->getRecipient(), "Valuation Form Submission", "valuationForm.html.twig", [
"submitted" => [
"name" => $valuationForm->get('name')->getData(),
"email" => $valuationForm->get('email')->getData(),
"phone" => $valuationForm->get('phone')->getData(),
"valuationType" => $valuationForm->get('valuationType')->getData(),
"propertyType" => $valuationForm->get('propertyType')->getData(),
"propertyAddress" => $valuationForm->get('propertyAddress')->getData()
]
]);
throw new RedirectException(new \Symfony\Component\HttpFoundation\RedirectResponse('/valuation?sent=1'));
}
$this->valuationForm = $valuationForm;
}
public function getDirectory()
{
return "ValuationForm";
}
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"], '?'),
'valuationForm' => $this->valuationForm->createView(),
));
}
public function getRecipient()
{
$data = $this->block->getData();
return $data['recipient'];
}
}