Collection.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\ResourceModel\Theme;
  7. /**
  8. * Theme collection
  9. */
  10. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection implements
  11. \Magento\Framework\View\Design\Theme\Label\ListInterface,
  12. \Magento\Framework\View\Design\Theme\ListInterface
  13. {
  14. /**
  15. * Default page size
  16. */
  17. const DEFAULT_PAGE_SIZE = 6;
  18. /**
  19. * @var string
  20. */
  21. protected $_idFieldName = 'theme_id';
  22. /**
  23. * Collection initialization
  24. *
  25. * @return void
  26. */
  27. protected function _construct()
  28. {
  29. $this->_init(\Magento\Theme\Model\Theme::class, \Magento\Theme\Model\ResourceModel\Theme::class);
  30. }
  31. /**
  32. * Add title for parent themes
  33. *
  34. * @return $this
  35. */
  36. public function addParentTitle()
  37. {
  38. $this->getSelect()->joinLeft(
  39. ['parent' => $this->getMainTable()],
  40. 'main_table.parent_id = parent.theme_id',
  41. ['parent_theme_title' => 'parent.theme_title']
  42. );
  43. return $this;
  44. }
  45. /**
  46. * Add area filter
  47. *
  48. * @param string $area
  49. * @return $this
  50. */
  51. public function addAreaFilter($area = \Magento\Framework\App\Area::AREA_FRONTEND)
  52. {
  53. $this->getSelect()->where('main_table.area=?', $area);
  54. return $this;
  55. }
  56. /**
  57. * Add type filter in relations
  58. *
  59. * @param int $typeParent
  60. * @param int $typeChild
  61. * @return $this
  62. */
  63. public function addTypeRelationFilter($typeParent, $typeChild)
  64. {
  65. $this->getSelect()->join(
  66. ['parent' => $this->getMainTable()],
  67. 'main_table.parent_id = parent.theme_id',
  68. ['parent_type' => 'parent.type']
  69. )->where(
  70. 'parent.type = ?',
  71. $typeParent
  72. )->where(
  73. 'main_table.type = ?',
  74. $typeChild
  75. );
  76. return $this;
  77. }
  78. /**
  79. * Add type filter
  80. *
  81. * @param string|array $type
  82. * @return $this
  83. */
  84. public function addTypeFilter($type)
  85. {
  86. $this->addFieldToFilter('main_table.type', ['in' => $type]);
  87. return $this;
  88. }
  89. /**
  90. * Filter visible themes in backend (physical and virtual only)
  91. *
  92. * @return $this
  93. */
  94. public function filterVisibleThemes()
  95. {
  96. $this->addTypeFilter(
  97. [
  98. \Magento\Framework\View\Design\ThemeInterface::TYPE_PHYSICAL,
  99. \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL,
  100. ]
  101. );
  102. return $this;
  103. }
  104. /**
  105. * Return array for select field
  106. *
  107. * @return array
  108. */
  109. public function toOptionArray()
  110. {
  111. return $this->_toOptionArray('theme_id', 'theme_title');
  112. }
  113. /**
  114. * Return array for grid column
  115. *
  116. * @return array
  117. */
  118. public function toOptionHash()
  119. {
  120. return $this->_toOptionHash('theme_id', 'theme_title');
  121. }
  122. /**
  123. * Get theme from DB by area and theme_path
  124. *
  125. * @param string $fullPath
  126. * @return \Magento\Theme\Model\Theme
  127. */
  128. public function getThemeByFullPath($fullPath)
  129. {
  130. $this->_reset()->clear();
  131. list($area, $themePath) = explode('/', $fullPath, 2);
  132. $this->addFieldToFilter('area', $area);
  133. $this->addFieldToFilter('theme_path', $themePath);
  134. return $this->getFirstItem();
  135. }
  136. /**
  137. * Set page size
  138. *
  139. * @param int $size
  140. * @return $this
  141. */
  142. public function setPageSize($size = self::DEFAULT_PAGE_SIZE)
  143. {
  144. return parent::setPageSize($size);
  145. }
  146. /**
  147. * Update all child themes relations
  148. *
  149. * @param \Magento\Framework\View\Design\ThemeInterface $themeModel
  150. * @return $this
  151. */
  152. public function updateChildRelations(\Magento\Framework\View\Design\ThemeInterface $themeModel)
  153. {
  154. $parentThemeId = $themeModel->getParentId();
  155. $this->addFieldToFilter('parent_id', ['eq' => $themeModel->getId()])->load();
  156. /** @var $theme \Magento\Framework\View\Design\ThemeInterface */
  157. foreach ($this->getItems() as $theme) {
  158. $theme->setParentId($parentThemeId)->save();
  159. }
  160. return $this;
  161. }
  162. /**
  163. * Filter frontend physical theme.
  164. * All themes or per page if set page and page size (page size is optional)
  165. *
  166. * @param int $page
  167. * @param int $pageSize
  168. * @return $this
  169. */
  170. public function filterPhysicalThemes(
  171. $page = null,
  172. $pageSize = \Magento\Theme\Model\ResourceModel\Theme\Collection::DEFAULT_PAGE_SIZE
  173. ) {
  174. $this->addAreaFilter(
  175. \Magento\Framework\App\Area::AREA_FRONTEND
  176. )->addTypeFilter(
  177. \Magento\Framework\View\Design\ThemeInterface::TYPE_PHYSICAL
  178. );
  179. if ($page) {
  180. $this->setPageSize($pageSize)->setCurPage($page);
  181. }
  182. return $this;
  183. }
  184. /**
  185. * Filter theme customization
  186. *
  187. * @param string $area
  188. * @param int $type
  189. * @return $this
  190. */
  191. public function filterThemeCustomizations(
  192. $area = \Magento\Framework\App\Area::AREA_FRONTEND,
  193. $type = \Magento\Framework\View\Design\ThemeInterface::TYPE_VIRTUAL
  194. ) {
  195. $this->addAreaFilter($area)->addTypeFilter($type);
  196. return $this;
  197. }
  198. /**
  199. * @inheritdoc
  200. */
  201. public function getLabels()
  202. {
  203. $labels = $this->loadRegisteredThemes();
  204. return $labels->toOptionArray();
  205. }
  206. /**
  207. * @return $this
  208. */
  209. public function loadRegisteredThemes()
  210. {
  211. $this->_reset()->clear();
  212. return $this->setOrder('theme_title', \Magento\Framework\Data\Collection::SORT_ORDER_ASC)
  213. ->filterVisibleThemes()->addAreaFilter(\Magento\Framework\App\Area::AREA_FRONTEND);
  214. }
  215. }