Exemplo n.º 1
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());
        }