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,105 @@ 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
+ if (is_string ($ field ) && count (explode ('. ' , $ field )) === 1 ) {
196
+ $ field = 'main_table. ' . $ field ;
197
+ }
198
+
199
+ return parent ::addFieldToFilter ($ field , $ condition );
200
+ }
201
+
202
+ /**
203
+ * Add fulltext filter
204
+ *
205
+ * @param string $value
206
+ * @return $this
207
+ */
208
+ public function addFullTextFilter (string $ value )
209
+ {
210
+ $ fields = self ::FIELDS_TO_FULLTEXT_SEARCH ;
211
+ $ whereCondition = '' ;
212
+ foreach ($ fields as $ key => $ field ) {
213
+ $ field = $ field === 'region '
214
+ ? $ this ->getRegionNameExpresion ()
215
+ : 'main_table. ' . $ field ;
216
+ $ condition = $ this ->_getConditionSql (
217
+ $ this ->getConnection ()->quoteIdentifier ($ field ),
218
+ ['like ' => "% $ value% " ]
219
+ );
220
+ $ whereCondition .= ($ key === 0 ? '' : ' OR ' ) . $ condition ;
221
+ }
222
+ if ($ whereCondition ) {
223
+ $ this ->getSelect ()->where ($ whereCondition );
224
+ }
225
+
226
+ return $ this ;
227
+ }
228
+
229
+ /**
230
+ * Join region name table by current locale
231
+ *
232
+ * @return $this
233
+ */
234
+ private function joinRegionNameTable ()
235
+ {
236
+ $ locale = $ this ->localeResolver ->getLocale ();
237
+ $ connection = $ this ->getConnection ();
238
+ $ regionIdField = $ connection ->quoteIdentifier ('main_table.region_id ' );
239
+ $ localeCondition = $ connection ->quoteInto ("rnt.locale=? " , $ locale );
240
+
241
+ $ this ->getSelect ()
242
+ ->joinLeft (
243
+ ['rct ' => $ this ->getTable ('directory_country_region ' )],
244
+ "rct.region_id= {$ regionIdField }" ,
245
+ []
246
+ )->joinLeft (
247
+ ['rnt ' => $ this ->getTable ('directory_country_region_name ' )],
248
+ "rnt.region_id= {$ regionIdField } AND {$ localeCondition }" ,
249
+ ['region ' => $ this ->getRegionNameExpresion ()]
250
+ );
251
+
252
+ return $ this ;
253
+ }
254
+
255
+ /**
256
+ * Get SQL Expresion to define Region Name field by locale
257
+ *
258
+ * @return \Zend_Db_Expr
259
+ */
260
+ private function getRegionNameExpresion (): \Zend_Db_Expr
261
+ {
262
+ $ connection = $ this ->getConnection ();
263
+ $ defaultNameExpr = $ connection ->getIfNullSql (
264
+ $ connection ->quoteIdentifier ('rct.default_name ' ),
265
+ $ connection ->quoteIdentifier ('main_table.region ' )
266
+ );
267
+
268
+ return $ connection ->getIfNullSql (
269
+ $ connection ->quoteIdentifier ('rnt.name ' ),
270
+ $ defaultNameExpr
271
+ );
272
+ }
143
273
}
0 commit comments