Collection.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model\ResourceModel\Subscriber;
  7. use Magento\Newsletter\Model\Queue as ModelQueue;
  8. /**
  9. * Newsletter subscribers collection
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  17. {
  18. /**
  19. * Queue link table name
  20. *
  21. * @var string
  22. */
  23. protected $_queueLinkTable;
  24. /**
  25. * Store table name
  26. *
  27. * @var string
  28. */
  29. protected $_storeTable;
  30. /**
  31. * Queue joined flag
  32. *
  33. * @var boolean
  34. */
  35. protected $_queueJoinedFlag = false;
  36. /**
  37. * Flag that indicates apply of customers info on load
  38. *
  39. * @var boolean
  40. */
  41. protected $_showCustomersInfo = false;
  42. /**
  43. * Filter for count
  44. *
  45. * @var array
  46. */
  47. protected $_countFilterPart = [];
  48. /**
  49. * Customer Eav data
  50. *
  51. * @var \Magento\Eav\Helper\Data
  52. */
  53. protected $_customerHelperData;
  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 \Magento\Eav\Helper\Data $customerHelperData
  60. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  61. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  62. */
  63. public function __construct(
  64. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  65. \Psr\Log\LoggerInterface $logger,
  66. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  67. \Magento\Framework\Event\ManagerInterface $eventManager,
  68. \Magento\Eav\Helper\Data $customerHelperData,
  69. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  70. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  71. ) {
  72. $this->_customerHelperData = $customerHelperData;
  73. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  74. }
  75. /**
  76. * Constructor
  77. * Configures collection
  78. *
  79. * @return void
  80. */
  81. protected function _construct()
  82. {
  83. parent::_construct();
  84. $this->_init(
  85. \Magento\Newsletter\Model\Subscriber::class,
  86. \Magento\Newsletter\Model\ResourceModel\Subscriber::class
  87. );
  88. $this->_queueLinkTable = $this->getTable('newsletter_queue_link');
  89. $this->_storeTable = $this->getTable('store');
  90. $this->_map['fields']['type'] = $this->getResource()->getConnection()->getCheckSql(
  91. 'main_table.customer_id = 0',
  92. 1,
  93. 2
  94. );
  95. $this->_map['fields']['website_id'] = 'store.website_id';
  96. $this->_map['fields']['group_id'] = 'store.group_id';
  97. $this->_map['fields']['store_id'] = 'main_table.store_id';
  98. }
  99. /**
  100. * Set loading mode subscribers by queue
  101. *
  102. * @param ModelQueue $queue
  103. * @return $this
  104. */
  105. public function useQueue(ModelQueue $queue)
  106. {
  107. $this->getSelect()->join(
  108. ['link' => $this->_queueLinkTable],
  109. "link.subscriber_id = main_table.subscriber_id",
  110. []
  111. )->where(
  112. "link.queue_id = ? ",
  113. $queue->getId()
  114. );
  115. $this->_queueJoinedFlag = true;
  116. return $this;
  117. }
  118. /**
  119. * Set using of links to only unsendet letter subscribers.
  120. *
  121. * @return $this
  122. */
  123. public function useOnlyUnsent()
  124. {
  125. if ($this->_queueJoinedFlag) {
  126. $this->addFieldToFilter('link.letter_sent_at', ['null' => 1]);
  127. }
  128. return $this;
  129. }
  130. /**
  131. * Adds customer info to select
  132. *
  133. * @return $this
  134. */
  135. public function showCustomerInfo()
  136. {
  137. $this->getSelect()->joinLeft(
  138. [
  139. 'customer' => $this->getTable('customer_entity')
  140. ],
  141. 'main_table.customer_id = customer.entity_id',
  142. ['firstname', 'lastname']
  143. );
  144. return $this;
  145. }
  146. /**
  147. * Add type field expression to select
  148. *
  149. * @return $this
  150. */
  151. public function addSubscriberTypeField()
  152. {
  153. $this->getSelect()->columns(['type' => new \Zend_Db_Expr($this->_getMappedField('type'))]);
  154. return $this;
  155. }
  156. /**
  157. * Sets flag for customer info loading on load
  158. *
  159. * @return $this
  160. */
  161. public function showStoreInfo()
  162. {
  163. $this->getSelect()->join(
  164. ['store' => $this->_storeTable],
  165. 'store.store_id = main_table.store_id',
  166. ['group_id', 'website_id']
  167. );
  168. return $this;
  169. }
  170. /**
  171. * Returns select count sql
  172. *
  173. * @return string
  174. */
  175. public function getSelectCountSql()
  176. {
  177. $select = parent::getSelectCountSql();
  178. $countSelect = clone $this->getSelect();
  179. $countSelect->reset(\Magento\Framework\DB\Select::HAVING);
  180. return $select;
  181. }
  182. /**
  183. * Load only subscribed customers
  184. *
  185. * @return $this
  186. */
  187. public function useOnlyCustomers()
  188. {
  189. $this->addFieldToFilter('main_table.customer_id', ['gt' => 0]);
  190. return $this;
  191. }
  192. /**
  193. * Show only with subscribed status
  194. *
  195. * @return $this
  196. */
  197. public function useOnlySubscribed()
  198. {
  199. $this->addFieldToFilter(
  200. 'main_table.subscriber_status',
  201. \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
  202. );
  203. return $this;
  204. }
  205. /**
  206. * Filter collection by specified store ids
  207. *
  208. * @param int[]|int $storeIds
  209. * @return $this
  210. */
  211. public function addStoreFilter($storeIds)
  212. {
  213. $this->addFieldToFilter('main_table.store_id', ['in' => $storeIds]);
  214. return $this;
  215. }
  216. /**
  217. * Get queue joined flag
  218. *
  219. * @return bool
  220. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  221. */
  222. public function getQueueJoinedFlag()
  223. {
  224. return $this->_queueJoinedFlag;
  225. }
  226. }