src/Controller/DamageReportController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Exception\ApiException;
  4. use App\Service\DropzoneHelper;
  5. use App\Service\ServiceflowApi;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Uid\Uuid;
  13. use Twig\Environment;
  14. class DamageReportController extends AbstractController
  15. {
  16. /**
  17. * @Route("/", name="form1")
  18. */
  19. public function index(ServiceflowApi $serviceflowApi, DropzoneHelper $dropzoneHelper, Session $session): Response
  20. {
  21. $uuid = $session->get('uuid');
  22. if (!$uuid) {
  23. $uuid = Uuid::v4();
  24. $session->set('uuid', $uuid);
  25. }
  26. return $this->render('form2.html.twig', [
  27. 'mode' => 'create',
  28. 'brands' => $serviceflowApi->getBrands(),
  29. 'damageCauses' => $serviceflowApi->getCauses(),
  30. 'areaCodes' => $serviceflowApi->getAreaCodes(),
  31. 'insurers' => $serviceflowApi->getInsurers(),
  32. 'uuid' => $uuid,
  33. 'uploadedFiles' => $dropzoneHelper->getFiles($uuid),
  34. 'uploadDirectories' => $dropzoneHelper->getDirectories(),
  35. 'viewVariants' => [
  36. 1 => [117, 118, 120, 126],
  37. 2 => [121, 122, 123, 124, 125],
  38. 3 => [116, 167],
  39. ],
  40. ]);
  41. }
  42. /**
  43. * @Route("/detailed-information/{uuid}", name="form2")
  44. */
  45. public function step2(ServiceflowApi $serviceflowApi, DropzoneHelper $dropzoneHelper, string $uuid = null): Response
  46. {
  47. $sketch = $serviceflowApi->getSketch((string)$uuid);
  48. if (empty($sketch)) {
  49. throw new ApiException('Zgłoszenie o podanym ID nie zostało znalezione.');
  50. } elseif ((int)$sketch['drp_status'] !== 3) {
  51. throw new ApiException('Zgłoszenie o podanym ID jest już niedostępne.');
  52. }
  53. return $this->render('form2.html.twig', [
  54. 'mode' => 'update',
  55. 'brands' => $serviceflowApi->getBrands(),
  56. 'damageCauses' => $serviceflowApi->getCauses(),
  57. 'areaCodes' => $serviceflowApi->getAreaCodes(),
  58. 'insurers' => $serviceflowApi->getInsurers(),
  59. 'uuid' => $uuid,
  60. 'userMessage' => $sketch['car_user_message'],
  61. 'caseData' => $sketch['case_data'],
  62. 'fields' => $sketch['drp_fields'],
  63. 'addWitness' => count(array_filter($sketch['drp_fields'], static function ($value, $key) {
  64. return preg_match('~^witness_\d+$~', $key);
  65. }, ARRAY_FILTER_USE_BOTH)) > 0,
  66. 'uploadedFiles' => $dropzoneHelper->getFiles($uuid),
  67. 'uploadDirectories' => $dropzoneHelper->getDirectories(),
  68. ]);
  69. }
  70. /**
  71. * @Route("/uuid", name="get_uuid")
  72. */
  73. public function uuid(DropzoneHelper $dropzoneHelper, Session $session, Environment $twig, Request $request): JsonResponse
  74. {
  75. if (!$request->isXmlHttpRequest()) {
  76. return new JsonResponse([]);
  77. }
  78. $uuid = $session->get('uuid');
  79. if (!$uuid) {
  80. $uuid = Uuid::v4();
  81. $session->set('uuid', $uuid);
  82. }
  83. return new JsonResponse([
  84. 'uuid' => $uuid,
  85. 'attachments' => $twig->render('attachments.html.twig', [
  86. 'uuid' => $uuid,
  87. 'mode' => 'create',
  88. 'uploadedFiles' => [],
  89. 'uploadDirectories' => $dropzoneHelper->getDirectories(),
  90. ])
  91. ]);
  92. }
  93. }