Skip to content

Commit ca40259

Browse files
author
Kasian,Andrii(akasian)
committed
Merge pull request #612 from magento-dragons/MAGETWO-42874
[DRAGONS] Bug Fixes, Merchant Beta
2 parents 90cbb60 + 6f36ffa commit ca40259

File tree

20 files changed

+1275
-282
lines changed

20 files changed

+1275
-282
lines changed

app/code/Magento/Catalog/Block/Product/ProductList/Toolbar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function getPagerUrl($params = [])
376376
{
377377
$urlParams = [];
378378
$urlParams['_current'] = true;
379-
$urlParams['_escape'] = true;
379+
$urlParams['_escape'] = false;
380380
$urlParams['_use_rewrite'] = true;
381381
$urlParams['_query'] = $params;
382382
return $this->getUrl('*/*/*', $urlParams);

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php

Lines changed: 74 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
*/
66
namespace Magento\CatalogSearch\Model\Adapter\Mysql\Filter;
77

8+
use Magento\Catalog\Model\Product;
9+
use Magento\CatalogSearch\Model\Search\TableMapper;
810
use Magento\Eav\Model\Config;
911
use Magento\Framework\App\Resource;
1012
use Magento\Framework\App\ScopeResolverInterface;
1113
use Magento\Framework\DB\Adapter\AdapterInterface;
1214
use Magento\Framework\Search\Adapter\Mysql\ConditionManager;
1315
use Magento\Framework\Search\Adapter\Mysql\Filter\PreprocessorInterface;
14-
use Magento\Framework\Search\Adapter\Mysql\Query\QueryContainer;
1516
use Magento\Framework\Search\Request\FilterInterface;
17+
use Magento\Store\Model\Store;
1618

19+
/**
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
*/
1722
class Preprocessor implements PreprocessorInterface
1823
{
1924
/**
@@ -32,117 +37,121 @@ class Preprocessor implements PreprocessorInterface
3237
private $config;
3338

3439
/**
35-
* @var Resource
40+
* @var string
3641
*/
37-
private $resource;
42+
private $attributePrefix;
3843

3944
/**
40-
* @var string
45+
* @var AdapterInterface
4146
*/
42-
private $attributePrefix;
47+
private $connection;
48+
49+
/**
50+
* @var TableMapper
51+
*/
52+
private $tableMapper;
4353

4454
/**
4555
* @param ConditionManager $conditionManager
4656
* @param ScopeResolverInterface $scopeResolver
4757
* @param Config $config
4858
* @param Resource $resource
59+
* @param TableMapper $tableMapper
4960
* @param string $attributePrefix
5061
*/
5162
public function __construct(
5263
ConditionManager $conditionManager,
5364
ScopeResolverInterface $scopeResolver,
5465
Config $config,
5566
Resource $resource,
67+
TableMapper $tableMapper,
5668
$attributePrefix
5769
) {
5870
$this->conditionManager = $conditionManager;
5971
$this->scopeResolver = $scopeResolver;
6072
$this->config = $config;
61-
$this->resource = $resource;
73+
$this->connection = $resource->getConnection(Resource::DEFAULT_READ_RESOURCE);
6274
$this->attributePrefix = $attributePrefix;
75+
$this->tableMapper = $tableMapper;
6376
}
6477

6578
/**
6679
* {@inheritdoc}
6780
*/
68-
public function process(FilterInterface $filter, $isNegation, $query, QueryContainer $queryContainer)
81+
public function process(FilterInterface $filter, $isNegation, $query)
6982
{
70-
return $this->processQueryWithField($filter, $isNegation, $query, $queryContainer);
83+
return $this->processQueryWithField($filter, $isNegation, $query);
7184
}
7285

7386
/**
7487
* @param FilterInterface $filter
7588
* @param bool $isNegation
7689
* @param string $query
77-
* @param QueryContainer $queryContainer
7890
* @return string
7991
*/
80-
private function processQueryWithField(FilterInterface $filter, $isNegation, $query, QueryContainer $queryContainer)
92+
private function processQueryWithField(FilterInterface $filter, $isNegation, $query)
8193
{
8294
$currentStoreId = $this->scopeResolver->getScope()->getId();
83-
95+
$select = null;
96+
/** @var \Magento\Catalog\Model\Resource\Eav\Attribute $attribute */
8497
$attribute = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $filter->getField());
85-
$select = $this->getConnection()->select();
8698
$table = $attribute->getBackendTable();
87-
if ($filter->getField() == 'price') {
88-
$query = str_replace('price', 'min_price', $query);
89-
$select->from(['main_table' => $this->resource->getTableName('catalog_product_index_price')], 'entity_id')
90-
->where($query);
91-
} elseif ($filter->getField() == 'category_ids') {
92-
return 'category_index.category_id = ' . $filter->getValue();
93-
} else {
94-
if ($attribute->isStatic()) {
95-
$select->from(['main_table' => $table], 'entity_id')
96-
->where($query);
99+
if ($filter->getField() === 'price') {
100+
$filterQuery = str_replace(
101+
$this->connection->quoteIdentifier('price'),
102+
$this->connection->quoteIdentifier('price_index.min_price'),
103+
$query
104+
);
105+
return $filterQuery;
106+
} elseif ($filter->getField() === 'category_ids') {
107+
return 'category_ids_index.category_id = ' . $filter->getValue();
108+
} elseif ($attribute->isStatic()) {
109+
$alias = $this->tableMapper->getMappingAlias($filter);
110+
$filterQuery = str_replace(
111+
$this->connection->quoteIdentifier($attribute->getAttributeCode()),
112+
$this->connection->quoteIdentifier($alias . '.' . $attribute->getAttributeCode()),
113+
$query
114+
);
115+
return $filterQuery;
116+
} elseif ($filter->getType() === FilterInterface::TYPE_TERM) {
117+
$alias = $this->tableMapper->getMappingAlias($filter);
118+
if (is_array($filter->getValue())) {
119+
$value = sprintf(
120+
'%s IN (%s)',
121+
($isNegation ? 'NOT' : ''),
122+
implode(',', $filter->getValue())
123+
);
97124
} else {
98-
if ($filter->getType() == FilterInterface::TYPE_TERM) {
99-
if (is_array($filter->getValue())) {
100-
$value = sprintf(
101-
'%s IN (%s)',
102-
($isNegation ? 'NOT' : ''),
103-
implode(',', $filter->getValue())
104-
);
105-
} else {
106-
$value = ($isNegation ? '!' : '') . '= ' . $filter->getValue();
107-
}
108-
$filterQuery = sprintf(
109-
'cpie.store_id = %d AND cpie.attribute_id = %d AND cpie.value %s',
110-
$this->scopeResolver->getScope()->getId(),
111-
$attribute->getId(),
112-
$value
113-
);
114-
$queryContainer->addFilter($filterQuery);
115-
return '';
116-
}
117-
$ifNullCondition = $this->getConnection()->getIfNullSql('current_store.value', 'main_table.value');
118-
119-
$select->from(['main_table' => $table], 'entity_id')
120-
->joinLeft(
121-
['current_store' => $table],
122-
'current_store.attribute_id = main_table.attribute_id AND current_store.store_id = '
123-
. $currentStoreId,
124-
null
125-
)
126-
->columns([$filter->getField() => $ifNullCondition])
127-
->where(
128-
'main_table.attribute_id = ?',
129-
$attribute->getAttributeId()
130-
)
131-
->where('main_table.store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
132-
->having($query);
125+
$value = ($isNegation ? '!' : '') . '= ' . $filter->getValue();
133126
}
127+
$filterQuery = sprintf(
128+
'%1$s.value %2$s',
129+
$alias,
130+
$value
131+
);
132+
return $filterQuery;
133+
} else {
134+
$select = $this->connection->select();
135+
$ifNullCondition = $this->connection->getIfNullSql('current_store.value', 'main_table.value');
136+
137+
$select->from(['main_table' => $table], 'entity_id')
138+
->joinLeft(
139+
['current_store' => $table],
140+
'current_store.attribute_id = main_table.attribute_id AND current_store.store_id = '
141+
. $currentStoreId,
142+
null
143+
)
144+
->columns([$filter->getField() => $ifNullCondition])
145+
->where(
146+
'main_table.attribute_id = ?',
147+
$attribute->getAttributeId()
148+
)
149+
->where('main_table.store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
150+
->having($query);
134151
}
135152

136153
return 'search_index.entity_id IN (
137154
select entity_id from ' . $this->conditionManager->wrapBrackets($select) . ' as filter
138155
)';
139156
}
140-
141-
/**
142-
* @return AdapterInterface
143-
*/
144-
private function getConnection()
145-
{
146-
return $this->resource->getConnection(Resource::DEFAULT_READ_RESOURCE);
147-
}
148157
}

app/code/Magento/CatalogSearch/Model/Search/IndexBuilder.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,33 @@ class IndexBuilder implements IndexBuilderInterface
4848
*/
4949
private $conditionManager;
5050

51+
/**
52+
* @var TableMapper
53+
*/
54+
private $tableMapper;
55+
5156
/**
5257
* @param \Magento\Framework\App\Resource $resource
5358
* @param ScopeConfigInterface $config
5459
* @param StoreManagerInterface $storeManager
5560
* @param ConditionManager $conditionManager
5661
* @param IndexScopeResolver $scopeResolver
62+
* @param TableMapper $tableMapper
5763
*/
5864
public function __construct(
5965
Resource $resource,
6066
ScopeConfigInterface $config,
6167
StoreManagerInterface $storeManager,
6268
ConditionManager $conditionManager,
63-
IndexScopeResolver $scopeResolver
69+
IndexScopeResolver $scopeResolver,
70+
TableMapper $tableMapper
6471
) {
6572
$this->resource = $resource;
6673
$this->config = $config;
6774
$this->storeManager = $storeManager;
6875
$this->conditionManager = $conditionManager;
6976
$this->scopeResolver = $scopeResolver;
77+
$this->tableMapper = $tableMapper;
7078
}
7179

7280
/**
@@ -89,19 +97,7 @@ public function build(RequestInterface $request)
8997
[]
9098
);
9199

92-
if ($this->isNeedToAddFilters($request)) {
93-
$select
94-
->joinLeft(
95-
['category_index' => $this->resource->getTableName('catalog_category_product_index')],
96-
'search_index.entity_id = category_index.product_id',
97-
[]
98-
)
99-
->joinLeft(
100-
['cpie' => $this->resource->getTableName('catalog_product_index_eav')],
101-
'search_index.entity_id = cpie.entity_id AND search_index.attribute_id = cpie.attribute_id',
102-
[]
103-
);
104-
}
100+
$select = $this->tableMapper->addTables($select, $request);
105101

106102
$select = $this->processDimensions($request, $select);
107103

@@ -185,15 +181,6 @@ private function getSelect()
185181
return $this->getReadConnection()->select();
186182
}
187183

188-
/**
189-
* @param RequestInterface $request
190-
* @return bool
191-
*/
192-
private function isNeedToAddFilters(RequestInterface $request)
193-
{
194-
return $this->hasFilters($request->getQuery());
195-
}
196-
197184
/**
198185
* @param QueryInterface $query
199186
* @return bool

0 commit comments

Comments
 (0)