Collection.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model\ResourceModel\Problem;
  7. use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. /**
  10. * Newsletter problems collection
  11. *
  12. * @SuppressWarnings(PHPMD.LongVariable)
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  19. {
  20. /**
  21. * True when subscribers info joined
  22. *
  23. * @var bool
  24. */
  25. protected $_subscribersInfoJoinedFlag = false;
  26. /**
  27. * True when grouped
  28. *
  29. * @var bool
  30. */
  31. protected $_problemGrouped = false;
  32. /**
  33. * Customer collection factory
  34. *
  35. * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
  36. */
  37. protected $_customerCollectionFactory;
  38. /**
  39. * @var CustomerRepository
  40. */
  41. protected $customerRepository;
  42. /**
  43. * Customer View Helper
  44. *
  45. * @var \Magento\Customer\Helper\View
  46. */
  47. protected $_customerView;
  48. /**
  49. * checks if customer data is loaded
  50. *
  51. * @var boolean
  52. */
  53. protected $_loadCustomersDataFlag = false;
  54. /**
  55. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  56. * @param \Psr\Log\LoggerInterface $logger
  57. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  58. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  59. * @param CustomerRepository $customerRepository
  60. * @param \Magento\Customer\Helper\View $customerView
  61. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  62. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  63. */
  64. public function __construct(
  65. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  66. \Psr\Log\LoggerInterface $logger,
  67. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  68. \Magento\Framework\Event\ManagerInterface $eventManager,
  69. CustomerRepository $customerRepository,
  70. \Magento\Customer\Helper\View $customerView,
  71. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  72. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  73. ) {
  74. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  75. $this->customerRepository = $customerRepository;
  76. $this->_customerView = $customerView;
  77. }
  78. /**
  79. * Define resource model and model
  80. *
  81. * @return void
  82. */
  83. protected function _construct()
  84. {
  85. $this->_init(\Magento\Newsletter\Model\Problem::class, \Magento\Newsletter\Model\ResourceModel\Problem::class);
  86. }
  87. /**
  88. * Set customer loaded status
  89. *
  90. * @param bool $flag
  91. * @return $this
  92. */
  93. protected function _setIsLoaded($flag = true)
  94. {
  95. if (!$flag) {
  96. $this->_loadCustomersDataFlag = false;
  97. }
  98. return parent::_setIsLoaded($flag);
  99. }
  100. /**
  101. * Adds subscribers info
  102. *
  103. * @return $this
  104. */
  105. public function addSubscriberInfo()
  106. {
  107. $this->getSelect()->joinLeft(
  108. ['subscriber' => $this->getTable('newsletter_subscriber')],
  109. 'main_table.subscriber_id = subscriber.subscriber_id',
  110. ['subscriber_email', 'customer_id', 'subscriber_status']
  111. );
  112. $this->addFilterToMap('subscriber_id', 'main_table.subscriber_id');
  113. $this->_subscribersInfoJoinedFlag = true;
  114. return $this;
  115. }
  116. /**
  117. * Adds queue info
  118. *
  119. * @return $this
  120. */
  121. public function addQueueInfo()
  122. {
  123. $this->getSelect()->joinLeft(
  124. ['queue' => $this->getTable('newsletter_queue')],
  125. 'main_table.queue_id = queue.queue_id',
  126. ['queue_start_at', 'queue_finish_at']
  127. )->joinLeft(
  128. ['template' => $this->getTable('newsletter_template')],
  129. 'queue.template_id = template.template_id',
  130. ['template_subject', 'template_code', 'template_sender_name', 'template_sender_email']
  131. );
  132. return $this;
  133. }
  134. /**
  135. * Loads customers info to collection
  136. *
  137. * @return void
  138. */
  139. protected function _addCustomersData()
  140. {
  141. if ($this->_loadCustomersDataFlag) {
  142. return;
  143. }
  144. $this->_loadCustomersDataFlag = true;
  145. foreach ($this->getItems() as $item) {
  146. if ($item->getCustomerId()) {
  147. $customerId = $item->getCustomerId();
  148. try {
  149. $customer = $this->customerRepository->getById($customerId);
  150. $problems = $this->getItemsByColumnValue('customer_id', $customerId);
  151. $customerName = $this->_customerView->getCustomerName($customer);
  152. foreach ($problems as $problem) {
  153. $problem->setCustomerName($customerName)
  154. ->setCustomerFirstName($customer->getFirstname())
  155. ->setCustomerLastName($customer->getLastname());
  156. }
  157. } catch (NoSuchEntityException $e) {
  158. // do nothing if customer is not found by id
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. * Loads collection and adds customers info
  165. *
  166. * @param bool $printQuery
  167. * @param bool $logQuery
  168. * @return $this
  169. */
  170. public function load($printQuery = false, $logQuery = false)
  171. {
  172. parent::load($printQuery, $logQuery);
  173. if ($this->_subscribersInfoJoinedFlag) {
  174. $this->_addCustomersData();
  175. }
  176. return $this;
  177. }
  178. }