<?php
namespace App\Controller;
use App\Exception\ApiException;
use App\Service\DropzoneHelper;
use App\Service\ServiceflowApi;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Uid\Uuid;
use Twig\Environment;
class DamageReportController extends AbstractController
{
/**
* @Route("/", name="form1")
*/
public function index(ServiceflowApi $serviceflowApi, DropzoneHelper $dropzoneHelper, Session $session): Response
{
$uuid = $session->get('uuid');
if (!$uuid) {
$uuid = Uuid::v4();
$session->set('uuid', $uuid);
}
return $this->render('form2.html.twig', [
'mode' => 'create',
'brands' => $serviceflowApi->getBrands(),
'damageCauses' => $serviceflowApi->getCauses(),
'areaCodes' => $serviceflowApi->getAreaCodes(),
'insurers' => $serviceflowApi->getInsurers(),
'uuid' => $uuid,
'uploadedFiles' => $dropzoneHelper->getFiles($uuid),
'uploadDirectories' => $dropzoneHelper->getDirectories(),
]);
}
/**
* @Route("/detailed-information/{uuid}", name="form2")
*/
public function step2(ServiceflowApi $serviceflowApi, DropzoneHelper $dropzoneHelper, string $uuid = null): Response
{
$sketch = $serviceflowApi->getSketch((string)$uuid);
if (empty($sketch)) {
throw new ApiException('Zgłoszenie o podanym ID nie zostało znalezione.');
} elseif ((int)$sketch['drp_status'] !== 3) {
throw new ApiException('Zgłoszenie o podanym ID jest już niedostępne.');
}
return $this->render('form2.html.twig', [
'mode' => 'update',
'brands' => $serviceflowApi->getBrands(),
'damageCauses' => $serviceflowApi->getCauses(),
'areaCodes' => $serviceflowApi->getAreaCodes(),
'insurers' => $serviceflowApi->getInsurers(),
'uuid' => $uuid,
'userMessage' => $sketch['car_user_message'],
'caseData' => $sketch['case_data'],
'fields' => $sketch['drp_fields'],
'addWitness' => count(array_filter($sketch['drp_fields'], static function ($value, $key) {
return preg_match('~^witness_\d+$~', $key);
}, ARRAY_FILTER_USE_BOTH)) > 0,
'uploadedFiles' => $dropzoneHelper->getFiles($uuid),
'uploadDirectories' => $dropzoneHelper->getDirectories(),
]);
}
/**
* @Route("/uuid", name="get_uuid")
*/
public function uuid(DropzoneHelper $dropzoneHelper, Session $session, Environment $twig, Request $request): JsonResponse
{
if (!$request->isXmlHttpRequest()) {
return new JsonResponse([]);
}
$uuid = $session->get('uuid');
if (!$uuid) {
$uuid = Uuid::v4();
$session->set('uuid', $uuid);
}
return new JsonResponse([
'uuid' => $uuid,
'attachments' => $twig->render('attachments.html.twig', [
'uuid' => $uuid,
'mode' => 'create',
'uploadedFiles' => [],
'uploadDirectories' => $dropzoneHelper->getDirectories(),
])
]);
}
}