123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Eav attribute set model
- *
- * @method int getEntityTypeId()
- * @method \Magento\Eav\Model\Entity\Attribute\Set setEntityTypeId(int $value)
- * @method string getAttributeSetName()
- * @method \Magento\Eav\Model\Entity\Attribute\Set setAttributeSetName(string $value)
- * @method int getSortOrder()
- * @method \Magento\Eav\Model\Entity\Attribute\Set setSortOrder(int $value)
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- namespace Magento\Eav\Model\Entity\Attribute;
- use Magento\Eav\Model\Entity\Type;
- use Magento\Framework\Api\AttributeValueFactory;
- use Magento\Framework\Exception\LocalizedException;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Set extends \Magento\Framework\Model\AbstractExtensibleModel implements
- \Magento\Eav\Api\Data\AttributeSetInterface
- {
- /**#@+
- * Constants
- */
- const KEY_ATTRIBUTE_SET_ID = 'attribute_set_id';
- const KEY_ATTRIBUTE_SET_NAME = 'attribute_set_name';
- const KEY_SORT_ORDER = 'sort_order';
- const KEY_ENTITY_TYPE_ID = 'entity_type_id';
- /**#@-*/
- /**#@-*/
- protected $_resource;
- /**
- * Prefix of model events names
- *
- * @var string
- */
- protected $_eventPrefix = 'eav_entity_attribute_set';
- /**
- * @var \Magento\Eav\Model\Config
- */
- protected $_eavConfig;
- /**
- * @var \Magento\Eav\Model\Entity\Attribute\GroupFactory
- */
- protected $_attrGroupFactory;
- /**
- * @var \Magento\Eav\Model\Entity\AttributeFactory
- */
- protected $_attributeFactory;
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
- */
- protected $_resourceAttribute;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
- * @param AttributeValueFactory $customAttributeFactory
- * @param \Magento\Eav\Model\Config $eavConfig
- * @param GroupFactory $attrGroupFactory
- * @param \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceAttribute
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
- AttributeValueFactory $customAttributeFactory,
- \Magento\Eav\Model\Config $eavConfig,
- \Magento\Eav\Model\Entity\Attribute\GroupFactory $attrGroupFactory,
- \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
- \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceAttribute,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = []
- ) {
- parent::__construct(
- $context,
- $registry,
- $extensionFactory,
- $customAttributeFactory,
- $resource,
- $resourceCollection,
- $data
- );
- $this->_eavConfig = $eavConfig;
- $this->_attrGroupFactory = $attrGroupFactory;
- $this->_attributeFactory = $attributeFactory;
- $this->_resourceAttribute = $resourceAttribute;
- }
- /**
- * Initialize resource model
- *
- * @return void
- * @codeCoverageIgnore
- */
- protected function _construct()
- {
- $this->_init(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set::class);
- }
- /**
- * Init attribute set from skeleton (another attribute set)
- *
- * @param int $skeletonId
- * @return $this
- */
- public function initFromSkeleton($skeletonId)
- {
- $groups = $this->_attrGroupFactory->create()->getResourceCollection()->setAttributeSetFilter(
- $skeletonId
- )->load();
- $newGroups = [];
- foreach ($groups as $group) {
- $newGroup = clone $group;
- $newGroup->setId(null)->setAttributeSetId($this->getId())->setDefaultId($group->getDefaultId());
- $groupAttributesCollection = $this->_attributeFactory
- ->create()
- ->getResourceCollection()
- ->setAttributeGroupFilter(
- $group->getId()
- )->load();
- $newAttributes = [];
- foreach ($groupAttributesCollection as $attribute) {
- $newAttribute = $this->_attributeFactory->create()
- ->setId($attribute->getId())
- //->setAttributeGroupId($newGroup->getId())
- ->setAttributeSetId($this->getId())
- ->setEntityTypeId($this->getEntityTypeId())
- ->setSortOrder($attribute->getSortOrder());
- $newAttributes[] = $newAttribute;
- }
- $newGroup->setAttributes($newAttributes);
- $newGroups[] = $newGroup;
- }
- $this->setGroups($newGroups);
- return $this;
- }
- /**
- * Collect data for save
- *
- * @param array $data
- * @return $this
- * @throws LocalizedException
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function organizeData($data)
- {
- $modelGroupArray = [];
- $modelAttributeArray = [];
- $attributeIds = [];
- if ($data['attributes']) {
- $ids = [];
- foreach ($data['attributes'] as $attribute) {
- $ids[] = $attribute[0];
- }
- $attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids);
- }
- if ($data['groups']) {
- foreach ($data['groups'] as $group) {
- $modelGroup = $this->initGroupModel($group);
- if ($data['attributes']) {
- foreach ($data['attributes'] as $attribute) {
- if ($attribute[1] == $group[0] && in_array($attribute[0], $attributeIds)) {
- $modelAttribute = $this->_attributeFactory->create();
- $modelAttribute->setId(
- $attribute[0]
- )->setAttributeGroupId(
- $attribute[1]
- )->setAttributeSetId(
- $this->getId()
- )->setEntityTypeId(
- $this->getEntityTypeId()
- )->setSortOrder(
- $attribute[2]
- );
- $modelAttributeArray[] = $modelAttribute;
- }
- }
- $modelGroup->setAttributes($modelAttributeArray);
- $modelAttributeArray = [];
- }
- $modelGroupArray[] = $modelGroup;
- }
- $this->setGroups($modelGroupArray);
- }
- if ($data['not_attributes']) {
- $modelAttributeArray = [];
- $data['not_attributes'] = array_filter($data['not_attributes']);
- foreach ($data['not_attributes'] as $entityAttributeId) {
- $entityAttribute = $this->_resourceAttribute->getEntityAttribute($entityAttributeId);
- if (!$entityAttribute) {
- throw new LocalizedException(
- __(
- 'The entity attribute with the "%1" ID isn\'t found. Reset the attribute and try again.',
- $entityAttributeId
- )
- );
- }
- $modelAttribute = $this->_eavConfig->getAttribute(
- $this->getEntityTypeId(),
- $entityAttribute['attribute_id']
- );
- $modelAttribute->setEntityAttributeId($entityAttributeId);
- $modelAttributeArray[] = $modelAttribute;
- }
- $this->setRemoveAttributes($modelAttributeArray);
- }
- if ($data['removeGroups']) {
- $modelGroupArray = [];
- foreach ($data['removeGroups'] as $groupId) {
- $modelGroup = $this->_attrGroupFactory->create();
- $modelGroup->setId($groupId);
- $modelGroupArray[] = $modelGroup;
- }
- $this->setRemoveGroups($modelGroupArray);
- }
- $this->setAttributeSetName($data['attribute_set_name'])->setEntityTypeId($this->getEntityTypeId());
- return $this;
- }
- /**
- * @param array $group
- * @return Group
- */
- private function initGroupModel($group)
- {
- $modelGroup = $this->_attrGroupFactory->create();
- $modelGroup->setId(
- is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null
- )->setAttributeGroupName(
- $group[1]
- )->setAttributeSetId(
- $this->getId()
- )->setSortOrder(
- $group[2]
- );
- if ($modelGroup->getId()) {
- $group = $this->_attrGroupFactory->create()->load($modelGroup->getId());
- if ($group->getId()) {
- $modelGroup->setAttributeGroupCode($group->getAttributeGroupCode());
- }
- }
- return $modelGroup;
- }
- /**
- * Validate attribute set name
- *
- * @return bool
- * @throws LocalizedException
- */
- public function validate()
- {
- $attributeSetName = $this->getAttributeSetName();
- if ($attributeSetName == '') {
- throw new LocalizedException(__('The attribute set name is empty. Enter the name and try again.'));
- }
- if (!$this->_getResource()->validate($this, $attributeSetName)) {
- throw new LocalizedException(
- __('A "%1" attribute set name already exists. Create a new name and try again.', $attributeSetName)
- );
- }
- return true;
- }
- /**
- * Add set info to attributes
- *
- * @param string|Type $entityType
- * @param array $attributes
- * @param int $setId
- * @return $this
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- public function addSetInfo($entityType, array $attributes, $setId = null)
- {
- $attributeIds = [];
- $entityType = $this->_eavConfig->getEntityType($entityType);
- foreach ($attributes as $attribute) {
- $attribute = $this->_eavConfig->getAttribute($entityType, $attribute);
- if ($setId && is_array($attribute->getAttributeSetInfo($setId))) {
- continue;
- }
- if (!$attribute->getAttributeId()) {
- continue;
- }
- $attributeIds[] = $attribute->getAttributeId();
- }
- if ($attributeIds) {
- $setInfo = $this->_getResource()->getSetInfo($attributeIds, $setId);
- foreach ($attributes as $attribute) {
- $attribute = $this->_eavConfig->getAttribute($entityType, $attribute);
- if (!$attribute->getAttributeId()) {
- continue;
- }
- if (!in_array($attribute->getAttributeId(), $attributeIds)) {
- continue;
- }
- if (is_numeric($setId)) {
- $attributeSetInfo = $attribute->getAttributeSetInfo();
- if (!is_array($attributeSetInfo)) {
- $attributeSetInfo = [];
- }
- if (isset($setInfo[$attribute->getAttributeId()][$setId])) {
- $attributeSetInfo[$setId] = $setInfo[$attribute->getAttributeId()][$setId];
- }
- $attribute->setAttributeSetInfo($attributeSetInfo);
- } else {
- if (isset($setInfo[$attribute->getAttributeId()])) {
- $attribute->setAttributeSetInfo($setInfo[$attribute->getAttributeId()]);
- } else {
- $attribute->setAttributeSetInfo([]);
- }
- }
- }
- }
- return $this;
- }
- /**
- * Return default Group Id for current or defined Attribute Set
- *
- * @param int $setId
- * @return int|null
- */
- public function getDefaultGroupId($setId = null)
- {
- if ($setId === null) {
- $setId = $this->getId();
- }
- return $setId ? $this->_getResource()->getDefaultGroupId($setId) : null;
- }
- /**
- * Get resource instance
- *
- * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
- * @deprecated 101.0.0 because resource models should be used directly
- */
- protected function _getResource()
- {
- return $this->_resource ?: parent::_getResource();
- }
- /**
- * {@inheritdoc}
- * @codeCoverageIgnoreStart
- */
- public function getAttributeSetId()
- {
- return $this->getData(self::KEY_ATTRIBUTE_SET_ID);
- }
- /**
- * {@inheritdoc}
- */
- public function getAttributeSetName()
- {
- return $this->getData(self::KEY_ATTRIBUTE_SET_NAME);
- }
- /**
- * {@inheritdoc}
- */
- public function getSortOrder()
- {
- return $this->getData(self::KEY_SORT_ORDER);
- }
- /**
- * {@inheritdoc}
- */
- public function getEntityTypeId()
- {
- return $this->getData(self::KEY_ENTITY_TYPE_ID);
- }
- /**
- * Set attribute set name.
- *
- * @param string $name
- * @return void
- */
- public function setName($name)
- {
- $this->setData('attribute_set_name', $name);
- }
- /**
- * Set attribute set ID
- *
- * @param int $attributeSetId
- * @return $this
- */
- public function setAttributeSetId($attributeSetId)
- {
- return $this->setData(self::KEY_ATTRIBUTE_SET_ID, $attributeSetId);
- }
- /**
- * Set attribute set name
- *
- * @param string $attributeSetName
- * @return $this
- */
- public function setAttributeSetName($attributeSetName)
- {
- return $this->setData(self::KEY_ATTRIBUTE_SET_NAME, $attributeSetName);
- }
- /**
- * Set attribute set sort order index
- *
- * @param int $sortOrder
- * @return $this
- */
- public function setSortOrder($sortOrder)
- {
- return $this->setData(self::KEY_SORT_ORDER, $sortOrder);
- }
- /**
- * Set attribute set entity type id
- *
- * @param int $entityTypeId
- * @return $this
- */
- public function setEntityTypeId($entityTypeId)
- {
- return $this->setData(self::KEY_ENTITY_TYPE_ID, $entityTypeId);
- }
- /**
- * {@inheritdoc}
- *
- * @return \Magento\Eav\Api\Data\AttributeSetExtensionInterface|null|null
- */
- public function getExtensionAttributes()
- {
- return $this->_getExtensionAttributes();
- }
- /**
- * {@inheritdoc}
- *
- * @param \Magento\Eav\Api\Data\AttributeSetExtensionInterface|null $extensionAttributes
- * @return $this
- */
- public function setExtensionAttributes(\Magento\Eav\Api\Data\AttributeSetExtensionInterface $extensionAttributes)
- {
- return $this->_setExtensionAttributes($extensionAttributes);
- }
- //@codeCoverageIgnoreEnd
- }
|