Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:ctidigital/magento2-configurator…
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
chevli committed Mar 31, 2017
2 parents 5749cc8 + c5ed559 commit 33bc26c
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 3 deletions.
148 changes: 148 additions & 0 deletions Model/Component/AttributeSets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace CtiDigital\Configurator\Model\Component;

use CtiDigital\Configurator\Model\Exception\ComponentException;
use CtiDigital\Configurator\Model\LoggingInterface;
use Magento\Eav\Api\AttributeSetRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Api\Data\AttributeSetInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\ObjectManagerInterface;
use Magento\Eav\Model\AttributeSetRepository;

/**
* Class AttributeSets
* @package CtiDigital\Configurator\Model\Component
* @SuppressWarnings(PHPMD.LongVariable)
*/
class AttributeSets extends YamlComponentAbstract
{
protected $alias = 'attribute_sets';
protected $name = 'Attribute Sets';
protected $description = 'Component to create/maintain attribute sets.';

/**
* @var EavSetup
*/
protected $eavSetup;

/**
* @var AttributeSetRepository
*/
protected $attributeSetRepository;

/**
* AttributeSets constructor.
* @param LoggingInterface $log
* @param ObjectManagerInterface $objectManager
* @param EavSetup $eavSetup
* @param AttributeSetRepositoryInterface $attributeSetRepository
*/
public function __construct(
LoggingInterface $log,
ObjectManagerInterface $objectManager,
EavSetup $eavSetup,
AttributeSetRepositoryInterface $attributeSetRepository
) {

parent::__construct($log, $objectManager);

$this->eavSetup = $eavSetup;
$this->attributeSetRepository = $attributeSetRepository;
}

/**
* @param array $attributeConfigurationData
*/
protected function processData($attributeConfigurationData = null)
{
try {
foreach ($attributeConfigurationData['attribute_sets'] as $attributeSetConfiguration) {
$this->processAttributeSet($attributeSetConfiguration);
}
} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
}
}

/**
* @param array $attributeSetConfig
*/
protected function processAttributeSet(array $attributeSetConfig)
{
$this->eavSetup->addAttributeSet(Product::ENTITY, $attributeSetConfig['name']);

$attributeSetId = $this->eavSetup->getAttributeSetId(Product::ENTITY, $attributeSetConfig['name']);
$attributeSetEntity = $this->attributeSetRepository->get($attributeSetId);
if (array_key_exists('inherit', $attributeSetConfig)) {
$attributeSetEntity->initFromSkeleton($this->getAttributeSetId($attributeSetConfig['inherit']));
$this->attributeSetRepository->save($attributeSetEntity);
}

if (array_key_exists('groups', $attributeSetConfig) && count($attributeSetConfig['groups']) > 0) {
$this->addAttributeGroups($attributeSetEntity, $attributeSetConfig['groups']);
$this->addAttributeGroupAssociations($attributeSetEntity, $attributeSetConfig['groups']);
}
}

/**
* @param AttributeSetInterface $attributeSetEntity
* @param array $attributeGroupData
*/
protected function addAttributeGroups(AttributeSetInterface $attributeSetEntity, array $attributeGroupData)
{
/* if ($attributeSetEntity->getDefaultGroupId()) {
$this->eavSetup->removeAttributeGroup(
Product::ENTITY,
$attributeSetEntity->getId(),
$attributeSetEntity->getDefaultGroupId()
);
}*/

foreach ($attributeGroupData as $group) {
$attributeSetName = $attributeSetEntity->getAttributeSetName();
$this->eavSetup->addAttributeGroup(Product::ENTITY, $attributeSetName, $group['name']);
}
}

/**
* @param AttributeSetInterface $attributeSetEntity
* @param array $attributeGroupData
*/
protected function addAttributeGroupAssociations(
AttributeSetInterface $attributeSetEntity,
array $attributeGroupData
) {
foreach ($attributeGroupData as $group) {
foreach ($group['attributes'] as $attributeCode) {
$attributeData = $this->eavSetup->getAttribute(Product::ENTITY, $attributeCode);

if (count($attributeData) === 0) {
throw new ComponentException("Attribute '{$attributeCode}' does not exist.");
}

$this->eavSetup->addAttributeToGroup(
Product::ENTITY,
$attributeSetEntity->getId(),
$group['name'],
$attributeCode
);
}
}
}

/**
* @param $attributeSetName
* @return string
*/
protected function getAttributeSetId($attributeSetName)
{
$attributeSetData = $this->eavSetup->getAttributeSet(Product::ENTITY, $attributeSetName);
if (array_key_exists('attribute_set_id', $attributeSetData)) {
return $attributeSetData['attribute_set_id'];
}

throw new ComponentException('Could not find attribute set name.');
}
}
192 changes: 192 additions & 0 deletions Model/Component/Attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

namespace CtiDigital\Configurator\Model\Component;

use CtiDigital\Configurator\Model\Exception\ComponentException;
use CtiDigital\Configurator\Model\LoggingInterface;
use Magento\Catalog\Model\Product;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\ObjectManagerInterface;

/**
* Class Attributes
* @package CtiDigital\Configurator\Model\Component
* @SuppressWarnings(PHPMD.LongVariable)
*/
class Attributes extends YamlComponentAbstract
{

protected $alias = 'attributes';
protected $name = 'Attributes';
protected $description = 'Component to create/maintain attributes.';

/**
* @var EavSetup
*/
protected $eavSetup;

/**
* @var array
*/
protected $cachedAttributeConfig;

/**
* @var Product\Attribute\Repository
*/
protected $productAttributeRepository;

public function __construct(
LoggingInterface $log,
ObjectManagerInterface $objectManager,
EavSetup $eavSetup,
Product\Attribute\Repository $repository
) {
parent::__construct($log, $objectManager);
$this->eavSetup = $eavSetup;
$this->productAttributeRepository = $repository;
}

/**
* @param array $attributeConfigurationData
*/
protected function processData($attributeConfigurationData = null)
{
try {
foreach ($attributeConfigurationData['attributes'] as $attributeCode => $attributeConfiguration) {
$this->processAttribute($attributeCode, $attributeConfiguration);
}
} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
}
}

/**
* @param $attributeCode
* @param $attributeConfig
*/
protected function processAttribute($attributeCode, array $attributeConfig)
{
$updateAttribute = true;
$attributeExists = false;
$attributeArray = $this->eavSetup->getAttribute(Product::ENTITY, $attributeCode);
if ($attributeArray && $attributeArray['attribute_id']) {
$attributeExists = true;
$this->log->logComment(sprintf('Attribute %s exists. Checking for updates.', $attributeCode));
$updateAttribute = $this->checkForAttributeUpdates($attributeCode, $attributeArray, $attributeConfig);

if (isset($attributeConfig['option'])) {
$newAttributeOptions = $this->manageAttributeOptions($attributeCode, $attributeConfig['option']);
$attributeConfig['option']['values'] = $newAttributeOptions;
}
}

if ($updateAttribute) {

$attributeConfig['user_defined'] = 1;

if (isset($attributeConfig['product_types'])) {
$attributeConfig['apply_to'] = implode(',', $attributeConfig['product_types']);
}

$this->eavSetup->addAttribute(
Product::ENTITY,
$attributeCode,
$attributeConfig
);

if ($attributeExists) {
$this->log->logInfo(sprintf('Attribute %s updated.', $attributeCode));
return;
}

$this->log->logInfo(sprintf('Attribute %s created.', $attributeCode));
}
}

protected function checkForAttributeUpdates($attributeCode, $attributeArray, $attributeConfig)
{
$requiresUpdate = false;
$nest = 1;
foreach ($attributeConfig as $name => $value) {

if ($name == "product_types") {
$value = implode(',', $value);
}

$name = $this->mapAttributeConfig($name);

if ($name == 'option') {
continue;
}

if (!isset($attributeArray[$name])) {
$this->log->logError(sprintf(
'Attribute %s type %s does not exist or is not mapped',
$attributeCode,
$name
), $nest);
continue;
}

if ($attributeArray[$name] != $value) {
$this->log->logInfo(sprintf(
'Update required for %s as %s is "%s" but should be "%s"',
$attributeCode,
$name,
$attributeArray[$name],
$value
), $nest);

$requiresUpdate = true;

continue;
}

$this->log->logComment(sprintf(
'No Update required for %s as %s is still "%s"',
$attributeCode,
$name,
$value
), $nest);
}

return $requiresUpdate;
}

protected function mapAttributeConfig($name)
{
if ($name == 'label') {
$name = 'frontend_label';
}

if ($name == 'type') {
$name = 'backend_type';
}

if ($name == 'input') {
$name = 'frontend_input';
}

if ($name == 'product_types') {
$name = 'apply_to';
}
return $name;
}

private function manageAttributeOptions($attributeCode, $option)
{
$attributeOptions = $this->productAttributeRepository->get($attributeCode)->getOptions();

// Loop through existing attributes options
$existingAttributeOptions = array();
foreach ($attributeOptions as $attributeOption) {
$value = $attributeOption->getLabel();
$existingAttributeOptions[] = $value;
}

$optionsToAdd = array_diff($option['values'], $existingAttributeOptions);
//$optionsToRemove = array_diff($existingAttributeOptions, $option['values']);

return $optionsToAdd;
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Do also include sample files with your component that works
| System Configuration | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Categories | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Products | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Attributes | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Blocks | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Admin Roles | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Admin Users | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
Expand All @@ -56,7 +57,6 @@ Do also include sample files with your component that works
| Tax Rules | :white_check_mark: | :grey_exclamation: | :white_check_mark: |
| Tax Rates | :x: | :x: | :x: |
| Attribute Sets | :x: | :x: | :x: |
| Attributes | :x: | :x: | :x: |
| Customers | :x: | :x: | :x: |
| Related Products | :x: | :x: | :x: |
| SQL | :x: | :x: | :x: |
Expand Down
39 changes: 39 additions & 0 deletions Samples/Components/Attributes/attribute_sets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
attribute_sets:
-
name: Shirts
inherit: Default
groups:
-
name: General
attributes:
- colour
- color
-
name: Prices
attributes:
- rrp
- test_attr
-
name: Example Attribute Set 2
inherit: Default
groups:
-
name: Prices
attributes:
- rrp
-
name: Matthew Attribute Set
inherit: Default
groups:
-
name: Prices
attributes:
- rrp
-
name: Matthew Attribute Set 2
groups:
-
name: Prices
attributes:
- rrp

Loading

0 comments on commit 33bc26c

Please sign in to comment.