src/JuridicusBundle/Controller/PRController.php line 319

Open in your IDE?
  1. <?php
  2. namespace JF\JuridicusBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  6. use JMS\SecurityExtraBundle\Annotation\Secure;
  7. use JMS\DiExtraBundle\Annotation as DI;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use JF\JuridicusBundle\Form\AboType;
  11. use JF\JuridicusBundle\Entity\Abo;
  12. use JF\JuridicusBundle\Entity\AboZeitraum;
  13. use JF\JuridicusBundle\Form\LockaboType;
  14. use JF\JuridicusBundle\Services\AboManager;
  15. use JF\JuridicusBundle\Entity\ProtokollInfothekPruefer;
  16. use JF\JuridicusBundle\Repository\PdfZeitschriftPRRepository;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. /**
  19. * @Route("/PR")
  20. */
  21. class PRController extends BaseController
  22. {
  23. /**
  24. * @DI\Inject("jf_juridicus.pr_repository")
  25. *
  26. * @var PdfZeitschriftPRRepository
  27. */
  28. protected $repo;
  29. /**
  30. * @DI\Inject("jf_juridicus.abo_manager")
  31. *
  32. * @var AboManager
  33. */
  34. protected $aboManager;
  35. /**
  36. * @Route("/", name="pr_main")
  37. */
  38. public function showAction()
  39. {
  40. /* Felix Baltes, Zeitschriften entfernen, 2017-06 pr_konzept */
  41. return $this->redirect($this->generateUrl('pr_archiv'));
  42. }
  43. /**
  44. * @Route("/konzept.html", name="pr_konzept")
  45. */
  46. public function showKonzeptAction()
  47. {
  48. /* Felix Baltes, Zeitschriften entfernen, 2017-06 */
  49. return $this->redirect($this->generateUrl('pr_main'));
  50. /*
  51. $response = $this->render('JFJuridicusBundle:PR:konzept.html.twig');
  52. $response->setETag(md5($response->getContent()));
  53. $response->isNotModified($this->getRequest());
  54. return $response;
  55. */
  56. }
  57. /**
  58. * @Route("/bezugspreise.html", name="pr_bezugspreise")
  59. */
  60. public function showBezugspreiseAction()
  61. {
  62. /* Felix Baltes, Zeitschriften entfernen, 2017-06 */
  63. return $this->redirect($this->generateUrl('pr_main'));
  64. /*
  65. $response = $this->render('JFJuridicusBundle:PR:bezugspreise.html.twig');
  66. $response->setETag(md5($response->getContent()));
  67. $response->isNotModified($this->getRequest());
  68. return $response;
  69. */
  70. }
  71. /**
  72. * @Route("/leseprobe", name="pr_leseprobe")
  73. */
  74. public function showLeseprobeAction()
  75. {
  76. /* Felix Baltes, Zeitschriften entfernen, 2017-06 */
  77. return $this->redirect($this->generateUrl('pr_main'));
  78. /*
  79. $response = $this->render('JFJuridicusBundle:PR:leseprobe.html.twig');
  80. $response->setETag(md5($response->getContent()));
  81. $response->isNotModified($this->getRequest());
  82. return $response;
  83. */
  84. }
  85. /**
  86. * @Route("/Leseprobe_PR.pdf", name="pr_leseprobe_pdf")
  87. */
  88. public function leseprobePdfAction()
  89. {
  90. // letzten PR aus dem Archiv holen
  91. $pdf = $this->repo->findLastBefore(new \DateTime('today -6 months'));
  92. if (!$pdf) {
  93. throw $this->createNotFoundException();
  94. }
  95. return $pdf->createResponse(true);
  96. }
  97. /**
  98. * bestellscheinAction
  99. * URL existiert nicht mehr
  100. *
  101. * @Route("/Bestellung", name="pr_bestellschein")
  102. */
  103. public function bestellscheinAction()
  104. {
  105. return new Response('', 410);
  106. $datumStartOnline = $this->getDefaultStartDatumOnline();
  107. $datumStartPrint = $this->getDefaultStartDatumPrint();
  108. $abo = new Abo();
  109. $abo->setDatumStart($datumStartOnline);
  110. $form = $this->createForm(AboType::class, $abo, array(
  111. 'em' => $this->getDoctrine()->getManager()
  112. ));
  113. return $this->render('@JFJuridicusBundle/PR/bestellschein.html.twig', array(
  114. 'form' => $form->createView(),
  115. 'default_date_online' => $datumStartOnline,
  116. 'default_date_print' => $datumStartPrint,
  117. ));
  118. }
  119. /**
  120. * @Method({"POST"})
  121. */
  122. public function createLockBestellungAction(Request $request)
  123. {
  124. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  125. return new Response('', 410);
  126. $em = $this->getDoctrine()->getManager();
  127. $kunde = $this->getKunde();
  128. $datumStart = $this->getDefaultStartDatumOnline();
  129. $abo = new Abo();
  130. $abo
  131. ->setZeitschrift('PR')
  132. ->setOnline(true)
  133. ->setTyp(Abo::ABO_LOCKABO_6)
  134. ->setIp($request->getClientIp())
  135. ->setDatumStart($datumStart)
  136. ->setKunde($kunde)
  137. ->setVorname($kunde->getVorname())
  138. ->setNachname($kunde->getNachname())
  139. ->setStrasse($kunde->getStrasse())
  140. ->setHausnummer($kunde->getHausnummer())
  141. ->setPlz($kunde->getPlz())
  142. ->setOrt($kunde->getOrt())
  143. ->setGeburtsdatum($kunde->getGeburtsdatum())
  144. ->setEmail($kunde->getEmail())
  145. ->setTelefon($kunde->getHandynummer())
  146. ;
  147. $form = $this->createForm(LockaboType::class, array(), array());
  148. $form->handleRequest($request); //changedFrom bind
  149. if ($form->isValid()) {
  150. $em->getConnection()->beginTransaction();
  151. // speichern, um eine Abo-Id zu gewährleisten
  152. $abo->calculateDatumEnde();
  153. $em->persist($abo);
  154. $em->flush();
  155. $this->get('session')->set('abo_id', $abo->getId());
  156. // Lockabos sind immer kostenlose Online-Abos
  157. $aboZeitraum = $this->aboManager->createRestrictedAboZeitraum($abo);
  158. $aboAusgaben = $this->aboManager->createAboAusgaben($aboZeitraum);
  159. $this->aboManager->sendAboZeitschriften($aboZeitraum);
  160. $em->flush();
  161. $em->getConnection()->commit();
  162. return $this->redirect($this->generateUrl('pr_bestellung_create_success'));
  163. }
  164. return $this->redirect($this->generateUrl('homepage'));
  165. }
  166. /**
  167. * @Route("/Bestellung/create", name="pr_bestellung_create")
  168. * @Method({"POST"})
  169. */
  170. public function createBestellungAction(Request $request)
  171. {
  172. return new Response('', 410);
  173. $em = $this->getDoctrine()->getManager();
  174. $abo = new Abo();
  175. $abo
  176. ->setZeitschrift('PR')
  177. ->setIp($request->getClientIp())
  178. ;
  179. $form = $this->createForm(AboType::class, $abo, array('em' => $em));
  180. $form->handleRequest($request); //changedFrom bind
  181. if ($form->isValid()) {
  182. $em->getConnection()->beginTransaction();
  183. $abo->calculateDatumEnde();
  184. // speichern, um eine Abo-Id zu gewährleisten
  185. $em->persist($abo);
  186. $em->flush();
  187. $this->get('session')->set('abo_id', $abo->getId());
  188. if ($abo->getOnline()) {
  189. // ersten Abo-Zeitraum hinzufügen
  190. $aboZeitraum = $this->aboManager->createRestrictedAboZeitraum($abo);
  191. $aboAusgaben = $this->aboManager->createAboAusgaben($aboZeitraum);
  192. $this->aboManager->sendAboZeitschriften($aboZeitraum);
  193. if ($abo->getTyp() === Abo::ABO_FULL) {
  194. // bei Vollabos ggfs auch zweiten Abo-Zeitraum, falls Jahresgrenze überschritten wird
  195. $januaryFirst = new \DateTime('first day of january this year 00:00:00');
  196. if ($abo->getDatumStart() < $januaryFirst) {
  197. // zweiten Abo-Zeitraum hinzufügen
  198. $decemberLast = new \DateTime('last day of december this year 00:00:00');
  199. $aboZeitraum2 = $this->aboManager->createRestrictedAboZeitraum($abo, $januaryFirst, $decemberLast);
  200. $aboAusgaben2 = $this->aboManager->createAboAusgaben($aboZeitraum2);
  201. $this->aboManager->sendAboZeitschriften($aboZeitraum2);
  202. }
  203. }
  204. }
  205. $em->flush();
  206. $em->getConnection()->commit();
  207. return $this->redirect($this->generateUrl('pr_bestellung_create_success'));
  208. }
  209. return $this->render('@JFJuridicusBundle/PR/bestellschein.html.twig', array(
  210. 'form' => $form->createView(),
  211. 'default_date_online' => $this->getDefaultStartDatumOnline(),
  212. 'default_date_print' => $this->getDefaultStartDatumPrint(),
  213. ));
  214. }
  215. /**
  216. * @Route("/Bestellung/success", name="pr_bestellung_create_success")
  217. */
  218. public function createBestellungSuccessAction()
  219. {
  220. $session = $this->get('session');
  221. $abo_id = $session->get('abo_id');
  222. $session->remove('abo_id');
  223. if (!$abo_id) {
  224. return $this->redirect($this->generateUrl('homepage'));
  225. }
  226. $em = $this->getDoctrine()->getManager();
  227. $abo = $em->getRepository('JFJuridicusBundle:Abo')->find($abo_id);
  228. $content = $this->renderView(
  229. 'JFJuridicusBundle:Abo:show.html.twig',
  230. array('abo' => $abo)
  231. );
  232. $mailer = $this->get('jf_juridicus.system_mailer');
  233. $message = \Swift_Message::newInstance()
  234. ->setSubject('Systemnachricht: Neues PR Abo')
  235. ->setBody($content, 'text/html')
  236. ;
  237. $mailer->send($message);
  238. return $this->render('@JFJuridicusBundle/PR/bestellschein_success.html.twig', array('abo' => $abo));
  239. }
  240. /**
  241. * bestellscheinPdfAction
  242. * URL existiert nicht mehr => umleiten
  243. *
  244. * @Route("/PR-Bestellung.pdf", name="pr_bestellschein_pdf")
  245. */
  246. public function bestellscheinPdfAction()
  247. {
  248. return new Response('', 410);
  249. }
  250. /**
  251. * archivAction
  252. *
  253. * Stellt alle Ausgaben zur Verfügung, die älter als 6 Monate ist.
  254. *
  255. * @Route("/archiv", name="pr_archiv")
  256. */
  257. public function archivAction(PdfZeitschriftPRRepository $repo)
  258. {
  259. $entitiesByYear = $repo->findArchivSince(new \DateTime('-6 months'));
  260. return $this->render('@JFJuridicusBundle/PR/archiv.html.twig', array('entities_by_year' => $entitiesByYear));
  261. }
  262. /**
  263. * @Route("/{date}.pdf", name="pr_download")
  264. * @ParamConverter("date", options={"format": "d-m-Y"})
  265. */
  266. public function downloadAction(\DateTime $date, PdfZeitschriftPRRepository $repo)
  267. {
  268. $pdf = $repo->findOneBy(array('released_at' => $date));
  269. if (!$pdf) {
  270. throw $this->createNotFoundException('Die Zeitschrift wurde nicht gefunden.');
  271. }
  272. // check Download Permissions
  273. $restrict_date = new \DateTime('today -6 months');
  274. if ($date < $restrict_date) {
  275. // Archiv-Zeitschriften (älter als 6 Monate) immer erlaubt
  276. return $pdf->createResponse(true);
  277. } else {
  278. throw $this->createAccessDeniedException();
  279. }
  280. }
  281. /**
  282. * @return \DateTime
  283. */
  284. protected function getDefaultStartDatumOnline(PdfZeitschriftPRRepository $repo)
  285. {
  286. $datumLastRelease = $repo->findLast()->getReleasedAt();
  287. $datumStart = new \DateTime('first day of this month 00:00:00');
  288. if ($datumLastRelease < $datumStart) {
  289. $datumStart->modify('-1 month');
  290. }
  291. return $datumStart;
  292. }
  293. /**
  294. * @return \DateTime
  295. */
  296. protected function getDefaultStartDatumPrint()
  297. {
  298. $datumStart = new \DateTime('first day of next month 00:00:00');
  299. return $datumStart;
  300. }
  301. }