Collection.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\ResourceModel\Design\Config\Scope;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\App\ScopeTreeProviderInterface;
  9. use Magento\Framework\Data\Collection\EntityFactoryInterface;
  10. use Magento\Theme\Model\Design\Config\MetadataProviderInterface;
  11. use Magento\Theme\Model\Design\Config\ValueProcessor;
  12. /**
  13. * Data collection
  14. *
  15. */
  16. class Collection extends \Magento\Framework\Data\Collection
  17. {
  18. /**
  19. * @var ScopeTreeProviderInterface
  20. */
  21. protected $scopeTree;
  22. /**
  23. * @var MetadataProviderInterface
  24. */
  25. protected $metadataProvider;
  26. /**
  27. * @var ScopeConfigInterface
  28. */
  29. protected $appConfig;
  30. /**
  31. * @var ValueProcessor
  32. */
  33. protected $valueProcessor;
  34. /**
  35. * Collection constructor
  36. *
  37. * @param EntityFactoryInterface $entityFactory
  38. * @param ScopeTreeProviderInterface $scopeTree
  39. * @param MetadataProviderInterface $metadataProvider
  40. * @param ScopeConfigInterface $appConfig
  41. * @param ValueProcessor $valueProcessor
  42. */
  43. public function __construct(
  44. EntityFactoryInterface $entityFactory,
  45. ScopeTreeProviderInterface $scopeTree,
  46. MetadataProviderInterface $metadataProvider,
  47. ScopeConfigInterface $appConfig,
  48. ValueProcessor $valueProcessor
  49. ) {
  50. parent::__construct($entityFactory);
  51. $this->scopeTree = $scopeTree;
  52. $this->metadataProvider = $metadataProvider;
  53. $this->appConfig = $appConfig;
  54. $this->valueProcessor = $valueProcessor;
  55. }
  56. /**
  57. * Load data
  58. *
  59. * @param bool $printQuery
  60. * @param bool $logQuery
  61. * @return \Magento\Theme\Model\ResourceModel\Design\Config\Scope\Collection
  62. *
  63. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  64. */
  65. public function loadData($printQuery = false, $logQuery = false)
  66. {
  67. if (!$this->isLoaded()) {
  68. $default = $this->scopeTree->get();
  69. $this->prepareItemData();
  70. foreach ($default['scopes'] as $website) {
  71. $this->prepareItemData($website);
  72. foreach ($website['scopes'] as $group) {
  73. foreach ($group['scopes'] as $store) {
  74. $this->prepareItemData($website, $group, $store);
  75. }
  76. }
  77. }
  78. $this->_setIsLoaded(true);
  79. }
  80. return $this;
  81. }
  82. /**
  83. * Retrieve fields metadata
  84. *
  85. * @param string $scope
  86. * @param int $scopeId
  87. * @return array
  88. */
  89. protected function getMetadataValues($scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = null)
  90. {
  91. $result = [];
  92. foreach ($this->metadataProvider->get() as $itemName => $itemData) {
  93. if (isset($itemData['use_in_grid']) && (boolean)$itemData['use_in_grid']) {
  94. $result[$itemName] = $this->valueProcessor->process(
  95. $this->appConfig->getValue($itemData['path'], $scope, $scopeId),
  96. $scope,
  97. $scopeId,
  98. $itemData
  99. );
  100. }
  101. }
  102. return $result;
  103. }
  104. /**
  105. * Prepare item data depend on scope
  106. *
  107. * @param array $websiteScope
  108. * @param array $groupScope
  109. * @param array $storeScope
  110. *
  111. * @return void
  112. */
  113. protected function prepareItemData(array $websiteScope = [], array $groupScope = [], array $storeScope = [])
  114. {
  115. $result = [
  116. 'store_website_id' => isset($websiteScope['scope_id']) ? $websiteScope['scope_id'] : null,
  117. 'store_group_id' => isset($groupScope['scope_id']) ? $groupScope['scope_id'] : null,
  118. 'store_id' => isset($storeScope['scope_id']) ? $storeScope['scope_id'] : null,
  119. ];
  120. $result = array_merge($result, $this->getScopeData($websiteScope, $groupScope, $storeScope));
  121. $this->_addItem(new \Magento\Framework\DataObject($result));
  122. }
  123. /**
  124. * Retrieve scope data
  125. *
  126. * @param array $websiteScope
  127. * @param array $groupScope
  128. * @param array $storeScope
  129. * @return array
  130. */
  131. protected function getScopeData(array $websiteScope, array $groupScope, array $storeScope)
  132. {
  133. if (isset($storeScope['scope'])) {
  134. $data = $this->getMetadataValues($storeScope['scope'], $storeScope['scope_id']);
  135. } elseif (isset($groupScope['scope'])) {
  136. $data = $this->getMetadataValues($groupScope['scope'], $groupScope['scope_id']);
  137. } elseif (isset($websiteScope['scope'])) {
  138. $data = $this->getMetadataValues($websiteScope['scope'], $websiteScope['scope_id']);
  139. } else {
  140. $data = $this->getMetadataValues();
  141. }
  142. return $data;
  143. }
  144. }