Collection.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model\ResourceModel\Queue;
  7. /**
  8. * Newsletter queue collection.
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  16. {
  17. /**
  18. * True when subscribers info joined
  19. *
  20. * @var bool
  21. */
  22. protected $_addSubscribersFlag = false;
  23. /**
  24. * True when filtered by store
  25. *
  26. * @var bool
  27. */
  28. protected $_isStoreFilter = false;
  29. /**
  30. * Date
  31. *
  32. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  33. */
  34. protected $_date;
  35. /**
  36. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  37. * @param \Psr\Log\LoggerInterface $logger
  38. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  39. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  40. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  41. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  42. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  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\Framework\Stdlib\DateTime\DateTime $date,
  50. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  51. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  52. ) {
  53. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  54. $this->_date = $date;
  55. }
  56. /**
  57. * Initializes collection
  58. *
  59. * @return void
  60. */
  61. protected function _construct()
  62. {
  63. $this->_map['fields']['queue_id'] = 'main_table.queue_id';
  64. $this->_init(\Magento\Newsletter\Model\Queue::class, \Magento\Newsletter\Model\ResourceModel\Queue::class);
  65. }
  66. /**
  67. * Joins templates information
  68. *
  69. * @return $this
  70. */
  71. public function addTemplateInfo()
  72. {
  73. $this->getSelect()->joinLeft(
  74. ['template' => $this->getTable('newsletter_template')],
  75. 'template.template_id=main_table.template_id',
  76. ['template_subject', 'template_sender_name', 'template_sender_email']
  77. );
  78. $this->_joinedTables['template'] = true;
  79. return $this;
  80. }
  81. /**
  82. * Adds subscribers info to select
  83. *
  84. * @return $this
  85. */
  86. protected function _addSubscriberInfoToSelect()
  87. {
  88. /** @var $select \Magento\Framework\DB\Select */
  89. $select = $this->getConnection()->select()->from(
  90. ['qlt' => $this->getTable('newsletter_queue_link')],
  91. 'COUNT(qlt.queue_link_id)'
  92. )->where(
  93. 'qlt.queue_id = main_table.queue_id'
  94. );
  95. $totalExpr = new \Zend_Db_Expr(sprintf('(%s)', $select->assemble()));
  96. $select = $this->getConnection()->select()->from(
  97. ['qls' => $this->getTable('newsletter_queue_link')],
  98. 'COUNT(qls.queue_link_id)'
  99. )->where(
  100. 'qls.queue_id = main_table.queue_id'
  101. )->where(
  102. 'qls.letter_sent_at IS NOT NULL'
  103. );
  104. $sentExpr = new \Zend_Db_Expr(sprintf('(%s)', $select->assemble()));
  105. $this->getSelect()->columns(['subscribers_sent' => $sentExpr, 'subscribers_total' => $totalExpr]);
  106. return $this;
  107. }
  108. /**
  109. * Adds subscribers info to select and loads collection
  110. *
  111. * @param bool $printQuery
  112. * @param bool $logQuery
  113. * @return $this
  114. */
  115. public function load($printQuery = false, $logQuery = false)
  116. {
  117. if ($this->_addSubscribersFlag && !$this->isLoaded()) {
  118. $this->_addSubscriberInfoToSelect();
  119. }
  120. return parent::load($printQuery, $logQuery);
  121. }
  122. /**
  123. * Joins subscribers information
  124. *
  125. * @return $this
  126. */
  127. public function addSubscribersInfo()
  128. {
  129. $this->_addSubscribersFlag = true;
  130. return $this;
  131. }
  132. /**
  133. * Checks if field is 'subscribers_total', 'subscribers_sent' to add specific filter or adds regular filter
  134. *
  135. * @param string $field
  136. * @param null|string|array $condition
  137. * @return $this
  138. */
  139. public function addFieldToFilter($field, $condition = null)
  140. {
  141. if (in_array($field, ['subscribers_total', 'subscribers_sent'])) {
  142. $this->addFieldToFilter('main_table.queue_id', ['in' => $this->_getIdsFromLink($field, $condition)]);
  143. return $this;
  144. } else {
  145. return parent::addFieldToFilter($field, $condition);
  146. }
  147. }
  148. /**
  149. * Returns ids from queue_link table
  150. *
  151. * @param string $field
  152. * @param null|string|array $condition
  153. * @return array
  154. */
  155. protected function _getIdsFromLink($field, $condition)
  156. {
  157. $select = $this->getConnection()->select()->from(
  158. $this->getTable('newsletter_queue_link'),
  159. ['queue_id', 'total' => new \Zend_Db_Expr('COUNT(queue_link_id)')]
  160. )->group(
  161. 'queue_id'
  162. )->having(
  163. $this->_getConditionSql('total', $condition)
  164. );
  165. if ($field == 'subscribers_sent') {
  166. $select->where('letter_sent_at IS NOT NULL');
  167. }
  168. $idList = $this->getConnection()->fetchCol($select);
  169. if (count($idList)) {
  170. return $idList;
  171. }
  172. return [0];
  173. }
  174. /**
  175. * Set filter for queue by subscriber.
  176. *
  177. * @param int $subscriberId
  178. * @return $this
  179. */
  180. public function addSubscriberFilter($subscriberId)
  181. {
  182. $this->getSelect()->join(
  183. ['link' => $this->getTable('newsletter_queue_link')],
  184. 'main_table.queue_id=link.queue_id',
  185. ['letter_sent_at']
  186. )->where(
  187. 'link.subscriber_id = ?',
  188. $subscriberId
  189. );
  190. return $this;
  191. }
  192. /**
  193. * Add filter by only ready for sending item
  194. *
  195. * @return $this
  196. */
  197. public function addOnlyForSendingFilter()
  198. {
  199. $this->getSelect()->where(
  200. 'main_table.queue_status in (?)',
  201. [\Magento\Newsletter\Model\Queue::STATUS_SENDING, \Magento\Newsletter\Model\Queue::STATUS_NEVER]
  202. )->where(
  203. 'main_table.queue_start_at < ?',
  204. $this->_date->gmtDate()
  205. )->where(
  206. 'main_table.queue_start_at IS NOT NULL'
  207. );
  208. return $this;
  209. }
  210. /**
  211. * Add filter by only not sent items
  212. *
  213. * @return $this
  214. */
  215. public function addOnlyUnsentFilter()
  216. {
  217. $this->addFieldToFilter('main_table.queue_status', \Magento\Newsletter\Model\Queue::STATUS_NEVER);
  218. return $this;
  219. }
  220. /**
  221. * Returns options array
  222. *
  223. * @return array
  224. */
  225. public function toOptionArray()
  226. {
  227. return $this->_toOptionArray('queue_id', 'template_subject');
  228. }
  229. /**
  230. * Filter collection by specified store ids
  231. *
  232. * @param int[]|int $storeIds
  233. * @return $this
  234. */
  235. public function addStoreFilter($storeIds)
  236. {
  237. if (!$this->_isStoreFilter) {
  238. $this->getSelect()->joinInner(
  239. ['store_link' => $this->getTable('newsletter_queue_store_link')],
  240. 'main_table.queue_id = store_link.queue_id',
  241. []
  242. )->where(
  243. 'store_link.store_id IN (?)',
  244. $storeIds
  245. )->group(
  246. 'main_table.queue_id'
  247. );
  248. $this->_isStoreFilter = true;
  249. }
  250. return $this;
  251. }
  252. }