Set.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Eav attribute set model
  8. *
  9. * @method int getEntityTypeId()
  10. * @method \Magento\Eav\Model\Entity\Attribute\Set setEntityTypeId(int $value)
  11. * @method string getAttributeSetName()
  12. * @method \Magento\Eav\Model\Entity\Attribute\Set setAttributeSetName(string $value)
  13. * @method int getSortOrder()
  14. * @method \Magento\Eav\Model\Entity\Attribute\Set setSortOrder(int $value)
  15. *
  16. * @author Magento Core Team <core@magentocommerce.com>
  17. */
  18. namespace Magento\Eav\Model\Entity\Attribute;
  19. use Magento\Eav\Model\Entity\Type;
  20. use Magento\Framework\Api\AttributeValueFactory;
  21. use Magento\Framework\Exception\LocalizedException;
  22. /**
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class Set extends \Magento\Framework\Model\AbstractExtensibleModel implements
  26. \Magento\Eav\Api\Data\AttributeSetInterface
  27. {
  28. /**#@+
  29. * Constants
  30. */
  31. const KEY_ATTRIBUTE_SET_ID = 'attribute_set_id';
  32. const KEY_ATTRIBUTE_SET_NAME = 'attribute_set_name';
  33. const KEY_SORT_ORDER = 'sort_order';
  34. const KEY_ENTITY_TYPE_ID = 'entity_type_id';
  35. /**#@-*/
  36. /**#@-*/
  37. protected $_resource;
  38. /**
  39. * Prefix of model events names
  40. *
  41. * @var string
  42. */
  43. protected $_eventPrefix = 'eav_entity_attribute_set';
  44. /**
  45. * @var \Magento\Eav\Model\Config
  46. */
  47. protected $_eavConfig;
  48. /**
  49. * @var \Magento\Eav\Model\Entity\Attribute\GroupFactory
  50. */
  51. protected $_attrGroupFactory;
  52. /**
  53. * @var \Magento\Eav\Model\Entity\AttributeFactory
  54. */
  55. protected $_attributeFactory;
  56. /**
  57. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
  58. */
  59. protected $_resourceAttribute;
  60. /**
  61. * @param \Magento\Framework\Model\Context $context
  62. * @param \Magento\Framework\Registry $registry
  63. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  64. * @param AttributeValueFactory $customAttributeFactory
  65. * @param \Magento\Eav\Model\Config $eavConfig
  66. * @param GroupFactory $attrGroupFactory
  67. * @param \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory
  68. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceAttribute
  69. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  70. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  71. * @param array $data
  72. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  73. * @codeCoverageIgnore
  74. */
  75. public function __construct(
  76. \Magento\Framework\Model\Context $context,
  77. \Magento\Framework\Registry $registry,
  78. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  79. AttributeValueFactory $customAttributeFactory,
  80. \Magento\Eav\Model\Config $eavConfig,
  81. \Magento\Eav\Model\Entity\Attribute\GroupFactory $attrGroupFactory,
  82. \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
  83. \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceAttribute,
  84. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  85. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  86. array $data = []
  87. ) {
  88. parent::__construct(
  89. $context,
  90. $registry,
  91. $extensionFactory,
  92. $customAttributeFactory,
  93. $resource,
  94. $resourceCollection,
  95. $data
  96. );
  97. $this->_eavConfig = $eavConfig;
  98. $this->_attrGroupFactory = $attrGroupFactory;
  99. $this->_attributeFactory = $attributeFactory;
  100. $this->_resourceAttribute = $resourceAttribute;
  101. }
  102. /**
  103. * Initialize resource model
  104. *
  105. * @return void
  106. * @codeCoverageIgnore
  107. */
  108. protected function _construct()
  109. {
  110. $this->_init(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set::class);
  111. }
  112. /**
  113. * Init attribute set from skeleton (another attribute set)
  114. *
  115. * @param int $skeletonId
  116. * @return $this
  117. */
  118. public function initFromSkeleton($skeletonId)
  119. {
  120. $groups = $this->_attrGroupFactory->create()->getResourceCollection()->setAttributeSetFilter(
  121. $skeletonId
  122. )->load();
  123. $newGroups = [];
  124. foreach ($groups as $group) {
  125. $newGroup = clone $group;
  126. $newGroup->setId(null)->setAttributeSetId($this->getId())->setDefaultId($group->getDefaultId());
  127. $groupAttributesCollection = $this->_attributeFactory
  128. ->create()
  129. ->getResourceCollection()
  130. ->setAttributeGroupFilter(
  131. $group->getId()
  132. )->load();
  133. $newAttributes = [];
  134. foreach ($groupAttributesCollection as $attribute) {
  135. $newAttribute = $this->_attributeFactory->create()
  136. ->setId($attribute->getId())
  137. //->setAttributeGroupId($newGroup->getId())
  138. ->setAttributeSetId($this->getId())
  139. ->setEntityTypeId($this->getEntityTypeId())
  140. ->setSortOrder($attribute->getSortOrder());
  141. $newAttributes[] = $newAttribute;
  142. }
  143. $newGroup->setAttributes($newAttributes);
  144. $newGroups[] = $newGroup;
  145. }
  146. $this->setGroups($newGroups);
  147. return $this;
  148. }
  149. /**
  150. * Collect data for save
  151. *
  152. * @param array $data
  153. * @return $this
  154. * @throws LocalizedException
  155. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  156. * @SuppressWarnings(PHPMD.NPathComplexity)
  157. */
  158. public function organizeData($data)
  159. {
  160. $modelGroupArray = [];
  161. $modelAttributeArray = [];
  162. $attributeIds = [];
  163. if ($data['attributes']) {
  164. $ids = [];
  165. foreach ($data['attributes'] as $attribute) {
  166. $ids[] = $attribute[0];
  167. }
  168. $attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids);
  169. }
  170. if ($data['groups']) {
  171. foreach ($data['groups'] as $group) {
  172. $modelGroup = $this->initGroupModel($group);
  173. if ($data['attributes']) {
  174. foreach ($data['attributes'] as $attribute) {
  175. if ($attribute[1] == $group[0] && in_array($attribute[0], $attributeIds)) {
  176. $modelAttribute = $this->_attributeFactory->create();
  177. $modelAttribute->setId(
  178. $attribute[0]
  179. )->setAttributeGroupId(
  180. $attribute[1]
  181. )->setAttributeSetId(
  182. $this->getId()
  183. )->setEntityTypeId(
  184. $this->getEntityTypeId()
  185. )->setSortOrder(
  186. $attribute[2]
  187. );
  188. $modelAttributeArray[] = $modelAttribute;
  189. }
  190. }
  191. $modelGroup->setAttributes($modelAttributeArray);
  192. $modelAttributeArray = [];
  193. }
  194. $modelGroupArray[] = $modelGroup;
  195. }
  196. $this->setGroups($modelGroupArray);
  197. }
  198. if ($data['not_attributes']) {
  199. $modelAttributeArray = [];
  200. $data['not_attributes'] = array_filter($data['not_attributes']);
  201. foreach ($data['not_attributes'] as $entityAttributeId) {
  202. $entityAttribute = $this->_resourceAttribute->getEntityAttribute($entityAttributeId);
  203. if (!$entityAttribute) {
  204. throw new LocalizedException(
  205. __(
  206. 'The entity attribute with the "%1" ID isn\'t found. Reset the attribute and try again.',
  207. $entityAttributeId
  208. )
  209. );
  210. }
  211. $modelAttribute = $this->_eavConfig->getAttribute(
  212. $this->getEntityTypeId(),
  213. $entityAttribute['attribute_id']
  214. );
  215. $modelAttribute->setEntityAttributeId($entityAttributeId);
  216. $modelAttributeArray[] = $modelAttribute;
  217. }
  218. $this->setRemoveAttributes($modelAttributeArray);
  219. }
  220. if ($data['removeGroups']) {
  221. $modelGroupArray = [];
  222. foreach ($data['removeGroups'] as $groupId) {
  223. $modelGroup = $this->_attrGroupFactory->create();
  224. $modelGroup->setId($groupId);
  225. $modelGroupArray[] = $modelGroup;
  226. }
  227. $this->setRemoveGroups($modelGroupArray);
  228. }
  229. $this->setAttributeSetName($data['attribute_set_name'])->setEntityTypeId($this->getEntityTypeId());
  230. return $this;
  231. }
  232. /**
  233. * @param array $group
  234. * @return Group
  235. */
  236. private function initGroupModel($group)
  237. {
  238. $modelGroup = $this->_attrGroupFactory->create();
  239. $modelGroup->setId(
  240. is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null
  241. )->setAttributeGroupName(
  242. $group[1]
  243. )->setAttributeSetId(
  244. $this->getId()
  245. )->setSortOrder(
  246. $group[2]
  247. );
  248. if ($modelGroup->getId()) {
  249. $group = $this->_attrGroupFactory->create()->load($modelGroup->getId());
  250. if ($group->getId()) {
  251. $modelGroup->setAttributeGroupCode($group->getAttributeGroupCode());
  252. }
  253. }
  254. return $modelGroup;
  255. }
  256. /**
  257. * Validate attribute set name
  258. *
  259. * @return bool
  260. * @throws LocalizedException
  261. */
  262. public function validate()
  263. {
  264. $attributeSetName = $this->getAttributeSetName();
  265. if ($attributeSetName == '') {
  266. throw new LocalizedException(__('The attribute set name is empty. Enter the name and try again.'));
  267. }
  268. if (!$this->_getResource()->validate($this, $attributeSetName)) {
  269. throw new LocalizedException(
  270. __('A "%1" attribute set name already exists. Create a new name and try again.', $attributeSetName)
  271. );
  272. }
  273. return true;
  274. }
  275. /**
  276. * Add set info to attributes
  277. *
  278. * @param string|Type $entityType
  279. * @param array $attributes
  280. * @param int $setId
  281. * @return $this
  282. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  283. */
  284. public function addSetInfo($entityType, array $attributes, $setId = null)
  285. {
  286. $attributeIds = [];
  287. $entityType = $this->_eavConfig->getEntityType($entityType);
  288. foreach ($attributes as $attribute) {
  289. $attribute = $this->_eavConfig->getAttribute($entityType, $attribute);
  290. if ($setId && is_array($attribute->getAttributeSetInfo($setId))) {
  291. continue;
  292. }
  293. if (!$attribute->getAttributeId()) {
  294. continue;
  295. }
  296. $attributeIds[] = $attribute->getAttributeId();
  297. }
  298. if ($attributeIds) {
  299. $setInfo = $this->_getResource()->getSetInfo($attributeIds, $setId);
  300. foreach ($attributes as $attribute) {
  301. $attribute = $this->_eavConfig->getAttribute($entityType, $attribute);
  302. if (!$attribute->getAttributeId()) {
  303. continue;
  304. }
  305. if (!in_array($attribute->getAttributeId(), $attributeIds)) {
  306. continue;
  307. }
  308. if (is_numeric($setId)) {
  309. $attributeSetInfo = $attribute->getAttributeSetInfo();
  310. if (!is_array($attributeSetInfo)) {
  311. $attributeSetInfo = [];
  312. }
  313. if (isset($setInfo[$attribute->getAttributeId()][$setId])) {
  314. $attributeSetInfo[$setId] = $setInfo[$attribute->getAttributeId()][$setId];
  315. }
  316. $attribute->setAttributeSetInfo($attributeSetInfo);
  317. } else {
  318. if (isset($setInfo[$attribute->getAttributeId()])) {
  319. $attribute->setAttributeSetInfo($setInfo[$attribute->getAttributeId()]);
  320. } else {
  321. $attribute->setAttributeSetInfo([]);
  322. }
  323. }
  324. }
  325. }
  326. return $this;
  327. }
  328. /**
  329. * Return default Group Id for current or defined Attribute Set
  330. *
  331. * @param int $setId
  332. * @return int|null
  333. */
  334. public function getDefaultGroupId($setId = null)
  335. {
  336. if ($setId === null) {
  337. $setId = $this->getId();
  338. }
  339. return $setId ? $this->_getResource()->getDefaultGroupId($setId) : null;
  340. }
  341. /**
  342. * Get resource instance
  343. *
  344. * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  345. * @deprecated 101.0.0 because resource models should be used directly
  346. */
  347. protected function _getResource()
  348. {
  349. return $this->_resource ?: parent::_getResource();
  350. }
  351. /**
  352. * {@inheritdoc}
  353. * @codeCoverageIgnoreStart
  354. */
  355. public function getAttributeSetId()
  356. {
  357. return $this->getData(self::KEY_ATTRIBUTE_SET_ID);
  358. }
  359. /**
  360. * {@inheritdoc}
  361. */
  362. public function getAttributeSetName()
  363. {
  364. return $this->getData(self::KEY_ATTRIBUTE_SET_NAME);
  365. }
  366. /**
  367. * {@inheritdoc}
  368. */
  369. public function getSortOrder()
  370. {
  371. return $this->getData(self::KEY_SORT_ORDER);
  372. }
  373. /**
  374. * {@inheritdoc}
  375. */
  376. public function getEntityTypeId()
  377. {
  378. return $this->getData(self::KEY_ENTITY_TYPE_ID);
  379. }
  380. /**
  381. * Set attribute set name.
  382. *
  383. * @param string $name
  384. * @return void
  385. */
  386. public function setName($name)
  387. {
  388. $this->setData('attribute_set_name', $name);
  389. }
  390. /**
  391. * Set attribute set ID
  392. *
  393. * @param int $attributeSetId
  394. * @return $this
  395. */
  396. public function setAttributeSetId($attributeSetId)
  397. {
  398. return $this->setData(self::KEY_ATTRIBUTE_SET_ID, $attributeSetId);
  399. }
  400. /**
  401. * Set attribute set name
  402. *
  403. * @param string $attributeSetName
  404. * @return $this
  405. */
  406. public function setAttributeSetName($attributeSetName)
  407. {
  408. return $this->setData(self::KEY_ATTRIBUTE_SET_NAME, $attributeSetName);
  409. }
  410. /**
  411. * Set attribute set sort order index
  412. *
  413. * @param int $sortOrder
  414. * @return $this
  415. */
  416. public function setSortOrder($sortOrder)
  417. {
  418. return $this->setData(self::KEY_SORT_ORDER, $sortOrder);
  419. }
  420. /**
  421. * Set attribute set entity type id
  422. *
  423. * @param int $entityTypeId
  424. * @return $this
  425. */
  426. public function setEntityTypeId($entityTypeId)
  427. {
  428. return $this->setData(self::KEY_ENTITY_TYPE_ID, $entityTypeId);
  429. }
  430. /**
  431. * {@inheritdoc}
  432. *
  433. * @return \Magento\Eav\Api\Data\AttributeSetExtensionInterface|null|null
  434. */
  435. public function getExtensionAttributes()
  436. {
  437. return $this->_getExtensionAttributes();
  438. }
  439. /**
  440. * {@inheritdoc}
  441. *
  442. * @param \Magento\Eav\Api\Data\AttributeSetExtensionInterface|null $extensionAttributes
  443. * @return $this
  444. */
  445. public function setExtensionAttributes(\Magento\Eav\Api\Data\AttributeSetExtensionInterface $extensionAttributes)
  446. {
  447. return $this->_setExtensionAttributes($extensionAttributes);
  448. }
  449. //@codeCoverageIgnoreEnd
  450. }