Skip to content

Commit

Permalink
Handle akeneo attributes of the type pim_catalog_text
Browse files Browse the repository at this point in the history
  • Loading branch information
erikfrerejean authored Dec 4, 2023
1 parent 7a8705d commit b82e8b8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/Enums/ProductAttributeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace WeDevelop\Akeneo\Enums;

enum ProductAttributeType: string
{
case BOOLEAN = 'pim_catalog_boolean';
case DATE = 'pim_catalog_date';
case FILE = 'pim_catalog_file';
case IMAGE = 'pim_catalog_image';
case METRIC = 'pim_catalog_metric';
case MULTISELECT = 'pim_catalog_multiselect';
case PRICE_COLLECTION = 'pim_catalog_price_collection';
case SIMPLESELECT = 'pim_catalog_simpleselect';
case TEXT = 'pim_catalog_text';
case TEXTAREA = 'pim_catalog_textarea';
}
3 changes: 2 additions & 1 deletion src/Imports/AkeneoImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\ValidationException;
use WeDevelop\Akeneo\Enums\ProductAttributeType;
use WeDevelop\Akeneo\Models\AkeneoImportInterface;
use WeDevelop\Akeneo\Models\Family;
use WeDevelop\Akeneo\Models\FamilyVariant;
Expand Down Expand Up @@ -340,7 +341,7 @@ protected function setProductAttributes(AkeneoImportInterface $productInstance,
$attributeValue->AttributeID = $attribute->ID;
$attributeValue->LocaleID = $locale?->ID;

if ($attribute->Type === ProductAttributeValue::PIM_CATALOG_TEXTAREA_TYPE) {
if (in_array($attribute->Type, [ProductAttributeType::TEXT, ProductAttributeType::TEXTAREA])) {
$attributeValue->TextValue = $value['data'];
} else {
$attributeValue->Value = is_array($value['data']) ? json_encode($value['data']) : $value['data'];
Expand Down
34 changes: 13 additions & 21 deletions src/Models/ProductAttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\Security\Member;
use WeDevelop\Akeneo\Enums\ProductAttributeType;
use WeDevelop\Akeneo\Util\AttributeParser;

/**
* @method ProductAttribute Attribute()
*/
class ProductAttributeValue extends DataObject
{
public const PIM_CATALOG_DATE = 'pim_catalog_date';
public const PIM_CATALOG_FILE_TYPE = 'pim_catalog_file';
public const PIM_CATALOG_IMAGE_TYPE = 'pim_catalog_image';
public const PIM_CATALOG_METRIC_TYPE = 'pim_catalog_metric';
public const PIM_CATALOG_MULTISELECT_TYPE = 'pim_catalog_multiselect';
public const PIM_CATALOG_PRICE_COLLECTION = 'pim_catalog_price_collection';
public const PIM_CATALOG_SIMPLESELECT_TYPE = 'pim_catalog_simpleselect';
public const PIM_CATALOG_TEXTAREA_TYPE = 'pim_catalog_textarea';
public const PIM_CATALOG_BOOLEAN_TYPE = 'pim_catalog_boolean';

/** @config */
private static string $table_name = 'Akeneo_ProductAttributeValue';

Expand Down Expand Up @@ -62,15 +53,16 @@ public function getValue()
}
$attribute = $this->Attribute();

return match ($attribute->Type) {
self::PIM_CATALOG_SIMPLESELECT_TYPE => $attribute->Options()->filter('Code', $value)->first()->Name,
self::PIM_CATALOG_MULTISELECT_TYPE => DBField::create_field('HTMLText', AttributeParser::MultiSelectParser($this)),
self::PIM_CATALOG_PRICE_COLLECTION => DBField::create_field('HTMLText', AttributeParser::PriceCollectionParser($this)),
self::PIM_CATALOG_FILE_TYPE, self::PIM_CATALOG_IMAGE_TYPE => ProductMediaFile::get()->find('Code', $value)?->getAttributeValue(),
self::PIM_CATALOG_DATE => DBDatetime::create()->setValue($value)->Nice(),
self::PIM_CATALOG_TEXTAREA_TYPE => DBField::create_field('HTMLText', $this->getField('TextValue')),
self::PIM_CATALOG_METRIC_TYPE => DBField::create_field('HTMLText', AttributeParser::MetricTypeParser($this)),
self::PIM_CATALOG_BOOLEAN_TYPE => (bool)$value ? _t(__CLASS__.'.Yes', 'Yes') : _t(__CLASS__.'.No', 'No'),
return match (ProductAttributeType::tryFrom($attribute->Type)) {
ProductAttributeType::BOOLEAN => (bool) $value ? _t(__CLASS__.'.Yes', 'Yes') : _t(__CLASS__.'.No', 'No'),
ProductAttributeType::DATE => DBDatetime::create()->setValue($value)->Nice(),
ProductAttributeType::FILE, ProductAttributeType::IMAGE => ProductMediaFile::get()->find('Code', $value)?->getAttributeValue(),
ProductAttributeType::METRIC => DBField::create_field('HTMLText', AttributeParser::MetricTypeParser($this)),
ProductAttributeType::MULTISELECT => DBField::create_field('HTMLText', AttributeParser::MultiSelectParser($this)),
ProductAttributeType::PRICE_COLLECTION => DBField::create_field('HTMLText', AttributeParser::PriceCollectionParser($this)),
ProductAttributeType::SIMPLESELECT => $attribute->Options()->filter('Code', $value)->first()->Name,
ProductAttributeType::TEXT => DBField::create_field('HTMLText', strval($value)),
ProductAttributeType::TEXTAREA => DBField::create_field('HTMLText', nl2br($this->getField('TextValue'))),
default => strval($value),
};
}
Expand All @@ -81,10 +73,10 @@ public function onAfterDelete()

$attribute = $this->Attribute();

if ($attribute->Type === self::PIM_CATALOG_FILE_TYPE) {
if ($attribute->Type === ProductAttributeType::FILE) {
$file = File::get()->byID($this->Value);
$file?->delete();
} elseif ($attribute->Type === self::PIM_CATALOG_IMAGE_TYPE) {
} elseif ($attribute->Type === ProductAttributeType::IMAGE) {
$image = Image::get()->byID($this->Value);
$image?->delete();
}
Expand Down
7 changes: 4 additions & 3 deletions src/Util/AttributeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace WeDevelop\Akeneo\Util;

use WeDevelop\Akeneo\Enums\ProductAttributeType;
use WeDevelop\Akeneo\Models\ProductAttributeOption;
use WeDevelop\Akeneo\Models\ProductAttributeValue;

abstract class AttributeParser
{
public static function MetricTypeParser(ProductAttributeValue $value): ?string
{
if ($value->Attribute()->Type !== ProductAttributeValue::PIM_CATALOG_METRIC_TYPE) {
if (ProductAttributeType::tryFrom($value->Attribute()->Type) !== ProductAttributeType::METRIC) {
throw new \RuntimeException('Not a metric attribute value');
}

Expand All @@ -34,7 +35,7 @@ public static function MetricTypeParser(ProductAttributeValue $value): ?string

public static function MultiSelectParser(ProductAttributeValue $value): string
{
if ($value->Attribute()->Type !== ProductAttributeValue::PIM_CATALOG_MULTISELECT_TYPE) {
if (ProductAttributeType::tryFrom($value->Attribute()->Type) !== ProductAttributeType::MULTISELECT) {
throw new \RuntimeException('Not a multi select attribute value');
}

Expand All @@ -52,7 +53,7 @@ public static function MultiSelectParser(ProductAttributeValue $value): string

public static function PriceCollectionParser(ProductAttributeValue $value): string
{
if ($value->Attribute()->Type !== ProductAttributeValue::PIM_CATALOG_PRICE_COLLECTION) {
if (ProductAttributeType::tryFrom($value->Attribute()->Type) !== ProductAttributeType::PRICE_COLLECTION) {
throw new \RuntimeException('Not a price collection attribute value');
}

Expand Down

0 comments on commit b82e8b8

Please sign in to comment.