Skip to content

Commit

Permalink
Merge pull request #93 from akeneo/release/100.3.0
Browse files Browse the repository at this point in the history
Release/100.3.0
  • Loading branch information
Dnd-Gimix authored Nov 28, 2019
2 parents f54d538 + b1d5130 commit 6ba944f
Show file tree
Hide file tree
Showing 15 changed files with 652 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Block/Adminhtml/System/Config/Form/Field/Configurable.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function renderCellTemplate($columnName)
/** @var array $options */
$options = [
self::TYPE_DEFAULT => __('Product model value'),
self::TYPE_SIMPLE => __('First Variant value'),
self::TYPE_SIMPLE => __('First Variation value'),
self::TYPE_MAPPING => __('Mapping'),
self::TYPE_QUERY => __('SQL Statement'),
self::TYPE_VALUE => __('Default value'),
Expand Down
129 changes: 129 additions & 0 deletions Block/Adminhtml/System/Config/Form/Field/Metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Akeneo\Connector\Block\Adminhtml\System\Config\Form\Field;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Model\Entity\Attribute\Source\Boolean as BooleanModel;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Data\Form\Element\Factory;
use Magento\Framework\Data\Form\Element\Select;
use Akeneo\Connector\Model\Source\Attribute\Metrics as MetricsSource;

/**
* Class Metrics
*
* @package Akeneo\Connector\Block\Adminhtml\System\Config\Form\Field
* @author Agence Dn'D <[email protected]>
* @copyright 2019 Agence Dn'D
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.dnd.fr/
*/
class Metrics extends AbstractFieldArray
{
/**
* This variable contains a Factory
*
* @var Factory $elementFactory
*/
protected $elementFactory;
/**
* This variable contains a SearchCriteriaBuilder
*
* @var SearchCriteriaBuilder $searchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
/**
* This variable contains a AttributeRepositoryInterface
*
* @var AttributeRepositoryInterface $attributeRepository
*/
protected $attributeRepository;
/**
* This variable contains a BooleanModel
*
* @var BooleanModel $booleanModel
*/
protected $booleanModel;
/**
* This variable contains a MetricsSource
*
* @var MetricsSource $metricsSource
*/
protected $metricsSource;

/**
* Image constructor
*
* @param Context $context
* @param Factory $elementFactory
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param AttributeRepositoryInterface $attributeRepository
* @param MetricsSource $metricsSource
* @param array $data
*/
public function __construct(
Context $context,
Factory $elementFactory,
SearchCriteriaBuilder $searchCriteriaBuilder,
AttributeRepositoryInterface $attributeRepository,
MetricsSource $metricsSource,
BooleanModel $booleanModel,
array $data = []
) {
parent::__construct($context, $data);

$this->elementFactory = $elementFactory;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->attributeRepository = $attributeRepository;
$this->metricsSource = $metricsSource;
$this->booleanModel = $booleanModel;
}

/**
* Initialise form fields
*
* @return void
*/
protected function _construct()
{
$this->addColumn('akeneo_metrics', ['label' => __('Akeneo Metric Attribute')]);
$this->addColumn('is_variant', ['label' => __('Used As Variant')]);
$this->addColumn('is_concat', ['label' => __('Concat Metric Unit')]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');

parent::_construct();
}

/**
* Render array cell for prototypeJS template
*
* @param string $columnName
*
* @return string
* @throws \Exception
*/
public function renderCellTemplate($columnName)
{
/** @var string[] $options */
$options = [];

if ($columnName == 'akeneo_metrics') {
$options = $this->metricsSource->getAllOptions();
}

if ($columnName == 'is_variant' || $columnName == 'is_concat') {
$options = $this->booleanModel->getAllOptions();
}

/** @var Select $element */
$element = $this->elementFactory->create('select');
$element->setForm($this->getForm())->setName($this->_getCellInputElementName($columnName))->setHtmlId(
$this->_getCellInputElementId('<%- _id %>', $columnName)
)->setValues($options);

return str_replace("\n", '', $element->getElementHtml());
}
}
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@

### Version 100.2.6 :
* Add check to prevent the creation of attributes and options with empty admin label
* Fix product association deletion with differential product import
* Fix product association deletion with differential product import

### Version 100.3.0 :
* Remove Akeneo attribute group import from connector (https://help.akeneo.com/magento2-connector/v100/articles/where-attributes.html#where-to-find-my-attribute-groups-in-magento-2)
* Remove automatic mapping for attributes "price", "special_price" and "cost" (https://help.akeneo.com/magento2-connector/v100/articles/what-data.html#attribute-types)
* Add metric as product variant and unit concatenation feature (https://help.akeneo.com/magento2-connector/v100/articles/05-configure-products.html#metric-attributes)
* Update wording for configurable product attribute mapping
59 changes: 55 additions & 4 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Config extends AbstractHelper
const PRODUCT_MEDIA_ENABLED = 'akeneo_connector/product/media_enabled';
const PRODUCT_MEDIA_IMAGES = 'akeneo_connector/product/media_images';
const PRODUCT_MEDIA_GALLERY = 'akeneo_connector/product/media_gallery';
const PRODUCT_METRICS = 'akeneo_connector/product/metrics';
const ATTRIBUTE_TYPES = 'akeneo_connector/attribute/types';
/**
* @var int PAGINATION_SIZE_DEFAULT_VALUE
Expand Down Expand Up @@ -204,9 +205,9 @@ public function getAkeneoApiClientSecret()
/**
* Retrieve the filter mode used
*
* @return string
* @see \Akeneo\Connector\Model\Source\Filters\Mode
*
* @return string
*/
public function getFilterMode()
{
Expand All @@ -216,9 +217,9 @@ public function getFilterMode()
/**
* Retrieve the type of filter to apply on the completeness
*
* @return string
* @see \Akeneo\Connector\Model\Source\Filters\Completeness
*
* @return string
*/
public function getCompletenessTypeFilter()
{
Expand Down Expand Up @@ -248,9 +249,9 @@ public function getCompletenessLocalesFilter()
/**
* Retrieve the status filter
*
* @return string
* @see \Akeneo\Connector\Model\Source\Filters\Status
*
* @return string
*/
public function getStatusFilter()
{
Expand Down Expand Up @@ -404,9 +405,10 @@ public function getWebsiteAttribute()
* Retrieve website mapping
*
* @param bool $withDefault
* @throws \Exception
*
* @return mixed[]
* @throws \Exception
*
*/
public function getWebsiteMapping($withDefault = true)
{
Expand Down Expand Up @@ -676,6 +678,55 @@ public function getMediaImportGalleryColumns()
return $images;
}

/**
* Retrieve metrics columns by define return needed
*
* @param bool|null $returnVariant
* @param bool|null $returnConcat
*
* @return array|mixed[]
*/
public function getMetricsColumns($returnVariant = null, $returnConcat = null)
{
/** @var array $metrics */
$metrics = [];
/** @var string $config */
$config = $this->scopeConfig->getValue(self::PRODUCT_METRICS);
if (!$config) {
return $metrics;
}

/** @var array $unserializeMetrics */
$unserializeMetrics = $this->serializer->unserialize($config);
if (!$unserializeMetrics) {
return $metrics;
}

/** @var mixed[] $metricsColumns */
$metricsColumns = [];
foreach ($unserializeMetrics as $unserializeMetric) {
if ($returnVariant === true && $returnConcat === null && $unserializeMetric['is_variant'] == 0) {
continue;
}
if ($returnVariant === null && $returnConcat === true && $unserializeMetric['is_concat'] == 0) {
continue;
}
if ($returnVariant === true && $returnConcat === false && $unserializeMetric['is_concat'] == 1) {
continue;
}
if ($returnVariant === false && $returnConcat === true && $unserializeMetric['is_variant'] == 1) {
continue;
}

/** @var string $metricAttributeCode */
$metricAttributeCode = $unserializeMetric['akeneo_metrics'];

$metricsColumns[] = $metricAttributeCode;
}

return $metricsColumns;
}

/**
* Check if media file exists
*
Expand Down
1 change: 1 addition & 0 deletions Helper/Import/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function getType($pimType = 'default')
'pim_catalog_identifier' => 'text',
'pim_catalog_text' => 'text',
'pim_catalog_metric' => 'text',
'pim_catalog_metric_select' => 'select',
'pim_catalog_number' => 'text',
'pim_catalog_textarea' => 'textarea',
'pim_catalog_date' => 'date',
Expand Down
72 changes: 72 additions & 0 deletions Helper/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace Akeneo\Connector\Helper\Import;

use Akeneo\Connector\Helper\Config as ConfigHelper;
use Akeneo\Connector\Helper\Serializer as JsonSerializer;
use Magento\Catalog\Model\Product as BaseProductModel;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\ResourceConnection;

/**
* Class Product
*
Expand All @@ -26,6 +33,34 @@ class Product extends Entities
* @var string VALUES_KEY
*/
const VALUES_KEY = 'values';
/**
* This variable contains a JsonSerializer
*
* @var JsonSerializer $serializer
*/
protected $serializer;

/**
* Product constructor
*
* @param Context $context
* @param ResourceConnection $connection
* @param DeploymentConfig $deploymentConfig
* @param BaseProductModel $product
* @param ConfigHelper $configHelper
* @param JsonSerializer $serializer
*/
public function __construct(
Context $context,
ResourceConnection $connection,
DeploymentConfig $deploymentConfig,
BaseProductModel $product,
ConfigHelper $configHelper,
JsonSerializer $serializer
) {
$this->serializer = $serializer;
parent::__construct($context, $connection, $deploymentConfig, $product, $configHelper);
}

/**
* Get columns from the api result
Expand Down Expand Up @@ -113,6 +148,15 @@ private function formatValues(array $values)
* @var array $value
*/
foreach ($values as $attribute => $value) {
if ($attribute === 'price' && !$this->isFieldInAttributeMapping('price')) {
continue;
}
if ($attribute === 'special_price' && !$this->isFieldInAttributeMapping('special_price')) {
continue;
}
if ($attribute === 'cost' && !$this->isFieldInAttributeMapping('cost')) {
continue;
}
/** @var array $attributeValue */
foreach ($value as $attributeValue) {
/** @var string $key */
Expand Down Expand Up @@ -213,4 +257,32 @@ private function getKey($attribute, array $attributeValue)

return (string)$key;
}

/**
* Check if a given attribute is mapped magento-side
*
* @param string $field
*
* @return bool
*/
public function isFieldInAttributeMapping($field)
{
/** @var string|array $matches */
$matches = $this->scopeConfig->getValue(ConfigHelper::PRODUCT_ATTRIBUTE_MAPPING);
$matches = $this->serializer->unserialize($matches);
if (!is_array($matches)) {
return false;
}
/** @var string[] $match */
foreach ($matches as $match) {
if (!isset($match['akeneo_attribute'], $match['magento_attribute'])) {
continue;
}
if ($match['magento_attribute'] === $field) {
return true;
}
}

return false;
}
}
Loading

0 comments on commit 6ba944f

Please sign in to comment.