diff --git a/Helper/Data.php b/Helper/Data.php index f0a4080..147f861 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -293,7 +293,7 @@ public function __construct( * Get config value * * @param string $path - * @param string $storeId + * @param string|null $storeId * @param null|int|string $scope * @return mixed */ diff --git a/Model/System/Config/Backend/MailjetList.php b/Model/System/Config/Backend/MailjetList.php new file mode 100644 index 0000000..e0ce90e --- /dev/null +++ b/Model/System/Config/Backend/MailjetList.php @@ -0,0 +1,208 @@ +configRepository = $configRepository; + $this->apiConnection = $apiConnection; + $this->configFactory = $configFactory; + $this->jobRepository = $jobRepository; + $this->storeManager = $storeManager; + $this->subscriberQueueRepository = $subscriberQueueRepository; + $this->subscriberCollectionFactory = $subscriberCollectionFactory; + $this->dataHelper = $dataHelper; + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); + } + + /** + * After Save + * + * @return \Mailjet\Mailjet\Model\System\Config\Backend\MailjetList + * @throws \Magento\Framework\Exception\LocalizedException + */ + public function afterSave() + { + $syncPreference = $this->getFieldsetDataValue('sync_preference'); + $websiteId = $this->getScopeId(); + + if ($this->getValue() && $this->isValueChanged() && $this->checkResyncAll($websiteId, $syncPreference)) { + + $configParams = $this->getConfigParams($websiteId); + $config = $this->configFactory->create(); + $config->setApiKey($configParams['apiKey']) + ->setSecretKey($configParams['secretKey']) + ->setList($configParams['list']) + ->setSyncPreference($syncPreference) + ->setStoreId(0) + ->setEcommerceData($configParams['ecommerceData']) + ->setDeleted(1); + + $this->configRepository->save($config); + + if ($config->getId()) { + $collection = $this->subscriberCollectionFactory->create(); + if ($websiteId) { + $website = $this->storeManager->getWebsite($websiteId); + $collection->addStoreFilter($website->getStoreIds()); + } + foreach ($collection as $subscriber) { + if ($subscriber->getSubscriberStatus() == Subscriber::STATUS_SUBSCRIBED) { + $this->subscriberQueueRepository->subscribe($subscriber, $config); + } else { + $this->subscriberQueueRepository->unsubscribe($subscriber, $config); + } + } + + $this->jobRepository->generateJobs([$config]); + } + + } + return parent::afterSave(); + } + + /** + * Check Resync All + * + * @param string|null $websiteId + * @param int|null $syncPreference + * @return bool|mixed + */ + private function checkResyncAll($websiteId, $syncPreference) + { + if ($websiteId) { + $isRsyncAll = $this->dataHelper->getConfigValue( + Data::CONFIG_PATH_ECOMMERCE_DATA, + $websiteId, + ScopeInterface::SCOPE_WEBSITES + ); + } else { + $isRsyncAll = $syncPreference == Data::SYNC_PREFERENCE_ALL || + $this->dataHelper->getConfigValue( + data::CONFIG_PATH_ACCOUNT_SYNC_PREFERENCE + ) == Data::SYNC_PREFERENCE_ALL; + } + return $isRsyncAll; + } + + /** + * Get Params from configuration + * + * @param string|null $websiteId + * @return array + */ + private function getConfigParams($websiteId) + { + $configParams = []; + $configParams['list'] = $this->getFieldsetDataValue('mailjet_list'); + if ($websiteId) { + $configParams['secretKey'] = $this->dataHelper->getConfigValue( + Data::CONFIG_PATH_ACCOUNT_SECRET_KEY, + $websiteId, + ScopeInterface::SCOPE_WEBSITES + ); + $configParams['ecommerceData'] = $this->dataHelper->getConfigValue( + Data::CONFIG_PATH_ECOMMERCE_DATA, + $websiteId, + ScopeInterface::SCOPE_WEBSITES + ); + $configParams['apiKey'] = $this->dataHelper->getConfigValue( + Data::CONFIG_PATH_ACCOUNT_API_KEY, + $websiteId, + ScopeInterface::SCOPE_WEBSITES + ); + + } else { + $configParams['secretKey'] = $this->dataHelper->getConfigValue(Data::CONFIG_PATH_ACCOUNT_SECRET_KEY); + $configParams['ecommerceData'] = $this->dataHelper->getConfigValue(Data::CONFIG_PATH_ECOMMERCE_DATA); + $configParams['apiKey'] = $this->dataHelper->getConfigValue(Data::CONFIG_PATH_ACCOUNT_API_KEY); + } + return $configParams; + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 37df0bc..b75d456 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -50,6 +50,7 @@ Mailjet\Mailjet\Model\System\Config\Source\MailjetList + Mailjet\Mailjet\Model\System\Config\Backend\MailjetList mailjet-mailjet-list 1