Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Set.php
Go to the documentation of this file.
1 <?php
20 
24 
30 {
34  const KEY_ATTRIBUTE_SET_ID = 'attribute_set_id';
35  const KEY_ATTRIBUTE_SET_NAME = 'attribute_set_name';
36  const KEY_SORT_ORDER = 'sort_order';
37  const KEY_ENTITY_TYPE_ID = 'entity_type_id';
41  protected $_resource;
42 
48  protected $_eventPrefix = 'eav_entity_attribute_set';
49 
53  protected $_eavConfig;
54 
58  protected $_attrGroupFactory;
59 
63  protected $_attributeFactory;
64 
69 
85  public function __construct(
86  \Magento\Framework\Model\Context $context,
87  \Magento\Framework\Registry $registry,
88  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
90  \Magento\Eav\Model\Config $eavConfig,
91  \Magento\Eav\Model\Entity\Attribute\GroupFactory $attrGroupFactory,
92  \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory,
93  \Magento\Eav\Model\ResourceModel\Entity\Attribute $resourceAttribute,
94  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
95  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
96  array $data = []
97  ) {
98  parent::__construct(
99  $context,
100  $registry,
101  $extensionFactory,
103  $resource,
104  $resourceCollection,
105  $data
106  );
107  $this->_eavConfig = $eavConfig;
108  $this->_attrGroupFactory = $attrGroupFactory;
109  $this->_attributeFactory = $attributeFactory;
110  $this->_resourceAttribute = $resourceAttribute;
111  }
112 
119  protected function _construct()
120  {
121  $this->_init(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set::class);
122  }
123 
130  public function initFromSkeleton($skeletonId)
131  {
132  $groups = $this->_attrGroupFactory->create()->getResourceCollection()->setAttributeSetFilter(
133  $skeletonId
134  )->load();
135 
136  $newGroups = [];
137  foreach ($groups as $group) {
138  $newGroup = clone $group;
139  $newGroup->setId(null)->setAttributeSetId($this->getId())->setDefaultId($group->getDefaultId());
140 
141  $groupAttributesCollection = $this->_attributeFactory
142  ->create()
143  ->getResourceCollection()
144  ->setAttributeGroupFilter(
145  $group->getId()
146  )->load();
147 
148  $newAttributes = [];
149  foreach ($groupAttributesCollection as $attribute) {
150  $newAttribute = $this->_attributeFactory->create()
151  ->setId($attribute->getId())
152  //->setAttributeGroupId($newGroup->getId())
153  ->setAttributeSetId($this->getId())
154  ->setEntityTypeId($this->getEntityTypeId())
155  ->setSortOrder($attribute->getSortOrder());
156  $newAttributes[] = $newAttribute;
157  }
158  $newGroup->setAttributes($newAttributes);
159  $newGroups[] = $newGroup;
160  }
161  $this->setGroups($newGroups);
162 
163  return $this;
164  }
165 
175  public function organizeData($data)
176  {
177  $modelGroupArray = [];
178  $modelAttributeArray = [];
179  $attributeIds = [];
180  if ($data['attributes']) {
181  $ids = [];
182  foreach ($data['attributes'] as $attribute) {
183  $ids[] = $attribute[0];
184  }
185  $attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids);
186  }
187  if ($data['groups']) {
188  foreach ($data['groups'] as $group) {
189  $modelGroup = $this->initGroupModel($group);
190 
191  if ($data['attributes']) {
192  foreach ($data['attributes'] as $attribute) {
193  if ($attribute[1] == $group[0] && in_array($attribute[0], $attributeIds)) {
194  $modelAttribute = $this->_attributeFactory->create();
195  $modelAttribute->setId(
196  $attribute[0]
197  )->setAttributeGroupId(
198  $attribute[1]
199  )->setAttributeSetId(
200  $this->getId()
201  )->setEntityTypeId(
202  $this->getEntityTypeId()
203  )->setSortOrder(
204  $attribute[2]
205  );
206  $modelAttributeArray[] = $modelAttribute;
207  }
208  }
209  $modelGroup->setAttributes($modelAttributeArray);
210  $modelAttributeArray = [];
211  }
212  $modelGroupArray[] = $modelGroup;
213  }
214  $this->setGroups($modelGroupArray);
215  }
216 
217  if ($data['not_attributes']) {
218  $modelAttributeArray = [];
219  $data['not_attributes'] = array_filter($data['not_attributes']);
220  foreach ($data['not_attributes'] as $entityAttributeId) {
221  $entityAttribute = $this->_resourceAttribute->getEntityAttribute($entityAttributeId);
222  if (!$entityAttribute) {
223  throw new LocalizedException(
224  __(
225  'The entity attribute with the "%1" ID isn\'t found. Reset the attribute and try again.',
226  $entityAttributeId
227  )
228  );
229  }
230  $modelAttribute = $this->_eavConfig->getAttribute(
231  $this->getEntityTypeId(),
232  $entityAttribute['attribute_id']
233  );
234  $modelAttribute->setEntityAttributeId($entityAttributeId);
235  $modelAttributeArray[] = $modelAttribute;
236  }
237  $this->setRemoveAttributes($modelAttributeArray);
238  }
239 
240  if ($data['removeGroups']) {
241  $modelGroupArray = [];
242  foreach ($data['removeGroups'] as $groupId) {
243  $modelGroup = $this->_attrGroupFactory->create();
244  $modelGroup->setId($groupId);
245 
246  $modelGroupArray[] = $modelGroup;
247  }
248  $this->setRemoveGroups($modelGroupArray);
249  }
250  $this->setAttributeSetName($data['attribute_set_name'])->setEntityTypeId($this->getEntityTypeId());
251 
252  return $this;
253  }
254 
259  private function initGroupModel($group)
260  {
261  $modelGroup = $this->_attrGroupFactory->create();
262  $modelGroup->setId(
263  is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null
264  )->setAttributeGroupName(
265  $group[1]
266  )->setAttributeSetId(
267  $this->getId()
268  )->setSortOrder(
269  $group[2]
270  );
271  if ($modelGroup->getId()) {
272  $group = $this->_attrGroupFactory->create()->load($modelGroup->getId());
273  if ($group->getId()) {
274  $modelGroup->setAttributeGroupCode($group->getAttributeGroupCode());
275  }
276  }
277  return $modelGroup;
278  }
279 
286  public function validate()
287  {
288  $attributeSetName = $this->getAttributeSetName();
289  if ($attributeSetName == '') {
290  throw new LocalizedException(__('The attribute set name is empty. Enter the name and try again.'));
291  }
292 
293  if (!$this->_getResource()->validate($this, $attributeSetName)) {
294  throw new LocalizedException(
295  __('A "%1" attribute set name already exists. Create a new name and try again.', $attributeSetName)
296  );
297  }
298 
299  return true;
300  }
301 
311  public function addSetInfo($entityType, array $attributes, $setId = null)
312  {
313  $attributeIds = [];
314  $entityType = $this->_eavConfig->getEntityType($entityType);
315  foreach ($attributes as $attribute) {
316  $attribute = $this->_eavConfig->getAttribute($entityType, $attribute);
317  if ($setId && is_array($attribute->getAttributeSetInfo($setId))) {
318  continue;
319  }
320  if (!$attribute->getAttributeId()) {
321  continue;
322  }
323  $attributeIds[] = $attribute->getAttributeId();
324  }
325 
326  if ($attributeIds) {
327  $setInfo = $this->_getResource()->getSetInfo($attributeIds, $setId);
328 
329  foreach ($attributes as $attribute) {
330  $attribute = $this->_eavConfig->getAttribute($entityType, $attribute);
331  if (!$attribute->getAttributeId()) {
332  continue;
333  }
334  if (!in_array($attribute->getAttributeId(), $attributeIds)) {
335  continue;
336  }
337  if (is_numeric($setId)) {
338  $attributeSetInfo = $attribute->getAttributeSetInfo();
339  if (!is_array($attributeSetInfo)) {
340  $attributeSetInfo = [];
341  }
342  if (isset($setInfo[$attribute->getAttributeId()][$setId])) {
343  $attributeSetInfo[$setId] = $setInfo[$attribute->getAttributeId()][$setId];
344  }
345  $attribute->setAttributeSetInfo($attributeSetInfo);
346  } else {
347  if (isset($setInfo[$attribute->getAttributeId()])) {
348  $attribute->setAttributeSetInfo($setInfo[$attribute->getAttributeId()]);
349  } else {
350  $attribute->setAttributeSetInfo([]);
351  }
352  }
353  }
354  }
355 
356  return $this;
357  }
358 
365  public function getDefaultGroupId($setId = null)
366  {
367  if ($setId === null) {
368  $setId = $this->getId();
369  }
370 
371  return $setId ? $this->_getResource()->getDefaultGroupId($setId) : null;
372  }
373 
380  protected function _getResource()
381  {
382  return $this->_resource ?: parent::_getResource();
383  }
384 
389  public function getAttributeSetId()
390  {
391  return $this->getData(self::KEY_ATTRIBUTE_SET_ID);
392  }
393 
397  public function getAttributeSetName()
398  {
399  return $this->getData(self::KEY_ATTRIBUTE_SET_NAME);
400  }
401 
405  public function getSortOrder()
406  {
407  return $this->getData(self::KEY_SORT_ORDER);
408  }
409 
413  public function getEntityTypeId()
414  {
415  return $this->getData(self::KEY_ENTITY_TYPE_ID);
416  }
417 
424  public function setName($name)
425  {
426  $this->setData('attribute_set_name', $name);
427  }
428 
436  {
437  return $this->setData(self::KEY_ATTRIBUTE_SET_ID, $attributeSetId);
438  }
439 
446  public function setAttributeSetName($attributeSetName)
447  {
448  return $this->setData(self::KEY_ATTRIBUTE_SET_NAME, $attributeSetName);
449  }
450 
457  public function setSortOrder($sortOrder)
458  {
459  return $this->setData(self::KEY_SORT_ORDER, $sortOrder);
460  }
461 
469  {
470  return $this->setData(self::KEY_ENTITY_TYPE_ID, $entityTypeId);
471  }
472 
478  public function getExtensionAttributes()
479  {
480  return $this->_getExtensionAttributes();
481  }
482 
489  public function setExtensionAttributes(\Magento\Eav\Api\Data\AttributeSetExtensionInterface $extensionAttributes)
490  {
491  return $this->_setExtensionAttributes($extensionAttributes);
492  }
493 
494  //@codeCoverageIgnoreEnd
495 }
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
addSetInfo($entityType, array $attributes, $setId=null)
Definition: Set.php:311
setAttributeSetName($attributeSetName)
Definition: Set.php:446
$attributes
Definition: matrix.phtml:13
setExtensionAttributes(\Magento\Eav\Api\Data\AttributeSetExtensionInterface $extensionAttributes)
Definition: Set.php:489
__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=[])
Definition: Set.php:85
setAttributeSetId($attributeSetId)
Definition: Set.php:435
if(!isset($_GET['name'])) $name
Definition: log.php:14