src/JuridicusBundle/Controller/PrueferController.php line 189

Open in your IDE?
  1. <?php
  2. namespace JF\JuridicusBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  7. use Symfony\Component\Process\Process;
  8. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use JF\JuridicusBundle\Entity\Kunde;
  15. use JF\JuridicusBundle\Entity\KundePruefung;
  16. use JF\JuridicusBundle\Entity\PrueferEmail;
  17. use JF\JuridicusBundle\Entity\Pruefer;
  18. use JF\JuridicusBundle\Entity\PrueferUser;
  19. use JF\JuridicusBundle\Entity\Dienststellung;
  20. use JF\JuridicusBundle\Entity\Titel;
  21. use JF\JuridicusBundle\Entity\User;
  22. use JF\JuridicusBundle\Entity\Pruefung;
  23. use JF\JuridicusBundle\Entity\PdfProtokollmappePruefer;
  24. use JF\JuridicusBundle\Entity\ProtokollInfothekPruefer;
  25. use JF\JuridicusBundle\Entity\PdfProtokollInfothekPruefer;
  26. use JF\JuridicusBundle\Entity\ProtokollInfothekPruefung;
  27. use JF\JuridicusBundle\Entity\Download;
  28. use JF\JuridicusBundle\Entity\Pruefungsamt;
  29. use JF\JuridicusBundle\Entity\Bundesland;
  30. use JF\JuridicusBundle\Form\PrueferType;
  31. use JF\JuridicusBundle\Form\PrueferUserType;
  32. use JF\JuridicusBundle\Form\PruefungsamtPreSelectType;
  33. use Symfony\Component\Form\Extension\Core\Type\DateType;
  34. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  35. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  36. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  37. use Swift_Mailer;
  38. use Swift_Message;
  39. use Doctrine\ORM\EntityManagerInterface;
  40. use JF\JuridicusBundle\Services\Sms;
  41. use JF\JuridicusBundle\Entity\MailTemplate;
  42. use JF\JuridicusBundle\Services\PdfCreator;
  43. /**
  44. * Pruefer controller.
  45. */
  46. class PrueferController extends AbstractController
  47. {
  48. /**
  49. * Lists all Pruefungsamt entities.
  50. *
  51. * @Route("/pruefungsprotokolle-{examNr}-staatsexamen", name="pruefer")
  52. */
  53. public function indexAction($examNr = 1)
  54. {
  55. $em = $this->getDoctrine()->getManager();
  56. if ($examNr == 1) {
  57. $examType = Pruefung::TYP_EXAMEN_1;
  58. } else {
  59. $examType = Pruefung::TYP_EXAMEN_2;
  60. }
  61. $pip_repo = $em->getRepository(ProtokollInfothekPruefer::class);
  62. $statistik = $pip_repo->filterParagraphsByExamen($examType);
  63. $bundeslaender = $em->getRepository(Bundesland::class)->findAll();
  64. $entities = $em->getRepository(Pruefung::class)->findNewest(8, $examType);
  65. $entities_indexed1 = array();
  66. $entities_indexed2 = array();
  67. $index = 0;
  68. foreach ($entities as $entity) {
  69. $datum = $entity->getDatum()->format('d.m.Y');
  70. if ($index%2 == 0) {
  71. $entities_indexed1[$datum][] = $entity;
  72. } else {
  73. $entities_indexed2[$datum][] = $entity;
  74. }
  75. $index++;
  76. }
  77. $filter_form = $this->createParagraphFilterForm($examNr);
  78. return $this->render('@JFJuridicusBundle/Pruefer/pruefungsamt_index.html.twig', array(
  79. 'statistik' => $statistik,
  80. 'filter_form' => $filter_form->createView(),
  81. 'entities_indexed1' => $entities_indexed1,
  82. 'entities_indexed2' => $entities_indexed2,
  83. 'examType' => $examType,
  84. 'entities' => $bundeslaender,
  85. ));
  86. }
  87. /**
  88. *
  89. * @param array $data
  90. * @return \Symfony\Component\Form\Form
  91. */
  92. private function createParagraphFilterForm($examNr)
  93. {
  94. $data = array('examen'=>$examNr);
  95. return $this->createFormBuilder($data)
  96. ->add('bundesland', EntityType::class, array(
  97. 'label' => false,
  98. 'class' => 'JF\JuridicusBundle\Entity\Bundesland',
  99. 'placeholder' => 'Bundesland wählen...',
  100. 'required' => false,
  101. ))
  102. ->add('fach', ChoiceType::class, array(
  103. 'label' => false,
  104. 'placeholder' => 'Fach auswählen...',
  105. 'choices' => array(
  106. 'Strafrecht' => 'Strafrecht',
  107. 'Öffentliches Recht' => 'Öffentliches Recht',
  108. 'Zivilrecht' => 'Zivilrecht',
  109. 'Arbeitsrecht' => 'Arbeitsrecht',
  110. 'Verwaltungsrecht' => 'Verwaltungsrecht',
  111. ),
  112. 'required' => false,
  113. ))
  114. ->add('startdatum', DateType::class, array(
  115. 'label' => false,
  116. 'placeholder' => 'Zeitraum von',
  117. 'widget' => 'single_text',
  118. //'format' => 'dd.MM.yyyy',
  119. 'required' => false,
  120. 'attr' => ['class' => 'js-datepicker', 'placeholder' => 'Zeitraum von ...',],
  121. ))
  122. ->add('endedatum', DateType::class, array(
  123. 'label' => false,
  124. 'placeholder' => 'bis ...',
  125. 'widget' => 'single_text',
  126. //'format' => 'dd.MM.yyyy',
  127. 'placeholder' => '',
  128. 'required' => false,
  129. 'attr' => ['class' => 'js-datepicker', 'placeholder' => 'bis',],
  130. ))
  131. ->add('examen', HiddenType::class, array(
  132. 'label' => false,
  133. 'required' => false,
  134. ))
  135. ->getForm();
  136. }
  137. /**
  138. * Finds and displays Pruefer entities for a given Prüfungsamt
  139. *
  140. * @Route("/pruefungsprotokolle-{examen}-staatsexamen/{bslug}",
  141. * name="pruefer_index_pruefungsamt",
  142. * requirements={"examen"="[12]"}
  143. * )
  144. */
  145. public function showPruefungsamtAction($bslug, $examen)
  146. {
  147. $em = $this->getDoctrine()->getManager();
  148. $pruefungsamt = $em->getRepository(Pruefungsamt::class)->findByExamenAndBSlug($examen, $bslug);
  149. if (!$pruefungsamt) {
  150. throw $this->createNotFoundException('Prüfungsamt wurde nicht gefunden.');
  151. }
  152. $pruefer_liste = $em->getRepository(Pruefer::class)
  153. ->findByPruefungsamt($pruefungsamt->getId(), $pruefungsamt->hasVollbestand());
  154. $statistik = $em->getRepository(ProtokollInfothekPruefer::class)->filterParagraphsByPruefungsamt($pruefungsamt);
  155. return $this->render('@JFJuridicusBundle/Pruefer/index.html.twig', array(
  156. 'pruefungsamt' => $pruefungsamt,
  157. 'pruefer_liste' => $pruefer_liste,
  158. 'typ_options' => Pruefung::getTypOptions(),
  159. 'statistik' => $statistik,
  160. ));
  161. }
  162. /**
  163. * Finds and displays a Pruefer entity.
  164. *
  165. * @Route("/pruefungsprotokolle-{examen}-staatsexamen/{bslug}/{pslug}_pruefer_protokolle_pruefung_jura",
  166. * name="pruefer_show",
  167. * requirements={"examen"="[12]"}
  168. * )
  169. */
  170. public function showAction($examen, $bslug, $pslug)
  171. {
  172. $em = $this->getDoctrine()->getManager();
  173. $bundesland = $em->getRepository(Bundesland::class)->findOneBySlug($bslug);
  174. if (!$bundesland) {
  175. throw $this->createNotFoundException('Bundesland wurde nicht gefunden.');
  176. }
  177. $pruefungsamt = $em->getRepository(Pruefungsamt::class)->findByExamenAndBSlug($examen, $bslug);
  178. if (!$pruefungsamt) {
  179. throw $this->createNotFoundException('Prüfungsamt wurde nicht gefunden.');
  180. }
  181. // Bei Vollbestand werden Prüfer ohne Protokolle umgeleitet - eventuell ändern?
  182. $pruefer = $em->getRepository(Pruefer::class)
  183. ->findByPruefungsamtAndSlug($pruefungsamt->getId(), $pslug, $pruefungsamt->hasVollbestand());
  184. if (!$pruefer) {
  185. return $this->redirect($this->generateUrl('pruefer_index_pruefungsamt', array(
  186. 'examen' => $examen, 'bslug' => $bslug,
  187. )));
  188. }
  189. $kunde = new Kunde();
  190. $kunde->setBundesland($bundesland);
  191. $kunde->setExamen($examen);
  192. $form = $this->createForm(PruefungsamtPreSelectType::class, $kunde);
  193. return $this->render('@JFJuridicusBundle/Pruefer/show.html.twig', array(
  194. 'bundesland' => $bundesland,
  195. 'examen' => $examen,
  196. 'pruefungsamt' => $pruefungsamt,
  197. 'pruefer' => $pruefer,
  198. 'form' => $form->createView(),
  199. 'typ_options' => Pruefung::getTypOptions(),
  200. ));
  201. }
  202. /**
  203. * Finds and displays a Pruefer and shows download text
  204. *
  205. * @Route("/pruefer/download/success", name="pruefer_download_success")
  206. * @Method({"POST"})
  207. */
  208. public function downloadSuccessAction(Request $request)
  209. {
  210. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  211. $em = $this->getDoctrine()->getManager();
  212. $user = $this->getUser();
  213. $kunde = $user->getKunde();
  214. /* @var $kunde \JF\JuridicusBundle\Entity\Kunde */
  215. $pruefer_id = $request->request->getInt('pruefer_id');
  216. $pruefung_id = $request->request->getInt('pruefung_id');
  217. $examen = $request->request->getInt('examen');
  218. if (!$pruefer_id or !$examen or !$pruefung_id) {
  219. throw new AccessDeniedException();
  220. }
  221. $pruefer = $em->getRepository(Pruefer::class)->find($pruefer_id);
  222. if (!$pruefer) {
  223. throw $this->createNotFoundException('Prüfer nicht gefunden');
  224. }
  225. if ($pruefer->getAliasFor()) {
  226. $pruefer = $pruefer->getAliasFor();
  227. }
  228. // Berechtigung checken
  229. $buchung = $em->getRepository(KundePruefung::class)->getDownloadPermission($kunde->getId(), $pruefer->getId());
  230. /* @var $buchung \JF\JuridicusBundle\Entity\KundePruefung */
  231. if (!$buchung) {
  232. // Donwload nicht erlaubt
  233. throw new AccessDeniedException();
  234. } else if ($buchung->needsFacebookPost()) {
  235. // umleiten
  236. return $this->redirect($this->generateUrl('facebook_write_post', array(
  237. 'id' => $buchung->getId(),
  238. 'pruefer_id' => $pruefer_id,
  239. 'pruefung_id' => $pruefung_id,
  240. 'examen' => $examen
  241. )));
  242. } else if ($buchung->needsKlausurprotokoll()) {
  243. // umleiten
  244. return $this->redirect($this->generateUrl('protokoll_klausur_edit', array('id' => $buchung->getId())));
  245. } else {
  246. // Download erlaubt
  247. $pdf = $em->getRepository(PdfProtokollmappePruefer::class)->findOneByPrueferAndExamen($pruefer->getId(), $examen);
  248. if (!$pdf) {
  249. throw $this->createNotFoundException('Protokolldatei nicht gefunden');
  250. }
  251. }
  252. return $this->render("@JFJuridicusBundle/Pruefer/download_success.html.twig", array(
  253. 'pruefer' => $pruefer,
  254. 'examen' => $examen,
  255. 'pdf_id' => $pdf->getId()
  256. ));
  257. }
  258. /**
  259. * shows download error text
  260. *
  261. * @Route("/pruefer/download/error", name="pruefer_download_error")
  262. */
  263. public function downloadErrorAction(Request $request)
  264. {
  265. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  266. return $this->render('@JFJuridicusBundle/Pruefer/download_error.html.twig', array());
  267. }
  268. /**
  269. * @Route("/pruefer/download/success/protokollmappe-{id}.pdf", name="pruefer_download")
  270. */
  271. public function downloadAction(PdfProtokollmappePruefer $pdf, PdfCreator $pdfCreator)
  272. {
  273. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  274. $em = $this->getDoctrine()->getManager();
  275. $user = $this->getUser();
  276. $kunde = $user->getKunde();
  277. /* @var $kunde \JF\JuridicusBundle\Entity\Kunde */
  278. $pruefer = $pdf->getPruefer();
  279. // Berechtigung checken
  280. $buchung = $em->getRepository(KundePruefung::class)->getDownloadPermission($kunde->getId(), $pruefer->getId());
  281. /* @var $buchung \JF\JuridicusBundle\Entity\KundePruefung */
  282. if (!$buchung || $buchung->needsKlausurprotokoll()) {
  283. throw new AccessDeniedException();
  284. }
  285. // Gespeicherten Stempel prüfen und bei Bedarf neu erzeugen
  286. $stempel = $kunde->getPdfStempel();
  287. $stempelPath = $stempel ? $stempel->getPath() : null;
  288. if (
  289. !$stempelPath
  290. || !is_file($stempelPath)
  291. || !is_readable($stempelPath)
  292. || filesize($stempelPath) === 0
  293. ) {
  294. $stempel = $pdfCreator->createStempel($kunde);
  295. if (!$stempel) {
  296. throw new \RuntimeException('Stempel konnte nicht erzeugt werden.');
  297. }
  298. $stempelPath = $stempel->getPath();
  299. clearstatcache(true, $stempelPath);
  300. if (
  301. !is_file($stempelPath)
  302. || !is_readable($stempelPath)
  303. || filesize($stempelPath) === 0
  304. ) {
  305. throw new \RuntimeException(
  306. 'Stempeldatei fehlt oder ist leer: '.$stempelPath
  307. );
  308. }
  309. $em->flush();
  310. }
  311. // Temporäre private Kopie für den Download
  312. $stamped_path = tempnam(sys_get_temp_dir(), 'protokoll_stamped_');
  313. if ($stamped_path === false) {
  314. throw new \RuntimeException(
  315. 'Temporäre Download-Datei konnte nicht angelegt werden.'
  316. );
  317. }
  318. //print_r($kunde->getPdfStempel()->getPath());
  319. //exit();
  320. // Protokoll stempeln
  321. $process = new Process([
  322. 'pdftk',
  323. $pdf->getPath(),
  324. 'stamp',
  325. $stempelPath, //$kunde->getPdfStempel()->getPath(),
  326. 'output',
  327. $stamped_path,
  328. 'owner_pw',
  329. 'stucha5Ac3ep', // später: $bag->get('tcpdf_owner_pw')
  330. 'allow',
  331. 'printing',
  332. ]);
  333. $process->run();
  334. if (!$process->isSuccessful()) {
  335. var_dump($process->getErrorOutput());
  336. exit();
  337. return $this->redirect($this->generateUrl('pruefer_download_error'));
  338. }
  339. $download = $em->getRepository(Download::class)->findOneBy(array(
  340. 'kunde' => $kunde->getId(), 'pruefer' => $pruefer->getId(), 'examen' => $pdf->getExamen()
  341. ));
  342. /* @var $download \JF\JuridicusBundle\Entity\Download */
  343. if (!$download) {
  344. $download = new Download();
  345. $download->setKunde($kunde)->setPruefer($pruefer)->setExamen($pdf->getExamen());
  346. }
  347. $download->setBuchung($buchung);
  348. $download->setAnzahlProtokolle($pdf->getAnzahlProtokolle());
  349. $download->setCreatedAt(new \DateTime());
  350. $download->setUpdatedAt(new \DateTime());
  351. $em->persist($download);
  352. $em->flush();
  353. $filename = sprintf('%s-%d-examen.pdf', $pruefer->getSlug(), $pdf->getExamen());
  354. $response = new Response();
  355. $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
  356. $response->headers->set('Content-Disposition', $disposition);
  357. $response->headers->set('Content-Type', $pdf->getMimeType());
  358. $response->setContent(@file_get_contents($stamped_path));
  359. unlink($stamped_path);
  360. return $response;
  361. }
  362. /**
  363. * Displays a form to create a new Pruefer entity.
  364. *
  365. * @Route("/create/pruefer/ajax/new", name="pruefer_ajax_new")
  366. */
  367. public function newAjaxAction()
  368. {
  369. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  370. $em = $this->getDoctrine()->getManager();
  371. $dienststellung_typeaheads = $em->getRepository(Dienststellung::class)->getTypeaheads();
  372. $titel_typeaheads = $em->getRepository(Titel::class)->getTypeaheads();
  373. $entity = new Pruefer();
  374. $form = $this->createForm(PrueferType::class, $entity);
  375. return $this->render("@JFJuridicusBundle/Pruefer/new.html.twig", array(
  376. 'entity' => $entity,
  377. 'form' => $form->createView(),
  378. 'dienststellungNames' => array_keys($dienststellung_typeaheads),
  379. 'titelNames' => array_keys($titel_typeaheads),
  380. ));
  381. }
  382. /**
  383. * Creates a new Pruefer entity per ajax
  384. *
  385. * @Route("/create/pruefer/ajax/create", name="pruefer_ajax_create")
  386. * @Method({"POST"})
  387. */
  388. public function createAjaxAction(Request $request)
  389. {
  390. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  391. $em = $this->getDoctrine()->getManager();
  392. $user = $this->getUser();
  393. $kunde = $user->getKunde();
  394. $pruefungsamt = $em->getRepository(Pruefungsamt::class)->findOneByUser($user->getId());
  395. $entity = new Pruefer();
  396. $entity->setQuelle('man');
  397. $entity->setCreator($user);
  398. $pruefungsamt_candidates = $kunde->getBundesland()->getPruefungsaemter();
  399. foreach ($pruefungsamt_candidates as $candidate) {
  400. $entity->addPruefungsaemter($candidate);
  401. }
  402. $form = $this->createForm(PrueferType::class, $entity);
  403. $form->handleRequest($request); //changedFrom bind
  404. if ($form->isValid()) {
  405. // Prüfungsämter abgleichen
  406. $existing_pruefer = $em->getRepository(Pruefer::class)->findExisting($entity, $pruefungsamt);
  407. if (empty($existing_pruefer)) {
  408. $entity->setAliasFor(
  409. $em->getRepository(Pruefer::class)->findAlias($entity, null, $pruefungsamt)
  410. );
  411. if (!$entity->getSlug()) {
  412. $entity->setSlug($this->slugify($entity->getVorname().' '.$entity->getNachname()));
  413. }
  414. $em->persist($entity);
  415. $em->flush();
  416. } else {
  417. foreach ($pruefungsamt_candidates as $candidate) {
  418. $candidate->removePruefer($entity);
  419. }
  420. foreach ($existing_pruefer as $p) {
  421. /* @var $p \JF\JuridicusBundle\Entity\Pruefer */
  422. foreach ($pruefungsamt_candidates as $candidate) {
  423. if (!$p->getPruefungsaemter()->contains($candidate)) {
  424. $p->addPruefungsaemter($candidate);
  425. }
  426. }
  427. $em->persist($p);
  428. }
  429. $em->flush();
  430. $entity = $existing_pruefer[0];
  431. }
  432. return new JsonResponse(array(
  433. 'success' => true,
  434. 'objectId' => $entity->getId(),
  435. 'objectName' => $entity->getFullTextWithOrt()
  436. ));
  437. }
  438. $dienststellung_typeaheads = $em->getRepository(Dienststellung::class)->getTypeaheads();
  439. $titel_typeaheads = $em->getRepository(Titel::class)->getTypeaheads();
  440. $html = $this->get('templating')->render("@JFJuridicusBundle/Pruefer/new.html.twig", array(
  441. 'entity' => $entity,
  442. 'form' => $form->createView(),
  443. 'dienststellungNames' => array_keys($dienststellung_typeaheads),
  444. 'titelNames' => array_keys($titel_typeaheads),
  445. ));
  446. return new JsonResponse(array(
  447. 'success' => false,
  448. 'html' => $html
  449. ));
  450. }
  451. /**
  452. *
  453. * @param string $text
  454. * @param string $style 'lower'|'upper'
  455. * @return string
  456. */
  457. public static function slugify($text, $style = NULL)
  458. {
  459. // replace non letter or digits by -
  460. $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  461. // trim
  462. $text = trim($text, '-');
  463. // transliterate
  464. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  465. if ($style == 'lower') {
  466. // lowercase
  467. $text = strtolower($text);
  468. } elseif ($style == 'upper') {
  469. // uppercase
  470. $text = strtoupper($text);
  471. }
  472. // remove unwanted characters
  473. $text = preg_replace('~[^-\w]+~', '', $text);
  474. if (empty($text)) {
  475. return 'n-a';
  476. }
  477. return $text;
  478. }
  479. /**
  480. * Lists all Pruefungsamt entities.
  481. * @Method({"GET"})
  482. * @Route("/pruefer/protokolle", name="pruefer_protokolle")
  483. */
  484. public function prueferBereichAction()
  485. {
  486. $this->denyAccessUnlessGranted('ROLE_PRUEFER');
  487. $em = $this->getDoctrine()->getManager();
  488. $user = $this->getUser();
  489. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByUser($user);
  490. if (!$prueferUser) {
  491. return $this->render('@JFJuridicusBundle/PrueferBereich/not_yet_verified.html.twig', array(
  492. ));
  493. }
  494. $pruefer = $prueferUser->getPruefer();
  495. if (!$pruefer) {
  496. return $this->render('@JFJuridicusBundle/PrueferBereich/not_yet_verified.html.twig', array(
  497. ));
  498. }
  499. $lastDate = new \DateTime("01-01-2011");
  500. //$output->writeln("Prüfer: ".$pruefer->getId()." Pruefungen: ".count($pruefer->getPrueferPruefungen()));
  501. foreach($pruefer->getPrueferPruefungen() as $prueferPruefung) {
  502. if ($lastDate < $prueferPruefung->getPruefung()->getDatum()) {
  503. //prüfe ob dazu auch ein Protokoll gibt
  504. $infothekPruefung = $em->getRepository(ProtokollInfothekPruefung::class)->findBy(['pruefung'=>$prueferPruefung->getPruefung()]);
  505. if($infothekPruefung) {
  506. $lastDate = $prueferPruefung->getPruefung()->getDatum();
  507. }
  508. }
  509. }
  510. foreach($pruefer->getAliases() as $allias) {
  511. //$output->writeln("Alias: ".$allias->getId()." Pruefungen: ".count($allias->getPrueferPruefungen()));
  512. foreach($allias->getPrueferPruefungen() as $prueferPruefung) {
  513. if ($lastDate < $prueferPruefung->getPruefung()->getDatum()) {
  514. $infothekPruefung = $em->getRepository(ProtokollInfothekPruefung::class)->findBy(['pruefung'=>$prueferPruefung->getPruefung()]);
  515. if($infothekPruefung) {
  516. $lastDate = $prueferPruefung->getPruefung()->getDatum();
  517. }
  518. }
  519. }
  520. }
  521. //Letztes Protokoll
  522. //$lastProtocol = $em->getRepository('JFJuridicusBundle:Pruefer')->findLastProtocol($pruefer);
  523. $pdf1Exam = $em->getRepository(PdfProtokollmappePruefer::class)->findOneByPrueferAndExamen($pruefer->getId(), 1);
  524. $pdf2Exam = $em->getRepository(PdfProtokollmappePruefer::class)->findOneByPrueferAndExamen($pruefer->getId(), 2);
  525. return $this->render('@JFJuridicusBundle/PrueferBereich/index.html.twig', array(
  526. "lastProtocol" => $lastDate,
  527. "pruererUser" => $prueferUser,
  528. "pruefer" => $pruefer,
  529. "pdf1Exam" => $pdf1Exam,
  530. "pdf2Exam" => $pdf2Exam
  531. ));
  532. }
  533. /**
  534. * @Method({"GET"})
  535. * @Route("/pruefer/protokolle/download/{exam}", name="pruefer_protokolle_download")
  536. */
  537. public function prueferBereichDownloadAction($exam) {
  538. $this->denyAccessUnlessGranted('ROLE_PRUEFER');
  539. $em = $this->getDoctrine()->getManager();
  540. $user = $this->getUser();
  541. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByUser($user);
  542. $pruefer = $prueferUser->getPruefer();
  543. if (!$pruefer) {
  544. throw $this->createNotFoundException('Pruefer noch nicht verifiziert');
  545. }
  546. // Download erlaubt
  547. $pdf = $em->getRepository(PdfProtokollmappePruefer::class)->findOneByPrueferAndExamen($pruefer->getId(), $exam);
  548. return $pdf->createResponse();
  549. }
  550. /**
  551. * @Method({"GET"})
  552. * @Route("/pruefer/registration", name="pruefer_registration")
  553. */
  554. public function prueferRegistrationAction()
  555. {
  556. $form = $this->createForm(PrueferUserType::class, new PrueferUser());
  557. return $this->render('@JFJuridicusBundle/PrueferBereich/registration.html.twig', array(
  558. "form" => $form->createView()
  559. ));
  560. }
  561. /**
  562. * @Method({"POST"})
  563. * @Route("/pruefer/registration/save", name="pruefer_registration_save")
  564. */
  565. public function prueferRegistrationSaveAction(Request $request)
  566. {
  567. $form = $this->createForm(PrueferUserType::class, new PrueferUser()); //changedFrom
  568. $form->handleRequest($request); //changedFrom bind
  569. if ($form->isSubmitted() && $form->isValid()) {
  570. $em = $this->getDoctrine()->getManager();
  571. $prueferUser = $form->getData();
  572. //dump($prueferUser);
  573. //früher fos_user - nicht sicher ob es jetzt user ist Hier [TODO] - wie war unterschied fos_user und prueferUser?
  574. $user = $em->getRepository(User::class)->findOneBy(['email'=>$prueferUser->getEmail()]);
  575. $prueferUserOld = $em->getRepository(PrueferUser::class)->findOneByEmail($prueferUser->getEmail());
  576. if ($prueferUserOld) {
  577. return $this->render('@JFJuridicusBundle/PrueferBereich/email_in_use.html.twig', array(
  578. ));
  579. //throw $this->createNotFoundException('Die von Ihnen genutzte E-Mail-Adresse ist bei juridicus bereits registriert. Bitte nutzen Sie die „Passwort vergessen“- Funktion. Sollte Ihnen wider Erwarten kein neues Passwort zugestellt werden, nehmen Sie bitte über info@juridicus.de Kontakt mit uns auf.');
  580. }
  581. // alten User ggfs. umbiegen
  582. if ($user) {
  583. $kunde_old = $user->getKunde();
  584. if ($kunde_old) {
  585. $kunde_old->setUser(null);
  586. $em->persist($kunde_old);
  587. $em->flush();
  588. } else {
  589. $user->addRole("ROLE_PRUEFER");
  590. }
  591. } else {
  592. // entsprechenden User neu erstellen
  593. $user = new User();
  594. $user->addRole("ROLE_PRUEFER");
  595. $user->setUsername($prueferUser->getEmail());
  596. $user->setEmail($prueferUser->getEmail());
  597. $user->setPlainPassword(substr(uniqid(md5(rand())), 8, 8));
  598. }
  599. $token = uniqid(md5(rand()));
  600. $user->setConfirmationToken($token);
  601. $user->setEnabled(false);
  602. $em->persist($user);
  603. $prueferUser->setUser($user);
  604. $em->persist($prueferUser);
  605. $em->flush();
  606. // finde neuestes Template
  607. $template = $em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG);
  608. $link = $this->generateUrl('pruefer_registration_activate', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL); //chagngedFrom true
  609. $replacements = array(
  610. 'vorname' => $prueferUser->getVorname(),
  611. 'nachname' => $prueferUser->getNachname(),
  612. 'titel' => $prueferUser->getTitel(),
  613. 'email' => $prueferUser->getEmail(),
  614. // 'handynummer' => $prueferUser->getHandynummer(),
  615. 'link' => $link
  616. );
  617. //$this->logger->info(sprintf('Link: %s ', $link));
  618. $mailerBcc = $this->getParameter('mailer_bcc');
  619. $mailUser = $this->getParameter('mailer_user');
  620. $message = (new Swift_Message($template->getBetreff()))
  621. ->setFrom(array($mailUser => 'Juridicus'))
  622. ->setReplyTo('info@juridicus.de')
  623. ->setTo($prueferUser->getEmail())
  624. ->setBcc('info@juridicus.de')
  625. ->setBody($template->replace($replacements), 'text/html')
  626. ;
  627. if (isset($mailerBcc)) {
  628. $message->setBcc(array($mailerBcc));
  629. }
  630. if ($this->mailer->send($message)) {
  631. // Versand speichern
  632. $email = new PrueferEmail();
  633. $email
  634. ->setTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG)
  635. ->setPrueferUser($prueferUser)
  636. ;
  637. $em->persist($email);
  638. $em->flush();
  639. $this->get('session')->set('registration_email_success', $prueferUser->getEmail());
  640. return $this->redirect($this->generateUrl('pruefer_registration_email_success'));
  641. } else {
  642. $this->get('session')->set('registration_email_error', $prueferUser->getEmail());
  643. return $this->redirect($this->generateUrl('pruefer_registration_email_error'));
  644. }
  645. }
  646. return $this->render('@JFJuridicusBundle/PrueferBereich/registration.html.twig', array(
  647. "form" => $form->createView()
  648. ));
  649. }
  650. /**
  651. * Tell the user to check his email provider
  652. *
  653. * @Route("/pruefer/email/success", name="pruefer_registration_email_success")
  654. */
  655. public function prueferEmailSuccessAction()
  656. {
  657. $user_manager = $this->container->get('fos_user.user_manager');
  658. $session = $this->container->get('session');
  659. $email = $session->get('registration_email_success');
  660. $session->remove('registration_email_success');
  661. if (empty($email)) {
  662. // the user does not come from the sendEmail action
  663. return $this->redirect($this->generateUrl('homepage'));
  664. }
  665. return $this->render('@JFJuridicusBundle/PrueferBereich/email_success.html.twig', array(
  666. 'email' => $email
  667. ));
  668. }
  669. /**
  670. * Tell the prueferUser that email sending failed
  671. *
  672. * @Route("/pruefer/email/error", name="pruefer_registration_email_error")
  673. */
  674. public function prueferEmailErrorAction()
  675. {
  676. $user_manager = $this->container->get('fos_user.user_manager');
  677. $session = $this->container->get('session');
  678. $email = $session->get('registration_email_error');
  679. $session->remove('registration_email_error');
  680. if (empty($email)) {
  681. // the user does not come from the sendEmail action
  682. return $this->redirect($this->generateUrl('homepage'));
  683. }
  684. return $this->render('@JFJuridicusBundle/PrueferBereich/email_error.html.twig', array(
  685. 'email' => $email
  686. ));
  687. }
  688. /**
  689. * Tell the user to check his sms
  690. *
  691. * @Route("/pruefer/sms/success", name="pruefer_registration_sms_success")
  692. */
  693. public function prueferSmsSuccessAction()
  694. {
  695. $user_manager = $this->get('fos_user.user_manager');
  696. $session = $this->get('session');
  697. $email = $session->get('registration_email_success');
  698. $session->remove('registration_email_success');
  699. $em = $this->getDoctrine()->getManager();
  700. if (empty($email)) {
  701. // the user does not come from the sendEmail action
  702. return $this->redirect($this->generateUrl('homepage'));
  703. }
  704. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByEmail($email);
  705. return $this->render('@JFJuridicusBundle/PrueferBereich/sms_success.html.twig', array(
  706. 'prueferUser' => $prueferUser
  707. ));
  708. }
  709. /**
  710. * Tell the user that email is checked
  711. *
  712. * @Route("/pruefer/email/check/success", name="pruefer_registration_email_check_success")
  713. */
  714. public function prueferEmailCheckSuccessAction()
  715. {
  716. $session = $this->container->get('session');
  717. $email = $session->get('registration_email_success');
  718. $session->remove('registration_email_success');
  719. $em = $this->getDoctrine()->getManager();
  720. if (empty($email)) {
  721. // the user does not come from the sendSms action
  722. return $this->redirect($this->generateUrl('homepage'));
  723. }
  724. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByEmail($email);
  725. return $this->render('@JFJuridicusBundle/PrueferBereich/email_checked.html.twig', array(
  726. 'email' => $email
  727. ));
  728. }
  729. /**
  730. * Tell the user that sms sending failed
  731. *
  732. * @Route("/pruefer/sms/error", name="pruefer_registration_sms_error")
  733. */
  734. public function prueferSmsErrorAction()
  735. {
  736. $user_manager = $this->container->get('fos_user.user_manager');
  737. $session = $this->container->get('session');
  738. $email = $session->get('registration_sms_error');
  739. $code = $session->get('registration_sms_code');
  740. $message = $session->get('registration_sms_message');
  741. $session->remove('registration_sms_error');
  742. $session->remove('registration_sms_code');
  743. $session->remove('registration_sms_message');
  744. $em = $this->getDoctrine()->getManager();
  745. if (empty($email) || empty($code) || empty($message)) {
  746. // the user does not come from the sendSms action
  747. return $this->redirect($this->generateUrl('homepage'));
  748. }
  749. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByEmail($email);
  750. return $this->render('@JFJuridicusBundle/PrueferBereich/sms_error.html.twig', array(
  751. 'prueferUser' => $prueferUser,
  752. 'code' => $code,
  753. 'message' => $message,
  754. ));
  755. }
  756. /**
  757. * Receive the confirmation token and send SMS with password
  758. *
  759. * @Route("pruefer/activate/{token}", name="pruefer_registration_activate")
  760. */
  761. public function prueferActivateAction($token)
  762. {
  763. $em = $this->getDoctrine()->getManager();
  764. $user_manager = $this->container->get('fos_user.user_manager');
  765. /* @var $user_manager \FOS\UserBundle\Model\UserManager */
  766. $sms_service = $this->container->get('jf_juridicus.sms');
  767. /* @var $sms_service \JF\JuridicusBundle\Services\Sms */
  768. $user = $user_manager->findUserByConfirmationToken($token);
  769. if (null === $user) {
  770. throw $this->createNotFoundException('Der Aktivierungscode ist ungültig oder wurde schon verwendet.');
  771. }
  772. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByUser($user);
  773. if (null === $prueferUser) {
  774. throw $this->createNotFoundException('Der Benutzer zu diesem Aktivierungscode ist nicht mehr vorhanden.');
  775. }
  776. /* Hier wurde die E-Mail mit dem Passwort versendet, jetzt wird diese E-Mail erst nach verifizierung versendet */
  777. $password_plain = substr(uniqid(md5(rand())), 8, 8);
  778. $user->setConfirmationToken(null);
  779. $user->setPlainPassword($password_plain);
  780. $user->setEnabled(true);
  781. $em->persist($user);
  782. //$this->get('logger')->info(sprintf('User: %s PW: %s', $user->getEmail(), $password_plain));
  783. // Senden des Passwords per SMS
  784. $template = $em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG_PASSWORT);
  785. $replacements = array(
  786. 'titel' => $prueferUser->getTitel(),
  787. 'vorname' => $prueferUser->getVorname(),
  788. 'nachname' => $prueferUser->getNachname(),
  789. 'email' => $prueferUser->getEmail(),
  790. 'link' => 'https://juridicus.de/pruefer/login',
  791. 'password' => $password_plain
  792. );
  793. // NEW send E-Mail with PW
  794. $mailer_user = $this->container->getParameter('mailer_user');
  795. $mailer_bcc = $this->container->getParameter('mailer_bcc');
  796. $mailer = $this->get('mailer');
  797. $message = \Swift_Message::newInstance()
  798. ->setSubject($template->getBetreff())
  799. ->setFrom(array($mailer_user => 'Juridicus'))
  800. ->setReplyTo('info@juridicus.de')
  801. ->setTo($prueferUser->getEmail())
  802. ->setBcc('info@juridicus.de')
  803. ->setBody($template->replace($replacements), 'text/html')
  804. ;
  805. if ($mailer->send($message)) {
  806. // Versand speichern
  807. $email = new PrueferEmail();
  808. $email
  809. ->setTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG_PASSWORT)
  810. ->setPrueferUser($prueferUser)
  811. ;
  812. $em->persist($email);
  813. $em->flush();
  814. $this->get('session')->set('registration_email_success', $prueferUser->getEmail());
  815. return $this->redirect($this->generateUrl('pruefer_registration_sms_success'));
  816. } else {
  817. $this->get('session')->set('registration_email_error', $prueferUser->getEmail());
  818. return $this->redirect($this->generateUrl('pruefer_registration_sms_error'));
  819. }
  820. /*$this->get('session')->set('registration_email_success', $prueferUser->getEmail());
  821. return $this->redirect($this->generateUrl('pruefer_registration_email_check_success'));
  822. */
  823. }
  824. }