Collection.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel\Contact;
  3. class Collection extends
  4. \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $_idFieldName = 'email_contact_id';
  10. /**
  11. * @var \Magento\Newsletter\Model\SubscriberFactory
  12. */
  13. private $subscriberFactory;
  14. /**
  15. * Initialize resource collection.
  16. *
  17. * @return null
  18. */
  19. public function _construct()
  20. {
  21. $this->_init(
  22. \Dotdigitalgroup\Email\Model\Contact::class,
  23. \Dotdigitalgroup\Email\Model\ResourceModel\Contact::class
  24. );
  25. }
  26. /**
  27. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  28. * @param \Psr\Log\LoggerInterface $logger
  29. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  30. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  31. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  32. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  33. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  34. */
  35. public function __construct(
  36. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  37. \Psr\Log\LoggerInterface $logger,
  38. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  39. \Magento\Framework\Event\ManagerInterface $eventManager,
  40. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  41. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  42. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  43. ) {
  44. $this->subscriberFactory = $subscriberFactory;
  45. parent::__construct(
  46. $entityFactory,
  47. $logger,
  48. $fetchStrategy,
  49. $eventManager,
  50. $connection,
  51. $resource
  52. );
  53. }
  54. /**
  55. * Load contact by customer id.
  56. *
  57. * @param int $customerId
  58. *
  59. * @return bool|\Dotdigitalgroup\Email\Model\Contact
  60. */
  61. public function loadByCustomerId($customerId)
  62. {
  63. $collection = $this->addFieldToFilter('customer_id', $customerId)
  64. ->setPageSize(1);
  65. if ($collection->getSize()) {
  66. return $collection->getFirstItem();
  67. }
  68. return false;
  69. }
  70. /**
  71. * Get all customer contacts not imported for a website.
  72. *
  73. * @param int $websiteId
  74. * @param int $pageSize
  75. *
  76. * @return $this
  77. */
  78. public function getContactsToImportForWebsite($websiteId, $pageSize = 100)
  79. {
  80. $collection = $this->addFieldToFilter('website_id', $websiteId)
  81. ->addFieldToFilter('email_imported', ['null' => true])
  82. ->addFieldToFilter('customer_id', ['neq' => '0']);
  83. $collection->getSelect()->limit($pageSize);
  84. return $collection;
  85. }
  86. /**
  87. * Get missing contacts.
  88. *
  89. * @param int $websiteId
  90. * @param int $pageSize
  91. *
  92. * @return $this
  93. */
  94. public function getMissingContacts($websiteId, $pageSize = 100)
  95. {
  96. $collection = $this->addFieldToFilter('contact_id', ['null' => true])
  97. ->addFieldToFilter('suppressed', ['null' => true])
  98. ->addFieldToFilter('website_id', $websiteId);
  99. $collection->getSelect()->limit($pageSize);
  100. return $collection->load();
  101. }
  102. /**
  103. * Load Contact by Email.
  104. *
  105. * @param string $email
  106. * @param int $websiteId
  107. *
  108. * @return bool|\Dotdigitalgroup\Email\Model\Contact
  109. */
  110. public function loadByCustomerEmail($email, $websiteId)
  111. {
  112. $collection = $this->addFieldToFilter('email', $email)
  113. ->addFieldToFilter('website_id', $websiteId)
  114. ->setPageSize(1);
  115. if ($collection->getSize()) {
  116. return $collection->getFirstItem();
  117. }
  118. return false;
  119. }
  120. /**
  121. * Contact subscribers to import for website.
  122. *
  123. * @param \Magento\Store\Model\Website $website
  124. * @param int $limit
  125. * @param bool $isCustomerCheck
  126. * @return $this
  127. */
  128. public function getSubscribersToImport(
  129. \Magento\Store\Model\Website $website,
  130. $limit = 1000,
  131. $isCustomerCheck = true
  132. ) {
  133. $storeIds = $website->getStoreIds();
  134. $collection = $this->addFieldToFilter('is_subscriber', ['notnull' => true])
  135. ->addFieldToFilter('subscriber_status', '1')
  136. ->addFieldToFilter('subscriber_imported', ['null' => true])
  137. ->addFieldToFilter('store_id', ['in' => $storeIds]);
  138. if ($isCustomerCheck) {
  139. $collection->addFieldToFilter('customer_id', ['neq' => 0]);
  140. } else {
  141. $collection->addFieldToFilter('customer_id', ['eq' => 0]);
  142. }
  143. $collection->getSelect()->limit($limit);
  144. return $collection;
  145. }
  146. /**
  147. * Contact subscribers to import for website.
  148. *
  149. * @param array $emails
  150. *
  151. * @return $this
  152. */
  153. public function getSubscribersToImportFromEmails($emails)
  154. {
  155. return $this->addFieldToFilter('email', ['in' => $emails]);
  156. }
  157. /**
  158. * Get all not imported guests for a website.
  159. *
  160. * @param int $websiteId
  161. * @param boolean $onlySubscriber
  162. *
  163. * @return $this
  164. */
  165. public function getGuests($websiteId, $onlySubscriber = false)
  166. {
  167. $guestCollection = $this->addFieldToFilter('is_guest', ['notnull' => true])
  168. ->addFieldToFilter('email_imported', ['null' => true])
  169. ->addFieldToFilter('website_id', $websiteId);
  170. if ($onlySubscriber) {
  171. $guestCollection->addFieldToFilter('is_subscriber', 1)
  172. ->addFieldToFilter(
  173. 'subscriber_status',
  174. \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
  175. );
  176. }
  177. return $guestCollection->load();
  178. }
  179. /**
  180. * Number contacts marked as imported.
  181. *
  182. * @return int
  183. */
  184. public function getNumberOfImportedContacts()
  185. {
  186. $this->addFieldToFilter('email_imported', ['notnull' => true]);
  187. return $this->getSize();
  188. }
  189. /**
  190. * Get the number of customers for a website.
  191. *
  192. * @param int $websiteId
  193. *
  194. * @return int
  195. */
  196. public function getNumberCustomerContacts($websiteId = 0)
  197. {
  198. return $this->addFieldToFilter('customer_id', ['gt' => '0'])
  199. ->addFieldToFilter('website_id', $websiteId)
  200. ->getSize();
  201. }
  202. /**
  203. * Get number of suppressed contacts as customer.
  204. *
  205. * @param int $websiteId
  206. *
  207. * @return int
  208. */
  209. public function getNumberCustomerSuppressed($websiteId = 0)
  210. {
  211. return $this->addFieldToFilter('customer_id', ['gt' => 0])
  212. ->addFieldToFilter('website_id', $websiteId)
  213. ->addFieldToFilter('suppressed', '1')
  214. ->getSize();
  215. }
  216. /**
  217. * Get number of synced customers.
  218. *
  219. * @param int $websiteId
  220. *
  221. * @return int
  222. */
  223. public function getNumberCustomerSynced($websiteId = 0)
  224. {
  225. return $this->addFieldToFilter('customer_id', ['gt' => 0])
  226. ->addFieldToFilter('website_id', $websiteId)
  227. ->addFieldToFilter('email_imported', '1')
  228. ->getSize();
  229. }
  230. /**
  231. * Get number of subscribers synced.
  232. *
  233. * @param int $websiteId
  234. *
  235. * @return int
  236. */
  237. public function getNumberSubscribersSynced($websiteId = 0)
  238. {
  239. return $this->addFieldToFilter(
  240. 'subscriber_status',
  241. \Dotdigitalgroup\Email\Model\Newsletter\Subscriber::STATUS_SUBSCRIBED
  242. )
  243. ->addFieldToFilter('subscriber_imported', '1')
  244. ->addFieldToFilter('website_id', $websiteId)
  245. ->getSize();
  246. }
  247. /**
  248. * Get number of subscribers.
  249. *
  250. * @param int $websiteId
  251. *
  252. * @return int
  253. */
  254. public function getNumberSubscribers($websiteId = 0)
  255. {
  256. return $this->addFieldToFilter(
  257. 'subscriber_status',
  258. \Dotdigitalgroup\Email\Model\Newsletter\Subscriber::STATUS_SUBSCRIBED
  259. )
  260. ->addFieldToFilter('website_id', $websiteId)
  261. ->getSize();
  262. }
  263. /**
  264. * Get subscribers data by emails
  265. *
  266. * @param array $emails
  267. * @return array
  268. */
  269. public function getSubscriberDataByEmails($emails)
  270. {
  271. $subscriberFactory = $this->subscriberFactory->create();
  272. $subscribersData = $subscriberFactory->getCollection()
  273. ->addFieldToFilter(
  274. 'subscriber_email',
  275. ['in' => $emails]
  276. )
  277. ->addFieldToSelect(['subscriber_email', 'store_id']);
  278. return $subscribersData->toArray();
  279. }
  280. /**
  281. * Get contacts to import by website
  282. *
  283. * @param int $websiteId
  284. * @param int $syncLimit
  285. * @param boolean $onlySubscriber
  286. * @return $this
  287. */
  288. public function getContactsToImportByWebsite($websiteId, $syncLimit, $onlySubscriber = false)
  289. {
  290. $collection = $this->addFieldToSelect('*')
  291. ->addFieldToFilter('customer_id', ['neq' => '0'])
  292. ->addFieldToFilter('email_imported', ['null' => true])
  293. ->addFieldToFilter('website_id', $websiteId)
  294. ->setPageSize($syncLimit);
  295. if ($onlySubscriber) {
  296. $collection->addFieldToFilter('is_subscriber', 1)
  297. ->addFieldToFilter(
  298. 'subscriber_status',
  299. \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
  300. );
  301. }
  302. return $collection;
  303. }
  304. }