<?php
namespace JF\JuridicusBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use JMS\SecurityExtraBundle\Annotation\Secure;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use JF\JuridicusBundle\Form\AboType;
use JF\JuridicusBundle\Entity\Abo;
use JF\JuridicusBundle\Entity\AboZeitraum;
use JF\JuridicusBundle\Form\LockaboType;
use JF\JuridicusBundle\Services\AboManager;
use JF\JuridicusBundle\Entity\ProtokollInfothekPruefer;
use JF\JuridicusBundle\Repository\PdfZeitschriftPRRepository;
use Doctrine\ORM\EntityManagerInterface;
/**
* @Route("/PR")
*/
class PRController extends BaseController
{
/**
* @DI\Inject("jf_juridicus.pr_repository")
*
* @var PdfZeitschriftPRRepository
*/
protected $repo;
/**
* @DI\Inject("jf_juridicus.abo_manager")
*
* @var AboManager
*/
protected $aboManager;
/**
* @Route("/", name="pr_main")
*/
public function showAction()
{
/* Felix Baltes, Zeitschriften entfernen, 2017-06 pr_konzept */
return $this->redirect($this->generateUrl('pr_archiv'));
}
/**
* @Route("/konzept.html", name="pr_konzept")
*/
public function showKonzeptAction()
{
/* Felix Baltes, Zeitschriften entfernen, 2017-06 */
return $this->redirect($this->generateUrl('pr_main'));
/*
$response = $this->render('JFJuridicusBundle:PR:konzept.html.twig');
$response->setETag(md5($response->getContent()));
$response->isNotModified($this->getRequest());
return $response;
*/
}
/**
* @Route("/bezugspreise.html", name="pr_bezugspreise")
*/
public function showBezugspreiseAction()
{
/* Felix Baltes, Zeitschriften entfernen, 2017-06 */
return $this->redirect($this->generateUrl('pr_main'));
/*
$response = $this->render('JFJuridicusBundle:PR:bezugspreise.html.twig');
$response->setETag(md5($response->getContent()));
$response->isNotModified($this->getRequest());
return $response;
*/
}
/**
* @Route("/leseprobe", name="pr_leseprobe")
*/
public function showLeseprobeAction()
{
/* Felix Baltes, Zeitschriften entfernen, 2017-06 */
return $this->redirect($this->generateUrl('pr_main'));
/*
$response = $this->render('JFJuridicusBundle:PR:leseprobe.html.twig');
$response->setETag(md5($response->getContent()));
$response->isNotModified($this->getRequest());
return $response;
*/
}
/**
* @Route("/Leseprobe_PR.pdf", name="pr_leseprobe_pdf")
*/
public function leseprobePdfAction()
{
// letzten PR aus dem Archiv holen
$pdf = $this->repo->findLastBefore(new \DateTime('today -6 months'));
if (!$pdf) {
throw $this->createNotFoundException();
}
return $pdf->createResponse(true);
}
/**
* bestellscheinAction
* URL existiert nicht mehr
*
* @Route("/Bestellung", name="pr_bestellschein")
*/
public function bestellscheinAction()
{
return new Response('', 410);
$datumStartOnline = $this->getDefaultStartDatumOnline();
$datumStartPrint = $this->getDefaultStartDatumPrint();
$abo = new Abo();
$abo->setDatumStart($datumStartOnline);
$form = $this->createForm(AboType::class, $abo, array(
'em' => $this->getDoctrine()->getManager()
));
return $this->render('@JFJuridicusBundle/PR/bestellschein.html.twig', array(
'form' => $form->createView(),
'default_date_online' => $datumStartOnline,
'default_date_print' => $datumStartPrint,
));
}
/**
* @Method({"POST"})
*/
public function createLockBestellungAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
return new Response('', 410);
$em = $this->getDoctrine()->getManager();
$kunde = $this->getKunde();
$datumStart = $this->getDefaultStartDatumOnline();
$abo = new Abo();
$abo
->setZeitschrift('PR')
->setOnline(true)
->setTyp(Abo::ABO_LOCKABO_6)
->setIp($request->getClientIp())
->setDatumStart($datumStart)
->setKunde($kunde)
->setVorname($kunde->getVorname())
->setNachname($kunde->getNachname())
->setStrasse($kunde->getStrasse())
->setHausnummer($kunde->getHausnummer())
->setPlz($kunde->getPlz())
->setOrt($kunde->getOrt())
->setGeburtsdatum($kunde->getGeburtsdatum())
->setEmail($kunde->getEmail())
->setTelefon($kunde->getHandynummer())
;
$form = $this->createForm(LockaboType::class, array(), array());
$form->handleRequest($request); //changedFrom bind
if ($form->isValid()) {
$em->getConnection()->beginTransaction();
// speichern, um eine Abo-Id zu gewährleisten
$abo->calculateDatumEnde();
$em->persist($abo);
$em->flush();
$this->get('session')->set('abo_id', $abo->getId());
// Lockabos sind immer kostenlose Online-Abos
$aboZeitraum = $this->aboManager->createRestrictedAboZeitraum($abo);
$aboAusgaben = $this->aboManager->createAboAusgaben($aboZeitraum);
$this->aboManager->sendAboZeitschriften($aboZeitraum);
$em->flush();
$em->getConnection()->commit();
return $this->redirect($this->generateUrl('pr_bestellung_create_success'));
}
return $this->redirect($this->generateUrl('homepage'));
}
/**
* @Route("/Bestellung/create", name="pr_bestellung_create")
* @Method({"POST"})
*/
public function createBestellungAction(Request $request)
{
return new Response('', 410);
$em = $this->getDoctrine()->getManager();
$abo = new Abo();
$abo
->setZeitschrift('PR')
->setIp($request->getClientIp())
;
$form = $this->createForm(AboType::class, $abo, array('em' => $em));
$form->handleRequest($request); //changedFrom bind
if ($form->isValid()) {
$em->getConnection()->beginTransaction();
$abo->calculateDatumEnde();
// speichern, um eine Abo-Id zu gewährleisten
$em->persist($abo);
$em->flush();
$this->get('session')->set('abo_id', $abo->getId());
if ($abo->getOnline()) {
// ersten Abo-Zeitraum hinzufügen
$aboZeitraum = $this->aboManager->createRestrictedAboZeitraum($abo);
$aboAusgaben = $this->aboManager->createAboAusgaben($aboZeitraum);
$this->aboManager->sendAboZeitschriften($aboZeitraum);
if ($abo->getTyp() === Abo::ABO_FULL) {
// bei Vollabos ggfs auch zweiten Abo-Zeitraum, falls Jahresgrenze überschritten wird
$januaryFirst = new \DateTime('first day of january this year 00:00:00');
if ($abo->getDatumStart() < $januaryFirst) {
// zweiten Abo-Zeitraum hinzufügen
$decemberLast = new \DateTime('last day of december this year 00:00:00');
$aboZeitraum2 = $this->aboManager->createRestrictedAboZeitraum($abo, $januaryFirst, $decemberLast);
$aboAusgaben2 = $this->aboManager->createAboAusgaben($aboZeitraum2);
$this->aboManager->sendAboZeitschriften($aboZeitraum2);
}
}
}
$em->flush();
$em->getConnection()->commit();
return $this->redirect($this->generateUrl('pr_bestellung_create_success'));
}
return $this->render('@JFJuridicusBundle/PR/bestellschein.html.twig', array(
'form' => $form->createView(),
'default_date_online' => $this->getDefaultStartDatumOnline(),
'default_date_print' => $this->getDefaultStartDatumPrint(),
));
}
/**
* @Route("/Bestellung/success", name="pr_bestellung_create_success")
*/
public function createBestellungSuccessAction()
{
$session = $this->get('session');
$abo_id = $session->get('abo_id');
$session->remove('abo_id');
if (!$abo_id) {
return $this->redirect($this->generateUrl('homepage'));
}
$em = $this->getDoctrine()->getManager();
$abo = $em->getRepository('JFJuridicusBundle:Abo')->find($abo_id);
$content = $this->renderView(
'JFJuridicusBundle:Abo:show.html.twig',
array('abo' => $abo)
);
$mailer = $this->get('jf_juridicus.system_mailer');
$message = \Swift_Message::newInstance()
->setSubject('Systemnachricht: Neues PR Abo')
->setBody($content, 'text/html')
;
$mailer->send($message);
return $this->render('@JFJuridicusBundle/PR/bestellschein_success.html.twig', array('abo' => $abo));
}
/**
* bestellscheinPdfAction
* URL existiert nicht mehr => umleiten
*
* @Route("/PR-Bestellung.pdf", name="pr_bestellschein_pdf")
*/
public function bestellscheinPdfAction()
{
return new Response('', 410);
}
/**
* archivAction
*
* Stellt alle Ausgaben zur Verfügung, die älter als 6 Monate ist.
*
* @Route("/archiv", name="pr_archiv")
*/
public function archivAction(PdfZeitschriftPRRepository $repo)
{
$entitiesByYear = $repo->findArchivSince(new \DateTime('-6 months'));
return $this->render('@JFJuridicusBundle/PR/archiv.html.twig', array('entities_by_year' => $entitiesByYear));
}
/**
* @Route("/{date}.pdf", name="pr_download")
* @ParamConverter("date", options={"format": "d-m-Y"})
*/
public function downloadAction(\DateTime $date, PdfZeitschriftPRRepository $repo)
{
$pdf = $repo->findOneBy(array('released_at' => $date));
if (!$pdf) {
throw $this->createNotFoundException('Die Zeitschrift wurde nicht gefunden.');
}
// check Download Permissions
$restrict_date = new \DateTime('today -6 months');
if ($date < $restrict_date) {
// Archiv-Zeitschriften (älter als 6 Monate) immer erlaubt
return $pdf->createResponse(true);
} else {
throw $this->createAccessDeniedException();
}
}
/**
* @return \DateTime
*/
protected function getDefaultStartDatumOnline(PdfZeitschriftPRRepository $repo)
{
$datumLastRelease = $repo->findLast()->getReleasedAt();
$datumStart = new \DateTime('first day of this month 00:00:00');
if ($datumLastRelease < $datumStart) {
$datumStart->modify('-1 month');
}
return $datumStart;
}
/**
* @return \DateTime
*/
protected function getDefaultStartDatumPrint()
{
$datumStart = new \DateTime('first day of next month 00:00:00');
return $datumStart;
}
}