123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- <?php
- namespace Dotdigitalgroup\Email\Setup;
- use Magento\Framework\Setup\InstallDataInterface;
- use Magento\Framework\Setup\ModuleContextInterface;
- use Magento\Framework\Setup\ModuleDataSetupInterface;
- /**
- * @codeCoverageIgnore
- */
- class InstallData implements InstallDataInterface
- {
- /**
- * @var \Magento\Config\Model\ResourceModel\Config
- */
- private $config;
- /**
- * @var \Magento\Catalog\Model\Product\TypeFactory
- */
- private $typefactory;
- /**
- * @var \Magento\Catalog\Model\Product\VisibilityFactory
- */
- private $visibilityFactory;
- /**
- * @var \Magento\Sales\Model\Order\Config
- */
- private $orderConfigFactory;
- /**
- * @var \Magento\Framework\Math\Random
- */
- private $randomMath;
- /**
- * InstallData constructor.
- *
- * @param \Magento\Config\Model\ResourceModel\Config $config
- * @param \Magento\Catalog\Model\Product\TypeFactory $typeFactory
- * @param \Magento\Catalog\Model\Product\VisibilityFactory $visibilityFactory
- * @param \Magento\Sales\Model\Order\Config $orderConfigFactory
- * @param \Magento\Framework\Math\Random $random
- */
- public function __construct(
- \Magento\Config\Model\ResourceModel\Config $config,
- \Magento\Catalog\Model\Product\TypeFactory $typeFactory,
- \Magento\Catalog\Model\Product\VisibilityFactory $visibilityFactory,
- \Magento\Sales\Model\Order\Config $orderConfigFactory,
- \Magento\Framework\Math\Random $random
- ) {
- $this->config = $config;
- $this->typefactory = $typeFactory;
- $this->visibilityFactory = $visibilityFactory;
- $this->orderConfigFactory = $orderConfigFactory;
- $this->randomMath = $random;
- }
- /**
- * {@inheritdoc}
- */
- public function install(
- ModuleDataSetupInterface $setup,
- ModuleContextInterface $context
- ) {
- $installer = $setup;
- $installer->startSetup();
- /**
- * Populate table
- */
- $this->populateEmailContactTable($installer);
- $this->updateContactsWithCustomersThatAreSubscribers($installer);
- $this->populateEmailOrderTable($installer);
- $this->populateEmailReviewTable($installer);
- $this->populateEmailWishlistTable($installer);
- $this->populateEmailCatalogTable($installer);
- /**
- * Save config value
- */
- $this->saveAllOrderStatusesAsString($this->config);
- $this->saveAllProductTypesAsString($this->config);
- $this->saveAllProductVisibilitiesAsString($this->config);
- $this->generateAndSaveCode();
- $installer->endSetup();
- }
- /**
- * @param ModuleDataSetupInterface $installer
- *
- * @return null
- */
- private function populateEmailContactTable($installer)
- {
- $select = $installer->getConnection()->select()
- ->from(
- [
- 'customer' => $installer->getTable('customer_entity')
- ],
- [
- 'customer_id' => 'entity_id',
- 'email',
- 'website_id',
- 'store_id'
- ]
- );
- $insertArray = ['customer_id', 'email', 'website_id', 'store_id'];
- $sqlQuery = $select->insertFromSelect(
- $installer->getTable(Schema::EMAIL_CONTACT_TABLE),
- $insertArray,
- false
- );
- $installer->getConnection()->query($sqlQuery);
- //Subscribers that are not customers
- $select = $installer->getConnection()->select()
- ->from(
- [
- 'subscriber' => $installer->getTable(
- 'newsletter_subscriber'
- )
- ],
- [
- 'email' => 'subscriber_email',
- 'col2' => new \Zend_Db_Expr('1'),
- 'col3' => new \Zend_Db_Expr('1'),
- 'store_id',
- ]
- )
- ->where('customer_id =?', 0)
- ->where('subscriber_status =?', 1);
- $insertArray = [
- 'email',
- 'is_subscriber',
- 'subscriber_status',
- 'store_id'
- ];
- $sqlQuery = $select->insertFromSelect(
- $installer->getTable(Schema::EMAIL_CONTACT_TABLE),
- $insertArray,
- false
- );
- $installer->getConnection()->query($sqlQuery);
- }
- /**
- * @param ModuleDataSetupInterface $installer
- *
- * @return null
- */
- private function updateContactsWithCustomersThatAreSubscribers($installer)
- {
- //Update contacts with customers that are subscribers
- $select = $installer->getConnection()->select();
- $select->from(
- $installer->getTable('newsletter_subscriber'),
- 'customer_id'
- )
- ->where('subscriber_status =?', 1)
- ->where('customer_id >?', 0);
- $customerIds = $select->getConnection()->fetchCol($select);
- if (!empty($customerIds)) {
- $installer->getConnection()->update(
- $installer->getTable(Schema::EMAIL_CONTACT_TABLE),
- [
- 'is_subscriber' => new \Zend_Db_Expr('1'),
- 'subscriber_status' => new \Zend_Db_Expr('1')
- ],
- ["customer_id in (?)" => $customerIds]
- );
- }
- }
- /**
- * @param ModuleDataSetupInterface $installer
- *
- * @return null
- */
- private function populateEmailOrderTable($installer)
- {
- $dotmailerTableConnection = $installer->getConnection();
- $dotmailerTableName = $installer->getTable(Schema::EMAIL_ORDER_TABLE);
- $dotmailerTableTargetColumns = [
- 'order_id',
- 'quote_id',
- 'store_id',
- 'created_at',
- 'updated_at',
- 'order_status'
- ];
- $magentoTableConnection = $installer->getConnection('sales');
- $magentoTableName = $installer->getTable('sales_order', 'sales');
- $magentoTableIdColumn = 'entity_id';
- $magentoTableSourceColumns = [
- 'order_id' => 'entity_id',
- 'quote_id',
- 'store_id',
- 'created_at',
- 'updated_at',
- 'order_status' => 'status'
- ];
- $batchSize = 1000;
- $this->batchPopulateTable(
- $dotmailerTableConnection,
- $dotmailerTableName,
- $dotmailerTableTargetColumns,
- $magentoTableConnection,
- $magentoTableName,
- $magentoTableIdColumn,
- $magentoTableSourceColumns,
- $batchSize
- );
- }
- /**
- * @param \Magento\Framework\DB\Adapter\AdapterInterface $dotmailerTableConnection
- * @param string $dotmailerTableName
- * @param array $dotmailerTableTargetColumns
- * @param \Magento\Framework\DB\Adapter\AdapterInterface $magentoTableConnection
- * @param string $magentoTableName
- * @param string $magentoTableIdColumn
- * @param array $magentoTableSourceColumns
- * @param int $batchSize
- *
- * @return null
- */
- private function batchPopulateTable(
- $dotmailerTableConnection,
- $dotmailerTableName,
- $dotmailerTableTargetColumns,
- $magentoTableConnection,
- $magentoTableName,
- $magentoTableIdColumn,
- $magentoTableSourceColumns,
- $batchSize
- ) {
- $sourceConnection = $magentoTableConnection;
- $sourceTableName = $magentoTableName;
- $sourceTableIdColumn = $magentoTableIdColumn;
- $sourceTableColumns = $magentoTableSourceColumns;
- $minSourceIdSelect = $sourceConnection->select()
- ->from($sourceTableName, [$sourceTableIdColumn])
- ->order("$sourceTableIdColumn ASC");
- $minSourceId = $sourceConnection->fetchRow($minSourceIdSelect)[$sourceTableIdColumn];
- if ($minSourceId) {
- $maxSourceIdSelect = $sourceConnection->select()
- ->from($sourceTableName, [$sourceTableIdColumn])
- ->order("$sourceTableIdColumn DESC");
- $maxOrderId = $sourceConnection->fetchRow($maxSourceIdSelect)[$sourceTableIdColumn];
- $batchMinId = $minSourceId;
- $batchMaxId = $minSourceId + $batchSize;
- $moreRecords = true;
- while ($moreRecords) {
- $sourceBatchSelect = $sourceConnection->select()
- ->from($sourceTableName, $sourceTableColumns)
- ->where('entity_id >= ?', $batchMinId)
- ->where('entity_id < ?', $batchMaxId);
- $pageOfResults = $sourceConnection->fetchAll($sourceBatchSelect);
- $dotmailerTableConnection->insertArray(
- $dotmailerTableName,
- $dotmailerTableTargetColumns,
- $pageOfResults
- );
- $moreRecords = $maxOrderId >= $batchMaxId;
- $batchMinId = $batchMinId + $batchSize;
- $batchMaxId = $batchMaxId + $batchSize;
- }
- }
- }
- /**
- * @param ModuleDataSetupInterface $installer
- *
- * @return null
- */
- private function populateEmailReviewTable($installer)
- {
- $inCond = $installer->getConnection()->prepareSqlCondition(
- 'review_detail.customer_id',
- ['notnull' => true]
- );
- $select = $installer->getConnection()->select()
- ->from(
- ['review' => $installer->getTable('review')],
- [
- 'review_id' => 'review.review_id',
- 'created_at' => 'review.created_at'
- ]
- )
- ->joinLeft(
- ['review_detail' => $installer->getTable('review_detail')],
- 'review_detail.review_id = review.review_id',
- [
- 'store_id' => 'review_detail.store_id',
- 'customer_id' => 'review_detail.customer_id'
- ]
- )
- ->where($inCond);
- $insertArray = [
- 'review_id',
- 'created_at',
- 'store_id',
- 'customer_id'
- ];
- $sqlQuery = $select->insertFromSelect(
- $installer->getTable(Schema::EMAIL_REVIEW_TABLE),
- $insertArray,
- false
- );
- $installer->getConnection()->query($sqlQuery);
- }
- /**
- * @param ModuleDataSetupInterface $installer
- *
- * @return null
- */
- private function populateEmailWishlistTable($installer)
- {
- $select = $installer->getConnection()->select()
- ->from(
- ['wishlist' => $installer->getTable('wishlist')],
- [
- 'wishlist_id',
- 'customer_id',
- 'created_at' => 'updated_at'
- ]
- )->joinLeft(
- ['ce' => $installer->getTable('customer_entity')],
- 'wishlist.customer_id = ce.entity_id',
- ['store_id']
- )->joinInner(
- ['wi' => $installer->getTable('wishlist_item')],
- 'wishlist.wishlist_id = wi.wishlist_id',
- ['item_count' => 'count(wi.wishlist_id)']
- )->group('wi.wishlist_id');
- $insertArray = [
- 'wishlist_id',
- 'customer_id',
- 'created_at',
- 'store_id',
- 'item_count'
- ];
- $sqlQuery = $select->insertFromSelect(
- $installer->getTable(Schema::EMAIL_WISHLIST_TABLE),
- $insertArray,
- false
- );
- $installer->getConnection()->query($sqlQuery);
- }
- /**
- * @param ModuleDataSetupInterface $installer
- *
- * @return null
- */
- private function populateEmailCatalogTable($installer)
- {
- $emailCatalogTable = $installer->getTable(Schema::EMAIL_CATALOG_TABLE);
- $select = $installer->getConnection()->select()
- ->from(
- [
- 'catalog' => $installer->getTable(
- 'catalog_product_entity'
- )
- ],
- [
- 'product_id' => 'catalog.entity_id',
- 'created_at' => 'catalog.created_at'
- ]
- )
- ->where(
- 'catalog.entity_id NOT IN (?)',
- $installer->getConnection()->select()->from($emailCatalogTable, ['id'])
- );
- $insertArray = ['product_id', 'created_at'];
- $sqlQuery = $select->insertFromSelect(
- $emailCatalogTable,
- $insertArray,
- false
- );
- $installer->getConnection()->query($sqlQuery);
- }
- /**
- * @param \Magento\Config\Model\ResourceModel\Config $configModel
- *
- * @return null
- */
- private function saveAllOrderStatusesAsString($configModel)
- {
- $options = array_keys($this->orderConfigFactory->getStatuses());
- $statusString = implode(',', $options);
- $configModel->saveConfig(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_STATUS,
- $statusString,
- 'website',
- 0
- );
- }
- /**
- * @param \Magento\Config\Model\ResourceModel\Config $configModel
- * @return null
- */
- private function saveAllProductTypesAsString($configModel)
- {
- $types = $this->typefactory
- ->create()
- ->toOptionArray();
- $options = [];
- foreach ($types as $type) {
- $options[] = $type['value'];
- }
- $typeString = implode(',', $options);
- $configModel->saveConfig(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CATALOG_TYPE,
- $typeString,
- 'website',
- '0'
- );
- }
- /**
- * @param \Magento\Config\Model\ResourceModel\Config $configModel
- *
- * @return null
- */
- private function saveAllProductVisibilitiesAsString($configModel)
- {
- $visibilities = $this->visibilityFactory
- ->create()
- ->toOptionArray();
- $options = [];
- foreach ($visibilities as $visibility) {
- $options[] = $visibility['value'];
- }
- $visibilityString = implode(',', $options);
- $configModel->saveConfig(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_CATALOG_VISIBILITY,
- $visibilityString,
- 'website',
- '0'
- );
- }
- /**
- * Generate and save code
- */
- private function generateAndSaveCode()
- {
- $code = $this->randomMath->getRandomString(32);
- $this->config->saveConfig(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_PASSCODE,
- $code,
- 'default',
- '0'
- );
- }
- }
|