|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogGraphQl\Model\ResourceModel\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer; |
| 11 | +use Magento\Catalog\Model\Indexer\Product\Flat\State; |
| 12 | +use Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver; |
| 13 | +use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler; |
| 14 | +use Magento\Catalog\Model\Product\OptionFactory; |
| 15 | +use Magento\Catalog\Model\ResourceModel\Category; |
| 16 | +use Magento\Catalog\Model\ResourceModel\Helper; |
| 17 | +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; |
| 18 | +use Magento\Catalog\Model\ResourceModel\Product\Gallery; |
| 19 | +use Magento\Catalog\Model\ResourceModel\Url; |
| 20 | +use Magento\CatalogUrlRewrite\Model\Storage\DbStorage; |
| 21 | +use Magento\Customer\Api\GroupManagementInterface; |
| 22 | +use Magento\Customer\Model\Session; |
| 23 | +use Magento\Eav\Model\Config; |
| 24 | +use Magento\Eav\Model\EntityFactory; |
| 25 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 26 | +use Magento\Framework\App\ObjectManager; |
| 27 | +use Magento\Framework\App\ResourceConnection; |
| 28 | +use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; |
| 29 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 30 | +use Magento\Framework\DB\Select; |
| 31 | +use Magento\Framework\EntityManager\MetadataPool; |
| 32 | +use Magento\Framework\Event\ManagerInterface; |
| 33 | +use Magento\Framework\Indexer\DimensionFactory; |
| 34 | +use Magento\Framework\Module\Manager; |
| 35 | +use Magento\Framework\Stdlib\DateTime; |
| 36 | +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 37 | +use Magento\Framework\Validator\UniversalFactory; |
| 38 | +use Magento\Store\Model\StoreManagerInterface; |
| 39 | +use Psr\Log\LoggerInterface; |
| 40 | +use Zend_Db_Expr; |
| 41 | +use Zend_Db_Select_Exception; |
| 42 | + |
| 43 | +/** |
| 44 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 45 | + */ |
| 46 | +class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection |
| 47 | +{ |
| 48 | + /** |
| 49 | + * @var TableMaintainer |
| 50 | + */ |
| 51 | + private $tableMaintainer; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory |
| 55 | + * @param LoggerInterface $logger |
| 56 | + * @param FetchStrategyInterface $fetchStrategy |
| 57 | + * @param ManagerInterface $eventManager |
| 58 | + * @param Config $eavConfig |
| 59 | + * @param ResourceConnection $resource |
| 60 | + * @param EntityFactory $eavEntityFactory |
| 61 | + * @param Helper $resourceHelper |
| 62 | + * @param UniversalFactory $universalFactory |
| 63 | + * @param StoreManagerInterface $storeManager |
| 64 | + * @param Manager $moduleManager |
| 65 | + * @param State $catalogProductFlatState |
| 66 | + * @param ScopeConfigInterface $scopeConfig |
| 67 | + * @param OptionFactory $productOptionFactory |
| 68 | + * @param Url $catalogUrl |
| 69 | + * @param TimezoneInterface $localeDate |
| 70 | + * @param Session $customerSession |
| 71 | + * @param DateTime $dateTime |
| 72 | + * @param GroupManagementInterface $groupManagement |
| 73 | + * @param AdapterInterface|null $connection |
| 74 | + * @param ProductLimitationFactory|null $productLimitationFactory |
| 75 | + * @param MetadataPool|null $metadataPool |
| 76 | + * @param TableMaintainer|null $tableMaintainer |
| 77 | + * @param PriceTableResolver|null $priceTableResolver |
| 78 | + * @param DimensionFactory|null $dimensionFactory |
| 79 | + * @param Category|null $categoryResourceModel |
| 80 | + * @param DbStorage|null $urlFinder |
| 81 | + * @param GalleryReadHandler|null $productGalleryReadHandler |
| 82 | + * @param Gallery|null $mediaGalleryResource |
| 83 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 84 | + */ |
| 85 | + public function __construct( |
| 86 | + \Magento\Framework\Data\Collection\EntityFactory $entityFactory, |
| 87 | + LoggerInterface $logger, |
| 88 | + FetchStrategyInterface $fetchStrategy, |
| 89 | + ManagerInterface $eventManager, |
| 90 | + Config $eavConfig, |
| 91 | + ResourceConnection $resource, |
| 92 | + EntityFactory $eavEntityFactory, |
| 93 | + Helper $resourceHelper, |
| 94 | + UniversalFactory $universalFactory, |
| 95 | + StoreManagerInterface $storeManager, |
| 96 | + Manager $moduleManager, |
| 97 | + State $catalogProductFlatState, |
| 98 | + ScopeConfigInterface $scopeConfig, |
| 99 | + OptionFactory $productOptionFactory, |
| 100 | + Url $catalogUrl, |
| 101 | + TimezoneInterface $localeDate, |
| 102 | + Session $customerSession, |
| 103 | + DateTime $dateTime, |
| 104 | + GroupManagementInterface $groupManagement, |
| 105 | + AdapterInterface $connection = null, |
| 106 | + ProductLimitationFactory $productLimitationFactory = null, |
| 107 | + MetadataPool $metadataPool = null, |
| 108 | + TableMaintainer $tableMaintainer = null, |
| 109 | + PriceTableResolver $priceTableResolver = null, |
| 110 | + DimensionFactory $dimensionFactory = null, |
| 111 | + Category $categoryResourceModel = null, |
| 112 | + DbStorage $urlFinder = null, |
| 113 | + GalleryReadHandler $productGalleryReadHandler = null, |
| 114 | + Gallery $mediaGalleryResource = null |
| 115 | + ) { |
| 116 | + parent::__construct( |
| 117 | + $entityFactory, |
| 118 | + $logger, |
| 119 | + $fetchStrategy, |
| 120 | + $eventManager, |
| 121 | + $eavConfig, |
| 122 | + $resource, |
| 123 | + $eavEntityFactory, |
| 124 | + $resourceHelper, |
| 125 | + $universalFactory, |
| 126 | + $storeManager, |
| 127 | + $moduleManager, |
| 128 | + $catalogProductFlatState, |
| 129 | + $scopeConfig, |
| 130 | + $productOptionFactory, |
| 131 | + $catalogUrl, |
| 132 | + $localeDate, |
| 133 | + $customerSession, |
| 134 | + $dateTime, |
| 135 | + $groupManagement, |
| 136 | + $connection, |
| 137 | + $productLimitationFactory, |
| 138 | + $metadataPool, |
| 139 | + $tableMaintainer, |
| 140 | + $priceTableResolver, |
| 141 | + $dimensionFactory, |
| 142 | + $categoryResourceModel, |
| 143 | + $urlFinder, |
| 144 | + $productGalleryReadHandler, |
| 145 | + $mediaGalleryResource |
| 146 | + ); |
| 147 | + |
| 148 | + $this->tableMaintainer = $tableMaintainer ?: ObjectManager::getInstance() |
| 149 | + ->get(TableMaintainer::class); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Join minimal position to the select |
| 154 | + * |
| 155 | + * @param array $categoryIds |
| 156 | + * @return void |
| 157 | + * @throws Zend_Db_Select_Exception |
| 158 | + */ |
| 159 | + public function joinMinimalPosition(array $categoryIds): void |
| 160 | + { |
| 161 | + $this->_applyProductLimitations(); |
| 162 | + $filters = $this->_productLimitationFilters; |
| 163 | + $positions = []; |
| 164 | + $connection = $this->getConnection(); |
| 165 | + $select = $this->getSelect(); |
| 166 | + |
| 167 | + foreach ($categoryIds as $categoryId) { |
| 168 | + $table = 'cat_index_' . $categoryId; |
| 169 | + $conditions = [ |
| 170 | + $table . '.product_id=e.entity_id', |
| 171 | + $connection->quoteInto( |
| 172 | + $table . '.store_id=?', |
| 173 | + $filters['store_id'], |
| 174 | + 'int' |
| 175 | + ), |
| 176 | + $connection->quoteInto( |
| 177 | + $table . '.category_id=?', |
| 178 | + $categoryId, |
| 179 | + 'int' |
| 180 | + ) |
| 181 | + ]; |
| 182 | + |
| 183 | + $joinCond = implode(' AND ', $conditions); |
| 184 | + $fromPart = $select->getPart(Select::FROM); |
| 185 | + if (isset($fromPart[$table])) { |
| 186 | + $fromPart[$table]['joinCondition'] = $joinCond; |
| 187 | + $select->setPart(Select::FROM, $fromPart); |
| 188 | + } else { |
| 189 | + $select->joinLeft( |
| 190 | + [$table => $this->tableMaintainer->getMainTable($this->getStoreId())], |
| 191 | + $joinCond, |
| 192 | + [] |
| 193 | + ); |
| 194 | + } |
| 195 | + $positions[] = $connection->getIfNullSql($table . '.position', '~0'); |
| 196 | + } |
| 197 | + |
| 198 | + $columns = $select->getPart(Select::COLUMNS); |
| 199 | + $columnIndex = false; |
| 200 | + $minPos = $connection->getLeastSql($positions); |
| 201 | + foreach ($columns as $index => [,, $columnAlias]) { |
| 202 | + if ($columnAlias === 'cat_index_position') { |
| 203 | + $columnIndex = $index; |
| 204 | + break; |
| 205 | + } |
| 206 | + } |
| 207 | + if ($columnIndex) { |
| 208 | + $columns[$columnIndex][1] = $minPos; |
| 209 | + $select->setPart(Select::COLUMNS, $columns); |
| 210 | + } else { |
| 211 | + $select->columns(['cat_index_position' => $minPos]); |
| 212 | + } |
| 213 | + $this->_joinFields['position'] = ['table' => '', 'field' => 'cat_index_position']; |
| 214 | + } |
| 215 | +} |
0 commit comments