Skip to content

Commit

Permalink
fix product image retrieval and voucher generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bennanis committed Dec 12, 2024
1 parent 286d759 commit 5cafd99
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 284 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.1</version>
<version>1.0.2</version>
<authors>
<author>
<name></name>
Expand Down
2 changes: 1 addition & 1 deletion Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static function connectModule( $auth )
'timezone' => $timezone,
'url_client' => $urlClient,
'ecommerce_version' => reset( $ecommerce_version ),
'module_version' => '1.0.1'
'module_version' => '1.0.2'
];

$response = SpmShopConnection::saveConfiguration( $auth, $config );
Expand Down
87 changes: 72 additions & 15 deletions Data/ProductImagesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Model\Base\Lang;
use Thelia\Model\ConfigQuery;

class ProductImagesData
{
Expand Down Expand Up @@ -39,21 +40,8 @@ public static function formatProductImage( ProductImage $productImage, Lang $lan
'is_default' => false,
];
} else if ( $action == 'update') {
$url = '';
$url = self::getImageUrl( $productImage, $dispatcher);

try {
$imgSourcePath = $productImage->getUploadDir().DS.$productImage->getFile();

$productImageEvent = new ImageEvent();
$productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image');

$dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS);
$url = $productImageEvent->getFileUrl();
} catch (\Throwable $th) {
$url = null;
}


$productSaleElementsProductImages = $productImage->getProductSaleElementsProductImages()->getFirst();

$data = [
Expand All @@ -66,7 +54,76 @@ public static function formatProductImage( ProductImage $productImage, Lang $lan
'updated_at' => $productImage->getUpdatedAt()->format('Y-m-d\TH:i:s.u\Z')
];
}

return $data;
}

/**
* Retrieves image url
*
* @param ProductImage $productImage
* @param EventDispatcherInterface $dispatcher
*
*/
public static function getImageUrl( ProductImage $productImage, $dispatcher ){

if ( !empty( $productImage ) ) {
try {
$imgSourcePath = $productImage->getUploadDir().DS.$productImage->getFile();

$productImageEvent = new ImageEvent();
$productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image');

$dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS);
$url = $productImageEvent->getFileUrl();

return $url;
} catch (\Throwable $th) {
//throw $th;
}

try {
$cacheDirFromWebRoot = ConfigQuery::read('image_cache_dir_from_web_root', 'cache/images/');
$cacheSubdirectory = '/product/';
$cacheDirectory = THELIA_ROOT . 'web/' . $cacheDirFromWebRoot . $cacheSubdirectory;
$pattern = $cacheDirectory . '*-' . strtolower($productImage->getFile());
$cachedFiles = glob($pattern);
if (!empty($cachedFiles)) {
$largestFile = null;
$largestSize = 0;

foreach ($cachedFiles as $file) {
if (file_exists($file)) {
$size = filesize($file);
if ($size > $largestSize) {
$largestSize = $size;
$largestFile = $file;
}
}
}

$fileName = basename($largestFile);
$sourceFilePath = sprintf(
"%s%s/%s/%s",
THELIA_ROOT,
ConfigQuery::read('image_cache_dir_from_web_root'),
"product",
$fileName
);

$productImageEvent = new ImageEvent();
$productImageEvent->setSourceFilepath($sourceFilePath)->setCacheSubdirectory('product');

$dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS);
$url = $productImageEvent->getFileUrl();

return $url;
}
} catch (\Throwable $th) {
//throw $th;
}
}

return null;
}
}
Loading

0 comments on commit 5cafd99

Please sign in to comment.