AbstractSwatch.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options;
  7. use \Magento\Swatches\Model\Swatch as SwatchModel;
  8. /**
  9. * Backend swatch abstract block
  10. */
  11. abstract class AbstractSwatch extends \Magento\Eav\Block\Adminhtml\Attribute\Edit\Options\Options
  12. {
  13. /**
  14. * @var \Magento\Catalog\Model\Product\Media\Config
  15. */
  16. protected $mediaConfig;
  17. /**
  18. * Helper to move image from tmp to catalog
  19. *
  20. * @var \Magento\Swatches\Helper\Media
  21. */
  22. protected $swatchHelper;
  23. /**
  24. * Prepare option values of user defined attribute
  25. *
  26. * @codeCoverageIgnore
  27. * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option $option
  28. * @param string $inputType
  29. * @param array $defaultValues
  30. * @return array
  31. */
  32. protected function _prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues)
  33. {
  34. $optionId = $option->getId();
  35. $value['checked'] = in_array($optionId, $defaultValues) ? 'checked="checked"' : '';
  36. $value['intype'] = $inputType;
  37. $value['id'] = $optionId;
  38. $value['sort_order'] = $option->getSortOrder();
  39. foreach ($this->getStores() as $store) {
  40. $value = array_merge(
  41. $value,
  42. $this->createStoreValues($store->getId(), $optionId)
  43. );
  44. }
  45. return [$value];
  46. }
  47. /**
  48. * @param \Magento\Backend\Block\Template\Context $context
  49. * @param \Magento\Framework\Registry $registry
  50. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
  51. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  52. * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
  53. * @param \Magento\Swatches\Helper\Media $swatchHelper
  54. * @param array $data
  55. */
  56. public function __construct(
  57. \Magento\Backend\Block\Template\Context $context,
  58. \Magento\Framework\Registry $registry,
  59. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
  60. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  61. \Magento\Catalog\Model\Product\Media\Config $mediaConfig,
  62. \Magento\Swatches\Helper\Media $swatchHelper,
  63. array $data = []
  64. ) {
  65. parent::__construct($context, $registry, $attrOptionCollectionFactory, $universalFactory, $data);
  66. $this->swatchHelper = $swatchHelper;
  67. $this->mediaConfig = $mediaConfig;
  68. }
  69. /**
  70. * Create store values
  71. *
  72. * Method not intended to escape HTML entities
  73. * Escaping will be applied in template files
  74. *
  75. * @param integer $storeId
  76. * @param integer $optionId
  77. * @return array
  78. */
  79. protected function createStoreValues($storeId, $optionId)
  80. {
  81. $value = [];
  82. $storeValues = $this->getStoreOptionValues($storeId);
  83. $swatchStoreValue = isset($storeValues['swatch']) ? $storeValues['swatch'] : null;
  84. $value['store' . $storeId] = isset($storeValues[$optionId]) ? $storeValues[$optionId] : '';
  85. $value['swatch' . $storeId] = isset($swatchStoreValue[$optionId]) ? $swatchStoreValue[$optionId] : '';
  86. return $value;
  87. }
  88. /**
  89. * Retrieve attribute option values for given store id
  90. *
  91. * @param int $storeId
  92. * @return array
  93. */
  94. public function getStoreOptionValues($storeId)
  95. {
  96. $values = $this->getData('store_option_values_' . $storeId);
  97. if ($values === null) {
  98. $values = [];
  99. $valuesCollection = $this->_attrOptionCollectionFactory->create();
  100. $valuesCollection->setAttributeFilter(
  101. $this->getAttributeObject()->getId()
  102. );
  103. $this->addCollectionStoreFilter($valuesCollection, $storeId);
  104. $valuesCollection->getSelect()->joinLeft(
  105. ['swatch_table' => $valuesCollection->getTable('eav_attribute_option_swatch')],
  106. 'swatch_table.option_id = main_table.option_id AND swatch_table.store_id = '.$storeId,
  107. 'swatch_table.value AS label'
  108. );
  109. $valuesCollection->load();
  110. foreach ($valuesCollection as $item) {
  111. $values[$item->getId()] = $item->getValue();
  112. $values['swatch'][$item->getId()] = $item->getLabel();
  113. }
  114. $this->setData('store_option_values_' . $storeId, $values);
  115. }
  116. return $values;
  117. }
  118. /**
  119. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $valuesCollection
  120. * @param int $storeId
  121. * @return void
  122. */
  123. private function addCollectionStoreFilter($valuesCollection, $storeId)
  124. {
  125. $joinCondition = $valuesCollection->getConnection()->quoteInto(
  126. 'tsv.option_id = main_table.option_id AND tsv.store_id = ?',
  127. $storeId
  128. );
  129. $select = $valuesCollection->getSelect();
  130. $select->joinLeft(
  131. ['tsv' => $valuesCollection->getTable('eav_attribute_option_value')],
  132. $joinCondition,
  133. 'value'
  134. );
  135. if (\Magento\Store\Model\Store::DEFAULT_STORE_ID == $storeId) {
  136. $select->where(
  137. 'tsv.store_id = ?',
  138. $storeId
  139. );
  140. }
  141. $valuesCollection->setOrder('value', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
  142. }
  143. }