Skip to content

Commit 0a17e83

Browse files
committed
Merge remote-tracking branch 'origin/MC-33679' into 2.4.1-develop-pr25
2 parents f5c8afd + bbdec92 commit 0a17e83

File tree

12 files changed

+683
-96
lines changed

12 files changed

+683
-96
lines changed

app/code/Magento/Customer/Model/Customer/DataProviderWithDefaultAddresses.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
use Magento\Customer\Model\Address;
1010
use Magento\Customer\Model\Customer;
1111
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
12+
use Magento\Directory\Model\CountryFactory;
1213
use Magento\Eav\Model\Config;
1314
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1415
use Magento\Eav\Model\Entity\Type;
16+
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\Session\SessionManagerInterface;
1618
use Magento\Customer\Model\FileUploaderDataResolver;
1719
use Magento\Customer\Model\AttributeMetadataResolver;
20+
use Magento\Ui\DataProvider\AbstractDataProvider;
1821

1922
/**
2023
* Refactored version of Magento\Customer\Model\Customer\DataProvider with eliminated usage of addresses collection.
2124
*/
22-
class DataProviderWithDefaultAddresses extends \Magento\Ui\DataProvider\AbstractDataProvider
25+
class DataProviderWithDefaultAddresses extends AbstractDataProvider
2326
{
2427
/**
2528
* @var array
@@ -49,7 +52,7 @@ class DataProviderWithDefaultAddresses extends \Magento\Ui\DataProvider\Abstract
4952
private $allowToShowHiddenAttributes;
5053

5154
/**
52-
* @var \Magento\Directory\Model\CountryFactory
55+
* @var CountryFactory
5356
*/
5457
private $countryFactory;
5558

@@ -69,14 +72,14 @@ class DataProviderWithDefaultAddresses extends \Magento\Ui\DataProvider\Abstract
6972
* @param string $requestFieldName
7073
* @param CustomerCollectionFactory $customerCollectionFactory
7174
* @param Config $eavConfig
72-
* @param \Magento\Directory\Model\CountryFactory $countryFactory
75+
* @param CountryFactory $countryFactory
7376
* @param SessionManagerInterface $session
7477
* @param FileUploaderDataResolver $fileUploaderDataResolver
7578
* @param AttributeMetadataResolver $attributeMetadataResolver
7679
* @param bool $allowToShowHiddenAttributes
7780
* @param array $meta
7881
* @param array $data
79-
* @throws \Magento\Framework\Exception\LocalizedException
82+
* @throws LocalizedException
8083
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8184
*/
8285
public function __construct(
@@ -85,7 +88,7 @@ public function __construct(
8588
string $requestFieldName,
8689
CustomerCollectionFactory $customerCollectionFactory,
8790
Config $eavConfig,
88-
\Magento\Directory\Model\CountryFactory $countryFactory,
91+
CountryFactory $countryFactory,
8992
SessionManagerInterface $session,
9093
FileUploaderDataResolver $fileUploaderDataResolver,
9194
AttributeMetadataResolver $attributeMetadataResolver,
@@ -158,17 +161,21 @@ public function getData(): array
158161
*/
159162
private function prepareDefaultAddress($address): array
160163
{
161-
$addressData = [];
162-
163-
if (!empty($address)) {
164-
$addressData = $address->getData();
165-
if (isset($addressData['street']) && !\is_array($address['street'])) {
166-
$addressData['street'] = explode("\n", $addressData['street']);
167-
}
168-
$countryId = $addressData['country_id'] ?? null;
169-
$addressData['country'] = $this->countryFactory->create()->loadByCode($countryId)->getName();
164+
if (!$address) {
165+
return [];
170166
}
171167

168+
$addressData = $address->getData();
169+
if (isset($addressData['street']) && !is_array($addressData['street'])) {
170+
$addressData['street'] = explode("\n", $addressData['street']);
171+
}
172+
if (!empty($addressData['country_id'])) {
173+
$addressData['country'] = $this->countryFactory->create()
174+
->loadByCode($addressData['country_id'])
175+
->getName();
176+
}
177+
$addressData['region'] = $address->getRegion();
178+
172179
return $addressData;
173180
}
174181

@@ -177,7 +184,7 @@ private function prepareDefaultAddress($address): array
177184
*
178185
* @param Type $entityType
179186
* @return array
180-
* @throws \Magento\Framework\Exception\LocalizedException
187+
* @throws LocalizedException
181188
*/
182189
private function getAttributesMeta(Type $entityType): array
183190
{

app/code/Magento/Customer/Model/ResourceModel/Address/Grid/Collection.php

Lines changed: 152 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,84 @@
66
*/
77
namespace Magento\Customer\Model\ResourceModel\Address\Grid;
88

9+
use Magento\Framework\Api\ExtensibleDataInterface;
910
use Magento\Framework\Api\Search\SearchResultInterface;
1011
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;
1119
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
20+
use Magento\Framework\View\Element\UiComponent\DataProvider\Document;
21+
use Psr\Log\LoggerInterface;
1222

1323
/**
1424
* Class getting collection of addresses assigned to customer
1525
*/
1626
class Collection extends AbstractCollection implements SearchResultInterface
1727
{
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+
1841
/**
1942
* @var AggregationInterface
2043
*/
2144
private $aggregations;
2245

2346
/**
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
2856
* @param string $mainTable
2957
* @param string $eventPrefix
3058
* @param string $eventObject
3159
* @param string $resourceModel
60+
* @param ResolverInterface $localeResolver
3261
* @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
3564
*
3665
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
3766
*/
3867
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,
4372
$mainTable,
4473
$eventPrefix,
4574
$eventObject,
4675
$resourceModel,
47-
$model = \Magento\Framework\View\Element\UiComponent\DataProvider\Document::class,
76+
ResolverInterface $localeResolver,
77+
$model = Document::class,
4878
$connection = null,
49-
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
79+
AbstractDb $resource = null
5080
) {
5181
$this->_eventPrefix = $eventPrefix;
5282
$this->_eventObject = $eventObject;
83+
$this->_idFieldName = 'entity_id';
84+
$this->localeResolver = $localeResolver;
5385
$this->_init($model, $resourceModel);
5486
$this->setMainTable($mainTable);
55-
$this->_idFieldName = 'entity_id';
5687
parent::__construct(
5788
$entityFactory,
5889
$logger,
@@ -65,8 +96,17 @@ public function __construct(
6596

6697
/**
6798
* @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
70110
*/
71111
public function getAggregations()
72112
{
@@ -75,9 +115,6 @@ public function getAggregations()
75115

76116
/**
77117
* @inheritdoc
78-
*
79-
* @param AggregationInterface $aggregations
80-
* @return $this
81118
*/
82119
public function setAggregations($aggregations)
83120
{
@@ -88,7 +125,7 @@ public function setAggregations($aggregations)
88125
/**
89126
* Get search criteria.
90127
*
91-
* @return \Magento\Framework\Api\SearchCriteriaInterface|null
128+
* @return SearchCriteriaInterface|null
92129
*/
93130
public function getSearchCriteria()
94131
{
@@ -98,11 +135,11 @@ public function getSearchCriteria()
98135
/**
99136
* Set search criteria.
100137
*
101-
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
138+
* @param SearchCriteriaInterface $searchCriteria
102139
* @return $this
103140
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
104141
*/
105-
public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
142+
public function setSearchCriteria(SearchCriteriaInterface $searchCriteria = null)
106143
{
107144
return $this;
108145
}
@@ -132,12 +169,105 @@ public function setTotalCount($totalCount)
132169
/**
133170
* Set items list.
134171
*
135-
* @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
172+
* @param ExtensibleDataInterface[] $items
136173
* @return $this
137174
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
138175
*/
139176
public function setItems(array $items = null)
140177
{
141178
return $this;
142179
}
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+
}
143273
}

0 commit comments

Comments
 (0)