Set.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;
  7. class Set extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  8. {
  9. /**
  10. * EAV cache id
  11. */
  12. const ATTRIBUTES_CACHE_ID = 'EAV_ENTITY_ATTRIBUTES_BY_SET_ID';
  13. /**
  14. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\GroupFactory
  15. */
  16. protected $_attrGroupFactory;
  17. /**
  18. * @var \Magento\Eav\Model\Config
  19. */
  20. protected $eavConfig;
  21. /**
  22. * Constructor
  23. *
  24. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  25. * @param GroupFactory $attrGroupFactory
  26. * @param \Magento\Eav\Model\Config $eavConfig
  27. * @param string|null $connectionName
  28. */
  29. public function __construct(
  30. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  31. \Magento\Eav\Model\ResourceModel\Entity\Attribute\GroupFactory $attrGroupFactory,
  32. \Magento\Eav\Model\Config $eavConfig,
  33. $connectionName = null
  34. ) {
  35. parent::__construct($context, $connectionName);
  36. $this->_attrGroupFactory = $attrGroupFactory;
  37. $this->eavConfig = $eavConfig;
  38. }
  39. /**
  40. * Initialize connection
  41. *
  42. * @return void
  43. * @codeCoverageIgnore
  44. */
  45. protected function _construct()
  46. {
  47. $this->_init('eav_attribute_set', 'attribute_set_id');
  48. }
  49. /**
  50. * Perform actions after object save
  51. *
  52. * @param \Magento\Framework\Model\AbstractModel $object
  53. * @return $this
  54. */
  55. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  56. {
  57. if ($object->getGroups()) {
  58. /* @var $group \Magento\Eav\Model\Entity\Attribute\Group */
  59. foreach ($object->getGroups() as $group) {
  60. $group->setAttributeSetId($object->getId());
  61. if ($group->itemExists() && !$group->getId()) {
  62. continue;
  63. }
  64. $group->save();
  65. }
  66. }
  67. if ($object->getRemoveGroups()) {
  68. foreach ($object->getRemoveGroups() as $group) {
  69. /* @var $group \Magento\Eav\Model\Entity\Attribute\Group */
  70. $group->delete();
  71. }
  72. $this->_attrGroupFactory->create()->updateDefaultGroup($object->getId());
  73. }
  74. if ($object->getRemoveAttributes()) {
  75. foreach ($object->getRemoveAttributes() as $attribute) {
  76. /* @var $attribute \Magento\Eav\Model\Entity\Attribute */
  77. $attribute->deleteEntity();
  78. }
  79. }
  80. return parent::_afterSave($object);
  81. }
  82. /**
  83. * Perform actions before object delete
  84. *
  85. * @param \Magento\Framework\Model\AbstractModel $object
  86. * @return $this
  87. * @throws \Magento\Framework\Exception\StateException
  88. * @throws \Magento\Framework\Exception\LocalizedException
  89. */
  90. protected function _beforeDelete(\Magento\Framework\Model\AbstractModel $object)
  91. {
  92. /** @var \Magento\Eav\Api\Data\AttributeSetInterface $object */
  93. $defaultAttributeSetId = $this->eavConfig
  94. ->getEntityType($object->getEntityTypeId())
  95. ->getDefaultAttributeSetId();
  96. if ($object->getAttributeSetId() == $defaultAttributeSetId) {
  97. throw new \Magento\Framework\Exception\StateException(
  98. __('The default attribute set can\'t be deleted.')
  99. );
  100. }
  101. return parent::_beforeDelete($object);
  102. }
  103. /**
  104. * Validate attribute set name
  105. *
  106. * @param \Magento\Eav\Model\Entity\Attribute\Set $object
  107. * @param string $attributeSetName
  108. * @return bool
  109. */
  110. public function validate($object, $attributeSetName)
  111. {
  112. $connection = $this->getConnection();
  113. $bind = ['attribute_set_name' => trim($attributeSetName), 'entity_type_id' => $object->getEntityTypeId()];
  114. $select = $connection->select()->from(
  115. $this->getMainTable()
  116. )->where(
  117. 'attribute_set_name = :attribute_set_name'
  118. )->where(
  119. 'entity_type_id = :entity_type_id'
  120. );
  121. if ($object->getId()) {
  122. $bind['attribute_set_id'] = $object->getId();
  123. $select->where('attribute_set_id != :attribute_set_id');
  124. }
  125. return !$connection->fetchOne($select, $bind) ? true : false;
  126. }
  127. /**
  128. * Retrieve Set info by attributes
  129. *
  130. * @param array $attributeIds
  131. * @param int $setId
  132. * @return array
  133. */
  134. public function getSetInfo(array $attributeIds, $setId = null)
  135. {
  136. $cacheKey = self::ATTRIBUTES_CACHE_ID . $setId;
  137. if ($this->eavConfig->isCacheEnabled() && ($cache = $this->eavConfig->getCache()->load($cacheKey))) {
  138. $setInfoData = $this->getSerializer()->unserialize($cache);
  139. } else {
  140. $attributeSetData = $this->fetchAttributeSetData($setId);
  141. $setInfoData = [];
  142. foreach ($attributeSetData as $row) {
  143. $data = [
  144. 'group_id' => $row['attribute_group_id'],
  145. 'group_sort' => $row['group_sort_order'],
  146. 'sort' => $row['sort_order'],
  147. ];
  148. $setInfoData[$row['attribute_id']][$row['attribute_set_id']] = $data;
  149. }
  150. if ($this->eavConfig->isCacheEnabled()) {
  151. $this->eavConfig->getCache()->save(
  152. $this->getSerializer()->serialize($setInfoData),
  153. $cacheKey,
  154. [
  155. \Magento\Eav\Model\Cache\Type::CACHE_TAG,
  156. \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
  157. ]
  158. );
  159. }
  160. }
  161. $setInfo = [];
  162. foreach ($attributeIds as $attributeId) {
  163. $setInfo[$attributeId] = isset($setInfoData[$attributeId]) ? $setInfoData[$attributeId] : [];
  164. }
  165. return $setInfo;
  166. }
  167. /**
  168. * Return default attribute group id for attribute set id
  169. *
  170. * @param int $setId
  171. * @return int|null
  172. */
  173. public function getDefaultGroupId($setId)
  174. {
  175. $connection = $this->getConnection();
  176. $bind = ['attribute_set_id' => (int)$setId];
  177. $select = $connection->select()->from(
  178. $this->getTable('eav_attribute_group'),
  179. 'attribute_group_id'
  180. )->where(
  181. 'attribute_set_id = :attribute_set_id'
  182. )->where(
  183. 'default_id = 1'
  184. )->limit(
  185. 1
  186. );
  187. return $connection->fetchOne($select, $bind);
  188. }
  189. /**
  190. * Returns data from eav_entity_attribute table for given $setId (or all if $setId is null)
  191. *
  192. * @param int $setId
  193. * @return array
  194. */
  195. protected function fetchAttributeSetData($setId = null)
  196. {
  197. $connection = $this->getConnection();
  198. $select = $connection->select()->from(
  199. ['entity' => $this->getTable('eav_entity_attribute')],
  200. ['attribute_id', 'attribute_set_id', 'attribute_group_id', 'sort_order']
  201. )->joinLeft(
  202. ['attribute_group' => $this->getTable('eav_attribute_group')],
  203. 'entity.attribute_group_id = attribute_group.attribute_group_id',
  204. ['group_sort_order' => 'sort_order']
  205. );
  206. $bind = [];
  207. if (is_numeric($setId)) {
  208. $bind[':attribute_set_id'] = $setId;
  209. $select->where('entity.attribute_set_id = :attribute_set_id');
  210. }
  211. return $connection->fetchAll($select, $bind);
  212. }
  213. }