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. $em->persist($download);
  350. $em->flush();
  351. $filename = sprintf('%s-%d-examen.pdf', $pruefer->getSlug(), $pdf->getExamen());
  352. $response = new Response();
  353. $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
  354. $response->headers->set('Content-Disposition', $disposition);
  355. $response->headers->set('Content-Type', $pdf->getMimeType());
  356. $response->setContent(@file_get_contents($stamped_path));
  357. unlink($stamped_path);
  358. return $response;
  359. }
  360. /**
  361. * Displays a form to create a new Pruefer entity.
  362. *
  363. * @Route("/create/pruefer/ajax/new", name="pruefer_ajax_new")
  364. */
  365. public function newAjaxAction()
  366. {
  367. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  368. $em = $this->getDoctrine()->getManager();
  369. $dienststellung_typeaheads = $em->getRepository(Dienststellung::class)->getTypeaheads();
  370. $titel_typeaheads = $em->getRepository(Titel::class)->getTypeaheads();
  371. $entity = new Pruefer();
  372. $form = $this->createForm(PrueferType::class, $entity);
  373. return $this->render("@JFJuridicusBundle/Pruefer/new.html.twig", array(
  374. 'entity' => $entity,
  375. 'form' => $form->createView(),
  376. 'dienststellungNames' => array_keys($dienststellung_typeaheads),
  377. 'titelNames' => array_keys($titel_typeaheads),
  378. ));
  379. }
  380. /**
  381. * Creates a new Pruefer entity per ajax
  382. *
  383. * @Route("/create/pruefer/ajax/create", name="pruefer_ajax_create")
  384. * @Method({"POST"})
  385. */
  386. public function createAjaxAction(Request $request)
  387. {
  388. $this->denyAccessUnlessGranted('ROLE_KUNDE');
  389. $em = $this->getDoctrine()->getManager();
  390. $user = $this->getUser();
  391. $kunde = $user->getKunde();
  392. $pruefungsamt = $em->getRepository(Pruefungsamt::class)->findOneByUser($user->getId());
  393. $entity = new Pruefer();
  394. $entity->setQuelle('man');
  395. $entity->setCreator($user);
  396. $pruefungsamt_candidates = $kunde->getBundesland()->getPruefungsaemter();
  397. foreach ($pruefungsamt_candidates as $candidate) {
  398. $entity->addPruefungsaemter($candidate);
  399. }
  400. $form = $this->createForm(PrueferType::class, $entity);
  401. $form->handleRequest($request); //changedFrom bind
  402. if ($form->isValid()) {
  403. // Prüfungsämter abgleichen
  404. $existing_pruefer = $em->getRepository(Pruefer::class)->findExisting($entity, $pruefungsamt);
  405. if (empty($existing_pruefer)) {
  406. $entity->setAliasFor(
  407. $em->getRepository(Pruefer::class)->findAlias($entity, null, $pruefungsamt)
  408. );
  409. if (!$entity->getSlug()) {
  410. $entity->setSlug($this->slugify($entity->getVorname().' '.$entity->getNachname()));
  411. }
  412. $em->persist($entity);
  413. $em->flush();
  414. } else {
  415. foreach ($pruefungsamt_candidates as $candidate) {
  416. $candidate->removePruefer($entity);
  417. }
  418. foreach ($existing_pruefer as $p) {
  419. /* @var $p \JF\JuridicusBundle\Entity\Pruefer */
  420. foreach ($pruefungsamt_candidates as $candidate) {
  421. if (!$p->getPruefungsaemter()->contains($candidate)) {
  422. $p->addPruefungsaemter($candidate);
  423. }
  424. }
  425. $em->persist($p);
  426. }
  427. $em->flush();
  428. $entity = $existing_pruefer[0];
  429. }
  430. return new JsonResponse(array(
  431. 'success' => true,
  432. 'objectId' => $entity->getId(),
  433. 'objectName' => $entity->getFullTextWithOrt()
  434. ));
  435. }
  436. $dienststellung_typeaheads = $em->getRepository(Dienststellung::class)->getTypeaheads();
  437. $titel_typeaheads = $em->getRepository(Titel::class)->getTypeaheads();
  438. $html = $this->get('templating')->render("@JFJuridicusBundle/Pruefer/new.html.twig", array(
  439. 'entity' => $entity,
  440. 'form' => $form->createView(),
  441. 'dienststellungNames' => array_keys($dienststellung_typeaheads),
  442. 'titelNames' => array_keys($titel_typeaheads),
  443. ));
  444. return new JsonResponse(array(
  445. 'success' => false,
  446. 'html' => $html
  447. ));
  448. }
  449. /**
  450. *
  451. * @param string $text
  452. * @param string $style 'lower'|'upper'
  453. * @return string
  454. */
  455. public static function slugify($text, $style = NULL)
  456. {
  457. // replace non letter or digits by -
  458. $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  459. // trim
  460. $text = trim($text, '-');
  461. // transliterate
  462. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  463. if ($style == 'lower') {
  464. // lowercase
  465. $text = strtolower($text);
  466. } elseif ($style == 'upper') {
  467. // uppercase
  468. $text = strtoupper($text);
  469. }
  470. // remove unwanted characters
  471. $text = preg_replace('~[^-\w]+~', '', $text);
  472. if (empty($text)) {
  473. return 'n-a';
  474. }
  475. return $text;
  476. }
  477. /**
  478. * Lists all Pruefungsamt entities.
  479. * @Method({"GET"})
  480. * @Route("/pruefer/protokolle", name="pruefer_protokolle")
  481. */
  482. public function prueferBereichAction()
  483. {
  484. $this->denyAccessUnlessGranted('ROLE_PRUEFER');
  485. $em = $this->getDoctrine()->getManager();
  486. $user = $this->getUser();
  487. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByUser($user);
  488. if (!$prueferUser) {
  489. return $this->render('@JFJuridicusBundle/PrueferBereich/not_yet_verified.html.twig', array(
  490. ));
  491. }
  492. $pruefer = $prueferUser->getPruefer();
  493. if (!$pruefer) {
  494. return $this->render('@JFJuridicusBundle/PrueferBereich/not_yet_verified.html.twig', array(
  495. ));
  496. }
  497. $lastDate = new \DateTime("01-01-2011");
  498. //$output->writeln("Prüfer: ".$pruefer->getId()." Pruefungen: ".count($pruefer->getPrueferPruefungen()));
  499. foreach($pruefer->getPrueferPruefungen() as $prueferPruefung) {
  500. if ($lastDate < $prueferPruefung->getPruefung()->getDatum()) {
  501. //prüfe ob dazu auch ein Protokoll gibt
  502. $infothekPruefung = $em->getRepository(ProtokollInfothekPruefung::class)->findBy(['pruefung'=>$prueferPruefung->getPruefung()]);
  503. if($infothekPruefung) {
  504. $lastDate = $prueferPruefung->getPruefung()->getDatum();
  505. }
  506. }
  507. }
  508. foreach($pruefer->getAliases() as $allias) {
  509. //$output->writeln("Alias: ".$allias->getId()." Pruefungen: ".count($allias->getPrueferPruefungen()));
  510. foreach($allias->getPrueferPruefungen() as $prueferPruefung) {
  511. if ($lastDate < $prueferPruefung->getPruefung()->getDatum()) {
  512. $infothekPruefung = $em->getRepository(ProtokollInfothekPruefung::class)->findBy(['pruefung'=>$prueferPruefung->getPruefung()]);
  513. if($infothekPruefung) {
  514. $lastDate = $prueferPruefung->getPruefung()->getDatum();
  515. }
  516. }
  517. }
  518. }
  519. //Letztes Protokoll
  520. //$lastProtocol = $em->getRepository('JFJuridicusBundle:Pruefer')->findLastProtocol($pruefer);
  521. $pdf1Exam = $em->getRepository(PdfProtokollmappePruefer::class)->findOneByPrueferAndExamen($pruefer->getId(), 1);
  522. $pdf2Exam = $em->getRepository(PdfProtokollmappePruefer::class)->findOneByPrueferAndExamen($pruefer->getId(), 2);
  523. return $this->render('@JFJuridicusBundle/PrueferBereich/index.html.twig', array(
  524. "lastProtocol" => $lastDate,
  525. "pruererUser" => $prueferUser,
  526. "pruefer" => $pruefer,
  527. "pdf1Exam" => $pdf1Exam,
  528. "pdf2Exam" => $pdf2Exam
  529. ));
  530. }
  531. /**
  532. * @Method({"GET"})
  533. * @Route("/pruefer/protokolle/download/{exam}", name="pruefer_protokolle_download")
  534. */
  535. public function prueferBereichDownloadAction($exam) {
  536. $this->denyAccessUnlessGranted('ROLE_PRUEFER');
  537. $em = $this->getDoctrine()->getManager();
  538. $user = $this->getUser();
  539. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByUser($user);
  540. $pruefer = $prueferUser->getPruefer();
  541. if (!$pruefer) {
  542. throw $this->createNotFoundException('Pruefer noch nicht verifiziert');
  543. }
  544. // Download erlaubt
  545. $pdf = $em->getRepository(PdfProtokollmappePruefer::class)->findOneByPrueferAndExamen($pruefer->getId(), $exam);
  546. return $pdf->createResponse();
  547. }
  548. /**
  549. * @Method({"GET"})
  550. * @Route("/pruefer/registration", name="pruefer_registration")
  551. */
  552. public function prueferRegistrationAction()
  553. {
  554. $form = $this->createForm(PrueferUserType::class, new PrueferUser());
  555. return $this->render('@JFJuridicusBundle/PrueferBereich/registration.html.twig', array(
  556. "form" => $form->createView()
  557. ));
  558. }
  559. /**
  560. * @Method({"POST"})
  561. * @Route("/pruefer/registration/save", name="pruefer_registration_save")
  562. */
  563. public function prueferRegistrationSaveAction(Request $request)
  564. {
  565. $form = $this->createForm(PrueferUserType::class, new PrueferUser()); //changedFrom
  566. $form->handleRequest($request); //changedFrom bind
  567. if ($form->isSubmitted() && $form->isValid()) {
  568. $em = $this->getDoctrine()->getManager();
  569. $prueferUser = $form->getData();
  570. //dump($prueferUser);
  571. //früher fos_user - nicht sicher ob es jetzt user ist Hier [TODO] - wie war unterschied fos_user und prueferUser?
  572. $user = $em->getRepository(User::class)->findOneBy(['email'=>$prueferUser->getEmail()]);
  573. $prueferUserOld = $em->getRepository(PrueferUser::class)->findOneByEmail($prueferUser->getEmail());
  574. if ($prueferUserOld) {
  575. return $this->render('@JFJuridicusBundle/PrueferBereich/email_in_use.html.twig', array(
  576. ));
  577. //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.');
  578. }
  579. // alten User ggfs. umbiegen
  580. if ($user) {
  581. $kunde_old = $user->getKunde();
  582. if ($kunde_old) {
  583. $kunde_old->setUser(null);
  584. $em->persist($kunde_old);
  585. $em->flush();
  586. } else {
  587. $user->addRole("ROLE_PRUEFER");
  588. }
  589. } else {
  590. // entsprechenden User neu erstellen
  591. $user = new User();
  592. $user->addRole("ROLE_PRUEFER");
  593. $user->setUsername($prueferUser->getEmail());
  594. $user->setEmail($prueferUser->getEmail());
  595. $user->setPlainPassword(substr(uniqid(md5(rand())), 8, 8));
  596. }
  597. $token = uniqid(md5(rand()));
  598. $user->setConfirmationToken($token);
  599. $user->setEnabled(false);
  600. $em->persist($user);
  601. $prueferUser->setUser($user);
  602. $em->persist($prueferUser);
  603. $em->flush();
  604. // finde neuestes Template
  605. $template = $em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG);
  606. $link = $this->generateUrl('pruefer_registration_activate', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL); //chagngedFrom true
  607. $replacements = array(
  608. 'vorname' => $prueferUser->getVorname(),
  609. 'nachname' => $prueferUser->getNachname(),
  610. 'titel' => $prueferUser->getTitel(),
  611. 'email' => $prueferUser->getEmail(),
  612. // 'handynummer' => $prueferUser->getHandynummer(),
  613. 'link' => $link
  614. );
  615. //$this->logger->info(sprintf('Link: %s ', $link));
  616. $mailerBcc = $this->getParameter('mailer_bcc');
  617. $mailUser = $this->getParameter('mailer_user');
  618. $message = (new Swift_Message($template->getBetreff()))
  619. ->setFrom(array($mailUser => 'Juridicus'))
  620. ->setReplyTo('info@juridicus.de')
  621. ->setTo($prueferUser->getEmail())
  622. ->setBcc('info@juridicus.de')
  623. ->setBody($template->replace($replacements), 'text/html')
  624. ;
  625. if (isset($mailerBcc)) {
  626. $message->setBcc(array($mailerBcc));
  627. }
  628. if ($this->mailer->send($message)) {
  629. // Versand speichern
  630. $email = new PrueferEmail();
  631. $email
  632. ->setTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG)
  633. ->setPrueferUser($prueferUser)
  634. ;
  635. $em->persist($email);
  636. $em->flush();
  637. $this->get('session')->set('registration_email_success', $prueferUser->getEmail());
  638. return $this->redirect($this->generateUrl('pruefer_registration_email_success'));
  639. } else {
  640. $this->get('session')->set('registration_email_error', $prueferUser->getEmail());
  641. return $this->redirect($this->generateUrl('pruefer_registration_email_error'));
  642. }
  643. }
  644. return $this->render('@JFJuridicusBundle/PrueferBereich/registration.html.twig', array(
  645. "form" => $form->createView()
  646. ));
  647. }
  648. /**
  649. * Tell the user to check his email provider
  650. *
  651. * @Route("/pruefer/email/success", name="pruefer_registration_email_success")
  652. */
  653. public function prueferEmailSuccessAction()
  654. {
  655. $user_manager = $this->container->get('fos_user.user_manager');
  656. $session = $this->container->get('session');
  657. $email = $session->get('registration_email_success');
  658. $session->remove('registration_email_success');
  659. if (empty($email)) {
  660. // the user does not come from the sendEmail action
  661. return $this->redirect($this->generateUrl('homepage'));
  662. }
  663. return $this->render('@JFJuridicusBundle/PrueferBereich/email_success.html.twig', array(
  664. 'email' => $email
  665. ));
  666. }
  667. /**
  668. * Tell the prueferUser that email sending failed
  669. *
  670. * @Route("/pruefer/email/error", name="pruefer_registration_email_error")
  671. */
  672. public function prueferEmailErrorAction()
  673. {
  674. $user_manager = $this->container->get('fos_user.user_manager');
  675. $session = $this->container->get('session');
  676. $email = $session->get('registration_email_error');
  677. $session->remove('registration_email_error');
  678. if (empty($email)) {
  679. // the user does not come from the sendEmail action
  680. return $this->redirect($this->generateUrl('homepage'));
  681. }
  682. return $this->render('@JFJuridicusBundle/PrueferBereich/email_error.html.twig', array(
  683. 'email' => $email
  684. ));
  685. }
  686. /**
  687. * Tell the user to check his sms
  688. *
  689. * @Route("/pruefer/sms/success", name="pruefer_registration_sms_success")
  690. */
  691. public function prueferSmsSuccessAction()
  692. {
  693. $user_manager = $this->get('fos_user.user_manager');
  694. $session = $this->get('session');
  695. $email = $session->get('registration_email_success');
  696. $session->remove('registration_email_success');
  697. $em = $this->getDoctrine()->getManager();
  698. if (empty($email)) {
  699. // the user does not come from the sendEmail action
  700. return $this->redirect($this->generateUrl('homepage'));
  701. }
  702. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByEmail($email);
  703. return $this->render('@JFJuridicusBundle/PrueferBereich/sms_success.html.twig', array(
  704. 'prueferUser' => $prueferUser
  705. ));
  706. }
  707. /**
  708. * Tell the user that email is checked
  709. *
  710. * @Route("/pruefer/email/check/success", name="pruefer_registration_email_check_success")
  711. */
  712. public function prueferEmailCheckSuccessAction()
  713. {
  714. $session = $this->container->get('session');
  715. $email = $session->get('registration_email_success');
  716. $session->remove('registration_email_success');
  717. $em = $this->getDoctrine()->getManager();
  718. if (empty($email)) {
  719. // the user does not come from the sendSms action
  720. return $this->redirect($this->generateUrl('homepage'));
  721. }
  722. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByEmail($email);
  723. return $this->render('@JFJuridicusBundle/PrueferBereich/email_checked.html.twig', array(
  724. 'email' => $email
  725. ));
  726. }
  727. /**
  728. * Tell the user that sms sending failed
  729. *
  730. * @Route("/pruefer/sms/error", name="pruefer_registration_sms_error")
  731. */
  732. public function prueferSmsErrorAction()
  733. {
  734. $user_manager = $this->container->get('fos_user.user_manager');
  735. $session = $this->container->get('session');
  736. $email = $session->get('registration_sms_error');
  737. $code = $session->get('registration_sms_code');
  738. $message = $session->get('registration_sms_message');
  739. $session->remove('registration_sms_error');
  740. $session->remove('registration_sms_code');
  741. $session->remove('registration_sms_message');
  742. $em = $this->getDoctrine()->getManager();
  743. if (empty($email) || empty($code) || empty($message)) {
  744. // the user does not come from the sendSms action
  745. return $this->redirect($this->generateUrl('homepage'));
  746. }
  747. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByEmail($email);
  748. return $this->render('@JFJuridicusBundle/PrueferBereich/sms_error.html.twig', array(
  749. 'prueferUser' => $prueferUser,
  750. 'code' => $code,
  751. 'message' => $message,
  752. ));
  753. }
  754. /**
  755. * Receive the confirmation token and send SMS with password
  756. *
  757. * @Route("pruefer/activate/{token}", name="pruefer_registration_activate")
  758. */
  759. public function prueferActivateAction($token)
  760. {
  761. $em = $this->getDoctrine()->getManager();
  762. $user_manager = $this->container->get('fos_user.user_manager');
  763. /* @var $user_manager \FOS\UserBundle\Model\UserManager */
  764. $sms_service = $this->container->get('jf_juridicus.sms');
  765. /* @var $sms_service \JF\JuridicusBundle\Services\Sms */
  766. $user = $user_manager->findUserByConfirmationToken($token);
  767. if (null === $user) {
  768. throw $this->createNotFoundException('Der Aktivierungscode ist ungültig oder wurde schon verwendet.');
  769. }
  770. $prueferUser = $em->getRepository(PrueferUser::class)->findOneByUser($user);
  771. if (null === $prueferUser) {
  772. throw $this->createNotFoundException('Der Benutzer zu diesem Aktivierungscode ist nicht mehr vorhanden.');
  773. }
  774. /* Hier wurde die E-Mail mit dem Passwort versendet, jetzt wird diese E-Mail erst nach verifizierung versendet */
  775. $password_plain = substr(uniqid(md5(rand())), 8, 8);
  776. $user->setConfirmationToken(null);
  777. $user->setPlainPassword($password_plain);
  778. $user->setEnabled(true);
  779. $em->persist($user);
  780. //$this->get('logger')->info(sprintf('User: %s PW: %s', $user->getEmail(), $password_plain));
  781. // Senden des Passwords per SMS
  782. $template = $em->getRepository(MailTemplate::class)->findCurrentByTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG_PASSWORT);
  783. $replacements = array(
  784. 'titel' => $prueferUser->getTitel(),
  785. 'vorname' => $prueferUser->getVorname(),
  786. 'nachname' => $prueferUser->getNachname(),
  787. 'email' => $prueferUser->getEmail(),
  788. 'link' => 'https://juridicus.de/pruefer/login',
  789. 'password' => $password_plain
  790. );
  791. // NEW send E-Mail with PW
  792. $mailer_user = $this->container->getParameter('mailer_user');
  793. $mailer_bcc = $this->container->getParameter('mailer_bcc');
  794. $mailer = $this->get('mailer');
  795. $message = \Swift_Message::newInstance()
  796. ->setSubject($template->getBetreff())
  797. ->setFrom(array($mailer_user => 'Juridicus'))
  798. ->setReplyTo('info@juridicus.de')
  799. ->setTo($prueferUser->getEmail())
  800. ->setBcc('info@juridicus.de')
  801. ->setBody($template->replace($replacements), 'text/html')
  802. ;
  803. if ($mailer->send($message)) {
  804. // Versand speichern
  805. $email = new PrueferEmail();
  806. $email
  807. ->setTyp(PrueferEmail::PRUEFER_BEREICH_REGISTRIERUNG_PASSWORT)
  808. ->setPrueferUser($prueferUser)
  809. ;
  810. $em->persist($email);
  811. $em->flush();
  812. $this->get('session')->set('registration_email_success', $prueferUser->getEmail());
  813. return $this->redirect($this->generateUrl('pruefer_registration_sms_success'));
  814. } else {
  815. $this->get('session')->set('registration_email_error', $prueferUser->getEmail());
  816. return $this->redirect($this->generateUrl('pruefer_registration_sms_error'));
  817. }
  818. /*$this->get('session')->set('registration_email_success', $prueferUser->getEmail());
  819. return $this->redirect($this->generateUrl('pruefer_registration_email_check_success'));
  820. */
  821. }
  822. }