<?php
namespace App\Controller\Frontend;
use App\Application\Abo\AboService;
use App\Application\Abonnent\AbonnentService;
use App\Application\Content\ContentBackendData;
use App\Application\Content\ContentFrontendData;
use App\Application\Content\ContentService;
use App\Entity\Abo\AboBestellung;
use App\Entity\Abo\AdressMutation;
use App\Entity\Abo\FerienMutation;
use App\Entity\Abonnent\Abonnenttoken;
use App\Entity\Abonnent\Exceptions\AbonnentLoginFailedException;
use App\Entity\Abonnent\Exceptions\AbonnentLoginInactiveException;
use App\Form\Abo\AboBestellungType;
use App\Form\Abo\AbonnentLoginType;
use App\Form\Abo\AdressMutationType;
use App\Form\Abo\ChangePasswordFormType;
use App\Form\Abo\FerienMutationType;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
#[Route(path: '/abo')]
#[Cache(expires: '+10 minutes')]
class AboController extends AbstractController
{
/**
* Hinweis: Route soll mit ../abo enden (NICHT .../abo/) gewünscht gemäss UW-App.
*/
#[Route(path: '', name: 'fe.abo')]
public function abo(ContentService $contentService): Response
{
$content = $contentService->getContentByCode('ABOSERVICE', new ContentFrontendData(), true);
return $this->render('frontend/abo/abo.html.twig', [
'content' => $content,
]);
}
#[Route(path: '/mutation/ferienumleitung', name: 'fe.abo_ferienumleitung', defaults: ['type' => 'umleitung'])]
#[Route(path: '/mutation/ferienabounterbruch', name: 'fe.abo_ferienabounterbruch', defaults: ['type' => 'unterbruch'])]
#[Route(path: '/mutation/ferienepaper', name: 'fe.abo_ferienepaper', defaults: ['type' => 'epaper'])]
public function ferienMutation(Request $request, $type, AboService $aboService, ContentService $contentService): Response
{
$mailSendMessage = '';
switch ($type) {
case 'umleitung':
$content = $contentService->getContentByCode('ABO_MUTATION_FERIEN_UMLEITUNG', new ContentBackendData(), true);
$mutation = FerienMutation::newUmleitung();
break;
case 'unterbruch':
$content = $contentService->getContentByCode('ABO_MUTATION_FERIEN_UNTERBRUCH', new ContentBackendData(), true);
$mutation = FerienMutation::newUnterbruch();
break;
// case 'epaper':
// $content = $contentService->getContentByCode('ABO_MUTATION_FERIEN_EPAPER', new ContentBackendData(), true);
// $mutation = FerienMutation::newEPaper();
// break;
default:
throw $this->createNotFoundException();
}
$form = $this->createForm(FerienMutationType::class, $mutation, ['csrf_protection' => false]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$aboService->mailFerienMutationToAboservice($mutation);
$mailSendMessage = $mutation->orderSendMessage();
}
return $this->render('frontend/abo/ferienmutation.html.twig', [
'content' => $content,
'mailSendMessage' => $mailSendMessage,
'mutation' => $mutation,
'form' => $form->createView(),
]);
}
#[Route(path: '/mutation/adresse', name: 'fe.abo_adressmutation')]
public function adressMutation(Request $request, AboService $aboService, ContentService $contentService): Response
{
$mailSendMessage = '';
$content = $contentService->getContentByCode('ABO_MUTATION_ADRESSE', new ContentBackendData(), true);
$mutation = AdressMutation::createMutation();
$form = $this->createForm(AdressMutationType::class, $mutation, ['csrf_protection' => false]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$aboService->mailAdressMutationToAboservice($mutation);
$mailSendMessage = $mutation->orderSendMessage();
}
return $this->render('frontend/abo/adressmutation.html.twig', [
'content' => $content,
'mailSendMessage' => $mailSendMessage,
'mutation' => $mutation,
'form' => $form->createView(),
]);
}
#[Route(path: '/bestellung/print', name: 'fe.abo_order_print', defaults: ['type' => 'print'])]
#[Route(path: '/bestellung/print/danke', name: 'fe.abo_order_print_danke', defaults: ['type' => 'print'])]
#[Route(path: '/bestellung/epaper', name: 'fe.abo_order_epaper', defaults: ['type' => 'epaper'])]
#[Route(path: '/bestellung/epaper/danke', name: 'fe.abo_order_epaper_danke', defaults: ['type' => 'epaper'])]
#[Route(path: '/bestellung/kombi', name: 'fe.abo_order_kombi', defaults: ['type' => 'kombi'])]
#[Route(path: '/bestellung/kombi/danke', name: 'fe.abo_order_kombi_danke', defaults: ['type' => 'kombi'])]
#[Route(path: '/bestellung/epaper-zusatz', name: 'fe.abo_order_epaperzusatz', defaults: ['type' => 'epaper-zusatz'])]
#[Route(path: '/bestellung/epaper-zusatz/danke', name: 'fe.abo_order_epaperzusatz_danke', defaults: ['type' => 'epaper-zusatz'])]
public function aboBestellung(Request $request, $type, AboService $aboService, ContentService $contentService): RedirectResponse|Response
{
$mailSendMessage = '';
switch ($type) {
case 'print':
$content = $contentService->getContentByCode('ABO_NEU_PRINT', new ContentBackendData(), true);
$options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
$bestellung = AboBestellung::newPrintAbo($options);
$routeDanke = 'fe.abo_order_print_danke';
break;
case 'epaper':
$content = $contentService->getContentByCode('ABO_NEU_EPAPER', new ContentBackendData(), true);
$options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
$bestellung = AboBestellung::newEPaperAbo($options);
$routeDanke = 'fe.abo_order_epaper_danke';
break;
case 'kombi':
$content = $contentService->getContentByCode('ABO_NEU_KOMBI', new ContentBackendData(), true);
$options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
$bestellung = AboBestellung::newKombiAbo($options);
$routeDanke = 'fe.abo_order_kombi_danke';
break;
case 'epaper-zusatz':
$content = $contentService->getContentByCode('ABO_NEU_EPAPER_ZUSATZ', new ContentBackendData(), true);
$options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
$bestellung = AboBestellung::newAddEPaperAbo($options);
$routeDanke = 'fe.abo_order_epaperzusatz_danke';
break;
default:
throw $this->createNotFoundException();
}
$form = $this->createForm(AboBestellungType::class, $bestellung, ['csrf_protection' => false]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$aboService->mailBestellungToAboservice($bestellung);
return $this->redirectToRoute($routeDanke);
}
// Mail Message
if (str_ends_with((string) $request->get('_route'), '_danke')) {
$mailSendMessage = $bestellung->orderSendMessage();
}
return $this->render('frontend/abo/abobestellung.html.twig', [
'content' => $content,
'mailSendMessage' => $mailSendMessage,
'bestellung' => $bestellung,
'form' => $form->createView(),
]);
}
#[Route(path: '/login', name: 'fe.abo_login')]
#[Cache(maxage: 0, smaxage: 0)]
public function login(Request $request, AbonnentService $abonnentService): RedirectResponse|Response
{
/**
* @var Abonnenttoken $loginToken
*/
$loginToken = null;
$abonnent = $abonnentService->authenticate($request);
$loginErrorMsg = '';
$data = [
'username' => '',
'password' => '',
'http_referer' => '', // hier leer lassen, sonst evlt. schleifenproblem mit logout
];
$form = $this->createForm(AbonnentLoginType::class, $data, ['csrf_protection' => false]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
try {
$loginToken = $abonnentService->login((string) $data['username'], (string) $data['password']);
// set cookie and redirect to referer set on login form or to self if not set!
$httpReferer = $data['http_referer'] ?: $this->generateUrl('fe.abo_login');
$response = new RedirectResponse($httpReferer);
$response->headers->setCookie(new Cookie($abonnentService::TOKEN_COOKIE_NAME, $loginToken->getToken(), $loginToken->getExpireAt(), '/', null, true, true));
return $response;
// end
} catch (AbonnentLoginInactiveException) {
$loginErrorMsg = 'Login ist abgelaufen!';
} catch (AbonnentLoginFailedException) {
$loginErrorMsg = 'Login Daten nicht korrekt!';
}
}
return $this->render('frontend/abo/login.html.twig', [
'abonnent' => $abonnent,
'form' => $form->createView(),
'login_error_msg' => $loginErrorMsg,
]);
}
#[Route(path: '/logout{success}', name: 'fe.abo_logout', defaults: ['success' => ''])]
#[Cache(maxage: 0, smaxage: 0)]
public function logout(Request $request, AbonnentService $abonnentService): RedirectResponse|Response
{
$success = $request->query->get('success');
if (!$success && $abonnentService->logout($request) === true) {
// empty auth cookie
$response = new RedirectResponse($this->generateUrl('fe.abo_logout', ['success' => '-success']));
$response->headers->setCookie(new Cookie($abonnentService::TOKEN_COOKIE_NAME, ''));
return $response;
}
return $this->render('frontend/abo/logout.html.twig');
// return $this->redirectToRoute('fe.home');
}
#[Route(path: '/change-password', name: 'fe.abo_change_password')]
#[Cache(maxage: 0, smaxage: 0)]
public function changePassword(Request $request, EntityManagerInterface $entityManager, UserPasswordHasherInterface $userPasswordHasher, AbonnentService $abonnentService): RedirectResponse|Response
{
$user = $abonnentService->authenticate($request);
if(!$user){
$this->addFlash('notice', 'Bitte zuerst anmelden!');
return $this->redirectToRoute('fe.abo_login');
}
$form = $this->createForm(ChangePasswordFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Encode(hash) the plain password, and set it.
$encodedPassword = $userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
);
$user
->setPassword($encodedPassword)
->setLastPasswordChange(new \DateTime())
;
$entityManager->flush();
$this->addFlash('notice', 'Das Passwort wurde erfolgreich gesetzt!');
return $this->redirectToRoute('fe.abo_login');
}
return $this->render('frontend/abo/reset_password/reset.html.twig', [
'resetForm' => $form->createView(),
'title' => 'Neues Kennwort setzen',
]);
}
}