Skip to content

Commit

Permalink
Added flags processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Allimant committed Nov 21, 2023
1 parent f30c835 commit 18b9372
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.1.15</version>
<version>1.1.16</version>
<authors>
<author>
<name>Chabreuil Antoine</name>
Expand Down
34 changes: 21 additions & 13 deletions Services/BrevoProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 57 additions & 4 deletions Trait/DataExtractorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 18b9372

Please sign in to comment.