src/JuridicusBundle/Controller/PruefungController.php line 39

Open in your IDE?
  1. <?php
  2. namespace JF\JuridicusBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use JF\JuridicusBundle\Entity\Pruefung;
  11. use JF\JuridicusBundle\Entity\Pruefer;
  12. use JF\JuridicusBundle\Entity\Email;
  13. use JF\JuridicusBundle\Entity\Kunde;
  14. use JF\JuridicusBundle\Entity\ProtokollInfothekPruefer;
  15. use JF\JuridicusBundle\Entity\ShoutboxMessage;
  16. use JF\JuridicusBundle\Form\PruefungType;
  17. use JF\JuridicusBundle\Form\ShoutboxMessageType;
  18. use Symfony\Component\Form\Extension\Core\Type\DateType;
  19. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  20. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  21. use Swift_Mailer;
  22. use Swift_Message;
  23. use JF\JuridicusBundle\Entity\MailTemplate;
  24. /**
  25.  * Pruefung controller.
  26.  */
  27. class PruefungController extends AbstractController
  28. {
  29.     /**
  30.      * Homepage
  31.      *
  32.      * @Route("/", name="homepage")
  33.      */
  34.     public function homeAction()
  35.     {
  36.         $em $this->getDoctrine()->getManager();
  37.         $anzahl_pruefer $em->getRepository(Pruefer::class)->count();
  38.         $anzahl_protokolle $em->getRepository(Pruefer::class)->countProtokolle();
  39.         $statistik1 $em->getRepository(ProtokollInfothekPruefer::class)->filterParagraphsByExamen(1);
  40.         $statistik2 $em->getRepository(ProtokollInfothekPruefer::class)->filterParagraphsByExamen(2);
  41.         $filter_form $this->createParagraphFilterForm();
  42.         $entities1 $em->getRepository(Pruefung::class)->findNewest(4,Pruefung::TYP_EXAMEN_1);
  43.         $entities_indexed_1 = array();
  44.         foreach ($entities1 as $entity) {
  45.             $entities_indexed_1[] = $entity;
  46.         }
  47.         
  48.         $entities2 $em->getRepository(Pruefung::class)->findNewest(4,Pruefung::TYP_EXAMEN_2);
  49.         $entities_indexed_2 = array();
  50.         foreach ($entities2 as $entity) {
  51.             $entities_indexed_2[] = $entity;
  52.         }
  53.         
  54.         //get recent posts from wordpress
  55.         /*
  56.         $conn = new mysqli("localhost", "wp_user", "Kqx1_w41", "wordpress");
  57.         $sql = "SELECT * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 6";
  58.         $result = $conn -> query($sql);
  59.         $recentPosts = array();
  60.         if($result)
  61.         {
  62.             while ($row = $result->fetch_assoc()) {
  63.                 $row['post_content'] = substr(preg_replace('/(\\[.*\\])/','', strip_tags($row['post_content'])),0,556)."...";
  64.                 $recentPosts[] = $row;
  65.             }
  66.         }
  67.         */
  68.         return $this->render("@JFJuridicusBundle/Pruefung/home.html.twig", array(
  69.             'filter_form'       => $filter_form->createView(),
  70.             'statistik1'        => $statistik1,
  71.             'statistik2'        => $statistik2,
  72.             'entities_indexed_1'=> $entities_indexed_1,
  73.             'entities_indexed_2'=> $entities_indexed_2,
  74.             'anzahl_protokolle' => $anzahl_protokolle,
  75.             'anzahl_pruefer'    => $anzahl_pruefer,
  76.             //'recent_wp_posts'    => $recentPosts
  77.         ));
  78.     }
  79.     /**
  80.      * Lists newest Pruefung entities.
  81.      *
  82.      * @Route("/rss.xml", name="rss")
  83.      */
  84.     public function rssAction()
  85.     {
  86.         $em $this->getDoctrine()->getManager();
  87.         $entities $em->getRepository(Pruefung::class)->findNewest(5);
  88.         $entities_indexed = array();
  89.         foreach ($entities as $entity) {
  90.             $datum $entity->getDatum()->format('d.m.Y');
  91.             $entities_indexed[$datum][] = $entity;
  92.         }
  93.         $timestamp = new \DateTime();
  94.         $response $this->render("@JFJuridicusBundle/Pruefung/rss.xml.twig", array(
  95.             'timestamp'        => $timestamp,
  96.             'entities_indexed' => $entities_indexed,
  97.         ));
  98.         // $response->headers->set('Content-Type', 'application/xml; charset=UTF-8');
  99.         return $response;
  100.     }
  101.     /**
  102.      * Lists all Pruefung entities.
  103.      *
  104.      * @Route("/termine/{page}/pruefer_protokolle_pruefung_jura_examen", name="termine", defaults={"page" = 1})
  105.      * @Route("/termine/{page}/pruefer_protokolle_pruefung_jura_examen_{type}", name="termine_with_type", defaults={"page" = 1, "type" = false})
  106.      * @Template("@JFJuridicusBundle/Pruefung/index.html.twig")
  107.      */
  108.     public function indexAction($page$type false)
  109.     {
  110.         $repo $this->getDoctrine()->getRepository(Pruefung::class);
  111.         $total_termine $repo->countTermine();
  112.         $termine_per_page $this->getParameter('max_termine_on_page');
  113.         $last_page ceil($total_termine $termine_per_page);
  114.         $previous_page $page $page 1;
  115.         $next_page $page $last_page $page $last_page;
  116.         $entities $repo->getTerminePaged($termine_per_page$page $termine_per_page $termine_per_page$type);
  117.         //teile in die zwei Spalten
  118.         $entities_indexed_1 = array();
  119.         $entities_indexed_2 = array();
  120.         $index 0;
  121.         foreach ($entities as $entity) {
  122.             if ($index%== 0) {
  123.                 $entities_indexed_1[] = $entity;
  124.             } else {
  125.                 $entities_indexed_2[] = $entity;
  126.             }
  127.             $index++;
  128.         }
  129.         return array(
  130.             'entities_indexed_1' => $entities_indexed_1,
  131.             'entities_indexed_2' => $entities_indexed_2,
  132.             'last_page'            => $last_page,
  133.             'previous_page'        => $previous_page,
  134.             'current_page'         => $page,
  135.             'next_page'           => $next_page,
  136.             'total_termine'        => $total_termine
  137.         );
  138.     }
  139.     /**
  140.      * Finds and displays a Pruefung entity.
  141.      *
  142.      * @Route("/pruefung/{id}/show", name="pruefung_show", requirements={"id"="\d+"})
  143.      */
  144.     public function showAction(Pruefung $pruefung)
  145.     {
  146.         return $this->render("@JFJuridicusBundle/Pruefung/show.html.twig", array(
  147.             'entity' => $pruefung,
  148.         ));
  149.     }
  150.     /**
  151.      * Displays a form to create a new Pruefung entity.
  152.      *
  153.      * @Route("/pruefung/examen/new", name="pruefung_new_for_examen")
  154.      */
  155.     public function newForExamenAction()
  156.     {
  157.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  158.         $em $this->getDoctrine()->getManager();
  159.         $user $this->getUser();
  160.         $kunde $user->getKunde();
  161.         $pruefungsamt $kunde->getPruefungsamt();
  162.         if (!$pruefungsamt) {
  163.             throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
  164.         }
  165.         $pruefer_typeaheads $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
  166.         $entity Pruefung::createForPruefungsamt($pruefungsamt$kunde->getExamen());
  167.         $form $this->createForm(PruefungType::class, $entity, array('em' => $em));
  168.         return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
  169.             'create_action' => $this->generateUrl('pruefung_create_for_examen'),
  170.             'prueferObjs'   => $pruefer_typeaheads,
  171.             'entity'        => $entity,
  172.             'form'          => $form->createView(),
  173.         ));
  174.     }
  175.     /**
  176.      * Displays a form to create a new Pruefung entity of type wahlfach
  177.      *
  178.      * @Route("/pruefung/wahlfach/new", name="pruefung_new_for_wahlfach")
  179.      */
  180.     public function newForWahlfachAction()
  181.     {
  182.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  183.         $em $this->getDoctrine()->getManager();
  184.         $user $this->getUser();
  185.         $kunde $user->getKunde();
  186.         $pruefungsamt $kunde->getPruefungsamt();
  187.         if (!$pruefungsamt) {
  188.             throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
  189.         }
  190.         $entity Pruefung::createForPruefungsamt($pruefungsamt$kunde->getExamen() + 2);
  191.         $form $this->createForm(PruefungType::class, $entity, array('em' => $em));
  192.         $pruefer_typeaheads $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
  193.         return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
  194.             'create_action' => $this->generateUrl('pruefung_create_for_wahlfach'),
  195.             'entity'        => $entity,
  196.             'form'          => $form->createView(),
  197.             'prueferObjs'   => $pruefer_typeaheads,
  198.         ));
  199.     }
  200.     /**
  201.      * Creates a new Pruefung entity.
  202.      *
  203.      * @Route("/pruefung/examen/create", name="pruefung_create_for_examen")
  204.      * @Method({"POST"})
  205.      */
  206.     public function createForExamenAction(Request $request)
  207.     {
  208.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  209.         $em $this->getDoctrine()->getManager();
  210.         $user $this->getUser();
  211.         $kunde $user->getKunde();
  212.         $pruefungsamt $kunde->getPruefungsamt();
  213.         if (!$pruefungsamt) {
  214.             throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
  215.         }
  216.         $entity Pruefung::createForPruefungsamt($pruefungsamt$kunde->getExamen());
  217.          $em->persist($entity);
  218.           
  219.         $form $this->createForm(PruefungType::class, $entity, array('em' => $em));
  220.         $form->handleRequest($request); //changedFrom
  221.         if ($form->isValid()) {
  222.             $entity->setCreator($user);
  223.             
  224.             $em->persist($entity);
  225.             $em->flush();
  226.             return $this->redirect($this->generateUrl('account'));
  227.         }
  228.         $pruefer_typeaheads $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
  229.         return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
  230.             'create_action' => $this->generateUrl('pruefung_create_for_examen'),
  231.             'entity'        => $entity,
  232.             'form'          => $form->createView(),
  233.             'prueferObjs'   => $pruefer_typeaheads,
  234.         ));
  235.     }
  236.     /**
  237.      * Creates a new Wahlfach-Pruefung entity.
  238.      *
  239.      * @Route("/pruefung/wahlfach/create", name="pruefung_create_for_wahlfach")
  240.      * @Method({"POST"})
  241.      */
  242.     public function createForWahlfachAction(Request $request)
  243.     {
  244.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  245.         $em $this->getDoctrine()->getManager();
  246.         $user $this->getUser();
  247.         $kunde $user->getKunde();
  248.         $pruefungsamt $kunde->getPruefungsamt();
  249.         if (!$pruefungsamt) {
  250.             throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
  251.         }
  252.         $entity Pruefung::createForPruefungsamt($pruefungsamt$kunde->getExamen() + 2);
  253.         $form $this->createForm(PruefungType::class, $entity, array('em' => $em));
  254.         $form->handleRequest($request); //changedFrom bind
  255.         if ($form->isValid()) {
  256.             $entity->setCreator($user);
  257.             $em->persist($entity);
  258.             $em->flush();
  259.             return $this->redirect($this->generateUrl('account'));
  260.         }
  261.         $pruefer_typeaheads $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
  262.         return $this->render('@JFJuridicusBundle/Pruefung/new.html.twig', array(
  263.             'create_action' => $this->generateUrl('pruefung_create_for_wahlfach'),
  264.             'entity'        => $entity,
  265.             'form'          => $form->createView(),
  266.             'prueferObjs'   => $pruefer_typeaheads,
  267.         ));
  268.     }
  269.     /**
  270.      * Displays a form to edit an existing Pruefung entity.
  271.      *
  272.      * @Route("/pruefung/{id}/edit", name="pruefung_edit", requirements={"id"="\d+"})
  273.      */
  274.     public function editAction(Pruefung $entity)
  275.     {
  276.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  277.         $em $this->getDoctrine()->getManager();
  278.         $user $this->getUser();
  279.         $kunde $user->getKunde();
  280.         $pruefungsamt $kunde->getPruefungsamt();
  281.         if (!$pruefungsamt) {
  282.             throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
  283.         }
  284.         if ($entity->getCreator()->getId() !== $user->getId()) {
  285.             throw new AccessDeniedException();
  286.         }
  287.         $editForm $this->createForm(PruefungType::class, $entity, array('em' => $em));
  288.         return $this->render('@JFJuridicusBundle/Pruefung/edit.html.twig', array(
  289.             'entity'       => $entity,
  290.             'edit_form'    => $editForm->createView(),
  291.             'prueferObjs'  => $pruefer_typeaheads,
  292.         ));
  293.     }
  294.     /**
  295.      * Edits an existing Pruefung entity.
  296.      *
  297.      * @Route("/pruefung/{id}/update", name="pruefung_update", requirements={"id"="\d+"})
  298.      * @Method({"POST"})
  299.      */
  300.     public function updateAction(Request $requestPruefung $entity)
  301.     {
  302.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  303.         $em $this->getDoctrine()->getManager();
  304.         $user $this->getUser();
  305.         $kunde $user->getKunde();
  306.         if ($entity->getCreator()->getId() !== $user->getId()) {
  307.             throw new AccessDeniedException();
  308.         }
  309.         $pruefungsamt $kunde->getPruefungsamt();
  310.         if (!$pruefungsamt) {
  311.             throw $this->createNotFoundException('Das zugehörige Prüfungsamt konnte nicht identifiziert werden.');
  312.         }
  313.         $editForm $this->createForm(PruefungType::class, $entity, array('em' => $em));
  314.         $editForm->handleRequest($request); //changedFrom bind
  315.         if ($editForm->isValid()) {
  316.             $em->persist($entity);
  317.             $em->flush();
  318.             return $this->redirect($this->generateUrl('account'));
  319.         }
  320.         $pruefer_typeaheads $em->getRepository(Pruefer::class)->getTypeaheads($pruefungsamt->getId());
  321.         return $this->render('@JFJuridicusBundle/Pruefung/edit.html.twig', array(
  322.             'entity'       => $entity,
  323.             'edit_form'    => $editForm->createView(),
  324.             'prueferObjs'  => $pruefer_typeaheads,
  325.         ));
  326.     }
  327.     /**
  328.      * Gets shoutbox contents for an existing Pruefung entity.
  329.      *
  330.      * @Route("/pruefung/{id}/shoutbox", name="pruefung_shoutbox", requirements={"id"="\d+"})
  331.      * @Method({"GET"})
  332.      */
  333.     public function shoutboxAction(Pruefung $pruefung)
  334.     {
  335.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  336.         $kunde $this->getUser()->getKunde();
  337.         $buchung $pruefung->findBuchung($kunde);
  338.         if (!$buchung) {
  339.             throw new AccessDeniedException();
  340.         }
  341.         $em $this->getDoctrine()->getManager();
  342.         $shoutbox_messages $em->getRepository(ShoutboxMessage::class)->findByPruefungId($pruefung->getId());
  343.         return $this->render('@JFJuridicusBundle/Pruefung/shoutbox_content.html.twig', array(
  344.             'shoutbox_messages' => $shoutbox_messages,
  345.             'pruefung'          => $pruefung,
  346.             'kunde'             => $kunde
  347.         ));
  348.     }
  349.     /**
  350.      * Gets shoutbox contents for an existing Pruefung entity.
  351.      *
  352.      * @Route("/pruefung/{id}/shoutbox_add", name="pruefung_shoutbox_add", requirements={"id"="\d+"})
  353.      * @Method({"POST"})
  354.      */
  355.     public function shoutboxAddAction(Request $requestPruefung $pruefung)
  356.     {
  357.         $this->denyAccessUnlessGranted('ROLE_KUNDE');
  358.         $kunde $this->getUser()->getKunde();
  359.         $buchung $pruefung->findBuchung($kunde);
  360.         if (!$buchung) {
  361.             throw new AccessDeniedException();
  362.         }
  363.         $em $this->getDoctrine()->getManager();
  364.         $message = new ShoutboxMessage();
  365.         $message->setBuchung($buchung);
  366.         $messageForm $this->createForm(ShoutboxMessageType::class, $message);
  367.         
  368.         $messageForm->handleRequest($request); //changedFrom bind
  369.         if ($messageForm->isValid()) {
  370.             $em->persist($message);
  371.             $em->flush();
  372.             $this->sendShoutboxEmail($pruefung$kunde);
  373.         }
  374.         $shoutbox_messages $em->getRepository(ShoutboxMessage::class)->findByPruefungId($pruefung->getId());
  375.         return $this->render('@JFJuridicusBundle/Pruefung/shoutbox_content.html.twig', array(
  376.             'shoutbox_messages' => $shoutbox_messages,
  377.             'kunde'             => $kunde
  378.         ));
  379.     }
  380.     /**
  381.      * sendet eine Nachricht an alle Kunden, die eine Prüfung gebucht haben, dass eine neue Shoutbox-Nachricht vorliegt
  382.      *
  383.      * @param Pruefung $pruefung
  384.      * @param Kunde $exclude_kunde Kunde, der die Nachricht geschrieben hat (bekommt keine E-Mail)
  385.      */
  386.     private function sendShoutboxEmail(Pruefung $pruefungKunde $exclude_kunde)
  387.     {
  388.         // finde neuestes Template
  389.         $em $this->getDoctrine()->getManager();
  390.         $template $em->getRepository(MailTemplate::class)->findCurrentByTyp(Email::SHOUTBOX_INFO);
  391.         $mailerBcc $this->getParameter('mailer_bcc');
  392.         $mailUser     $this->getParameter('mailer_user');
  393.             
  394.         foreach ($pruefung->getKundePruefungen() as $buchung) {
  395.             /* @var $buchung \JF\JuridicusBundle\Entity\KundePruefung */
  396.             $kunde $buchung->getKunde();
  397.             if ($kunde->getId() != $exclude_kunde->getId()) {
  398.                 // E-Mail senden
  399.                 $replacements = array(
  400.                     'vorname'     => $kunde->getVorname(),
  401.                     'nachname'    => $kunde->getNachname(),
  402.                     'email'       => $kunde->getEmail(),
  403.                     'handynummer' => $kunde->getHandynummer(),
  404.                 );
  405.                 $message = (new Swift_Message($template->getBetreff()))
  406.                     ->setFrom(array($mailer_user => 'Juridicus'))
  407.                     ->setReplyTo('info@juridicus.de')
  408.                     ->setTo($kunde->getEmail())
  409.                     ->setBody($template->replace($replacements), 'text/html')
  410.                 ;
  411.                 if (isset($mailerBcc)) {
  412.                     $message->setBcc(array($mailerBcc));
  413.                 }
  414.                 
  415.                 if ($this->mailer->send($message)) {
  416.                     // Versand speichern
  417.                     $email = new Email();
  418.                     $email
  419.                         ->setTyp(Email::SHOUTBOX_INFO)
  420.                         ->setKunde($kunde)
  421.                         ->setKundePruefung($buchung)
  422.                     ;
  423.                     $em->persist($email);
  424.                     $em->flush();
  425.                 }
  426.             }
  427.         }
  428.     }
  429.     /**
  430.      * Filtert Paragraphen
  431.      *
  432.      * @Route("/paragraph/filter", name="paragraph_filter")
  433.      * @Method({"POST"})
  434.      */
  435.     public function paragraphFilterAction(Request $request)
  436.     {
  437.         $form $this->createParagraphFilterForm();
  438.         $form->handleRequest($request); //changedFrom bind
  439.         if ($form->isValid()) {
  440.             $data $form->getData();
  441.             $em $this->getDoctrine()->getManager();
  442.             $statistik $em->getRepository(ProtokollInfothekPruefer::class)->filterParagraphs($data);
  443.             return $this->render('@JFJuridicusBundle/Paragraph/filter_result.html.twig', array(
  444.                 'error' => false,
  445.                 'statistik' => $statistik
  446.             ));
  447.         } else {
  448.             return $this->render('@JFJuridicusBundle/Paragraph/filter_result.html.twig', array(
  449.                 'error' => true,
  450.                 'message' => 'Ungültige Filterparameter',
  451.             ));
  452.         }
  453.     }
  454.     /**
  455.      *
  456.      * @param array $data
  457.      * @return \Symfony\Component\Form\Form
  458.      */
  459.     private function createParagraphFilterForm(array $data = array())
  460.     {
  461.         return $this->createFormBuilder($data)
  462.                 ->add('bundesland'EntityType::class, array(
  463.                      'label'       => false,
  464.                     'class' => 'JF\JuridicusBundle\Entity\Bundesland',
  465.                     'placeholder' => 'Bundesland wählen...',
  466.                     'required' => false,
  467.                 ))
  468.                 ->add('fach'ChoiceType::class, array(
  469.                     'label'       => false,
  470.                     'placeholder' => 'Fach auswählen...',
  471.                     'choices' => array(
  472.                         'Strafrecht' => 'Strafrecht',
  473.                         'Öffentliches Recht' => 'Öffentliches Recht',
  474.                         'Zivilrecht' => 'Zivilrecht',
  475.                         'Arbeitsrecht' => 'Arbeitsrecht',
  476.                         'Verwaltungsrecht' => 'Verwaltungsrecht',
  477.                     ),
  478.                     'required' => false,
  479.                 ))
  480.                 ->add('startdatum'DateType::class, array(
  481.                     'label'       => false,
  482.                     'attr'        => ['class' => 'js-datepicker''placeholder' => 'Zeitraum von ...',],
  483.                     'placeholder' => 'Zeitraum von',
  484.                     'widget'      => 'single_text',
  485.                     //'format'      => 'dd.MM.yyyy',
  486.                     'required'    => false
  487.                 ))
  488.                 ->add('endedatum'DateType::class, array(
  489.                     'label'       => false,
  490.                     'placeholder' => 'bis ...',
  491.                     'widget'      => 'single_text',
  492.                     //'format'      => 'dd.MM.yyyy',
  493.                     'placeholder' => '',
  494.                     'required'    => false,
  495.                     'attr'        => ['class' => 'js-datepicker''placeholder' => 'bis',],
  496.                 ))
  497.                 ->add('examen'ChoiceType::class, array(
  498.                     'label'       => false,
  499.                     'placeholder' => 'Examen auswählen...',
  500.                     'choices' => array_flip(array( //changedFrom added array_flip
  501.                         '1' => '1. Staatsexamen',
  502.                         '2' => '2. Staatsexamen',
  503.                      )),
  504.                     'required' => false,
  505.                 ))
  506.                 ->getForm();
  507.     }
  508. }