Collection.php 5.8 KB

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