<?php
namespace App\Controller;
use App\Entity\Claim;
use App\Entity\ClaimFile;
use App\Form\ClaimHaftType;
use App\Form\ClaimMFType;
use App\Form\ClaimSachType;
use App\Form\ClaimTechType;
use App\Service\ApiService;
use App\Service\FileUploadService;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
class AppController extends AbstractController
{
/** @var FileUploadService $fileUploadService */
private $fileUploadService;
/** @var EntityManagerInterface $entityManager */
private $entityManager;
/** @var ApiService $apiService */
private $apiService;
/** @var ParameterBagInterface $parameterBag */
private $parameterBag;
public function __construct(FileUploadService $fileUploadService, EntityManagerInterface $entityManager, ApiService $apiService, ParameterBagInterface $parameterBag)
{
$this->fileUploadService = $fileUploadService;
$this->entityManager = $entityManager;
$this->apiService = $apiService;
$this->parameterBag = $parameterBag;
}
/**
* @Route("/", name="home")
*/
public function index(): Response
{
$links = [
Claim::TYPE_MF => 'Kantonale Dienstfahrzeuge',
Claim::TYPE_HAFT => 'Haftpflichtversicherung',
Claim::TYPE_SACH => 'Sachversicherung',
Claim::TYPE_GEBAEUDE_SACH => 'Sachversicherung (Gebäude)',
Claim::TYPE_TECH => 'Technische Versicherung',
];
return $this->render('app/index.html.twig', [
'controller_name' => 'AppController',
'links' => $links,
]);
}
/**
* @Route("/claim/form/{type}", name="claim_form")
*/
public function ClaimForm(Request $request, $type)
{
switch ($type) {
case Claim::TYPE_MF:
$formType = ClaimMFType::class;
$formTwigName = 'mf-form';
break;
case Claim::TYPE_HAFT:
$formType = ClaimHaftType::class;
$formTwigName = 'haft-form';
break;
case Claim::TYPE_SACH:
$formType = ClaimSachType::class;
$formTwigName = 'sach-form';
break;
case Claim::TYPE_GEBAEUDE_SACH:
$formType = ClaimSachType::class;
$formTwigName = 'gebaeude-sach-form';
break;
case Claim::TYPE_TECH:
$formType = ClaimTechType::class;
$formTwigName = 'tech-form';
break;
default:
throw new \Exception('Claim type not existing');
}
$claim = new Claim();
$claim->setClaimType($type);
$form = $this->createForm($formType, $claim, [
'action' => $this->generateUrl('claim_form', ['type' => $type]),
'method' => 'POST',
]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if (!$form->isValid()) {
$errors = [];
foreach ($form as $fieldName => $formField) {
$fieldErrors = [];
foreach ($formField->getErrors(true) as $error) {
$fieldErrors[] = $error->getMessage();
}
$errors[$fieldName] = $fieldErrors;
}
// Send errors if some was found
return new JsonResponse(['status' => 'fail', 'errors' => $errors]);
}
$claim->setCreatedAt(new \DateTime());
$claim->setUpdatedAt(new \DateTime());
$this->entityManager->persist($claim);
$files = $request->files->get('fileToUpload');
$hasError = false;
if ($files) {
$result = $this->fileUploadService->upload($form, $claim, $files);
$form = $result['form'];
$hasError = $result['hasError'];
}
if (!$hasError) {
$this->entityManager->flush();
$session = $request->getSession();
$session->getFlashBag()->add('success', 'Ihre Anfrage wurde erfolgreich gesendet.');
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'))) {
if($this->parameterBag->has('MAIL_TO') && !empty($this->parameterBag->get('MAIL_TO'))){
$mailParameters = [
'subject' => $this->parameterBag->get('MAIL_SUBJECT'),
'to' => $this->parameterBag->get('MAIL_TO'),
'body' => $this->parameterBag->get('MAIL_BODY')
];
if($this->parameterBag->has('MAIL_CC') && !empty($this->parameterBag->get('MAIL_CC'))){
$mailParameters['cc'] = $this->parameterBag->get('MAIL_CC');
}
if($this->parameterBag->has('MAIL_BCC') && !empty($this->parameterBag->get('MAIL_BCC'))){
$mailParameters['bcc'] = $this->parameterBag->get('MAIL_BCC');
}
$this->apiService->sendMails($mailParameters);
}
$confirmMailParameters = [
'subject' => 'Das Schadenformular wurde erfolgreich gesendet.',
'to' => $form->get('email')->getData(),
'body' => 'Vielen Dank für das einreichen des Schadenformulars.'
];
if($form->get('ccEmail')->getData() !== null ) {
$confirmMailParameters['cc'] = $form->get('ccEmail')->getData();
}
$this->apiService->sendMails($confirmMailParameters);
}
return new JsonResponse(['status' => 'ok', 'redirect' => $this->generateUrl('home')]);
}
}
return $this->renderForm('app/' . $formTwigName . '.html.twig', [
'form' => $form,
]);
}
/**
* @Route("/export/data/{id}", name="export_data", defaults={"id"=null})
*/
public function exportData($id, EntityManagerInterface $entityManager, ParameterBagInterface $params, SerializerInterface $serializer): JsonResponse
{
$claimRepo = $entityManager->getRepository(Claim::class);
if (null === $id) {
return $this->json($claimRepo->findNewEntries());
}
$claim = $claimRepo->find($id);
return $this->json($claim);
}
/**
* @Route("/ack/{id}", name="ack")
* @Security("is_granted('ROLE_USER')")
*/
public function ack(Claim $claim, EntityManagerInterface $entityManager): JsonResponse
{
$claim->setAckAt(new \DateTime());
$this->fileUploadService->removeClaimFile($claim);
$entityManager->persist($claim);
$entityManager->flush();
return $this->json([
'success' => true,
]);
}
/**
* @Route("/delete/{claim}", name="delete")
* @Security("is_granted('ROLE_USER')")
*/
public function delete(Claim $claim, EntityManagerInterface $entityManager): JsonResponse
{
$claim->setDeletedAt(new \DateTime());
$this->fileUploadService->removeClaimFile($claim);
$entityManager->persist($claim);
$entityManager->flush();
return $this->json([
'success' => true,
]);
}
}