/**
  * {@inheritdoc}
  */
 public function getStoreLabelsByAttributeId($attributeId)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getStoreLabelsByAttributeId');
     if (!$pluginInfo) {
         return parent::getStoreLabelsByAttributeId($attributeId);
     } else {
         return $this->___callPlugins('getStoreLabelsByAttributeId', func_get_args(), $pluginInfo);
     }
 }
 /**
  * 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(__('Entity attribute with id "%1" not found', $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;
 }
 /**
  * {@inheritdoc}
  */
 public function delete($entityType, $attributeCode, $optionId)
 {
     if (empty($attributeCode)) {
         throw new InputException(__('Empty attribute code'));
     }
     $attribute = $this->attributeRepository->get($entityType, $attributeCode);
     if (!$attribute->usesSource()) {
         throw new StateException(__('Attribute %1 doesn\'t have any option', $attributeCode));
     }
     $this->validateOption($attribute, $optionId);
     $removalMarker = ['option' => ['value' => [$optionId => []], 'delete' => [$optionId => '1']]];
     $attribute->addData($removalMarker);
     try {
         $this->resourceModel->save($attribute);
     } catch (\Exception $e) {
         throw new StateException(__('Cannot save attribute %1', $attributeCode));
     }
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function deleteById($attributeId)
 {
     /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
     $attribute = $this->attributeFactory->create();
     $this->eavResource->load($attribute, $attributeId);
     if (!$attribute->getAttributeId()) {
         throw new NoSuchEntityException(__('Attribute with id "%1" does not exist.', $attributeId));
     }
     $this->delete($attribute);
     return true;
 }
Beispiel #5
0
 /**
  * Collect data for save
  *
  * @param array $data
  * @return $this
  * @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->_attrGroupFactory->create();
             $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)->setAttributeGroupName($group[1])->setAttributeSetId($this->getId())->setSortOrder($group[2]);
             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 = [];
         foreach ($data['not_attributes'] as $attributeId) {
             $modelAttribute = $this->_attributeFactory->create();
             $modelAttribute->setEntityAttributeId($attributeId);
             $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;
 }
 /**
  * {@inheritdoc}
  */
 public function assign($entityTypeCode, $attributeSetId, $attributeGroupId, $attributeCode, $sortOrder)
 {
     try {
         $attributeSet = $this->setRepository->get($attributeSetId);
     } catch (NoSuchEntityException $ex) {
         throw new NoSuchEntityException(__('AttributeSet with id "%1" does not exist.', $attributeSetId));
     }
     $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
     if ($setEntityType->getEntityTypeCode() != $entityTypeCode) {
         throw new InputException(__('Wrong attribute set id provided'));
     }
     //Check if group exists. If not - expected exception
     $attributeGroup = $this->groupRepository->get($attributeGroupId);
     if ($attributeGroup->getAttributeSetId() != $attributeSetId) {
         throw new InputException(__('Attribute group does not belong to attribute set'));
     }
     /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
     $attribute = $this->attributeRepository->get($entityTypeCode, $attributeCode);
     $this->attributeResource->saveInSetIncluding($attribute, $attribute->getAttributeId(), $attributeSetId, $attributeGroupId, $sortOrder);
     $attribute->setAttributeSetId($attributeSetId);
     return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
 }
Beispiel #7
0
 /**
  * Retrieve Select For Flat Attribute update
  *
  * @param int $store
  * @return \Magento\Framework\DB\Select|null
  */
 public function getFlatUpdateSelect($store)
 {
     return $this->_eavEntityAttribute->getFlatUpdateSelect($this->getAttribute(), $store);
 }
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $this->_clearUselessAttributeValues($object);
     return parent::_afterSave($object);
 }
Beispiel #9
0
 /**
  * Save attribute/form relations after attribute save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _afterSave(AbstractModel $object)
 {
     $forms = $object->getData('used_in_forms');
     $connection = $this->getConnection();
     if (is_array($forms)) {
         $where = ['attribute_id=?' => $object->getId()];
         $connection->delete($this->_getFormAttributeTable(), $where);
         $data = [];
         foreach ($forms as $formCode) {
             $data[] = ['form_code' => $formCode, 'attribute_id' => (int) $object->getId()];
         }
         if ($data) {
             $connection->insertMultiple($this->_getFormAttributeTable(), $data);
         }
     }
     // update sort order
     if (!$object->isObjectNew() && $object->dataHasChangedFor('sort_order')) {
         $data = ['sort_order' => $object->getSortOrder()];
         $where = ['attribute_id=?' => (int) $object->getId()];
         $connection->update($this->getTable('eav_entity_attribute'), $data, $where);
     }
     // save scope attributes
     $websiteId = (int) $object->getWebsite()->getId();
     if ($websiteId) {
         $table = $this->_getEavWebsiteTable();
         $describe = $this->getConnection()->describeTable($table);
         $data = [];
         if (!$object->getScopeWebsiteId() || $object->getScopeWebsiteId() != $websiteId) {
             $data = $this->getScopeValues($object);
         }
         $data['attribute_id'] = (int) $object->getId();
         $data['website_id'] = (int) $websiteId;
         unset($describe['attribute_id']);
         unset($describe['website_id']);
         $updateColumns = [];
         foreach (array_keys($describe) as $columnName) {
             $data[$columnName] = $object->getData('scope_' . $columnName);
             $updateColumns[] = $columnName;
         }
         $connection->insertOnDuplicate($table, $data, $updateColumns);
     }
     return parent::_afterSave($object);
 }
 /**
  * {@inheritdoc}
  */
 public function getValidationRulesBeforeSave()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getValidationRulesBeforeSave');
     if (!$pluginInfo) {
         return parent::getValidationRulesBeforeSave();
     } else {
         return $this->___callPlugins('getValidationRulesBeforeSave', func_get_args(), $pluginInfo);
     }
 }