InstallData.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. namespace Dotdigitalgroup\Email\Setup;
  3. use Magento\Framework\Setup\InstallDataInterface;
  4. use Magento\Framework\Setup\ModuleContextInterface;
  5. use Magento\Framework\Setup\ModuleDataSetupInterface;
  6. /**
  7. * @codeCoverageIgnore
  8. */
  9. class InstallData implements InstallDataInterface
  10. {
  11. /**
  12. * @var \Magento\Config\Model\ResourceModel\Config
  13. */
  14. private $config;
  15. /**
  16. * @var \Magento\Catalog\Model\Product\TypeFactory
  17. */
  18. private $typefactory;
  19. /**
  20. * @var \Magento\Catalog\Model\Product\VisibilityFactory
  21. */
  22. private $visibilityFactory;
  23. /**
  24. * @var \Magento\Sales\Model\Order\Config
  25. */
  26. private $orderConfigFactory;
  27. /**
  28. * @var \Magento\Framework\Math\Random
  29. */
  30. private $randomMath;
  31. /**
  32. * InstallData constructor.
  33. *
  34. * @param \Magento\Config\Model\ResourceModel\Config $config
  35. * @param \Magento\Catalog\Model\Product\TypeFactory $typeFactory
  36. * @param \Magento\Catalog\Model\Product\VisibilityFactory $visibilityFactory
  37. * @param \Magento\Sales\Model\Order\Config $orderConfigFactory
  38. * @param \Magento\Framework\Math\Random $random
  39. */
  40. public function __construct(
  41. \Magento\Config\Model\ResourceModel\Config $config,
  42. \Magento\Catalog\Model\Product\TypeFactory $typeFactory,
  43. \Magento\Catalog\Model\Product\VisibilityFactory $visibilityFactory,
  44. \Magento\Sales\Model\Order\Config $orderConfigFactory,
  45. \Magento\Framework\Math\Random $random
  46. ) {
  47. $this->config = $config;
  48. $this->typefactory = $typeFactory;
  49. $this->visibilityFactory = $visibilityFactory;
  50. $this->orderConfigFactory = $orderConfigFactory;
  51. $this->randomMath = $random;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function install(
  57. ModuleDataSetupInterface $setup,
  58. ModuleContextInterface $context
  59. ) {
  60. $installer = $setup;
  61. $installer->startSetup();
  62. /**
  63. * Populate table
  64. */
  65. $this->populateEmailContactTable($installer);
  66. $this->updateContactsWithCustomersThatAreSubscribers($installer);
  67. $this->populateEmailOrderTable($installer);
  68. $this->populateEmailReviewTable($installer);
  69. $this->populateEmailWishlistTable($installer);
  70. $this->populateEmailCatalogTable($installer);
  71. /**
  72. * Save config value
  73. */
  74. $this->saveAllOrderStatusesAsString($this->config);
  75. $this->saveAllProductTypesAsString($this->config);
  76. $this->saveAllProductVisibilitiesAsString($this->config);
  77. $this->generateAndSaveCode();
  78. $installer->endSetup();
  79. }
  80. /**
  81. * @param ModuleDataSetupInterface $installer
  82. *
  83. * @return null
  84. */
  85. private function populateEmailContactTable($installer)
  86. {
  87. $select = $installer->getConnection()->select()
  88. ->from(
  89. [
  90. 'customer' => $installer->getTable('customer_entity')
  91. ],
  92. [
  93. 'customer_id' => 'entity_id',
  94. 'email',
  95. 'website_id',
  96. 'store_id'
  97. ]
  98. );
  99. $insertArray = ['customer_id', 'email', 'website_id', 'store_id'];
  100. $sqlQuery = $select->insertFromSelect(
  101. $installer->getTable(Schema::EMAIL_CONTACT_TABLE),
  102. $insertArray,
  103. false
  104. );
  105. $installer->getConnection()->query($sqlQuery);
  106. //Subscribers that are not customers
  107. $select = $installer->getConnection()->select()
  108. ->from(
  109. [
  110. 'subscriber' => $installer->getTable(
  111. 'newsletter_subscriber'
  112. )
  113. ],
  114. [
  115. 'email' => 'subscriber_email',
  116. 'col2' => new \Zend_Db_Expr('1'),
  117. 'col3' => new \Zend_Db_Expr('1'),
  118. 'store_id',
  119. ]
  120. )
  121. ->where('customer_id =?', 0)
  122. ->where('subscriber_status =?', 1);
  123. $insertArray = [
  124. 'email',
  125. 'is_subscriber',
  126. 'subscriber_status',
  127. 'store_id'
  128. ];
  129. $sqlQuery = $select->insertFromSelect(
  130. $installer->getTable(Schema::EMAIL_CONTACT_TABLE),
  131. $insertArray,
  132. false
  133. );
  134. $installer->getConnection()->query($sqlQuery);
  135. }
  136. /**
  137. * @param ModuleDataSetupInterface $installer
  138. *
  139. * @return null
  140. */
  141. private function updateContactsWithCustomersThatAreSubscribers($installer)
  142. {
  143. //Update contacts with customers that are subscribers
  144. $select = $installer->getConnection()->select();
  145. $select->from(
  146. $installer->getTable('newsletter_subscriber'),
  147. 'customer_id'
  148. )
  149. ->where('subscriber_status =?', 1)
  150. ->where('customer_id >?', 0);
  151. $customerIds = $select->getConnection()->fetchCol($select);
  152. if (!empty($customerIds)) {
  153. $installer->getConnection()->update(
  154. $installer->getTable(Schema::EMAIL_CONTACT_TABLE),
  155. [
  156. 'is_subscriber' => new \Zend_Db_Expr('1'),
  157. 'subscriber_status' => new \Zend_Db_Expr('1')
  158. ],
  159. ["customer_id in (?)" => $customerIds]
  160. );
  161. }
  162. }
  163. /**
  164. * @param ModuleDataSetupInterface $installer
  165. *
  166. * @return null
  167. */
  168. private function populateEmailOrderTable($installer)
  169. {
  170. $dotmailerTableConnection = $installer->getConnection();
  171. $dotmailerTableName = $installer->getTable(Schema::EMAIL_ORDER_TABLE);
  172. $dotmailerTableTargetColumns = [
  173. 'order_id',
  174. 'quote_id',
  175. 'store_id',
  176. 'created_at',
  177. 'updated_at',
  178. 'order_status'
  179. ];
  180. $magentoTableConnection = $installer->getConnection('sales');
  181. $magentoTableName = $installer->getTable('sales_order', 'sales');
  182. $magentoTableIdColumn = 'entity_id';
  183. $magentoTableSourceColumns = [
  184. 'order_id' => 'entity_id',
  185. 'quote_id',
  186. 'store_id',
  187. 'created_at',
  188. 'updated_at',
  189. 'order_status' => 'status'
  190. ];
  191. $batchSize = 1000;
  192. $this->batchPopulateTable(
  193. $dotmailerTableConnection,
  194. $dotmailerTableName,
  195. $dotmailerTableTargetColumns,
  196. $magentoTableConnection,
  197. $magentoTableName,
  198. $magentoTableIdColumn,
  199. $magentoTableSourceColumns,
  200. $batchSize
  201. );
  202. }
  203. /**
  204. * @param \Magento\Framework\DB\Adapter\AdapterInterface $dotmailerTableConnection
  205. * @param string $dotmailerTableName
  206. * @param array $dotmailerTableTargetColumns
  207. * @param \Magento\Framework\DB\Adapter\AdapterInterface $magentoTableConnection
  208. * @param string $magentoTableName
  209. * @param string $magentoTableIdColumn
  210. * @param array $magentoTableSourceColumns
  211. * @param int $batchSize
  212. *
  213. * @return null
  214. */
  215. private function batchPopulateTable(
  216. $dotmailerTableConnection,
  217. $dotmailerTableName,
  218. $dotmailerTableTargetColumns,
  219. $magentoTableConnection,
  220. $magentoTableName,
  221. $magentoTableIdColumn,
  222. $magentoTableSourceColumns,
  223. $batchSize
  224. ) {
  225. $sourceConnection = $magentoTableConnection;
  226. $sourceTableName = $magentoTableName;
  227. $sourceTableIdColumn = $magentoTableIdColumn;
  228. $sourceTableColumns = $magentoTableSourceColumns;
  229. $minSourceIdSelect = $sourceConnection->select()
  230. ->from($sourceTableName, [$sourceTableIdColumn])
  231. ->order("$sourceTableIdColumn ASC");
  232. $minSourceId = $sourceConnection->fetchRow($minSourceIdSelect)[$sourceTableIdColumn];
  233. if ($minSourceId) {
  234. $maxSourceIdSelect = $sourceConnection->select()
  235. ->from($sourceTableName, [$sourceTableIdColumn])
  236. ->order("$sourceTableIdColumn DESC");
  237. $maxOrderId = $sourceConnection->fetchRow($maxSourceIdSelect)[$sourceTableIdColumn];
  238. $batchMinId = $minSourceId;
  239. $batchMaxId = $minSourceId + $batchSize;
  240. $moreRecords = true;
  241. while ($moreRecords) {
  242. $sourceBatchSelect = $sourceConnection->select()
  243. ->from($sourceTableName, $sourceTableColumns)
  244. ->where('entity_id >= ?', $batchMinId)
  245. ->where('entity_id < ?', $batchMaxId);
  246. $pageOfResults = $sourceConnection->fetchAll($sourceBatchSelect);
  247. $dotmailerTableConnection->insertArray(
  248. $dotmailerTableName,
  249. $dotmailerTableTargetColumns,
  250. $pageOfResults
  251. );
  252. $moreRecords = $maxOrderId >= $batchMaxId;
  253. $batchMinId = $batchMinId + $batchSize;
  254. $batchMaxId = $batchMaxId + $batchSize;
  255. }
  256. }
  257. }
  258. /**
  259. * @param ModuleDataSetupInterface $installer
  260. *
  261. * @return null
  262. */
  263. private function populateEmailReviewTable($installer)
  264. {
  265. $inCond = $installer->getConnection()->prepareSqlCondition(
  266. 'review_detail.customer_id',
  267. ['notnull' => true]
  268. );
  269. $select = $installer->getConnection()->select()
  270. ->from(
  271. ['review' => $installer->getTable('review')],
  272. [
  273. 'review_id' => 'review.review_id',
  274. 'created_at' => 'review.created_at'
  275. ]
  276. )
  277. ->joinLeft(
  278. ['review_detail' => $installer->getTable('review_detail')],
  279. 'review_detail.review_id = review.review_id',
  280. [
  281. 'store_id' => 'review_detail.store_id',
  282. 'customer_id' => 'review_detail.customer_id'
  283. ]
  284. )
  285. ->where($inCond);
  286. $insertArray = [
  287. 'review_id',
  288. 'created_at',
  289. 'store_id',
  290. 'customer_id'
  291. ];
  292. $sqlQuery = $select->insertFromSelect(
  293. $installer->getTable(Schema::EMAIL_REVIEW_TABLE),
  294. $insertArray,
  295. false
  296. );
  297. $installer->getConnection()->query($sqlQuery);
  298. }
  299. /**
  300. * @param ModuleDataSetupInterface $installer
  301. *
  302. * @return null
  303. */
  304. private function populateEmailWishlistTable($installer)
  305. {
  306. $select = $installer->getConnection()->select()
  307. ->from(
  308. ['wishlist' => $installer->getTable('wishlist')],
  309. [
  310. 'wishlist_id',
  311. 'customer_id',
  312. 'created_at' => 'updated_at'
  313. ]
  314. )->joinLeft(
  315. ['ce' => $installer->getTable('customer_entity')],
  316. 'wishlist.customer_id = ce.entity_id',
  317. ['store_id']
  318. )->joinInner(
  319. ['wi' => $installer->getTable('wishlist_item')],
  320. 'wishlist.wishlist_id = wi.wishlist_id',
  321. ['item_count' => 'count(wi.wishlist_id)']
  322. )->group('wi.wishlist_id');
  323. $insertArray = [
  324. 'wishlist_id',
  325. 'customer_id',
  326. 'created_at',
  327. 'store_id',
  328. 'item_count'
  329. ];
  330. $sqlQuery = $select->insertFromSelect(
  331. $installer->getTable(Schema::EMAIL_WISHLIST_TABLE),
  332. $insertArray,
  333. false
  334. );
  335. $installer->getConnection()->query($sqlQuery);
  336. }
  337. /**
  338. * @param ModuleDataSetupInterface $installer
  339. *
  340. * @return null
  341. */
  342. private function populateEmailCatalogTable($installer)
  343. {
  344. $emailCatalogTable = $installer->getTable(Schema::EMAIL_CATALOG_TABLE);
  345. $select = $installer->getConnection()->select()
  346. ->from(
  347. [
  348. 'catalog' => $installer->getTable(
  349. 'catalog_product_entity'
  350. )
  351. ],
  352. [
  353. 'product_id' => 'catalog.entity_id',
  354. 'created_at' => 'catalog.created_at'
  355. ]
  356. )
  357. ->where(
  358. 'catalog.entity_id NOT IN (?)',
  359. $installer->getConnection()->select()->from($emailCatalogTable, ['id'])
  360. );
  361. $insertArray = ['product_id', 'created_at'];
  362. $sqlQuery = $select->insertFromSelect(
  363. $emailCatalogTable,
  364. $insertArray,
  365. false
  366. );
  367. $installer->getConnection()->query($sqlQuery);
  368. }
  369. /**
  370. * @param \Magento\Config\Model\ResourceModel\Config $configModel
  371. *
  372. * @return null
  373. */
  374. private function saveAllOrderStatusesAsString($configModel)
  375. {
  376. $options = array_keys($this->orderConfigFactory->getStatuses());
  377. $statusString = implode(',', $options);
  378. $configModel->saveConfig(
  379. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_STATUS,
  380. $statusString,
  381. 'website',
  382. 0
  383. );
  384. }
  385. /**
  386. * @param \Magento\Config\Model\ResourceModel\Config $configModel
  387. * @return null
  388. */
  389. private function saveAllProductTypesAsString($configModel)
  390. {
  391. $types = $this->typefactory
  392. ->create()
  393. ->toOptionArray();
  394. $options = [];
  395. foreach ($types as $type) {
  396. $options[] = $type['value'];
  397. }
  398. $typeString = implode(',', $options);
  399. $configModel->saveConfig(
  400. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CATALOG_TYPE,
  401. $typeString,
  402. 'website',
  403. '0'
  404. );
  405. }
  406. /**
  407. * @param \Magento\Config\Model\ResourceModel\Config $configModel
  408. *
  409. * @return null
  410. */
  411. private function saveAllProductVisibilitiesAsString($configModel)
  412. {
  413. $visibilities = $this->visibilityFactory
  414. ->create()
  415. ->toOptionArray();
  416. $options = [];
  417. foreach ($visibilities as $visibility) {
  418. $options[] = $visibility['value'];
  419. }
  420. $visibilityString = implode(',', $options);
  421. $configModel->saveConfig(
  422. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CATALOG_VISIBILITY,
  423. $visibilityString,
  424. 'website',
  425. '0'
  426. );
  427. }
  428. /**
  429. * Generate and save code
  430. */
  431. private function generateAndSaveCode()
  432. {
  433. $code = $this->randomMath->getRandomString(32);
  434. $this->config->saveConfig(
  435. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_PASSCODE,
  436. $code,
  437. 'default',
  438. '0'
  439. );
  440. }
  441. }