Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EavAttribute.php
Go to the documentation of this file.
1 <?php
7 
14 
19 {
20  const DEFAULT_STORE_ID = 0;
21 
25  private $swatchResource;
26 
30  const BASE_OPTION_TITLE = 'option';
31 
36 
40  protected $swatchFactory;
41 
45  protected $swatchHelper;
46 
52  protected $dependencyArray = [];
53 
59  protected $isSwatchExists;
60 
66  private $serializer;
67 
75  public function __construct(
76  \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory,
77  \Magento\Swatches\Model\SwatchFactory $swatchFactory,
78  \Magento\Swatches\Helper\Data $swatchHelper,
79  Json $serializer = null,
80  SwatchResource $swatchResource = null
81  ) {
82  $this->swatchCollectionFactory = $collectionFactory;
83  $this->swatchFactory = $swatchFactory;
84  $this->swatchHelper = $swatchHelper;
85  $this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class);
86  $this->swatchResource = $swatchResource ?: ObjectManager::getInstance()->create(SwatchResource::class);
87  }
88 
96  {
97  if ($this->swatchHelper->isSwatchAttribute($attribute)) {
98  $this->setProperOptionsArray($attribute);
99  $this->validateOptions($attribute);
100  $this->swatchHelper->assembleAdditionalDataEavAttribute($attribute);
101  }
102  $this->convertSwatchToDropdown($attribute);
103  }
104 
113  {
114  if ($this->swatchHelper->isSwatchAttribute($attribute)) {
115  $this->processSwatchOptions($attribute);
116  $this->saveDefaultSwatchOptionValue($attribute);
117  $this->saveSwatchParams($attribute);
118  }
119  }
120 
128  {
129  $canReplace = false;
130  if ($this->swatchHelper->isVisualSwatch($attribute)) {
131  $canReplace = true;
132  $defaultValue = $attribute->getData('defaultvisual');
133  $optionsArray = $attribute->getData('optionvisual');
134  $swatchesArray = $attribute->getData('swatchvisual');
135  } elseif ($this->swatchHelper->isTextSwatch($attribute)) {
136  $canReplace = true;
137  $defaultValue = $attribute->getData('defaulttext');
138  $optionsArray = $attribute->getData('optiontext');
139  $swatchesArray = $attribute->getData('swatchtext');
140  }
141  if ($canReplace == true) {
142  if (!empty($optionsArray)) {
143  $attribute->setData('option', $optionsArray);
144  }
145  if (!empty($defaultValue)) {
146  $attribute->setData('default', $defaultValue);
147  } else {
148  $attribute->setData('default', [0 => $attribute->getDefaultValue()]);
149  }
150  if (!empty($swatchesArray)) {
151  $attribute->setData('swatch', $swatchesArray);
152  }
153  }
154  }
155 
164  {
166  $additionalData = $attribute->getData('additional_data');
167  if (!empty($additionalData)) {
168  $additionalData = $this->serializer->unserialize($additionalData);
169  if (is_array($additionalData) && isset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY])) {
170  $option = $attribute->getOption() ?: [];
171  $this->cleanEavAttributeOptionSwatchValues($option);
172  unset($additionalData[Swatch::SWATCH_INPUT_TYPE_KEY]);
173  $attribute->setData('additional_data', $this->serializer->serialize($additionalData));
174  }
175  }
176  }
177  }
178 
187  {
188  $optionsArray = $attribute->getData('option');
189 
190  if (!empty($optionsArray) && is_array($optionsArray)) {
191  $optionsArray = $this->prepareOptionIds($optionsArray);
192  $attributeSavedOptions = $attribute->getSource()->getAllOptions();
193  $this->prepareOptionLinks($optionsArray, $attributeSavedOptions);
194  }
195 
196  return $attribute;
197  }
198 
205  protected function prepareOptionIds(array $optionsArray)
206  {
207  if (isset($optionsArray['value']) && is_array($optionsArray['value'])) {
208  foreach (array_keys($optionsArray['value']) as $optionId) {
209  if (isset($optionsArray['delete']) && isset($optionsArray['delete'][$optionId])
210  && $optionsArray['delete'][$optionId] == 1
211  ) {
212  unset($optionsArray['value'][$optionId]);
213  }
214  }
215  }
216  return $optionsArray;
217  }
218 
226  protected function prepareOptionLinks(array $optionsArray, array $attributeSavedOptions)
227  {
228  $dependencyArray = [];
229  if (is_array($optionsArray['value'])) {
230  $optionCounter = 1;
231  foreach (array_keys($optionsArray['value']) as $baseOptionId) {
232  $dependencyArray[$baseOptionId] = $attributeSavedOptions[$optionCounter]['value'];
233  $optionCounter++;
234  }
235  }
236 
237  $this->dependencyArray = $dependencyArray;
238  }
239 
247  {
248  if ($this->swatchHelper->isVisualSwatch($attribute)) {
249  $this->processVisualSwatch($attribute);
250  $attributeOptions = $attribute->getOptiontext() ?: [];
251  $this->cleanTextSwatchValuesAfterSwitch($attributeOptions);
252  } elseif ($this->swatchHelper->isTextSwatch($attribute)) {
253  $this->processTextualSwatch($attribute);
254  }
255  }
256 
264  {
265  $swatchArray = $attribute->getData('swatch/value');
266  if (isset($swatchArray) && is_array($swatchArray)) {
267  foreach ($swatchArray as $optionId => $value) {
269  $isOptionForDelete = $this->isOptionForDelete($attribute, $optionId);
270  if ($optionId === null || $isOptionForDelete) {
271  //option was deleted by button with basket
272  continue;
273  }
274  $swatch = $this->loadSwatchIfExists($optionId, self::DEFAULT_STORE_ID);
275 
276  $swatchType = $this->determineSwatchType($value);
277 
278  $this->saveSwatchData($swatch, $optionId, self::DEFAULT_STORE_ID, $swatchType, $value);
279  $this->isSwatchExists = null;
280  }
281  }
282  }
283 
291  private function cleanEavAttributeOptionSwatchValues(array $attributeOptions, int $swatchType = null)
292  {
293  if (count($attributeOptions) && isset($attributeOptions['value'])) {
294  $optionsIDs = array_keys($attributeOptions['value']);
295 
296  $this->swatchResource->clearSwatchOptionByOptionIdAndType($optionsIDs, $swatchType);
297  }
298  }
299 
306  private function cleanTextSwatchValuesAfterSwitch(array $attributeOptions)
307  {
308  $this->cleanEavAttributeOptionSwatchValues($attributeOptions, Swatch::SWATCH_TYPE_TEXTUAL);
309  }
310 
315  private function determineSwatchType($value)
316  {
317  $swatchType = Swatch::SWATCH_TYPE_EMPTY;
318  if (!empty($value) && $value[0] == '#') {
319  $swatchType = Swatch::SWATCH_TYPE_VISUAL_COLOR;
320  } elseif (!empty($value) && $value[0] == '/') {
321  $swatchType = Swatch::SWATCH_TYPE_VISUAL_IMAGE;
322  }
323  return $swatchType;
324  }
325 
333  {
334  $swatchArray = $attribute->getData('swatch/value');
335  if (isset($swatchArray) && is_array($swatchArray)) {
336  foreach ($swatchArray as $optionId => $storeValues) {
338  $isOptionForDelete = $this->isOptionForDelete($attribute, $optionId);
339  if ($optionId === null || $isOptionForDelete) {
340  //option was deleted by button with basket
341  continue;
342  }
343  $defaultSwatchValue = reset($storeValues);
344  foreach ($storeValues as $storeId => $value) {
345  if (!$value) {
346  $value = $defaultSwatchValue;
347  }
348  $swatch = $this->loadSwatchIfExists($optionId, $storeId);
349  $swatch->isDeleted($isOptionForDelete);
350  $this->saveSwatchData(
351  $swatch,
352  $optionId,
353  $storeId,
354  \Magento\Swatches\Model\Swatch::SWATCH_TYPE_TEXTUAL,
355  $value
356  );
357  $this->isSwatchExists = null;
358  }
359  }
360  }
361  }
362 
369  protected function getAttributeOptionId($optionId)
370  {
371  if (substr($optionId, 0, 6) == self::BASE_OPTION_TITLE) {
372  $optionId = isset($this->dependencyArray[$optionId]) ? $this->dependencyArray[$optionId] : null;
373  }
374  return $optionId;
375  }
376 
385  {
386  $isOptionForDelete = $attribute->getData('option/delete/' . $optionId);
387  return isset($isOptionForDelete) && $isOptionForDelete;
388  }
389 
397  protected function loadSwatchIfExists($optionId, $storeId)
398  {
399  $collection = $this->swatchCollectionFactory->create();
400  $collection->addFieldToFilter('option_id', $optionId);
401  $collection->addFieldToFilter('store_id', $storeId);
402  $collection->setPageSize(1);
403 
404  $loadedSwatch = $collection->getFirstItem();
405  if ($loadedSwatch->getId()) {
406  $this->isSwatchExists = true;
407  }
408  return $loadedSwatch;
409  }
410 
421  protected function saveSwatchData($swatch, $optionId, $storeId, $type, $value)
422  {
423  if ($this->isSwatchExists) {
424  $swatch->setData('type', $type);
425  $swatch->setData('value', $value);
426  } else {
427  $swatch->setData('option_id', $optionId);
428  $swatch->setData('store_id', $storeId);
429  $swatch->setData('type', $type);
430  $swatch->setData('value', $value);
431  }
432  $swatch->save();
433  }
434 
441  protected function saveDefaultSwatchOptionValue(Attribute $attribute)
442  {
443  if (!$this->swatchHelper->isSwatchAttribute($attribute)) {
444  return;
445  }
446  $defaultValue = $attribute->getData('default/0');
447  if (!empty($defaultValue)) {
449  $swatch = $this->swatchFactory->create();
450  // created and removed on frontend option not exists in dependency array
451  if (substr($defaultValue, 0, 6) == self::BASE_OPTION_TITLE &&
452  isset($this->dependencyArray[$defaultValue])
453  ) {
454  $defaultValue = $this->dependencyArray[$defaultValue];
455  }
456  $swatch->getResource()->saveDefaultSwatchOption($attribute->getId(), $defaultValue);
457  }
458  }
459 
468  {
469  $options = null;
470  if ($this->swatchHelper->isVisualSwatch($attribute)) {
471  $options = $attribute->getData('optionvisual');
472  } elseif ($this->swatchHelper->isTextSwatch($attribute)) {
473  $options = $attribute->getData('optiontext');
474  }
475  if ($options && !$this->isOptionsValid($options, $attribute)) {
476  throw new InputException(__('Admin is a required field in each row'));
477  }
478  return true;
479  }
480 
488  protected function isOptionsValid(array $options, Attribute $attribute)
489  {
490  if (!isset($options['value'])) {
491  return false;
492  }
493  foreach ($options['value'] as $optionId => $option) {
494  // do not validate options marked as deleted
495  if ($this->isOptionForDelete($attribute, $optionId)) {
496  continue;
497  }
498  if (!isset($option[0]) || $option[0] === '') {
499  return false;
500  }
501  }
502  return true;
503  }
504 
511  {
512  if ($this->swatchHelper->isSwatchAttribute($attribute)) {
513  return true;
514  }
515  return $result;
516  }
517 }
saveSwatchData($swatch, $optionId, $storeId, $type, $value)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
prepareOptionLinks(array $optionsArray, array $attributeSavedOptions)
__()
Definition: __.php:13
__construct(\Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory, \Magento\Swatches\Model\SwatchFactory $swatchFactory, \Magento\Swatches\Helper\Data $swatchHelper, Json $serializer=null, SwatchResource $swatchResource=null)
afterUsesSource(Attribute $attribute, $result)
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
isOptionForDelete(Attribute $attribute, $optionId)
isOptionsValid(array $options, Attribute $attribute)