src/Controller/AppController.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Claim;
  4. use App\Entity\ClaimFile;
  5. use App\Form\ClaimHaftType;
  6. use App\Form\ClaimMFType;
  7. use App\Form\ClaimSachType;
  8. use App\Form\ClaimTechType;
  9. use App\Service\ApiService;
  10. use App\Service\FileUploadService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\Form\FormError;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\ParameterBag;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Serializer\Serializer;
  22. use Symfony\Component\Serializer\SerializerInterface;
  23. class AppController extends AbstractController
  24. {
  25.     /** @var FileUploadService $fileUploadService */
  26.     private $fileUploadService;
  27.     /** @var EntityManagerInterface $entityManager */
  28.     private $entityManager;
  29.     /** @var ApiService $apiService */
  30.     private $apiService;
  31.     /** @var ParameterBagInterface $parameterBag */
  32.     private $parameterBag;
  33.     public function __construct(FileUploadService $fileUploadServiceEntityManagerInterface $entityManagerApiService $apiServiceParameterBagInterface $parameterBag)
  34.     {
  35.         $this->fileUploadService $fileUploadService;
  36.         $this->entityManager $entityManager;
  37.         $this->apiService $apiService;
  38.         $this->parameterBag $parameterBag;
  39.     }
  40.     /**
  41.      * @Route("/", name="home")
  42.      */
  43.     public function index(): Response
  44.     {
  45.         $links = [
  46.             Claim::TYPE_MF => 'Kantonale Dienstfahrzeuge',
  47.             Claim::TYPE_HAFT => 'Haftpflichtversicherung',
  48.             Claim::TYPE_SACH => 'Sachversicherung',
  49.             Claim::TYPE_GEBAEUDE_SACH => 'Sachversicherung (Gebäude)',
  50.             Claim::TYPE_TECH => 'Technische Versicherung',
  51.         ];
  52.         return $this->render('app/index.html.twig', [
  53.             'controller_name' => 'AppController',
  54.             'links' => $links,
  55.         ]);
  56.     }
  57.     /**
  58.      * @Route("/claim/form/{type}", name="claim_form")
  59.      */
  60.     public function ClaimForm(Request $request$type)
  61.     {
  62.         switch ($type) {
  63.             case Claim::TYPE_MF:
  64.                 $formType ClaimMFType::class;
  65.                 $formTwigName 'mf-form';
  66.                 break;
  67.             case Claim::TYPE_HAFT:
  68.                 $formType ClaimHaftType::class;
  69.                 $formTwigName 'haft-form';
  70.                 break;
  71.             case Claim::TYPE_SACH:
  72.                 $formType ClaimSachType::class;
  73.                 $formTwigName 'sach-form';
  74.                 break;
  75.             case Claim::TYPE_GEBAEUDE_SACH:
  76.                 $formType ClaimSachType::class;
  77.                 $formTwigName 'gebaeude-sach-form';
  78.                 break;
  79.             case Claim::TYPE_TECH:
  80.                 $formType ClaimTechType::class;
  81.                 $formTwigName 'tech-form';
  82.                 break;
  83.             default:
  84.                 throw new \Exception('Claim type not existing');
  85.         }
  86.         $claim = new Claim();
  87.         $claim->setClaimType($type);
  88.         
  89.         $form $this->createForm($formType$claim, [
  90.             'action' => $this->generateUrl('claim_form', ['type' => $type]),
  91.             'method' => 'POST',
  92.         ]);
  93.         $form->handleRequest($request);
  94.         if ($form->isSubmitted()) {
  95.             if (!$form->isValid()) {
  96.                 $errors = [];
  97.                 foreach ($form as $fieldName => $formField) {
  98.                     $fieldErrors = [];
  99.                     foreach ($formField->getErrors(true) as $error) {
  100.                         $fieldErrors[] = $error->getMessage();
  101.                     }
  102.                     $errors[$fieldName] = $fieldErrors;
  103.                 }
  104.                 // Send errors if some was found
  105.                 return new JsonResponse(['status' => 'fail''errors' => $errors]);
  106.             }
  107.             $claim->setCreatedAt(new \DateTime());
  108.             $claim->setUpdatedAt(new \DateTime());
  109.             $this->entityManager->persist($claim);
  110.             $files $request->files->get('fileToUpload');
  111.             $hasError false;
  112.             if ($files) {
  113.                 $result $this->fileUploadService->upload($form$claim$files);
  114.                 $form $result['form'];
  115.                 $hasError $result['hasError'];
  116.             }
  117.             if (!$hasError) {
  118.                 $this->entityManager->flush();
  119.                 $session $request->getSession();
  120.                 $session->getFlashBag()->add('success''Ihre Anfrage wurde erfolgreich gesendet.');
  121.                 if ($this->parameterBag->has('BS_API_KEY') && $this->parameterBag->has('BS_API_URL') && !empty($this->parameterBag->get('BS_API_KEY')) && !empty($this->parameterBag->get('BS_API_URL'))) {
  122.                     if($this->parameterBag->has('MAIL_TO') && !empty($this->parameterBag->get('MAIL_TO'))){
  123.                         $mailParameters = [
  124.                             'subject' => $this->parameterBag->get('MAIL_SUBJECT'),
  125.                             'to' => $this->parameterBag->get('MAIL_TO'),
  126.                             'body' => $this->parameterBag->get('MAIL_BODY')
  127.                         ];
  128.                         if($this->parameterBag->has('MAIL_CC') && !empty($this->parameterBag->get('MAIL_CC'))){
  129.                             $mailParameters['cc'] = $this->parameterBag->get('MAIL_CC');
  130.                         }
  131.                         if($this->parameterBag->has('MAIL_BCC') && !empty($this->parameterBag->get('MAIL_BCC'))){
  132.                             $mailParameters['bcc'] = $this->parameterBag->get('MAIL_BCC');
  133.                         }
  134.                         $this->apiService->sendMails($mailParameters);
  135.                     }
  136.                     $confirmMailParameters = [
  137.                         'subject' => 'Das Schadenformular wurde erfolgreich gesendet.',
  138.                         'to' => $form->get('email')->getData(),
  139.                         'body' => 'Vielen Dank für das einreichen des Schadenformulars.'
  140.                     ];
  141.                     if($form->get('ccEmail')->getData() !== null ) {
  142.                         $confirmMailParameters['cc'] = $form->get('ccEmail')->getData();
  143.                     }
  144.                     $this->apiService->sendMails($confirmMailParameters);
  145.                 }
  146.                 return new JsonResponse(['status' => 'ok''redirect' => $this->generateUrl('home')]);
  147.             }
  148.         }
  149.         return $this->renderForm('app/' $formTwigName '.html.twig', [
  150.             'form' => $form,
  151.         ]);
  152.     }
  153.     /**
  154.      * @Route("/export/data/{id}", name="export_data", defaults={"id"=null})
  155.      */
  156.     public function exportData($idEntityManagerInterface $entityManagerParameterBagInterface $paramsSerializerInterface $serializer): JsonResponse
  157.     {
  158.         $claimRepo $entityManager->getRepository(Claim::class);
  159.         if (null === $id) {
  160.             return $this->json($claimRepo->findNewEntries());
  161.         }
  162.         $claim $claimRepo->find($id);
  163.         return $this->json($claim);
  164.     }
  165.     /**
  166.      * @Route("/ack/{id}", name="ack")
  167.      * @Security("is_granted('ROLE_USER')")
  168.      */
  169.     public function ack(Claim $claimEntityManagerInterface $entityManager): JsonResponse
  170.     {
  171.         $claim->setAckAt(new \DateTime());
  172.         $this->fileUploadService->removeClaimFile($claim);
  173.         $entityManager->persist($claim);
  174.         $entityManager->flush();
  175.         return $this->json([
  176.             'success' => true,
  177.         ]);
  178.     }
  179.     /**
  180.      * @Route("/delete/{claim}", name="delete")
  181.      * @Security("is_granted('ROLE_USER')")
  182.      */
  183.     public function delete(Claim $claimEntityManagerInterface $entityManager): JsonResponse
  184.     {
  185.         $claim->setDeletedAt(new \DateTime());
  186.         $this->fileUploadService->removeClaimFile($claim);
  187.         $entityManager->persist($claim);
  188.         $entityManager->flush();
  189.         return $this->json([
  190.             'success' => true,
  191.         ]);
  192.     }
  193. }