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