Esempio n. 1
0
        /// <summary>
        /// Uninstall the plugin
        /// </summary>
        public override void Uninstall()
        {
            //delete webhooks
            foreach (var store in _storeService.GetAllStores())
            {
                var currentSettings = _settingService.LoadSetting <MailChimpSettings>(store.Id);
                if (!string.IsNullOrEmpty(currentSettings.ListId) && !string.IsNullOrEmpty(currentSettings.WebhookId))
                {
                    _mailChimpManager.DeleteWebhook(currentSettings.ListId, currentSettings.WebhookId);
                }
            }

            //settings
            _settingService.DeleteSetting <MailChimpSettings>();

            //remove scheduled task
            var task = _scheduleTaskService.GetTaskByType("Nop.Plugin.Misc.MailChimp.Services.MailChimpSynchronizationTask, Nop.Plugin.Misc.MailChimp");

            if (task != null)
            {
                _scheduleTaskService.DeleteTask(task);
            }

            //database objects
            _mailChimpObjectContext.Uninstall();

            //locales
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.AccountInfo");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.AutoSyncRestart");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.ManualSync");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.ManualSync.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.ApiKey");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.ApiKey.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.AutoSync");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.AutoSync.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.AutoSyncEachMinutes");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.AutoSyncEachMinutes.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.List");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.List.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.UseEcommerceApi");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.Fields.UseEcommerceApi.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.SynchronizationError");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.SynchronizationStart");
            this.DeletePluginLocaleResource("Plugins.Misc.MailChimp.WebhookError");

            base.Uninstall();
        }
Esempio n. 2
0
        public ActionResult Configure(MailChimpModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Configure());
            }

            //load settings for a chosen store scope
            var storeId           = GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var mailChimpSettings = _settingService.LoadSetting <MailChimpSettings>(storeId);

            //update stores if the list was changed
            if (model.ListId != null && !model.ListId.Equals("0") && !model.ListId.Equals(mailChimpSettings.ListId))
            {
                if (storeId > 0)
                {
                    _synchronizationRecordService.CreateOrUpdateRecord(EntityType.Store, storeId, ActionType.Update);
                }
                else
                {
                    foreach (var store in _storeService.GetAllStores())
                    {
                        _synchronizationRecordService.CreateOrUpdateRecord(EntityType.Store, store.Id, ActionType.Update);
                    }
                }
            }

            //webhook
            if (!string.IsNullOrEmpty(model.ListId))
            {
                //delete current webhook
                if (!model.ListId.Equals(mailChimpSettings.ListId))
                {
                    _mailChimpManager.DeleteWebhook(mailChimpSettings.ListId, mailChimpSettings.WebhookId);
                    mailChimpSettings.WebhookId = string.Empty;
                }

                //and create new one
                if (!model.ListId.Equals("0"))
                {
                    //we use Task.Run() because child actions cannot be run asynchronously
                    mailChimpSettings.WebhookId = tasks.Task.Run(() => _mailChimpManager.CreateWebhook(model.ListId, mailChimpSettings.WebhookId)).Result;
                    if (string.IsNullOrEmpty(mailChimpSettings.WebhookId))
                    {
                        ErrorNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.WebhookError"));
                    }
                }
            }

            //settings
            mailChimpSettings.ApiKey          = model.ApiKey;
            mailChimpSettings.UseEcommerceApi = model.UseEcommerceApi;
            mailChimpSettings.ListId          = model.ListId;
            _settingService.SaveSetting(mailChimpSettings, x => x.ApiKey, 0, false);
            _settingService.SaveSetting(mailChimpSettings, x => x.UseEcommerceApi, 0, false);
            _settingService.SaveSettingOverridablePerStore(mailChimpSettings, x => x.ListId, model.ListId_OverrideForStore, storeId, false);
            _settingService.SaveSettingOverridablePerStore(mailChimpSettings, x => x.WebhookId, true, storeId, false);

            //now clear settings cache
            _settingService.ClearCache();

            //create or update synchronization task
            var task = FindScheduledTask();

            if (task != null)
            {
                //task parameters was changed
                if (task.Enabled != model.AutoSync || task.Seconds != model.AutoSyncEachMinutes * 60)
                {
                    task.Enabled = model.AutoSync;
                    task.Seconds = model.AutoSyncEachMinutes * 60;
                    _scheduleTaskService.UpdateTask(task);
                    SuccessNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.AutoSyncRestart"));
                }
                else
                {
                    SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved"));
                }
            }
            else
            {
                _scheduleTaskService.InsertTask(new ScheduleTask
                {
                    Name    = "MailChimp synchronization",
                    Seconds = model.AutoSyncEachMinutes * 60,
                    Enabled = model.AutoSync,
                    Type    = "Nop.Plugin.Misc.MailChimp.Services.MailChimpSynchronizationTask, Nop.Plugin.Misc.MailChimp",
                });
                SuccessNotification(_localizationService.GetResource("Plugins.Misc.MailChimp.AutoSyncRestart"));
            }

            return(Configure());
        }