<?php
namespace JF\JuridicusBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JF\JuridicusBundle\Entity\Pruefung;
use JF\JuridicusBundle\Entity\Pruefer;
use JF\JuridicusBundle\Entity\Email;
use JF\JuridicusBundle\Entity\Kunde;
use JF\JuridicusBundle\Entity\ProtokollInfothekPruefer;
use JF\JuridicusBundle\Entity\ShoutboxMessage;
use JF\JuridicusBundle\Form\PruefungType;
use JF\JuridicusBundle\Form\ShoutboxMessageType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/**
* Pruefung controller.
*/
class PruefungController extends AbstractController
{
/**
* Homepage
*
* @Route("/", name="homepage")
*/
public function homeAction()
{
$em = $this->getDoctrine()->getManager();
$anzahl_pruefer = $em->getRepository(Pruefer::class)->count();
$anzahl_protokolle = $em->getRepository(Pruefer::class)->countProtokolle();
$statistik1 = $em->getRepository(ProtokollInfothekPruefer::class)->filterParagraphsByExamen(1);
$statistik2 = $em->getRepository(ProtokollInfothekPruefer::class)->filterParagraphsByExamen(2);
$filter_form = $this->createParagraphFilterForm();
$entities1 = $em->getRepository(Pruefung::class)->findNewest(4,Pruefung::TYP_EXAMEN_1);
$entities_indexed_1 = array();
foreach ($entities1 as $entity) {
$entities_indexed_1[] = $entity;
}
$entities2 = $em->getRepository(Pruefung::class)->findNewest(4,Pruefung::TYP_EXAMEN_2);
$entities_indexed_2 = array();
foreach ($entities2 as $entity) {
$entities_indexed_2[] = $entity;
}
//get recent posts from wordpress
/*
$conn = new mysqli("localhost", "wp_user", "Kqx1_w41", "wordpress");
$sql = "SELECT * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 6";
$result = $conn -> query($sql);
$recentPosts = array();
if($result)
{
while ($row = $result->fetch_assoc()) {
$row['post_content'] = substr(preg_replace('/(\\[.*\\])/','', strip_tags($row['post_content'])),0,556)."...";
$recentPosts[] = $row;
}
}
*/
return $this->render("@JFJuridicusBundle/Pruefung/home.html.twig", array(
'filter_form' => $filter_form->createView(),
'statistik1' => $statistik1,
'statistik2' => $statistik2,
'entities_indexed_1'=> $entities_indexed_1,
'entities_indexed_2'=> $entities_indexed_2,
'anzahl_protokolle' => $anzahl_protokolle,
'anzahl_pruefer' => $anzahl_pruefer,
//'recent_wp_posts' => $recentPosts
));
}
/**
* Lists newest Pruefung entities.
*
* @Route("/rss.xml", name="rss")
*/
public function rssAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository(Pruefung::class)->findNewest(5);
$entities_indexed = array();
foreach ($entities as $entity) {
$datum = $entity->getDatum()->format('d.m.Y');
$entities_indexed[$datum][] = $entity;
}
$timestamp = new \DateTime();
$response = $this->render("@JFJuridicusBundle/Pruefung/rss.xml.twig", array(
'timestamp' => $timestamp,
'entities_indexed' => $entities_indexed,
));
// $response->headers->set('Content-Type', 'application/xml; charset=UTF-8');
return $response;
}
/**
* Lists all Pruefung entities.
*
* @Route("/termine/{page}/pruefer_protokolle_pruefung_jura_examen", name="termine", defaults={"page" = 1})
* @Route("/termine/{page}/pruefer_protokolle_pruefung_jura_examen_{type}", name="termine_with_type", defaults={"page" = 1, "type" = false})
* @Template("@JFJuridicusBundle/Pruefung/index.html.twig")
*/
public function indexAction($page, $type = false)
{
$repo = $this->getDoctrine()->getRepository(Pruefung::class);
$total_termine = $repo->countTermine();
$termine_per_page = $this->getParameter('max_termine_on_page');
$last_page = ceil($total_termine / $termine_per_page);
$previous_page = $page > 1 ? $page - 1 : 1;
$next_page = $page < $last_page ? $page + 1 : $last_page;
$entities = $repo->getTerminePaged($termine_per_page, $page * $termine_per_page - $termine_per_page, $type);
//teile in die zwei Spalten
$entities_indexed_1 = array();
$entities_indexed_2 = array();
$index = 0;
foreach ($entities as $entity) {
if ($index%2 == 0) {
$entities_indexed_1[] = $entity;
} else {
$entities_indexed_2[] = $entity;
}
$index++;
}
return array(
'entities_indexed_1' => $entities_indexed_1,
'entities_indexed_2' => $entities_indexed_2,
'last_page' => $last_page,
'previous_page' => $previous_page,
'current_page' => $page,
'next_page' => $next_page,
'total_termine' => $total_termine
);
}
/**
* Finds and displays a Pruefung entity.
*
* @Route("/pruefung/{id}/show", name="pruefung_show", requirements={"id"="\d+"})
*/
public function showAction(Pruefung $pruefung)
{
return $this->render("@JFJuridicusBundle/Pruefung/show.html.twig", array(
'entity' => $pruefung,
));
}
/**
* Displays a form to create a new Pruefung entity.
*
* @Route("/pruefung/examen/new", name="pruefung_new_for_examen")
*/
public function newForExamenAction()
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$kunde = $user->getKunde();
$pruefungsamt = $kunde->getPruefungsamt();
if (!$pruefungsamt) {
throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
}
$pruefer_typeaheads = $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
$entity = Pruefung::createForPruefungsamt($pruefungsamt, $kunde->getExamen());
$form = $this->createForm(PruefungType::class, $entity, array('em' => $em));
return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
'create_action' => $this->generateUrl('pruefung_create_for_examen'),
'prueferObjs' => $pruefer_typeaheads,
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Displays a form to create a new Pruefung entity of type wahlfach
*
* @Route("/pruefung/wahlfach/new", name="pruefung_new_for_wahlfach")
*/
public function newForWahlfachAction()
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$kunde = $user->getKunde();
$pruefungsamt = $kunde->getPruefungsamt();
if (!$pruefungsamt) {
throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
}
$entity = Pruefung::createForPruefungsamt($pruefungsamt, $kunde->getExamen() + 2);
$form = $this->createForm(PruefungType::class, $entity, array('em' => $em));
$pruefer_typeaheads = $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
'create_action' => $this->generateUrl('pruefung_create_for_wahlfach'),
'entity' => $entity,
'form' => $form->createView(),
'prueferObjs' => $pruefer_typeaheads,
));
}
/**
* Creates a new Pruefung entity.
*
* @Route("/pruefung/examen/create", name="pruefung_create_for_examen")
* @Method({"POST"})
*/
public function createForExamenAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$kunde = $user->getKunde();
$pruefungsamt = $kunde->getPruefungsamt();
if (!$pruefungsamt) {
throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
}
$entity = Pruefung::createForPruefungsamt($pruefungsamt, $kunde->getExamen());
$em->persist($entity);
$form = $this->createForm(PruefungType::class, $entity, array('em' => $em));
$form->handleRequest($request); //changedFrom
if ($form->isValid()) {
$entity->setCreator($user);
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('account'));
}
$pruefer_typeaheads = $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
'create_action' => $this->generateUrl('pruefung_create_for_examen'),
'entity' => $entity,
'form' => $form->createView(),
'prueferObjs' => $pruefer_typeaheads,
));
}
/**
* Creates a new Wahlfach-Pruefung entity.
*
* @Route("/pruefung/wahlfach/create", name="pruefung_create_for_wahlfach")
* @Method({"POST"})
*/
public function createForWahlfachAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$kunde = $user->getKunde();
$pruefungsamt = $kunde->getPruefungsamt();
if (!$pruefungsamt) {
throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
}
$entity = Pruefung::createForPruefungsamt($pruefungsamt, $kunde->getExamen() + 2);
$form = $this->createForm(PruefungType::class, $entity, array('em' => $em));
$form->handleRequest($request); //changedFrom bind
if ($form->isValid()) {
$entity->setCreator($user);
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('account'));
}
$pruefer_typeaheads = $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
'create_action' => $this->generateUrl('pruefung_create_for_wahlfach'),
'entity' => $entity,
'form' => $form->createView(),
'prueferObjs' => $pruefer_typeaheads,
));
}
/**
* Displays a form to edit an existing Pruefung entity.
*
* @Route("/pruefung/{id}/edit", name="pruefung_edit", requirements={"id"="\d+"})
*/
public function editAction(Pruefung $entity)
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$kunde = $user->getKunde();
$pruefungsamt = $kunde->getPruefungsamt();
if (!$pruefungsamt) {
throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
}
if ($entity->getCreator()->getId() !== $user->getId()) {
throw new AccessDeniedException();
}
$editForm = $this->createForm(PruefungType::class, $entity, array('em' => $em));
return $this->render('@JFJuridicusBundle/Pruefung/edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'prueferObjs' => $pruefer_typeaheads,
));
}
/**
* Edits an existing Pruefung entity.
*
* @Route("/pruefung/{id}/update", name="pruefung_update", requirements={"id"="\d+"})
* @Method({"POST"})
*/
public function updateAction(Request $request, Pruefung $entity)
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$kunde = $user->getKunde();
if ($entity->getCreator()->getId() !== $user->getId()) {
throw new AccessDeniedException();
}
$pruefungsamt = $kunde->getPruefungsamt();
if (!$pruefungsamt) {
throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
}
$editForm = $this->createForm(PruefungType::class, $entity, array('em' => $em));
$editForm->handleRequest($request); //changedFrom bind
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('account'));
}
$pruefer_typeaheads = $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
return $this->render('@JFJuridicusBundle/Pruefung/edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'prueferObjs' => $pruefer_typeaheads,
));
}
/**
* Gets shoutbox contents for an existing Pruefung entity.
*
* @Route("/pruefung/{id}/shoutbox", name="pruefung_shoutbox", requirements={"id"="\d+"})
* @Method({"GET"})
*/
public function shoutboxAction(Pruefung $pruefung)
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$kunde = $this->getUser()->getKunde();
$buchung = $pruefung->findBuchung($kunde);
if (!$buchung) {
throw new AccessDeniedException();
}
$em = $this->getDoctrine()->getManager();
$shoutbox_messages = $em->getRepository('JFJuridicusBundle:ShoutboxMessage')->findByPruefungId($pruefung->getId());
return $this->render('@JFJuridicusBundle/Pruefung/shoutbox_content.html.twig', array(
'shoutbox_messages' => $shoutbox_messages,
'pruefung' => $pruefung,
'kunde' => $kunde
));
}
/**
* Gets shoutbox contents for an existing Pruefung entity.
*
* @Route("/pruefung/{id}/shoutbox_add", name="pruefung_shoutbox_add", requirements={"id"="\d+"})
* @Method({"POST"})
*/
public function shoutboxAddAction(Request $request, Pruefung $pruefung)
{
$this->denyAccessUnlessGranted('ROLE_KUNDE');
$kunde = $this->getUser()->getKunde();
$buchung = $pruefung->findBuchung($kunde);
if (!$buchung) {
throw new AccessDeniedException();
}
$em = $this->getDoctrine()->getManager();
$message = new ShoutboxMessage();
$message->setBuchung($buchung);
$messageForm = $this->createForm(ShoutboxMessageType::class, $message);
$messageForm->handleRequest($request); //changedFrom bind
if ($messageForm->isValid()) {
$em->persist($message);
$em->flush();
$this->sendShoutboxEmail($pruefung, $kunde);
}
$shoutbox_messages = $em->getRepository(ShoutboxMessage::class)->findByPruefungId($pruefung->getId());
return $this->render('@JFJuridicusBundle/Pruefung/shoutbox_content.html.twig', array(
'shoutbox_messages' => $shoutbox_messages,
'kunde' => $kunde
));
}
/**
* sendet eine Nachricht an alle Kunden, die eine Prüfung gebucht haben, dass eine neue Shoutbox-Nachricht vorliegt
*
* @param Pruefung $pruefung
* @param Kunde $exclude_kunde Kunde, der die Nachricht geschrieben hat (bekommt keine E-Mail)
*/
private function sendShoutboxEmail(Pruefung $pruefung, Kunde $exclude_kunde)
{
// finde neuestes Template
$em = $this->getDoctrine()->getManager();
$template = $em->getRepository(MailTemplate::class)->findCurrentByTyp(Email::SHOUTBOX_INFO);
$mailer_user = $this->container->getParameter('mailer_user');
$mailer_bcc = $this->container->getParameter('mailer_bcc');
$mailer = $this->get('mailer');
foreach ($pruefung->getKundePruefungen() as $buchung) {
/* @var $buchung \JF\JuridicusBundle\Entity\KundePruefung */
$kunde = $buchung->getKunde();
if ($kunde->getId() != $exclude_kunde->getId()) {
// E-Mail senden
$replacements = array(
'vorname' => $kunde->getVorname(),
'nachname' => $kunde->getNachname(),
'email' => $kunde->getEmail(),
'handynummer' => $kunde->getHandynummer(),
);
$message = \Swift_Message::newInstance()
->setSubject($template->getBetreff())
->setFrom(array($mailer_user => 'Juridicus'))
->setReplyTo('info@juridicus.de')
->setTo($kunde->getEmail())
->setBody($template->replace($replacements), 'text/html')
;
if (isset($mailer_bcc)) {
$message->setBcc(array($mailer_bcc));
}
if ($mailer->send($message)) {
// Versand speichern
$email = new Email();
$email
->setTyp(Email::SHOUTBOX_INFO)
->setKunde($kunde)
->setKundePruefung($buchung)
;
$em->persist($email);
$em->flush();
}
}
}
}
/**
* Filtert Paragraphen
*
* @Route("/paragraph/filter", name="paragraph_filter")
* @Method({"POST"})
*/
public function paragraphFilterAction(Request $request)
{
$form = $this->createParagraphFilterForm();
$form->handleRequest($request); //changedFrom bind
if ($form->isValid()) {
$data = $form->getData();
$em = $this->getDoctrine()->getManager();
$statistik = $em->getRepository(ProtokollInfothekPruefer::class)->filterParagraphs($data);
return $this->render('@JFJuridicusBundle/Paragraph/filter_result.html.twig', array(
'error' => false,
'statistik' => $statistik
));
} else {
return $this->render('@JFJuridicusBundle/Paragraph/filter_result.html.twig', array(
'error' => true,
'message' => 'Ungültige Filterparameter',
));
}
}
/**
*
* @param array $data
* @return \Symfony\Component\Form\Form
*/
private function createParagraphFilterForm(array $data = array())
{
return $this->createFormBuilder($data)
->add('bundesland', EntityType::class, array(
'label' => false,
'class' => 'JF\JuridicusBundle\Entity\Bundesland',
'placeholder' => 'Bundesland wählen...',
'required' => false,
))
->add('fach', ChoiceType::class, array(
'label' => false,
'placeholder' => 'Fach auswählen...',
'choices' => array(
'Strafrecht' => 'Strafrecht',
'Öffentliches Recht' => 'Öffentliches Recht',
'Zivilrecht' => 'Zivilrecht',
'Arbeitsrecht' => 'Arbeitsrecht',
'Verwaltungsrecht' => 'Verwaltungsrecht',
),
'required' => false,
))
->add('startdatum', DateType::class, array(
'label' => false,
'attr' => ['class' => 'js-datepicker', 'placeholder' => 'Zeitraum von ...',],
'placeholder' => 'Zeitraum von',
'widget' => 'single_text',
//'format' => 'dd.MM.yyyy',
'required' => false
))
->add('endedatum', DateType::class, array(
'label' => false,
'placeholder' => 'bis ...',
'widget' => 'single_text',
//'format' => 'dd.MM.yyyy',
'placeholder' => '',
'required' => false,
'attr' => ['class' => 'js-datepicker', 'placeholder' => 'bis',],
))
->add('examen', ChoiceType::class, array(
'label' => false,
'placeholder' => 'Examen auswählen...',
'choices' => array_flip(array( //changedFrom added array_flip
'1' => '1. Staatsexamen',
'2' => '2. Staatsexamen',
)),
'required' => false,
))
->getForm();
}
}