src/Controller/Frontend/AboController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Application\Abo\AboService;
  4. use App\Application\Abonnent\AbonnentService;
  5. use App\Application\Content\ContentBackendData;
  6. use App\Application\Content\ContentFrontendData;
  7. use App\Application\Content\ContentService;
  8. use App\Entity\Abo\AboBestellung;
  9. use App\Entity\Abo\AdressMutation;
  10. use App\Entity\Abo\FerienMutation;
  11. use App\Entity\Abonnent\Abonnenttoken;
  12. use App\Entity\Abonnent\Exceptions\AbonnentLoginFailedException;
  13. use App\Entity\Abonnent\Exceptions\AbonnentLoginInactiveException;
  14. use App\Form\Abo\AboBestellungType;
  15. use App\Form\Abo\AbonnentLoginType;
  16. use App\Form\Abo\AdressMutationType;
  17. use App\Form\Abo\ChangePasswordFormType;
  18. use App\Form\Abo\FerienMutationType;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Cookie;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. #[Route(path: '/abo')]
  29. #[Cache(expires: '+10 minutes')]
  30. class AboController extends AbstractController
  31. {
  32. /**
  33. * Hinweis: Route soll mit ../abo enden (NICHT .../abo/) gewünscht gemäss UW-App.
  34. */
  35. #[Route(path: '', name: 'fe.abo')]
  36. public function abo(ContentService $contentService): Response
  37. {
  38. $content = $contentService->getContentByCode('ABOSERVICE', new ContentFrontendData(), true);
  39. return $this->render('frontend/abo/abo.html.twig', [
  40. 'content' => $content,
  41. ]);
  42. }
  43. #[Route(path: '/mutation/ferienumleitung', name: 'fe.abo_ferienumleitung', defaults: ['type' => 'umleitung'])]
  44. #[Route(path: '/mutation/ferienabounterbruch', name: 'fe.abo_ferienabounterbruch', defaults: ['type' => 'unterbruch'])]
  45. #[Route(path: '/mutation/ferienepaper', name: 'fe.abo_ferienepaper', defaults: ['type' => 'epaper'])]
  46. public function ferienMutation(Request $request, $type, AboService $aboService, ContentService $contentService): Response
  47. {
  48. $mailSendMessage = '';
  49. switch ($type) {
  50. case 'umleitung':
  51. $content = $contentService->getContentByCode('ABO_MUTATION_FERIEN_UMLEITUNG', new ContentBackendData(), true);
  52. $mutation = FerienMutation::newUmleitung();
  53. break;
  54. case 'unterbruch':
  55. $content = $contentService->getContentByCode('ABO_MUTATION_FERIEN_UNTERBRUCH', new ContentBackendData(), true);
  56. $mutation = FerienMutation::newUnterbruch();
  57. break;
  58. // case 'epaper':
  59. // $content = $contentService->getContentByCode('ABO_MUTATION_FERIEN_EPAPER', new ContentBackendData(), true);
  60. // $mutation = FerienMutation::newEPaper();
  61. // break;
  62. default:
  63. throw $this->createNotFoundException();
  64. }
  65. $form = $this->createForm(FerienMutationType::class, $mutation, ['csrf_protection' => false]);
  66. $form->handleRequest($request);
  67. if ($form->isSubmitted() && $form->isValid()) {
  68. $aboService->mailFerienMutationToAboservice($mutation);
  69. $mailSendMessage = $mutation->orderSendMessage();
  70. }
  71. return $this->render('frontend/abo/ferienmutation.html.twig', [
  72. 'content' => $content,
  73. 'mailSendMessage' => $mailSendMessage,
  74. 'mutation' => $mutation,
  75. 'form' => $form->createView(),
  76. ]);
  77. }
  78. #[Route(path: '/mutation/adresse', name: 'fe.abo_adressmutation')]
  79. public function adressMutation(Request $request, AboService $aboService, ContentService $contentService): Response
  80. {
  81. $mailSendMessage = '';
  82. $content = $contentService->getContentByCode('ABO_MUTATION_ADRESSE', new ContentBackendData(), true);
  83. $mutation = AdressMutation::createMutation();
  84. $form = $this->createForm(AdressMutationType::class, $mutation, ['csrf_protection' => false]);
  85. $form->handleRequest($request);
  86. if ($form->isSubmitted() && $form->isValid()) {
  87. $aboService->mailAdressMutationToAboservice($mutation);
  88. $mailSendMessage = $mutation->orderSendMessage();
  89. }
  90. return $this->render('frontend/abo/adressmutation.html.twig', [
  91. 'content' => $content,
  92. 'mailSendMessage' => $mailSendMessage,
  93. 'mutation' => $mutation,
  94. 'form' => $form->createView(),
  95. ]);
  96. }
  97. #[Route(path: '/bestellung/print', name: 'fe.abo_order_print', defaults: ['type' => 'print'])]
  98. #[Route(path: '/bestellung/print/danke', name: 'fe.abo_order_print_danke', defaults: ['type' => 'print'])]
  99. #[Route(path: '/bestellung/epaper', name: 'fe.abo_order_epaper', defaults: ['type' => 'epaper'])]
  100. #[Route(path: '/bestellung/epaper/danke', name: 'fe.abo_order_epaper_danke', defaults: ['type' => 'epaper'])]
  101. #[Route(path: '/bestellung/kombi', name: 'fe.abo_order_kombi', defaults: ['type' => 'kombi'])]
  102. #[Route(path: '/bestellung/kombi/danke', name: 'fe.abo_order_kombi_danke', defaults: ['type' => 'kombi'])]
  103. #[Route(path: '/bestellung/epaper-zusatz', name: 'fe.abo_order_epaperzusatz', defaults: ['type' => 'epaper-zusatz'])]
  104. #[Route(path: '/bestellung/epaper-zusatz/danke', name: 'fe.abo_order_epaperzusatz_danke', defaults: ['type' => 'epaper-zusatz'])]
  105. public function aboBestellung(Request $request, $type, AboService $aboService, ContentService $contentService): RedirectResponse|Response
  106. {
  107. $mailSendMessage = '';
  108. switch ($type) {
  109. case 'print':
  110. $content = $contentService->getContentByCode('ABO_NEU_PRINT', new ContentBackendData(), true);
  111. $options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
  112. $bestellung = AboBestellung::newPrintAbo($options);
  113. $routeDanke = 'fe.abo_order_print_danke';
  114. break;
  115. case 'epaper':
  116. $content = $contentService->getContentByCode('ABO_NEU_EPAPER', new ContentBackendData(), true);
  117. $options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
  118. $bestellung = AboBestellung::newEPaperAbo($options);
  119. $routeDanke = 'fe.abo_order_epaper_danke';
  120. break;
  121. case 'kombi':
  122. $content = $contentService->getContentByCode('ABO_NEU_KOMBI', new ContentBackendData(), true);
  123. $options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
  124. $bestellung = AboBestellung::newKombiAbo($options);
  125. $routeDanke = 'fe.abo_order_kombi_danke';
  126. break;
  127. case 'epaper-zusatz':
  128. $content = $contentService->getContentByCode('ABO_NEU_EPAPER_ZUSATZ', new ContentBackendData(), true);
  129. $options = AboBestellung::transformTextconfigToOptionsArray($content['content_text']);
  130. $bestellung = AboBestellung::newAddEPaperAbo($options);
  131. $routeDanke = 'fe.abo_order_epaperzusatz_danke';
  132. break;
  133. default:
  134. throw $this->createNotFoundException();
  135. }
  136. $form = $this->createForm(AboBestellungType::class, $bestellung, ['csrf_protection' => false]);
  137. $form->handleRequest($request);
  138. if ($form->isSubmitted() && $form->isValid()) {
  139. $aboService->mailBestellungToAboservice($bestellung);
  140. return $this->redirectToRoute($routeDanke);
  141. }
  142. // Mail Message
  143. if (str_ends_with((string) $request->get('_route'), '_danke')) {
  144. $mailSendMessage = $bestellung->orderSendMessage();
  145. }
  146. return $this->render('frontend/abo/abobestellung.html.twig', [
  147. 'content' => $content,
  148. 'mailSendMessage' => $mailSendMessage,
  149. 'bestellung' => $bestellung,
  150. 'form' => $form->createView(),
  151. ]);
  152. }
  153. #[Route(path: '/login', name: 'fe.abo_login')]
  154. #[Cache(maxage: 0, smaxage: 0)]
  155. public function login(Request $request, AbonnentService $abonnentService): RedirectResponse|Response
  156. {
  157. /**
  158. * @var Abonnenttoken $loginToken
  159. */
  160. $loginToken = null;
  161. $abonnent = $abonnentService->authenticate($request);
  162. $loginErrorMsg = '';
  163. $data = [
  164. 'username' => '',
  165. 'password' => '',
  166. 'http_referer' => '', // hier leer lassen, sonst evlt. schleifenproblem mit logout
  167. ];
  168. $form = $this->createForm(AbonnentLoginType::class, $data, ['csrf_protection' => false]);
  169. $form->handleRequest($request);
  170. if ($form->isSubmitted() && $form->isValid()) {
  171. $data = $form->getData();
  172. try {
  173. $loginToken = $abonnentService->login((string) $data['username'], (string) $data['password']);
  174. // set cookie and redirect to referer set on login form or to self if not set!
  175. $httpReferer = $data['http_referer'] ?: $this->generateUrl('fe.abo_login');
  176. $response = new RedirectResponse($httpReferer);
  177. $response->headers->setCookie(new Cookie($abonnentService::TOKEN_COOKIE_NAME, $loginToken->getToken(), $loginToken->getExpireAt(), '/', null, true, true));
  178. return $response;
  179. // end
  180. } catch (AbonnentLoginInactiveException) {
  181. $loginErrorMsg = 'Login ist abgelaufen!';
  182. } catch (AbonnentLoginFailedException) {
  183. $loginErrorMsg = 'Login Daten nicht korrekt!';
  184. }
  185. }
  186. return $this->render('frontend/abo/login.html.twig', [
  187. 'abonnent' => $abonnent,
  188. 'form' => $form->createView(),
  189. 'login_error_msg' => $loginErrorMsg,
  190. ]);
  191. }
  192. #[Route(path: '/logout{success}', name: 'fe.abo_logout', defaults: ['success' => ''])]
  193. #[Cache(maxage: 0, smaxage: 0)]
  194. public function logout(Request $request, AbonnentService $abonnentService): RedirectResponse|Response
  195. {
  196. $success = $request->query->get('success');
  197. if (!$success && $abonnentService->logout($request) === true) {
  198. // empty auth cookie
  199. $response = new RedirectResponse($this->generateUrl('fe.abo_logout', ['success' => '-success']));
  200. $response->headers->setCookie(new Cookie($abonnentService::TOKEN_COOKIE_NAME, ''));
  201. return $response;
  202. }
  203. return $this->render('frontend/abo/logout.html.twig');
  204. // return $this->redirectToRoute('fe.home');
  205. }
  206. #[Route(path: '/change-password', name: 'fe.abo_change_password')]
  207. #[Cache(maxage: 0, smaxage: 0)]
  208. public function changePassword(Request $request, EntityManagerInterface $entityManager, UserPasswordHasherInterface $userPasswordHasher, AbonnentService $abonnentService): RedirectResponse|Response
  209. {
  210. $user = $abonnentService->authenticate($request);
  211. if(!$user){
  212. $this->addFlash('notice', 'Bitte zuerst anmelden!');
  213. return $this->redirectToRoute('fe.abo_login');
  214. }
  215. $form = $this->createForm(ChangePasswordFormType::class);
  216. $form->handleRequest($request);
  217. if ($form->isSubmitted() && $form->isValid()) {
  218. // Encode(hash) the plain password, and set it.
  219. $encodedPassword = $userPasswordHasher->hashPassword(
  220. $user,
  221. $form->get('plainPassword')->getData()
  222. );
  223. $user
  224. ->setPassword($encodedPassword)
  225. ->setLastPasswordChange(new \DateTime())
  226. ;
  227. $entityManager->flush();
  228. $this->addFlash('notice', 'Das Passwort wurde erfolgreich gesetzt!');
  229. return $this->redirectToRoute('fe.abo_login');
  230. }
  231. return $this->render('frontend/abo/reset_password/reset.html.twig', [
  232. 'resetForm' => $form->createView(),
  233. 'title' => 'Neues Kennwort setzen',
  234. ]);
  235. }
  236. }