1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model\ResourceModel\Customer;
/**
* Customers collection
*
* @author Magento Core Team <core@magentocommerce.com>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Collection extends \Magento\Eav\Model\Entity\Collection\VersionControl\AbstractCollection
{
/**
* Name of collection model
*/
const CUSTOMER_MODEL_NAME = \Magento\Customer\Model\Customer::class;
/**
* @var \Magento\Framework\DataObject\Copy\Config
*/
protected $_fieldsetConfig;
/**
* @var string
*/
protected $_modelName;
/**
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Framework\App\ResourceConnection $resource
* @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
* @param \Magento\Eav\Model\ResourceModel\Helper $resourceHelper
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
* @param \Magento\Framework\DataObject\Copy\Config $fieldsetConfig
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param string $modelName
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Framework\App\ResourceConnection $resource,
\Magento\Eav\Model\EntityFactory $eavEntityFactory,
\Magento\Eav\Model\ResourceModel\Helper $resourceHelper,
\Magento\Framework\Validator\UniversalFactory $universalFactory,
\Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
\Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
$modelName = self::CUSTOMER_MODEL_NAME
) {
$this->_fieldsetConfig = $fieldsetConfig;
$this->_modelName = $modelName;
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$eavConfig,
$resource,
$eavEntityFactory,
$resourceHelper,
$universalFactory,
$entitySnapshot,
$connection
);
}
/**
* Resource initialization
*
* @return void
*/
protected function _construct()
{
$this->_init($this->_modelName, \Magento\Customer\Model\ResourceModel\Customer::class);
}
/**
* Group result by customer email
*
* @return $this
*/
public function groupByEmail()
{
$this->getSelect()->from(
['email' => $this->getEntity()->getEntityTable()],
['email_count' => new \Zend_Db_Expr('COUNT(email.entity_id)')]
)->where(
'email.entity_id = e.entity_id'
)->group(
'email.email'
);
return $this;
}
/**
* Add Name to select
*
* @return $this
*/
public function addNameToSelect()
{
$fields = [];
$customerAccount = $this->_fieldsetConfig->getFieldset('customer_account');
foreach ($customerAccount as $code => $field) {
if (isset($field['name'])) {
$fields[$code] = $code;
}
}
$connection = $this->getConnection();
$concatenate = [];
if (isset($fields['prefix'])) {
$concatenate[] = $connection->getCheckSql(
'{{prefix}} IS NOT NULL AND {{prefix}} != \'\'',
$connection->getConcatSql(['LTRIM(RTRIM({{prefix}}))', '\' \'']),
'\'\''
);
}
$concatenate[] = 'LTRIM(RTRIM({{firstname}}))';
$concatenate[] = '\' \'';
if (isset($fields['middlename'])) {
$concatenate[] = $connection->getCheckSql(
'{{middlename}} IS NOT NULL AND {{middlename}} != \'\'',
$connection->getConcatSql(['LTRIM(RTRIM({{middlename}}))', '\' \'']),
'\'\''
);
}
$concatenate[] = 'LTRIM(RTRIM({{lastname}}))';
if (isset($fields['suffix'])) {
$concatenate[] = $connection->getCheckSql(
'{{suffix}} IS NOT NULL AND {{suffix}} != \'\'',
$connection->getConcatSql(['\' \'', 'LTRIM(RTRIM({{suffix}}))']),
'\'\''
);
}
$nameExpr = $connection->getConcatSql($concatenate);
$this->addExpressionAttributeToSelect('name', $nameExpr, $fields);
return $this;
}
/**
* Get SQL for get record count
*
* @return \Magento\Framework\DB\Select
*/
public function getSelectCountSql()
{
$select = parent::getSelectCountSql();
$select->resetJoinLeft();
return $select;
}
/**
* Reset left join
*
* @param int $limit
* @param int $offset
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
*/
protected function _getAllIdsSelect($limit = null, $offset = null)
{
$idsSelect = parent::_getAllIdsSelect($limit, $offset);
$idsSelect->resetJoinLeft();
return $idsSelect;
}
}