-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Expand file tree
/
Copy pathSet.php
More file actions
495 lines (450 loc) · 15.4 KB
/
Set.php
File metadata and controls
495 lines (450 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* Copyright 2013 Adobe
* All Rights Reserved.
*/
/**
* 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)
*/
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
*/
public const KEY_ATTRIBUTE_SET_ID = 'attribute_set_id';
public const KEY_ATTRIBUTE_SET_NAME = 'attribute_set_name';
public const KEY_SORT_ORDER = 'sort_order';
public const KEY_ENTITY_TYPE_ID = 'entity_type_id';
/**#@-*/
/** @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb */
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 (isset($data['attributes'])) {
$ids = [];
foreach ($data['attributes'] as $attribute) {
$ids[] = $attribute[0];
}
$attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids);
}
if (isset($data['groups'])) {
foreach ($data['groups'] as $group) {
$modelGroup = $this->initGroupModel($group);
if (isset($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 (isset($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 (isset($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;
}
/**
* Initialise a group model
*
* @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
}