src/Service/VehicleImageService.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\Routing\RouterInterface;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Twig\Environment;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use ImageBundle\Service\ImageService as ImageBundleImageService;
  11. use ImageBundle\Service\TagService as ImageBundleTagService;
  12. class VehicleImageService
  13. {
  14.     private $params;
  15.     private $em;
  16.     private $mailer;
  17.     private $router;
  18.     private $twig;
  19.     private $hasErrors;
  20.     private $errorMessage;
  21.     private $currentUser;
  22.     private $imageService;
  23.     private $imageTagService;
  24.     public function __construct(ParameterBagInterface $paramsEntityManagerInterface $emMailerInterface $mailerRouterInterface $routerEnvironment $twigTokenStorageInterface $tokenStorageImageBundleImageService $imageServiceImageBundleTagService $imageTagService)
  25.     {
  26.         $this->params $params;
  27.         $this->em $em;
  28.         $this->mailer $mailer;
  29.         $this->router $router;
  30.         $this->twig $twig;
  31.         $this->currentUser $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;
  32.         $this->hasErrors false;
  33.         $this->errorMessage "";
  34.         $this->imageService $imageService;
  35.         $this->imageTagService $imageTagService;
  36.     }
  37.     private function decodeBase64ImageData(string $base64Content)
  38.     {
  39.         $base64Content str_replace('data:'''$base64Content);
  40.         $data explode(';base64,'$base64Content);
  41.         return base64_decode($data[1]);
  42.     }
  43.     public function uploadVehicleImage($estimateId$estimatePartnerId null$vehicleId$jobId null$vehiclePartId null$imageDataList$imageSource 'desktop')
  44.     {
  45.         $estimate $this->em->getRepository('App:Estimate')->find($estimateId);
  46.         if (!$estimate) {
  47.             return $this->setError('Estimate not found');
  48.         }
  49.         $vehiclePart null;
  50.         if ($vehiclePartId) {
  51.             $vehiclePart $this->em->getRepository('App:VehiclePart')->find($vehiclePartId);
  52.             if (!$vehiclePart) {
  53.                 return $this->setError('Vehicle part not found');
  54.             }
  55.         }
  56.         $job null;
  57.         if ($jobId) {
  58.             $job $this->em->getRepository('App:Job')->find($jobId);
  59.             if (!$job) {
  60.                 return $this->setError('Job not found');
  61.             }
  62.         }
  63.         $uploadedImagesList = [];
  64.         foreach ($imageDataList as $imageObject) {
  65.             //get mime and content from base64 data
  66.             $imageInfo json_decode($imageObject['imageInfo'], true);
  67.             if (!in_array($imageInfo['type'], ['image/jpeg''image/jpg''image/png'])) {
  68.                 return $this->setError('Invalid file image format');
  69.             }
  70.             //save temp file
  71.             $uploadedFilePath tempnam(sys_get_temp_dir(), $imageInfo['name']);
  72.             $imageData $this->decodeBase64ImageData($imageObject['imageBase64']);
  73.             file_put_contents($uploadedFilePath$imageData);
  74.             try {
  75.                 //imposta descrizione da parte se esiste o da label lavorazione
  76.                 $description null;
  77.                 if ($vehiclePart) {
  78.                     $description $vehiclePart->getLabel();
  79.                 }
  80.                 if ($job) {
  81.                     $description $job->getLabel();
  82.                 }
  83.                 //salva immagine
  84.                 $imageData = [
  85.                     'title' => date('Y-m-d') . '-' $estimate->getCode(),
  86.                     'description' => $description,
  87.                     'originalFilename' => md5(time() . mt_rand()) . ($imageInfo['type'] == 'image/png' '.png' '.jpg'),
  88.                     'mimeType' => $imageInfo['type'],
  89.                     'imageType' => ($vehiclePart || $job) ? 'damage' 'vehicle',
  90.                     'imageSource' => $imageSource,
  91.                 ];
  92.                 $imageId $this->imageService->persistImage($imageData$uploadedFilePath$estimate->getDealer()->getId());
  93.                 //recupera riferimenti s3 dell'immagine appena caricata
  94.                 $uploadedImage $this->imageService->getImage($imageId);
  95.                 $uploadedImagesList[] = [
  96.                     'id' => $uploadedImage['id'],
  97.                     'urls' => $uploadedImage['urls'],
  98.                 ];
  99.                 //aggiorna o crea il tag estimate
  100.                 $tags $this->imageTagService->getTags('estimate', [
  101.                     'tagReference' => $estimate->getId(),
  102.                     'includeImages' => true,
  103.                 ]);
  104.                 if (count($tags) == 0) {
  105.                     //tag non esiste, lo crea
  106.                     $tagObj = [
  107.                         'tagType' => 'estimate',
  108.                         'tagReference' => $estimate->getId(),
  109.                         'label' => $estimate->getCode(),
  110.                         'images' => [['id' => $imageId]],
  111.                     ];
  112.                 } else {
  113.                     //ogni immagine ha un solo tag 'estimate', aggiunge nuova immagine in coda
  114.                     $tagObj $tags[0];
  115.                     $tagObj['images'][] = ['id' => $imageId];
  116.                 }
  117.                 $this->imageTagService->persistTag($tagObj);
  118.                 if ($estimatePartnerId != null && $this->currentUser->getPartner()) {
  119.                     //l'utente รจ partner, aggiorna o crea il tag estimatePartner
  120.                     $tags $this->imageTagService->getTags('estimate-partner', [
  121.                         'tagReference' => $estimatePartnerId,
  122.                         'includeImages' => true,
  123.                     ]);
  124.                     if (!$tags) {
  125.                         //tag non esiste, lo crea
  126.                         $tagObj = [
  127.                             'tagType' => 'estimate-partner',
  128.                             'tagReference' => $estimatePartnerId,
  129.                             'label' => $estimatePartnerId,
  130.                             'images' => [['id' => $imageId]],
  131.                         ];
  132.                     } else {
  133.                         //un solo tag per estimate partner
  134.                         $tagObj $tags[0];
  135.                         //aggiunge nuova immagine in coda
  136.                         $tagObj['images'][] = ['id' => $imageId];
  137.                     }
  138.                     $this->imageTagService->persistTag($tagObj);
  139.                 }
  140.                 //aggiorna o crea il tag vehicle
  141.                 if ($vehicleId) {
  142.                     $tags $this->imageTagService->getTags('vehicle', [
  143.                         'tagReference' => $vehicleId,
  144.                         'includeImages' => true,
  145.                     ]);
  146.                     if (!$tags) {
  147.                         //tag non esiste, lo crea
  148.                         $tagObj = [
  149.                             'tagType' => 'vehicle',
  150.                             'tagReference' => $vehicleId,
  151.                             'label' => $vehicleId,
  152.                             'images' => [['id' => $imageId]],
  153.                         ];
  154.                     } else {
  155.                         //un solo tag per vehicle-part
  156.                         $tagObj $tags[0];
  157.                         //aggiunge nuova immagine in coda
  158.                         $tagObj['images'][] = ['id' => $imageId];
  159.                     }
  160.                     $this->imageTagService->persistTag($tagObj);
  161.                 }
  162.                 //aggiorna o crea il tag vehiclePart
  163.                 if ($vehiclePart) {
  164.                     $tags $this->imageTagService->getTags('vehicle-part', [
  165.                         'tagReference' => $vehiclePartId,
  166.                         'includeImages' => true,
  167.                     ]);
  168.                     if (!$tags) {
  169.                         //tag non esiste, lo crea
  170.                         $tagObj = [
  171.                             'tagType' => 'vehicle-part',
  172.                             'tagReference' => $vehiclePartId,
  173.                             'label' => $vehiclePart->getLabel(),
  174.                             'images' => [['id' => $imageId]],
  175.                         ];
  176.                     } else {
  177.                         //un solo tag per vehicle-part
  178.                         $tagObj $tags[0];
  179.                         //aggiunge nuova immagine in coda
  180.                         $tagObj['images'][] = ['id' => $imageId];
  181.                     }
  182.                     $this->imageTagService->persistTag($tagObj);
  183.                 }
  184.                 //aggiorna o crea il tag job
  185.                 if ($job) {
  186.                     $tags $this->imageTagService->getTags('job', [
  187.                         'tagReference' => $jobId,
  188.                         'includeImages' => true,
  189.                     ]);
  190.                     if (!$tags) {
  191.                         //tag non esiste, lo crea
  192.                         $tagObj = [
  193.                             'tagType' => 'job',
  194.                             'tagReference' => $jobId,
  195.                             'label' => $job->getLabel(),
  196.                             'images' => [['id' => $imageId]],
  197.                         ];
  198.                     } else {
  199.                         $tagObj $tags[0];
  200.                         //aggiunge nuova immagine in coda
  201.                         $tagObj['images'][] = ['id' => $imageId];
  202.                     }
  203.                     $this->imageTagService->persistTag($tagObj);
  204.                 }
  205.                 //username and surname can be empty, but $userFullName must have a value
  206.                 $userFullName trim($this->currentUser->getName() . ' ' $this->currentUser->getSurname() . ' ' $this->currentUser->getCompanyName());
  207.                 //aggiorna o crea il tag user
  208.                 $tags $this->imageTagService->getTags('user', [
  209.                     'tagReference' => $this->currentUser->getId(),
  210.                     'includeImages' => true,
  211.                 ]);
  212.                 if (!$tags) {
  213.                     //tag non esiste, lo crea
  214.                     $tagObj = [
  215.                         'tagType' => 'user',
  216.                         'tagReference' => $this->currentUser->getId(),
  217.                         'label' => $userFullName != '' $userFullName $this->currentUser->getId(),
  218.                         'images' => [['id' => $imageId]],
  219.                     ];
  220.                 } else {
  221.                     //ogni immagine ha un solo tag 'user', aggiunge nuova immagine in coda
  222.                     $tagObj $tags[0];
  223.                     $tagObj['images'][] = ['id' => $imageId];
  224.                 }
  225.                 $this->imageTagService->persistTag($tagObj);
  226.                 //aggiorna o crea il tag company
  227.                 $company null;
  228.                 if ($this->currentUser->getDealer()) {
  229.                     $company $this->currentUser->getDealer();
  230.                 }
  231.                 if ($this->currentUser->getPartner()) {
  232.                     $company $this->currentUser->getPartner();
  233.                 }
  234.                 if ($company) {
  235.                     $tags $this->imageTagService->getTags('company', [
  236.                         'tagReference' => $company->getId(),
  237.                         'includeImages' => true,
  238.                     ]);
  239.                     if (!$tags) {
  240.                         //tag non esiste, lo crea
  241.                         $tagObj = [
  242.                             'tagType' => 'company',
  243.                             'tagReference' => $company->getId(),
  244.                             'label' => $company->getCompanyName(),
  245.                             'images' => [['id' => $imageId]],
  246.                         ];
  247.                     } else {
  248.                         //ogni immagine ha un solo tag 'company', aggiunge nuova immagine in coda
  249.                         $tagObj $tags[0];
  250.                         $tagObj['images'][] = ['id' => $imageId];
  251.                     }
  252.                     $this->imageTagService->persistTag($tagObj);
  253.                 }
  254.             } catch (\Exception $e) {
  255.                 return $this->setError('Image upload failed');
  256.             }
  257.         }
  258.         return $uploadedImagesList;
  259.     }
  260.     public function getVehicleUploadedImages($estimateId$estimatePartnerId null)
  261.     {
  262.         $estimate $this->em->getRepository('App:Estimate')->find($estimateId);
  263.         if (!$estimate) {
  264.             return $this->setError('Estimate not found');
  265.         }
  266.         //estrae tutti i tag collegati all'estimate
  267.         $estimateTags $this->imageTagService->getTags('estimate', [
  268.             'tagReference' => $estimateId,
  269.         ]);
  270.         $images = [];
  271.         if ($estimateTags) {
  272.             //estrae le immagini che hanno il tag id corrispondente
  273.             $estimateTagsIds array_column($estimateTags,'id');
  274.             foreach($estimateTagsIds as $id){
  275.                 $imageObj $this->imageService->getImages([
  276.                     'tagIds' => [$id],
  277.                     'includeDetails' => true,
  278.                 ]);
  279.                 if ($imageObj) {
  280.                     foreach ($imageObj['images'] as $img){
  281.                         $images[] = $img;
  282.                     }
  283.                 }
  284.             }
  285.         }
  286.         //se settato, esclude le immagini non associate al estimatePartner
  287.         foreach ($images as $k => $image) {
  288.             if ($estimatePartnerId) {
  289.                 //immagine caricata da altro partner? La escludo
  290.                 if (isset($image['tags']['estimate-partner']) && count($image['tags']['estimate-partner']) > && $image['tags']['estimate-partner'][0]['tagReference'] != $estimatePartnerId) {
  291.                     unset($images[$k]);
  292.                 }
  293.             } else {
  294.                 if (isset($image['tags']['estimate-partner']) && count($image['tags']['estimate-partner']) > 0) {
  295.                     unset($images[$k]);
  296.                 }
  297.             }
  298.         }
  299.         return $images;
  300.     }
  301.     public function deleteUploadedImage($imageId)
  302.     {
  303.         //recupera tags associati all'immagine
  304.         $image $this->imageService->getImage($imageId);
  305.         //elimina prima i tag associati all'immagine
  306.         $imageTags $image['tags'];
  307.         if ($imageTags && isset($imageTags['estimate'])) {
  308.             $id $imageTags['estimate'][0]['id'];
  309.             $images $this->imageService->getImages(['tagIds' => [$id]]);
  310.             if ($images['result_count'] == 0) {
  311.                 //nessun altra immagine, elimina tutti i tags
  312.                 foreach ($imageTags as $tagType => $tags) {
  313.                     foreach ($tags as $tag) {
  314.                         $this->imageTagService->deleteTag($tag['id']);
  315.                     }
  316.                 }
  317.             }
  318.         }
  319.         $this->imageService->deleteImage($imageId);
  320.         return true;
  321.     }
  322.     public function deleteJobImages($jobPriceId)
  323.     {
  324.         //estrae tutti i tag collegati all'estimate
  325.         $jobTags $this->imageTagService->getTags('job', [
  326.             'tagReference' => $jobPriceId,
  327.         ]);
  328.         $images = [];
  329.         if ($jobTags) {
  330.             //estrae le immagini che hanno il tag estimate corrispondente
  331.             $imageObj $this->imageService->getImages([
  332.                 'tagIds' => array_column($jobTags'id'),
  333.                 'includeDetails' => true,
  334.             ]);
  335.             if ($imageObj) {
  336.                 $images $imageObj['images'];
  337.             }
  338.         }
  339.         foreach ($images as $k => $image) {
  340.             $this->deleteUploadedImage($image['id']);
  341.         }
  342.         return true;
  343.     }
  344.     public function moveImagesFromExtraToParent($childEstimate$childEstimatePartner$parentEstimate$parentEstimatePartner$vehicleIds)
  345.     {
  346.         //estimate tag
  347.         $tags $this->imageTagService->getTags('estimate', [
  348.             'tagReference' => $childEstimate->getId(),
  349.         ]);
  350.         if (!isset($tags[0])) {
  351.             return $this->setError('Estimate tag not found');
  352.         }
  353.         $tagEstimate $tags[0];
  354.         $tagEstimate['tagReference'] = $parentEstimate->getId();
  355.         $tagEstimate['label'] = $parentEstimate->getCode();
  356.         $this->imageTagService->persistTag($tagEstimate);
  357.         //estimate partner tag
  358.         $tags $this->imageTagService->getTags('estimate-partner', [
  359.             'tagReference' => $childEstimatePartner->getId(),
  360.         ]);
  361.         if (!isset($tags[0])) {
  362.             return $this->setError('Estimate Partner tag not found');
  363.         }
  364.         $tagEstimatePartner $tags[0];
  365.         $tagEstimatePartner['tagReference'] = $parentEstimatePartner->getId();
  366.         $tagEstimatePartner['label'] = $parentEstimatePartner->getId();
  367.         $this->imageTagService->persistTag($tagEstimatePartner);
  368.         //tag vehicle
  369.         foreach ($vehicleIds as $vehicleId) {
  370.             $vehicleTags $this->imageTagService->getTags('vehicle', [
  371.                 'tagReference' => $vehicleId['child'],
  372.             ]);
  373.             if(isset($vehicleTags)){
  374.                 foreach($vehicleTags as $tagVehicle){
  375.                     $tagVehicle['tagReference'] = $vehicleId['parent'];
  376.                     $tagVehicle['label'] = $vehicleId['parent'];
  377.                     $this->imageTagService->persistTag($tagVehicle);
  378.                 }
  379.             }
  380.         }
  381.         return true;
  382.     }
  383.     private function setError($message)
  384.     {
  385.         $this->hasErrors true;
  386.         $this->errorMessage $message;
  387.         return false;
  388.     }
  389.     public function getErrorMessage()
  390.     {
  391.         return $this->errorMessage;
  392.     }
  393.     public function hasErrors()
  394.     {
  395.         return $this->hasErrors;
  396.     }
  397. }