src/JuridicusBundle/Controller/PruefungController.php line 36

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