Customization.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Config;
  7. /**
  8. * Theme customization config model
  9. */
  10. class Customization
  11. {
  12. /**
  13. * @var \Magento\Store\Model\StoreManagerInterface
  14. */
  15. protected $_storeManager;
  16. /**
  17. * @var \Magento\Framework\View\DesignInterface
  18. */
  19. protected $_design;
  20. /**
  21. * @var \Magento\Framework\View\Design\Theme\ThemeProviderInterface
  22. */
  23. protected $themeProvider;
  24. /**
  25. * Theme customizations which are assigned to store views or as default
  26. *
  27. * @var array
  28. * @see self::_prepareThemeCustomizations()
  29. */
  30. protected $_assignedTheme;
  31. /**
  32. * Theme customizations which are not assigned to store views or as default
  33. *
  34. * @var array
  35. * @see self::_prepareThemeCustomizations()
  36. */
  37. protected $_unassignedTheme;
  38. /**
  39. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  40. * @param \Magento\Framework\View\DesignInterface $design
  41. * @param \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
  42. */
  43. public function __construct(
  44. \Magento\Store\Model\StoreManagerInterface $storeManager,
  45. \Magento\Framework\View\DesignInterface $design,
  46. \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
  47. ) {
  48. $this->_storeManager = $storeManager;
  49. $this->_design = $design;
  50. $this->themeProvider = $themeProvider;
  51. }
  52. /**
  53. * Return theme customizations which are assigned to store views
  54. *
  55. * @see self::_prepareThemeCustomizations()
  56. * @return array
  57. */
  58. public function getAssignedThemeCustomizations()
  59. {
  60. if ($this->_assignedTheme === null) {
  61. $this->_prepareThemeCustomizations();
  62. }
  63. return $this->_assignedTheme;
  64. }
  65. /**
  66. * Return theme customizations which are not assigned to store views.
  67. *
  68. * @see self::_prepareThemeCustomizations()
  69. * @return array
  70. */
  71. public function getUnassignedThemeCustomizations()
  72. {
  73. if ($this->_unassignedTheme === null) {
  74. $this->_prepareThemeCustomizations();
  75. }
  76. return $this->_unassignedTheme;
  77. }
  78. /**
  79. * Return stores grouped by assigned themes
  80. *
  81. * @return array
  82. */
  83. public function getStoresByThemes()
  84. {
  85. $storesByThemes = [];
  86. $stores = $this->_storeManager->getStores();
  87. /** @var $store \Magento\Store\Model\Store */
  88. foreach ($stores as $store) {
  89. $themeId = $this->_getConfigurationThemeId($store);
  90. if (!isset($storesByThemes[$themeId])) {
  91. $storesByThemes[$themeId] = [];
  92. }
  93. $storesByThemes[$themeId][] = $store;
  94. }
  95. return $storesByThemes;
  96. }
  97. /**
  98. * Check if current theme has assigned to any store
  99. *
  100. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  101. * @param null|\Magento\Store\Model\Store $store
  102. * @return bool
  103. */
  104. public function isThemeAssignedToStore($theme, $store = null)
  105. {
  106. if (null === $store) {
  107. $assignedThemes = $this->getAssignedThemeCustomizations();
  108. return isset($assignedThemes[$theme->getId()]);
  109. }
  110. return $this->_isThemeAssignedToSpecificStore($theme, $store);
  111. }
  112. /**
  113. * Check if there are any themes assigned
  114. *
  115. * @return bool
  116. */
  117. public function hasThemeAssigned()
  118. {
  119. return count($this->getAssignedThemeCustomizations()) > 0;
  120. }
  121. /**
  122. * Is theme assigned to specific store
  123. *
  124. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  125. * @param \Magento\Store\Model\Store $store
  126. * @return bool
  127. */
  128. protected function _isThemeAssignedToSpecificStore($theme, $store)
  129. {
  130. return $theme->getId() == $this->_getConfigurationThemeId($store);
  131. }
  132. /**
  133. * Get configuration theme id
  134. *
  135. * @param \Magento\Store\Model\Store $store
  136. * @return int
  137. */
  138. protected function _getConfigurationThemeId($store)
  139. {
  140. return $this->_design->getConfigurationDesignTheme(
  141. \Magento\Framework\App\Area::AREA_FRONTEND,
  142. ['store' => $store]
  143. );
  144. }
  145. /**
  146. * Fetch theme customization and sort them out to arrays:
  147. * self::_assignedTheme and self::_unassignedTheme.
  148. *
  149. * NOTE: To get into "assigned" list theme customization not necessary should be assigned to store-view directly.
  150. * It can be set to website or as default theme and be used by store-view via config fallback mechanism.
  151. *
  152. * @return $this
  153. */
  154. protected function _prepareThemeCustomizations()
  155. {
  156. /** @var \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection */
  157. $themeCollection = $this->themeProvider->getThemeCustomizations(\Magento\Framework\App\Area::AREA_FRONTEND);
  158. $assignedThemes = $this->getStoresByThemes();
  159. $this->_assignedTheme = [];
  160. $this->_unassignedTheme = [];
  161. /** @var $theme \Magento\Framework\View\Design\ThemeInterface */
  162. foreach ($themeCollection as $theme) {
  163. if (isset($assignedThemes[$theme->getId()])) {
  164. $theme->setAssignedStores($assignedThemes[$theme->getId()]);
  165. $this->_assignedTheme[$theme->getId()] = $theme;
  166. } else {
  167. $this->_unassignedTheme[$theme->getId()] = $theme;
  168. }
  169. }
  170. return $this;
  171. }
  172. }