src/Controller/DefaultController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use \DateTime;
  4. use \DateInterval;
  5. use App\Services\BlogReader;
  6. use App\Services\SiteConfig;
  7. use App\Entity\ContactFormSubmission;
  8. use App\Entity\Subscriber;
  9. use App\Form\Type\SubscribeForm;
  10. use App\Form\Type\ContactForm;
  11. use App\Services\SubscriptionHandler;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. class DefaultController extends AbstractController {
  17.     private SubscriptionHandler $handler;
  18.     private BlogReader $blogReader;
  19.     private SiteConfig $config;
  20.     function __construct(SubscriptionHandler $handlerBlogReader $blogReaderSiteConfig $config) {
  21.         $this->handler $handler;
  22.         $this->blogReader $blogReader;
  23.         $this->config $config;
  24.     }
  25.     function index(Request $request ): Response {
  26.         $formData = new Subscriber();
  27.         $form $this->createForm(SubscribeForm::class, $formData, ['required' => false] );
  28.         $form->handleRequest($request);
  29.         if ($form->isSubmitted() && $form->isValid()) {
  30.             // $form->getData() holds the submitted values
  31.             // but, the original `$task` variable has also been updated
  32.             $formData $form->getData();
  33.             $this->handler->processSubmission($formData$request);
  34.             // redirect back to this page
  35.             $routeName $request->attributes->get('_route');
  36.             $response $this->redirectToRoute($routeName);
  37.             // to show confirmation box
  38.             $response->headers->setCookie(
  39.                 Cookie::create('submit''true'0'/'nullnullfalse)
  40.             );
  41.             return $response;
  42.         }
  43.         $contactFormSubmission = new ContactFormSubmission();
  44.         $contactFrom $this->createForm(ContactForm::class, $contactFormSubmission, ['required' => false] );
  45.         $contactFrom->handleRequest($request);
  46.         if ($contactFrom->isSubmitted() && $contactFrom->isValid()) {
  47.             // $form->getData() holds the submitted values
  48.             // but, the original `$task` variable has also been updated
  49.             $contactFormSubmission $contactFrom->getData();
  50.             $this->handler->processContactSubmission($contactFormSubmission$request);
  51.             // redirect back to this page
  52.             $routeName $request->attributes->get('_route');
  53.             $response $this->redirectToRoute($routeName);
  54.             // to show confirmation box
  55.             $response->headers->setCookie(
  56.                 Cookie::create('submit''true'0'/'nullnullfalse)
  57.             );
  58.             $response->headers->clearCookie('contactFormVisible');
  59.             return $response;
  60.         }
  61.         $contents $this->renderView"default/index.html.twig", [
  62.             'i_host' => 'i.'.$request->getHost(),
  63.             'form' => $form->createView(),
  64.             'contactForm' => $contactFrom->createView(),
  65.             'posts' => $this->blogReader->getLatestExcerpts($this->config->getNumBlogPostsOnHomePage()),
  66.             'register_confirm_message_id_1' => 'Thank you for your interest! We’ll keep you in touch.',
  67.             'register_confirm_message_id_2' => '',
  68.         ]);
  69.         return new Response($contents);    
  70.     }
  71. }