Update.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Model\ResourceModel\Layout;
  7. /**
  8. * Layout update resource model
  9. */
  10. class Update extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  11. {
  12. /**
  13. * @var \Magento\Framework\Cache\FrontendInterface
  14. */
  15. private $_cache;
  16. /**
  17. * @var array
  18. */
  19. private $layoutUpdateCache;
  20. /**
  21. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  22. * @param \Magento\Framework\Cache\FrontendInterface $cache
  23. * @param string $connectionName
  24. */
  25. public function __construct(
  26. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  27. \Magento\Framework\Cache\FrontendInterface $cache,
  28. $connectionName = null
  29. ) {
  30. parent::__construct($context, $connectionName);
  31. $this->_cache = $cache;
  32. }
  33. /**
  34. * Define main table
  35. *
  36. * @return void
  37. */
  38. protected function _construct()
  39. {
  40. $this->_init('layout_update', 'layout_update_id');
  41. }
  42. /**
  43. * Retrieve layout updates by handle
  44. *
  45. * @param string $handle
  46. * @param \Magento\Framework\View\Design\ThemeInterface $theme
  47. * @param \Magento\Framework\App\ScopeInterface $store
  48. *
  49. * @return string
  50. * @throws \Magento\Framework\Exception\LocalizedException
  51. */
  52. public function fetchUpdatesByHandle(
  53. $handle,
  54. \Magento\Framework\View\Design\ThemeInterface $theme,
  55. \Magento\Framework\App\ScopeInterface $store
  56. ) {
  57. $bind = ['theme_id' => $theme->getId(), 'store_id' => $store->getId()];
  58. $cacheKey = implode('-', $bind);
  59. if (!isset($this->layoutUpdateCache[$cacheKey])) {
  60. $this->layoutUpdateCache[$cacheKey] = [];
  61. foreach ($this->getConnection()->fetchAll($this->_getFetchUpdatesByHandleSelect(), $bind) as $layout) {
  62. if (!isset($this->layoutUpdateCache[$cacheKey][$layout['handle']])) {
  63. $this->layoutUpdateCache[$cacheKey][$layout['handle']] = '';
  64. }
  65. $this->layoutUpdateCache[$cacheKey][$layout['handle']] .= $layout['xml'];
  66. }
  67. }
  68. return $this->layoutUpdateCache[$cacheKey][$handle] ?? '';
  69. }
  70. /**
  71. * Get select to fetch updates by handle
  72. *
  73. * @param bool $loadAllUpdates
  74. *
  75. * @return \Magento\Framework\DB\Select
  76. * @throws \Magento\Framework\Exception\LocalizedException
  77. */
  78. protected function _getFetchUpdatesByHandleSelect($loadAllUpdates = false)
  79. {
  80. //@todo Why it also loads layout updates for store_id=0, isn't it Admin Store View?
  81. //If 0 means 'all stores' why it then refers by foreign key to Admin in `store` and not to something named
  82. // 'All Stores'?
  83. $select = $this->getConnection()->select()->from(
  84. ['layout_update' => $this->getMainTable()],
  85. ['xml', 'handle']
  86. )->join(
  87. ['link' => $this->getTable('layout_link')],
  88. 'link.layout_update_id=layout_update.layout_update_id',
  89. ''
  90. )->where(
  91. 'link.store_id IN (0, :store_id)'
  92. )->where(
  93. 'link.theme_id = :theme_id'
  94. )->order(
  95. 'layout_update.sort_order ' . \Magento\Framework\DB\Select::SQL_ASC
  96. );
  97. if (!$loadAllUpdates) {
  98. $select->where('link.is_temporary = 0');
  99. }
  100. return $select;
  101. }
  102. /**
  103. * Update a "layout update link" if relevant data is provided
  104. *
  105. * @param \Magento\Widget\Model\Layout\Update|\Magento\Framework\Model\AbstractModel $object
  106. * @return $this
  107. */
  108. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  109. {
  110. $data = $object->getData();
  111. if (isset($data['store_id']) && isset($data['theme_id'])) {
  112. $this->getConnection()->insertOnDuplicate(
  113. $this->getTable('layout_link'),
  114. [
  115. 'store_id' => $data['store_id'],
  116. 'theme_id' => $data['theme_id'],
  117. 'layout_update_id' => $object->getId(),
  118. 'is_temporary' => (int)$object->getIsTemporary()
  119. ]
  120. );
  121. }
  122. $this->_cache->clean();
  123. return parent::_afterSave($object);
  124. }
  125. }