From 18b9372c83ca868359353d1881599f4bf072fc7d Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 21 Nov 2023 12:39:11 +0100 Subject: [PATCH] Added flags processing. --- Config/module.xml | 2 +- Services/BrevoProductService.php | 34 +++++++++++------- Trait/DataExtractorTrait.php | 61 +++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/Config/module.xml b/Config/module.xml index b608963..247fa62 100644 --- a/Config/module.xml +++ b/Config/module.xml @@ -13,7 +13,7 @@ en_US fr_FR - 1.1.15 + 1.1.16 Chabreuil Antoine diff --git a/Services/BrevoProductService.php b/Services/BrevoProductService.php index 4cc0112..6c3abe6 100644 --- a/Services/BrevoProductService.php +++ b/Services/BrevoProductService.php @@ -16,6 +16,7 @@ use Brevo\Trait\DataExtractorTrait; use Propel\Runtime\Exception\PropelException; use Psr\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Thelia\Core\Event\Image\ImageEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Exception\TheliaProcessException; @@ -42,10 +43,12 @@ class BrevoProductService protected array $metaDataMapping = []; + protected ?LibraryImageService $libraryImageService; + public function __construct( - private BrevoApiService $brevoApiService, + protected BrevoApiService $brevoApiService, protected EventDispatcherInterface $dispatcher, - private LibraryImageService $libraryImageService, + ContainerInterface $container ) { if (null === $this->baseSourceFilePath = ConfigQuery::read('images_library_path')) { $this->baseSourceFilePath = THELIA_LOCAL_DIR.'media'.DS.'images'; @@ -58,6 +61,9 @@ public function __construct( if (!empty($mappingString) && null === $this->metaDataMapping = json_decode($mappingString, true)) { throw new TheliaProcessException('Product metadata mapping error: JSON data seems invalid, please check syntax.'); } + + // Set image service manually, just in case the TheliaLibrary module is not enabled. + $this->libraryImageService = $container->get('thelia_library_image', ContainerInterface::NULL_ON_INVALID_REFERENCE); } public function getObjName(): string @@ -341,18 +347,20 @@ protected function getProductImageUrl(Product $product): ?string ->filterByVisible(1) ->orderBy('position')->findOne() ) { - // Search in library - if (null === $itemImage = LibraryItemImageQuery::create() - ->filterByItemType('product') - ->filterByItemId($product->getId()) - ->orderByPosition() - ->findOne()) { - return null; + // Search in library if the service is available + if (null !== $this->libraryImageService) { + if (null === $itemImage = LibraryItemImageQuery::create() + ->filterByItemType('product') + ->filterByItemId($product->getId()) + ->orderByPosition() + ->findOne()) { + return null; + } + + return URL::getInstance()->absoluteUrl( + $this->libraryImageService->getImagePublicUrl($itemImage->getLibraryImage()) + ); } - - return URL::getInstance()->absoluteUrl( - $this->libraryImageService->getImagePublicUrl($itemImage->getLibraryImage()) - ); } // Put source image file path diff --git a/Trait/DataExtractorTrait.php b/Trait/DataExtractorTrait.php index d23bc97..d5cd7d3 100644 --- a/Trait/DataExtractorTrait.php +++ b/Trait/DataExtractorTrait.php @@ -82,11 +82,44 @@ public function getMappedValues( $stmt->bindValue(':selector', $selector, $selectorType); $stmt->execute(); + // Decode flags + $flags = []; + if (\array_key_exists('flags', $dataQuery)) { + if (\is_array($dataQuery['flags'])) { + foreach ($dataQuery['flags'] as $flagDesc) { + $flags[$flagDesc['type']] = $flagDesc['arg'] ?? ''; + } + } + } + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { - // value should be less than 255 characters - $attributes[$key] = mb_substr($row[$key] ?? '', 0, 254); - if (\array_key_exists($key, $jsonMapping) && \array_key_exists($row[$key], $jsonMapping[$key])) { - $attributes[$key] = $jsonMapping[$key][$row[$key]]; + $value = $row[$key] ?? ''; + + // Process flags + foreach ($flags as $name => $arg) { + switch ($name) { + case 'strip_tags': + $value = strip_tags($value); + break; + case 'htmlspecialchars_decode': + $value = htmlspecialchars_decode($value); + break; + case 'truncate': + $value = mb_substr($value, 0, (int) $arg); + break; + case 'ellipsis': + $value = $this->truncate($value, (int) $arg); + break; + default: + Tlog::getInstance()->warning("Undefined flag : $name"); + break; + } + } + + $attributes[$key] = $value; + + if (\array_key_exists($key, $jsonMapping) && \array_key_exists($value, $jsonMapping[$key])) { + $attributes[$key] = $jsonMapping[$key][$value]; } } } catch (\Exception $ex) { @@ -123,4 +156,24 @@ public function getCustomerAttribute($customerId): array $customerId, ); } + + /** + * Truncates a string to a certain char length, stopping on a word. + * + * @param $string + * @param $length + * @return mixed|string + */ + protected function truncate($string, $length) { + // + if (mb_strlen($string) > $length) { + //limit hit! + $string = mb_substr($string,0, ($length - 1)); + + //stop on a word. + $string = mb_substr($string,0, mb_strrpos($string,' ')).'…'; + } + + return $string; + } }