PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Set.php

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