<?php
namespace App\Controller;
use \DateTime;
use \DateInterval;
use App\Services\BlogReader;
use App\Services\SiteConfig;
use App\Entity\ContactFormSubmission;
use App\Entity\Subscriber;
use App\Form\Type\SubscribeForm;
use App\Form\Type\ContactForm;
use App\Services\SubscriptionHandler;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends AbstractController {
private SubscriptionHandler $handler;
private BlogReader $blogReader;
private SiteConfig $config;
function __construct(SubscriptionHandler $handler, BlogReader $blogReader, SiteConfig $config) {
$this->handler = $handler;
$this->blogReader = $blogReader;
$this->config = $config;
}
function index(Request $request ): Response {
$formData = new Subscriber();
$form = $this->createForm(SubscribeForm::class, $formData, ['required' => false] );
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$formData = $form->getData();
$this->handler->processSubmission($formData, $request);
// redirect back to this page
$routeName = $request->attributes->get('_route');
$response = $this->redirectToRoute($routeName);
// to show confirmation box
$response->headers->setCookie(
Cookie::create('submit', 'true', 0, '/', null, null, false)
);
return $response;
}
$contactFormSubmission = new ContactFormSubmission();
$contactFrom = $this->createForm(ContactForm::class, $contactFormSubmission, ['required' => false] );
$contactFrom->handleRequest($request);
if ($contactFrom->isSubmitted() && $contactFrom->isValid()) {
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$contactFormSubmission = $contactFrom->getData();
$this->handler->processContactSubmission($contactFormSubmission, $request);
// redirect back to this page
$routeName = $request->attributes->get('_route');
$response = $this->redirectToRoute($routeName);
// to show confirmation box
$response->headers->setCookie(
Cookie::create('submit', 'true', 0, '/', null, null, false)
);
$response->headers->clearCookie('contactFormVisible');
return $response;
}
$contents = $this->renderView( "default/index.html.twig", [
'i_host' => 'i.'.$request->getHost(),
'form' => $form->createView(),
'contactForm' => $contactFrom->createView(),
'posts' => $this->blogReader->getLatestExcerpts($this->config->getNumBlogPostsOnHomePage()),
'register_confirm_message_id_1' => 'Thank you for your interest! We’ll keep you in touch.',
'register_confirm_message_id_2' => '',
]);
return new Response($contents);
}
}