1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\AdvancedSearch\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Search\Request\IndexScopeResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Search\Request\Dimension;
use Magento\Catalog\Model\Indexer\Category\Product\AbstractAction;
use Magento\Framework\Search\Request\IndexScopeResolverInterface as TableResolver;
use Magento\Catalog\Model\Indexer\Product\Price\DimensionCollectionFactory;
use Magento\Store\Model\Indexer\WebsiteDimensionProvider;
/**
* @api
* @since 100.1.0
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Index extends AbstractDb
{
/**
* @var StoreManagerInterface
* @since 100.1.0
*/
protected $storeManager;
/**
* @var MetadataPool
* @since 100.1.0
*/
protected $metadataPool;
/**
* @var TableResolver
*/
private $tableResolver;
/**
* @var DimensionCollectionFactory|null
*/
private $dimensionCollectionFactory;
/**
* @var int|null
*/
private $websiteId;
/**
* Index constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param MetadataPool $metadataPool
* @param string|null $connectionName
* @param TableResolver|null $tableResolver
* @param DimensionCollectionFactory|null $dimensionCollectionFactory
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
MetadataPool $metadataPool,
$connectionName = null,
TableResolver $tableResolver = null,
DimensionCollectionFactory $dimensionCollectionFactory = null
) {
parent::__construct($context, $connectionName);
$this->storeManager = $storeManager;
$this->metadataPool = $metadataPool;
$this->tableResolver = $tableResolver ?: ObjectManager::getInstance()->get(IndexScopeResolverInterface::class);
$this->dimensionCollectionFactory = $dimensionCollectionFactory
?: ObjectManager::getInstance()->get(DimensionCollectionFactory::class);
}
/**
* Implementation of abstract construct
* @return void
* @since 100.1.0
*/
protected function _construct()
{
}
/**
* Return array of price data per customer and website by products
*
* @param null|array $productIds
* @return array
* @since 100.1.0
*/
protected function _getCatalogProductPriceData($productIds = null)
{
$connection = $this->getConnection();
$catalogProductIndexPriceSelect = [];
foreach ($this->dimensionCollectionFactory->create() as $dimensions) {
if (!isset($dimensions[WebsiteDimensionProvider::DIMENSION_NAME]) ||
$this->websiteId === null ||
$dimensions[WebsiteDimensionProvider::DIMENSION_NAME]->getValue() === $this->websiteId) {
$select = $connection->select()->from(
$this->tableResolver->resolve('catalog_product_index_price', $dimensions),
['entity_id', 'customer_group_id', 'website_id', 'min_price']
);
if ($productIds) {
$select->where('entity_id IN (?)', $productIds);
}
$catalogProductIndexPriceSelect[] = $select;
}
}
$catalogProductIndexPriceUnionSelect = $connection->select()->union($catalogProductIndexPriceSelect);
$result = [];
foreach ($connection->fetchAll($catalogProductIndexPriceUnionSelect) as $row) {
$result[$row['website_id']][$row['entity_id']][$row['customer_group_id']] = round($row['min_price'], 2);
}
return $result;
}
/**
* Retrieve price data for product
*
* @param null|array $productIds
* @param int $storeId
* @return array
* @since 100.1.0
*/
public function getPriceIndexData($productIds, $storeId)
{
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
$this->websiteId = $websiteId;
$priceProductsIndexData = $this->_getCatalogProductPriceData($productIds);
$this->websiteId = null;
if (!isset($priceProductsIndexData[$websiteId])) {
return [];
}
return $priceProductsIndexData[$websiteId];
}
/**
* Prepare system index data for products.
*
* @param int $storeId
* @param null|array $productIds
* @return array
* @since 100.1.0
*/
public function getCategoryProductIndexData($storeId = null, $productIds = null)
{
$connection = $this->getConnection();
$catalogCategoryProductDimension = new Dimension(\Magento\Store\Model\Store::ENTITY, $storeId);
$catalogCategoryProductTableName = $this->tableResolver->resolve(
AbstractAction::MAIN_INDEX_TABLE,
[
$catalogCategoryProductDimension
]
);
$select = $connection->select()->from(
[$catalogCategoryProductTableName],
['category_id', 'product_id', 'position', 'store_id']
)->where(
'store_id = ?',
$storeId
);
if ($productIds) {
$select->where('product_id IN (?)', $productIds);
}
$result = [];
foreach ($connection->fetchAll($select) as $row) {
$result[$row['product_id']][$row['category_id']] = $row['position'];
}
return $result;
}
/**
* Retrieve moved categories product ids
*
* @param int $categoryId
* @return array
* @since 100.1.0
*/
public function getMovedCategoryProductIds($categoryId)
{
$connection = $this->getConnection();
$identifierField = $this->metadataPool->getMetadata(CategoryInterface::class)->getIdentifierField();
$select = $connection->select()->distinct()->from(
['c_p' => $this->getTable('catalog_category_product')],
['product_id']
)->join(
['c_e' => $this->getTable('catalog_category_entity')],
'c_p.category_id = c_e.' . $identifierField,
[]
)->where(
$connection->quoteInto('c_e.path LIKE ?', '%/' . $categoryId . '/%')
)->orWhere(
'c_p.category_id = ?',
$categoryId
);
return $connection->fetchCol($select);
}
}