6
6
*/
7
7
namespace Magento \Customer \Model \ResourceModel \Address \Grid ;
8
8
9
+ use Magento \Framework \Api \ExtensibleDataInterface ;
9
10
use Magento \Framework \Api \Search \SearchResultInterface ;
10
11
use Magento \Framework \Api \Search \AggregationInterface ;
12
+ use Magento \Framework \Api \SearchCriteriaInterface ;
13
+ use Magento \Framework \Data \Collection \Db \FetchStrategyInterface ;
14
+ use Magento \Framework \Data \Collection \EntityFactoryInterface ;
15
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
16
+ use Magento \Framework \Event \ManagerInterface ;
17
+ use Magento \Framework \Locale \ResolverInterface ;
18
+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
11
19
use Magento \Framework \Model \ResourceModel \Db \Collection \AbstractCollection ;
20
+ use Magento \Framework \View \Element \UiComponent \DataProvider \Document ;
21
+ use Psr \Log \LoggerInterface ;
12
22
13
23
/**
14
24
* Class getting collection of addresses assigned to customer
15
25
*/
16
26
class Collection extends AbstractCollection implements SearchResultInterface
17
27
{
28
+ /**
29
+ * List of fields to fulltext search
30
+ */
31
+ private const FIELDS_TO_FULLTEXT_SEARCH = [
32
+ 'firstname ' ,
33
+ 'lastname ' ,
34
+ 'street ' ,
35
+ 'city ' ,
36
+ 'region ' ,
37
+ 'postcode ' ,
38
+ 'telephone ' ,
39
+ ];
40
+
18
41
/**
19
42
* @var AggregationInterface
20
43
*/
21
44
private $ aggregations ;
22
45
23
46
/**
24
- * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
25
- * @param \Psr\Log\LoggerInterface $logger
26
- * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
27
- * @param \Magento\Framework\Event\ManagerInterface $eventManager
47
+ * @var ResolverInterface
48
+ */
49
+ private $ localeResolver ;
50
+
51
+ /**
52
+ * @param EntityFactoryInterface $entityFactory
53
+ * @param LoggerInterface $logger
54
+ * @param FetchStrategyInterface $fetchStrategy
55
+ * @param ManagerInterface $eventManager
28
56
* @param string $mainTable
29
57
* @param string $eventPrefix
30
58
* @param string $eventObject
31
59
* @param string $resourceModel
60
+ * @param ResolverInterface $localeResolver
32
61
* @param string $model
33
- * @param \Magento\Framework\DB\Adapter\ AdapterInterface|string|null $connection
34
- * @param \Magento\Framework\Model\ResourceModel\Db\ AbstractDb $resource
62
+ * @param AdapterInterface|string|null $connection
63
+ * @param AbstractDb $resource
35
64
*
36
65
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
37
66
*/
38
67
public function __construct (
39
- \ Magento \ Framework \ Data \ Collection \ EntityFactoryInterface $ entityFactory ,
40
- \ Psr \ Log \ LoggerInterface $ logger ,
41
- \ Magento \ Framework \ Data \ Collection \ Db \ FetchStrategyInterface $ fetchStrategy ,
42
- \ Magento \ Framework \ Event \ ManagerInterface $ eventManager ,
68
+ EntityFactoryInterface $ entityFactory ,
69
+ LoggerInterface $ logger ,
70
+ FetchStrategyInterface $ fetchStrategy ,
71
+ ManagerInterface $ eventManager ,
43
72
$ mainTable ,
44
73
$ eventPrefix ,
45
74
$ eventObject ,
46
75
$ resourceModel ,
47
- $ model = \Magento \Framework \View \Element \UiComponent \DataProvider \Document::class,
76
+ ResolverInterface $ localeResolver ,
77
+ $ model = Document::class,
48
78
$ connection = null ,
49
- \ Magento \ Framework \ Model \ ResourceModel \ Db \ AbstractDb $ resource = null
79
+ AbstractDb $ resource = null
50
80
) {
51
81
$ this ->_eventPrefix = $ eventPrefix ;
52
82
$ this ->_eventObject = $ eventObject ;
83
+ $ this ->_idFieldName = 'entity_id ' ;
84
+ $ this ->localeResolver = $ localeResolver ;
53
85
$ this ->_init ($ model , $ resourceModel );
54
86
$ this ->setMainTable ($ mainTable );
55
- $ this ->_idFieldName = 'entity_id ' ;
56
87
parent ::__construct (
57
88
$ entityFactory ,
58
89
$ logger ,
@@ -65,8 +96,17 @@ public function __construct(
65
96
66
97
/**
67
98
* @inheritdoc
68
- *
69
- * @return AggregationInterface
99
+ */
100
+ protected function _initSelect ()
101
+ {
102
+ parent ::_initSelect ();
103
+ $ this ->joinRegionNameTable ();
104
+
105
+ return $ this ;
106
+ }
107
+
108
+ /**
109
+ * @inheritdoc
70
110
*/
71
111
public function getAggregations ()
72
112
{
@@ -75,9 +115,6 @@ public function getAggregations()
75
115
76
116
/**
77
117
* @inheritdoc
78
- *
79
- * @param AggregationInterface $aggregations
80
- * @return $this
81
118
*/
82
119
public function setAggregations ($ aggregations )
83
120
{
@@ -88,7 +125,7 @@ public function setAggregations($aggregations)
88
125
/**
89
126
* Get search criteria.
90
127
*
91
- * @return \Magento\Framework\Api\ SearchCriteriaInterface|null
128
+ * @return SearchCriteriaInterface|null
92
129
*/
93
130
public function getSearchCriteria ()
94
131
{
@@ -98,11 +135,11 @@ public function getSearchCriteria()
98
135
/**
99
136
* Set search criteria.
100
137
*
101
- * @param \Magento\Framework\Api\ SearchCriteriaInterface $searchCriteria
138
+ * @param SearchCriteriaInterface $searchCriteria
102
139
* @return $this
103
140
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
104
141
*/
105
- public function setSearchCriteria (\ Magento \ Framework \ Api \ SearchCriteriaInterface $ searchCriteria = null )
142
+ public function setSearchCriteria (SearchCriteriaInterface $ searchCriteria = null )
106
143
{
107
144
return $ this ;
108
145
}
@@ -132,12 +169,101 @@ public function setTotalCount($totalCount)
132
169
/**
133
170
* Set items list.
134
171
*
135
- * @param \Magento\Framework\Api\ ExtensibleDataInterface[] $items
172
+ * @param ExtensibleDataInterface[] $items
136
173
* @return $this
137
174
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
138
175
*/
139
176
public function setItems (array $ items = null )
140
177
{
141
178
return $ this ;
142
179
}
180
+
181
+ /**
182
+ * @inheritdoc
183
+ */
184
+ public function addFieldToFilter ($ field , $ condition = null )
185
+ {
186
+ if ($ field === 'region ' ) {
187
+ $ conditionSql = $ this ->_getConditionSql (
188
+ $ this ->getRegionNameExpresion (),
189
+ $ condition
190
+ );
191
+ $ this ->getSelect ()->where ($ conditionSql );
192
+ return $ this ;
193
+ }
194
+
195
+ return parent ::addFieldToFilter ('main_table. ' . $ field , $ condition );
196
+ }
197
+
198
+ /**
199
+ * Add fulltext filter
200
+ *
201
+ * @param string $value
202
+ * @return $this
203
+ */
204
+ public function addFullTextFilter (string $ value )
205
+ {
206
+ $ fields = self ::FIELDS_TO_FULLTEXT_SEARCH ;
207
+ $ whereCondition = '' ;
208
+ foreach ($ fields as $ key => $ field ) {
209
+ $ field = $ field === 'region '
210
+ ? $ this ->getRegionNameExpresion ()
211
+ : 'main_table. ' . $ field ;
212
+ $ condition = $ this ->_getConditionSql (
213
+ $ this ->getConnection ()->quoteIdentifier ($ field ),
214
+ ['like ' => "% $ value% " ]
215
+ );
216
+ $ whereCondition .= ($ key === 0 ? '' : ' OR ' ) . $ condition ;
217
+ }
218
+ if ($ whereCondition ) {
219
+ $ this ->getSelect ()->where ($ whereCondition );
220
+ }
221
+
222
+ return $ this ;
223
+ }
224
+
225
+ /**
226
+ * Join region name table by current locale
227
+ *
228
+ * @return $this
229
+ */
230
+ private function joinRegionNameTable ()
231
+ {
232
+ $ locale = $ this ->localeResolver ->getLocale ();
233
+ $ connection = $ this ->getConnection ();
234
+ $ regionIdField = $ connection ->quoteIdentifier ('main_table.region_id ' );
235
+ $ localeCondition = $ connection ->quoteInto ("rnt.locale=? " , $ locale );
236
+
237
+ $ this ->getSelect ()
238
+ ->joinLeft (
239
+ ['rct ' => $ this ->getTable ('directory_country_region ' )],
240
+ "rct.region_id= {$ regionIdField }" ,
241
+ []
242
+ )->joinLeft (
243
+ ['rnt ' => $ this ->getTable ('directory_country_region_name ' )],
244
+ "rnt.region_id= {$ regionIdField } AND {$ localeCondition }" ,
245
+ ['region ' => $ this ->getRegionNameExpresion ()]
246
+ );
247
+
248
+ return $ this ;
249
+ }
250
+
251
+ /**
252
+ * Get SQL Expresion to define Region Name field by locale
253
+ *
254
+ * @return \Zend_Db_Expr
255
+ */
256
+ private function getRegionNameExpresion (): \Zend_Db_Expr
257
+ {
258
+ $ connection = $ this ->getConnection ();
259
+ $ defaultNameExpr = $ connection ->getIfNullSql (
260
+ $ connection ->quoteIdentifier ('rct.default_name ' ),
261
+ $ connection ->quoteIdentifier ('main_table.region ' )
262
+ );
263
+
264
+ return $ connection ->getIfNullSql (
265
+ $ connection ->quoteIdentifier ('rnt.name ' ),
266
+ $ defaultNameExpr
267
+ );
268
+ }
143
269
}
0 commit comments