<?php
namespace App\Controller\Front;
use App\Controller\Core\BaseFrontController;
use App\Entity\FormPromotion;
use App\Form\Front\FormPromotionType;
use App\Manager\FormPromotionManager;
use App\Manager\PromotionManager;
use App\Manager\PromotionsManager;
use App\Service\Mail\PromotionMail;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class PromotionController extends BaseFrontController
{
const FORM_PROMOTION = 'sd_form_promotion_entity';
/**
* @Route("/promociones", name="promotions", methods={"GET"})
* @Template("front/promotions/promotions.html.twig")
*/
public function promotions(PromotionsManager $promotionsManager, PromotionManager $promotionManager): array
{
$entity = $this->getFormSession(self::FORM_PROMOTION, new FormPromotion());
$form = $this->getFormView(FormPromotionType::class, $entity, 'promotions_action');
$this->locals['promotions'] = $promotionManager->current();
$this->locals['form'] = $form;
$this->locals['sd'] = $promotionsManager->find(1);
return $this->locals;
}
/**
* @Route("/promociones", name="promotions_action", methods={"POST"})
*/
public function promotionsAction(Request $request, FormPromotionManager $manager, PromotionMail $mail): RedirectResponse
{
$entity = new FormPromotion();
$form = $this->getForm(FormPromotionType::class, $entity, 'promotions_action');
$form->handleRequest($request);
if ($form->isValid()) {
$manager->save($entity);
$mail->load($entity)->send();
return $this->redirectToRoute('thanks_slug', ['slug' => 'promocion']);
}
$this->saveFormSession(self::FORM_PROMOTION, $entity);
return $this->redirectToRoute('promotions');
}
}