From 7879b0a3946c8a0605a45bbf38ad884121c25be9 Mon Sep 17 00:00:00 2001 From: yannleravallec Date: Wed, 6 Nov 2024 16:27:02 +0100 Subject: [PATCH] Copy file before transforming it into uploadedFile --- Service/CsvProductImporterService.php | 38 ++++++++++++++++++--------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Service/CsvProductImporterService.php b/Service/CsvProductImporterService.php index 795c8d6..b626fee 100644 --- a/Service/CsvProductImporterService.php +++ b/Service/CsvProductImporterService.php @@ -115,8 +115,8 @@ public function importProductsFromCsv($filePath, Country $country = null, string throw new \RuntimeException("Cannot open file: $filePath"); } - $headers = fgetcsv($handle, 1000, ','); - while (($data = fgetcsv($handle, 1000, ',')) !== false) { + $headers = fgetcsv($handle, 1000); + while (($data = fgetcsv($handle, 1000)) !== false) { if (!$productData = array_combine($headers, $data)) { throw new \RuntimeException('Problem while combining headers and data.'); } @@ -126,6 +126,10 @@ public function importProductsFromCsv($filePath, Country $country = null, string Tlog::getInstance()->addWarning('Missing Product reference'); continue; } + if (!$productData[self::PRICE_EXCL_TAX_COLUMN]) { + Tlog::getInstance()->addWarning('Missing Product price for:'. $productData[self::REF_COLUMN]); + continue; + } $product = $this->findOrCreateProduct( $productData, $country, @@ -304,13 +308,9 @@ private function dispatchProductEvent(ProductCreateEvent|ProductUpdateEvent $eve ->setDefaultCategory($category->getId()) ->setBasePrice($productData[self::PRICE_EXCL_TAX_COLUMN]) ->setBaseWeight($productData[self::WEIGHT_COLUMN] ?? 0) - ->setTaxRuleId($this->findOrCreateTax($productData, $country, $locale)->getId()); - - if ($isNew) { - $event->setVisible(1) - ->setCurrencyId(1); - } - + ->setTaxRuleId($this->findOrCreateTax($productData, $country, $locale)->getId()) + ->setVisible(1) + ->setCurrencyId(1); if ($event instanceof ProductUpdateEvent) { $event ->setChapo($productData[self::SHORT_DESCRIPTION_COLUMN]) @@ -342,7 +342,7 @@ private function createOrUpdateProductSaleElements(Product $product, array $prod $attributeAvList = []; $attributeAvListIds = []; foreach ($productData[self::ATTRIBUTES] as $attributeColumn => $attributeTitle) { - $attribute = $this->findOrCreateAttribute(substr($attributeColumn, 2), $locale); + $attribute = $this->findOrCreateAttribute($attributeColumn, $locale); $attributeAv = $this->findOrCreateAttributeAv($attributeTitle, $attribute, $locale); $attributeAvList[] = $attributeAv; $attributeAvListIds[] = $attributeAv->getId(); @@ -501,7 +501,7 @@ private function addAttributeToTemplate(Template $template, Attribute $attribute private function addFeaturesToProduct(Product $product, array $productData, string $locale): void { foreach ($productData[self::FEATURES] as $featureColumn => $featureTitle) { - $feature = $this->findOrCreateFeature(substr($featureColumn, 2), $locale); + $feature = $this->findOrCreateFeature($featureColumn, $locale); $featureAv = $this->findOrCreateFeatureAv($featureTitle, $feature, $locale); $this->addFeatureToTemplate($product->getTemplate(), $feature); $featureProduct = FeatureProductQuery::create() @@ -588,11 +588,25 @@ private function addImages(Product $product, ProductSaleElements $productSaleEle if (!$productImage->isNew() && file_exists($filePath) && is_file($filePath)) { $this->fileManager->deleteFile($filePath); } - $uploadedFile = new UploadedFile($filePath, $fileName); + + $uploadedFile = new UploadedFile($this->copyFile($filePath), $fileName); $event = new FileCreateOrUpdateEvent($product->getId()); $event->setModel($productImage) ->setUploadedFile($uploadedFile); $this->dispatcher->dispatch($event, TheliaEvents::IMAGE_SAVE); $this->addProductSaleElementImage($productSaleElements, $product, $event->getUploadedFile()->getFilename()); } + + private function copyFile(string $filePath): string + { + $fileInfo = pathinfo($filePath); + $directory = $fileInfo['dirname']; + $filename = $fileInfo['filename']; + $extension = $fileInfo['extension']; + + $newFilename = $filename . '_copy.' . $extension; + $newFilePath = $directory . '/' . $newFilename; + copy($filePath, $newFilePath); + return $newFilePath; + } }