5
5
*/
6
6
namespace Magento \CatalogSearch \Model \Adapter \Mysql \Filter ;
7
7
8
+ use Magento \Catalog \Model \Product ;
9
+ use Magento \CatalogSearch \Model \Search \TableMapper ;
8
10
use Magento \Eav \Model \Config ;
9
11
use Magento \Framework \App \Resource ;
10
12
use Magento \Framework \App \ScopeResolverInterface ;
11
13
use Magento \Framework \DB \Adapter \AdapterInterface ;
12
14
use Magento \Framework \Search \Adapter \Mysql \ConditionManager ;
13
15
use Magento \Framework \Search \Adapter \Mysql \Filter \PreprocessorInterface ;
14
- use Magento \Framework \Search \Adapter \Mysql \Query \QueryContainer ;
15
16
use Magento \Framework \Search \Request \FilterInterface ;
17
+ use Magento \Store \Model \Store ;
16
18
19
+ /**
20
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
+ */
17
22
class Preprocessor implements PreprocessorInterface
18
23
{
19
24
/**
@@ -32,117 +37,121 @@ class Preprocessor implements PreprocessorInterface
32
37
private $ config ;
33
38
34
39
/**
35
- * @var Resource
40
+ * @var string
36
41
*/
37
- private $ resource ;
42
+ private $ attributePrefix ;
38
43
39
44
/**
40
- * @var string
45
+ * @var AdapterInterface
41
46
*/
42
- private $ attributePrefix ;
47
+ private $ connection ;
48
+
49
+ /**
50
+ * @var TableMapper
51
+ */
52
+ private $ tableMapper ;
43
53
44
54
/**
45
55
* @param ConditionManager $conditionManager
46
56
* @param ScopeResolverInterface $scopeResolver
47
57
* @param Config $config
48
58
* @param Resource $resource
59
+ * @param TableMapper $tableMapper
49
60
* @param string $attributePrefix
50
61
*/
51
62
public function __construct (
52
63
ConditionManager $ conditionManager ,
53
64
ScopeResolverInterface $ scopeResolver ,
54
65
Config $ config ,
55
66
Resource $ resource ,
67
+ TableMapper $ tableMapper ,
56
68
$ attributePrefix
57
69
) {
58
70
$ this ->conditionManager = $ conditionManager ;
59
71
$ this ->scopeResolver = $ scopeResolver ;
60
72
$ this ->config = $ config ;
61
- $ this ->resource = $ resource ;
73
+ $ this ->connection = $ resource-> getConnection (Resource:: DEFAULT_READ_RESOURCE ) ;
62
74
$ this ->attributePrefix = $ attributePrefix ;
75
+ $ this ->tableMapper = $ tableMapper ;
63
76
}
64
77
65
78
/**
66
79
* {@inheritdoc}
67
80
*/
68
- public function process (FilterInterface $ filter , $ isNegation , $ query, QueryContainer $ queryContainer )
81
+ public function process (FilterInterface $ filter , $ isNegation , $ query )
69
82
{
70
- return $ this ->processQueryWithField ($ filter , $ isNegation , $ query, $ queryContainer );
83
+ return $ this ->processQueryWithField ($ filter , $ isNegation , $ query );
71
84
}
72
85
73
86
/**
74
87
* @param FilterInterface $filter
75
88
* @param bool $isNegation
76
89
* @param string $query
77
- * @param QueryContainer $queryContainer
78
90
* @return string
79
91
*/
80
- private function processQueryWithField (FilterInterface $ filter , $ isNegation , $ query, QueryContainer $ queryContainer )
92
+ private function processQueryWithField (FilterInterface $ filter , $ isNegation , $ query )
81
93
{
82
94
$ currentStoreId = $ this ->scopeResolver ->getScope ()->getId ();
83
-
95
+ $ select = null ;
96
+ /** @var \Magento\Catalog\Model\Resource\Eav\Attribute $attribute */
84
97
$ attribute = $ this ->config ->getAttribute (\Magento \Catalog \Model \Product::ENTITY , $ filter ->getField ());
85
- $ select = $ this ->getConnection ()->select ();
86
98
$ 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
+ );
97
124
} 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 ();
133
126
}
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 );
134
151
}
135
152
136
153
return 'search_index.entity_id IN (
137
154
select entity_id from ' . $ this ->conditionManager ->wrapBrackets ($ select ) . ' as filter
138
155
) ' ;
139
156
}
140
-
141
- /**
142
- * @return AdapterInterface
143
- */
144
- private function getConnection ()
145
- {
146
- return $ this ->resource ->getConnection (Resource::DEFAULT_READ_RESOURCE );
147
- }
148
157
}
0 commit comments