|
| 1 | +diff --git a/vendor/magento/module-banner/Model/Banner/Data.php b/vendor/magento/module-banner/Model/Banner/Data.php |
| 2 | +index 271fb8d5bef..c8e874fb6eb 100644 |
| 3 | +--- a/vendor/magento/module-banner/Model/Banner/Data.php |
| 4 | ++++ b/vendor/magento/module-banner/Model/Banner/Data.php |
| 5 | +@@ -6,9 +6,18 @@ |
| 6 | + |
| 7 | + namespace Magento\Banner\Model\Banner; |
| 8 | + |
| 9 | +-use Magento\Customer\CustomerData\SectionSourceInterface; |
| 10 | + use Magento\Banner\Model\Config; |
| 11 | ++use Magento\Banner\Model\ResourceModel\Banner; |
| 12 | ++use Magento\Banner\Model\ResourceModel\BannersByStore; |
| 13 | ++use Magento\CatalogRule\Model\ResourceModel\Rule; |
| 14 | ++use Magento\Checkout\Model\Session; |
| 15 | ++use Magento\Cms\Model\Template\FilterProvider; |
| 16 | ++use Magento\Customer\CustomerData\SectionSourceInterface; |
| 17 | ++use Magento\Framework\App\Http\Context; |
| 18 | ++use Magento\Framework\App\ObjectManager; |
| 19 | ++use Magento\Framework\Exception\NoSuchEntityException; |
| 20 | + use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 21 | ++use Magento\Store\Model\StoreManagerInterface; |
| 22 | + |
| 23 | + /** |
| 24 | + * Banner section. |
| 25 | +@@ -37,8 +46,6 @@ class Data implements SectionSourceInterface |
| 26 | + protected $banner; |
| 27 | + |
| 28 | + /** |
| 29 | +- * Store manager |
| 30 | +- * |
| 31 | + * @var \Magento\Store\Model\StoreManagerInterface |
| 32 | + */ |
| 33 | + protected $storeManager; |
| 34 | +@@ -89,15 +96,23 @@ class Data implements SectionSourceInterface |
| 35 | + private $data; |
| 36 | + |
| 37 | + /** |
| 38 | +- * @param \Magento\Checkout\Model\Session $checkoutSession |
| 39 | +- * @param \Magento\Banner\Model\ResourceModel\Banner $bannerResource |
| 40 | ++ * @var BannersByStore |
| 41 | ++ */ |
| 42 | ++ private BannersByStore $bannersByStore; |
| 43 | ++ |
| 44 | ++ /** |
| 45 | ++ * @param Session $checkoutSession |
| 46 | ++ * @param Banner $bannerResource |
| 47 | + * @param \Magento\Banner\Model\Banner $banner |
| 48 | +- * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
| 49 | +- * @param \Magento\Framework\App\Http\Context $httpContext |
| 50 | +- * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider |
| 51 | +- * @param \Magento\CatalogRule\Model\ResourceModel\Rule $catalogRule |
| 52 | ++ * @param StoreManagerInterface $storeManager |
| 53 | ++ * @param Context $httpContext |
| 54 | ++ * @param FilterProvider $filterProvider |
| 55 | ++ * @param Rule $catalogRule |
| 56 | + * @param TimezoneInterface $dateTime |
| 57 | + * @param array $data |
| 58 | ++ * @param BannersByStore|null $bannersByStore |
| 59 | ++ * @throws NoSuchEntityException |
| 60 | ++ * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 61 | + */ |
| 62 | + public function __construct( |
| 63 | + \Magento\Checkout\Model\Session $checkoutSession, |
| 64 | +@@ -108,7 +123,8 @@ class Data implements SectionSourceInterface |
| 65 | + \Magento\Cms\Model\Template\FilterProvider $filterProvider, |
| 66 | + \Magento\CatalogRule\Model\ResourceModel\Rule $catalogRule, |
| 67 | + TimezoneInterface $dateTime, |
| 68 | +- array $data = [] |
| 69 | ++ array $data = [], |
| 70 | ++ BannersByStore $bannersByStore = null |
| 71 | + ) { |
| 72 | + $this->checkoutSession = $checkoutSession; |
| 73 | + $this->bannerResource = $bannerResource; |
| 74 | +@@ -120,6 +136,7 @@ class Data implements SectionSourceInterface |
| 75 | + $this->catalogRule = $catalogRule; |
| 76 | + $this->dateTime = $dateTime; |
| 77 | + $this->data = $data; |
| 78 | ++ $this->bannersByStore = $bannersByStore ?? ObjectManager::getInstance()->get(BannersByStore::class); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | +@@ -296,7 +313,7 @@ class Data implements SectionSourceInterface |
| 83 | + $productId |
| 84 | + ); |
| 85 | + |
| 86 | +- $ruleIds = count($result) ? array_column($result, 'rule_id'): []; |
| 87 | ++ $ruleIds = count($result) ? array_column($result, 'rule_id') : []; |
| 88 | + |
| 89 | + return $ruleIds ? $this->getCatalogRuleRelatedBannerIds($ruleIds) : []; |
| 90 | + } |
| 91 | +@@ -311,21 +328,20 @@ class Data implements SectionSourceInterface |
| 92 | + protected function getBannersData($bannersIds) |
| 93 | + { |
| 94 | + $banners = []; |
| 95 | +- foreach ($bannersIds as $bannerId) { |
| 96 | +- if (!isset($this->banners[$bannerId])) { |
| 97 | +- $content = $this->bannerResource->getStoreContent($bannerId, $this->storeId); |
| 98 | +- if (!empty($content)) { |
| 99 | +- $this->banners[$bannerId] = [ |
| 100 | +- 'content' => $this->filterProvider->getPageFilter()->filter($content), |
| 101 | +- 'types' => $this->banner->load($bannerId)->getTypes(), |
| 102 | +- 'id' => $bannerId, |
| 103 | +- ]; |
| 104 | +- } else { |
| 105 | +- $this->banners[$bannerId] = null; |
| 106 | +- } |
| 107 | ++ |
| 108 | ++ $notLoadedBannersIds = array_diff($bannersIds, array_keys($this->banners)); |
| 109 | ++ $loadedBannerIds = array_intersect($bannersIds, array_keys($this->banners)); |
| 110 | ++ if (count($notLoadedBannersIds)) { |
| 111 | ++ [$banners, $emptyContentBanners] = $this->bannersByStore->execute($notLoadedBannersIds, $this->storeId); |
| 112 | ++ $this->banners += $emptyContentBanners; |
| 113 | ++ $this->banners += $banners; |
| 114 | ++ } |
| 115 | ++ if (count($loadedBannerIds)) { |
| 116 | ++ foreach ($loadedBannerIds as $loadedBannerId) { |
| 117 | ++ $banners[$loadedBannerId] = $this->banners[$loadedBannerId]; |
| 118 | + } |
| 119 | +- $banners[$bannerId] = $this->banners[$bannerId]; |
| 120 | + } |
| 121 | ++ |
| 122 | + return array_filter($banners); |
| 123 | + } |
| 124 | + } |
| 125 | +diff --git a/vendor/magento/module-banner/Model/ResourceModel/BannersByStore.php b/vendor/magento/module-banner/Model/ResourceModel/BannersByStore.php |
| 126 | +new file mode 100644 |
| 127 | +index 00000000000..4f0bef656bb |
| 128 | +--- /dev/null |
| 129 | ++++ b/vendor/magento/module-banner/Model/ResourceModel/BannersByStore.php |
| 130 | +@@ -0,0 +1,152 @@ |
| 131 | ++<?php |
| 132 | ++/** |
| 133 | ++ * Copyright © Magento, Inc. All rights reserved. |
| 134 | ++ * See COPYING.txt for license details. |
| 135 | ++ */ |
| 136 | ++declare(strict_types=1); |
| 137 | ++ |
| 138 | ++namespace Magento\Banner\Model\ResourceModel; |
| 139 | ++ |
| 140 | ++use Magento\Banner\Model\Config; |
| 141 | ++use Magento\Cms\Model\Template\FilterProvider; |
| 142 | ++use Magento\Framework\App\ResourceConnection; |
| 143 | ++use Magento\Framework\DB\Adapter\AdapterInterface; |
| 144 | ++use Magento\Framework\DB\Select; |
| 145 | ++use Magento\Framework\Event\ManagerInterface; |
| 146 | ++use Zend_Db_Select_Exception; |
| 147 | ++ |
| 148 | ++/** |
| 149 | ++ * Class returns banners content based on provided store and banner ids |
| 150 | ++ */ |
| 151 | ++class BannersByStore |
| 152 | ++{ |
| 153 | ++ /** |
| 154 | ++ * @var AdapterInterface |
| 155 | ++ */ |
| 156 | ++ private AdapterInterface $connection; |
| 157 | ++ |
| 158 | ++ /** |
| 159 | ++ * @var FilterProvider |
| 160 | ++ */ |
| 161 | ++ private FilterProvider $filterProvider; |
| 162 | ++ |
| 163 | ++ /** |
| 164 | ++ * @var ManagerInterface |
| 165 | ++ */ |
| 166 | ++ private ManagerInterface $eventManager; |
| 167 | ++ |
| 168 | ++ /** |
| 169 | ++ * @param ResourceConnection $resource |
| 170 | ++ * @param FilterProvider $filterProvider |
| 171 | ++ * @param ManagerInterface $eventManager |
| 172 | ++ */ |
| 173 | ++ public function __construct( |
| 174 | ++ ResourceConnection $resource, |
| 175 | ++ FilterProvider $filterProvider, |
| 176 | ++ ManagerInterface $eventManager |
| 177 | ++ ) { |
| 178 | ++ $this->connection = $resource->getConnection(); |
| 179 | ++ $this->filterProvider = $filterProvider; |
| 180 | ++ $this->eventManager = $eventManager; |
| 181 | ++ } |
| 182 | ++ |
| 183 | ++ /** |
| 184 | ++ * Return types as array |
| 185 | ++ * |
| 186 | ++ * @param string|array|null $types |
| 187 | ++ * @return array |
| 188 | ++ */ |
| 189 | ++ private function getTypes(string|array|null $types): array |
| 190 | ++ { |
| 191 | ++ if (is_array($types)) { |
| 192 | ++ return $types; |
| 193 | ++ } |
| 194 | ++ |
| 195 | ++ return empty($types) ? [] : explode(',', $types); |
| 196 | ++ } |
| 197 | ++ |
| 198 | ++ /** |
| 199 | ++ * Get select for banner content by store |
| 200 | ++ * |
| 201 | ++ * @param array $bannerIds |
| 202 | ++ * @param int $storeId |
| 203 | ++ * @return Select |
| 204 | ++ */ |
| 205 | ++ private function getSelect(array $bannerIds, int $storeId): Select |
| 206 | ++ { |
| 207 | ++ $select = $this->connection->select()->from( |
| 208 | ++ ['main_table' => $this->connection->getTableName('magento_banner_content')], |
| 209 | ++ ['banner_id as id', 'main_table.banner_content as content'] |
| 210 | ++ )->where( |
| 211 | ++ 'main_table.banner_id IN (?)', |
| 212 | ++ $bannerIds, |
| 213 | ++ \Zend_Db::INT_TYPE |
| 214 | ++ )->where( |
| 215 | ++ 'main_table.store_id = ' . $storeId |
| 216 | ++ ); |
| 217 | ++ $select->joinInner( |
| 218 | ++ ['banner' => $this->connection->getTableName('magento_banner')], |
| 219 | ++ 'main_table.banner_id = ' . 'banner.banner_id', |
| 220 | ++ ['types'] |
| 221 | ++ ); |
| 222 | ++ |
| 223 | ++ $this->eventManager->dispatch( |
| 224 | ++ 'magento_banner_resource_banner_content_select_init', |
| 225 | ++ ['select' => $select, 'banner_id' => $bannerIds] |
| 226 | ++ ); |
| 227 | ++ |
| 228 | ++ return $select; |
| 229 | ++ } |
| 230 | ++ |
| 231 | ++ /** |
| 232 | ++ * Get banners contents by specific store id |
| 233 | ++ * |
| 234 | ++ * @param array $bannerIds |
| 235 | ++ * @param int $storeId |
| 236 | ++ * @return array |
| 237 | ++ * @throws Zend_Db_Select_Exception |
| 238 | ++ */ |
| 239 | ++ private function getBannerContentsByStore(array $bannerIds, int $storeId): array |
| 240 | ++ { |
| 241 | ++ $defaultStoreSelect = $this->getSelect($bannerIds, 0); |
| 242 | ++ $storeSelect = $this->getSelect($bannerIds, $storeId); |
| 243 | ++ $select = $this->connection->select()->union([$defaultStoreSelect, $storeSelect]); |
| 244 | ++ return $this->connection->fetchAll($select); |
| 245 | ++ } |
| 246 | ++ |
| 247 | ++ /** |
| 248 | ++ * Return formatted content and types |
| 249 | ++ * |
| 250 | ++ * @param array $banners |
| 251 | ++ * @return array |
| 252 | ++ * @throws \Exception |
| 253 | ++ */ |
| 254 | ++ private function getBannersFormattedContent(array $banners): array |
| 255 | ++ { |
| 256 | ++ $formattedBanners = $emptyContentBanners = []; |
| 257 | ++ foreach ($banners as $banner) { |
| 258 | ++ $banner['types'] = $this->getTypes($banner['types']); |
| 259 | ++ if (!empty($banner['content'])) { |
| 260 | ++ $banner['content'] = $this->filterProvider->getPageFilter()->filter($banner['content']); |
| 261 | ++ } else { |
| 262 | ++ $emptyContentBanners[$banner['id']] = null; |
| 263 | ++ } |
| 264 | ++ $formattedBanners[$banner['id']] = $banner; |
| 265 | ++ } |
| 266 | ++ return [$formattedBanners, $emptyContentBanners]; |
| 267 | ++ } |
| 268 | ++ |
| 269 | ++ /** |
| 270 | ++ * Get banners by store |
| 271 | ++ * |
| 272 | ++ * @param array $bannerIds |
| 273 | ++ * @param int $storeId |
| 274 | ++ * @return array |
| 275 | ++ * @throws \Exception |
| 276 | ++ */ |
| 277 | ++ public function execute(array $bannerIds, int $storeId): array |
| 278 | ++ { |
| 279 | ++ $banners = $this->getBannerContentsByStore($bannerIds, $storeId); |
| 280 | ++ return $this->getBannersFormattedContent($banners); |
| 281 | ++ } |
| 282 | ++} |
0 commit comments