Design.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model;
  7. use \Magento\Framework\TranslateInterface;
  8. /**
  9. * Catalog Custom Category design Model
  10. *
  11. * @api
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Design extends \Magento\Framework\Model\AbstractModel
  17. {
  18. const APPLY_FOR_PRODUCT = 1;
  19. const APPLY_FOR_CATEGORY = 2;
  20. /**
  21. * Design package instance
  22. *
  23. * @var \Magento\Framework\View\DesignInterface
  24. */
  25. protected $_design = null;
  26. /**
  27. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  28. */
  29. protected $_localeDate;
  30. /**
  31. * @var TranslateInterface
  32. */
  33. private $translator;
  34. /**
  35. * @param \Magento\Framework\Model\Context $context
  36. * @param \Magento\Framework\Registry $registry
  37. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  38. * @param \Magento\Framework\View\DesignInterface $design
  39. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  40. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  41. * @param array $data
  42. * @param TranslateInterface|null $translator
  43. */
  44. public function __construct(
  45. \Magento\Framework\Model\Context $context,
  46. \Magento\Framework\Registry $registry,
  47. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  48. \Magento\Framework\View\DesignInterface $design,
  49. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  50. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  51. array $data = [],
  52. TranslateInterface $translator = null
  53. ) {
  54. $this->_localeDate = $localeDate;
  55. $this->_design = $design;
  56. $this->translator = $translator ?:
  57. \Magento\Framework\App\ObjectManager::getInstance()->get(TranslateInterface::class);
  58. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  59. }
  60. /**
  61. * Apply custom design
  62. *
  63. * @param string $design
  64. * @return $this
  65. */
  66. public function applyCustomDesign($design)
  67. {
  68. $this->_design->setDesignTheme($design);
  69. $this->translator->loadData(null, true);
  70. return $this;
  71. }
  72. /**
  73. * Get custom layout settings
  74. *
  75. * @param \Magento\Catalog\Model\Category|\Magento\Catalog\Model\Product $object
  76. * @return \Magento\Framework\DataObject
  77. */
  78. public function getDesignSettings($object)
  79. {
  80. if ($object instanceof \Magento\Catalog\Model\Product) {
  81. $currentCategory = $object->getCategory();
  82. } else {
  83. $currentCategory = $object;
  84. }
  85. $category = null;
  86. if ($currentCategory) {
  87. $category = $currentCategory->getParentDesignCategory($currentCategory);
  88. }
  89. if ($object instanceof \Magento\Catalog\Model\Product) {
  90. if ($category && $category->getCustomApplyToProducts()) {
  91. return $this->_mergeSettings($this->_extractSettings($category), $this->_extractSettings($object));
  92. } else {
  93. return $this->_extractSettings($object);
  94. }
  95. } else {
  96. return $this->_extractSettings($category);
  97. }
  98. }
  99. /**
  100. * Extract custom layout settings from category or product object
  101. *
  102. * @param \Magento\Catalog\Model\Category|\Magento\Catalog\Model\Product $object
  103. * @return \Magento\Framework\DataObject
  104. */
  105. protected function _extractSettings($object)
  106. {
  107. $settings = new \Magento\Framework\DataObject();
  108. if (!$object) {
  109. return $settings;
  110. }
  111. $date = $object->getCustomDesignDate();
  112. if (array_key_exists(
  113. 'from',
  114. $date
  115. ) && array_key_exists(
  116. 'to',
  117. $date
  118. ) && $this->_localeDate->isScopeDateInInterval(
  119. null,
  120. $date['from'],
  121. $date['to']
  122. )
  123. ) {
  124. $settings->setCustomDesign(
  125. $object->getCustomDesign()
  126. )->setPageLayout(
  127. $object->getPageLayout()
  128. )->setLayoutUpdates(
  129. (array)$object->getCustomLayoutUpdate()
  130. );
  131. }
  132. return $settings;
  133. }
  134. /**
  135. * Merge custom design settings
  136. *
  137. * @param \Magento\Framework\DataObject $categorySettings
  138. * @param \Magento\Framework\DataObject $productSettings
  139. * @return \Magento\Framework\DataObject
  140. */
  141. protected function _mergeSettings($categorySettings, $productSettings)
  142. {
  143. if ($productSettings->getCustomDesign()) {
  144. $categorySettings->setCustomDesign($productSettings->getCustomDesign());
  145. }
  146. if ($productSettings->getPageLayout()) {
  147. $categorySettings->setPageLayout($productSettings->getPageLayout());
  148. }
  149. if ($productSettings->getLayoutUpdates()) {
  150. $update = array_merge($categorySettings->getLayoutUpdates(), $productSettings->getLayoutUpdates());
  151. $categorySettings->setLayoutUpdates($update);
  152. }
  153. return $categorySettings;
  154. }
  155. }