Пример #1
0
        public ActionResult Configure()
        {
            //load settings for a chosen store scope
            var storeId           = GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var mailChimpSettings = _settingService.LoadSetting <MailChimpSettings>(storeId);

            var model = new MailChimpModel
            {
                ApiKey          = mailChimpSettings.ApiKey,
                UseEcommerceApi = mailChimpSettings.UseEcommerceApi,
                ListId          = mailChimpSettings.ListId,
                ActiveStoreScopeConfiguration = storeId
            };

            if (storeId > 0)
            {
                model.ListId_OverrideForStore = _settingService.SettingExists(mailChimpSettings, x => x.ListId, storeId);
            }

            //synchronization task
            var task = FindScheduledTask();

            if (task != null)
            {
                model.AutoSyncEachMinutes = task.Seconds / 60;
                model.AutoSync            = task.Enabled;
            }

            //get account info
            //we use Task.Run() because child actions cannot be run asynchronously
            model.AccountInfo = tasks.Task.Run(() => _mailChimpManager.GetAccountInfo()).Result;

            //prepare available lists
            model.AvailableLists = tasks.Task.Run(() => _mailChimpManager.GetAvailableLists()).Result;

            return(View("~/Plugins/Misc.MailChimp/Views/MailChimp/Configure.cshtml", model));
        }
Пример #2
0
        public async Task <IActionResult> Configure()
        {
            //load settings for a chosen store scope
            var storeId           = _storeContext.ActiveStoreScopeConfiguration;
            var mailChimpSettings = _settingService.LoadSetting <MailChimpSettings>(storeId);

            //prepare model
            var model = new ConfigurationModel
            {
                ApiKey                        = mailChimpSettings.ApiKey,
                PassEcommerceData             = mailChimpSettings.PassEcommerceData,
                ListId                        = mailChimpSettings.ListId,
                ListId_OverrideForStore       = storeId > 0 && _settingService.SettingExists(mailChimpSettings, settings => settings.ListId, storeId),
                ActiveStoreScopeConfiguration = storeId
            };

            //check whether synchronization is in progress
            model.SynchronizationStarted = _staticCacheManager.Get <int?>(_cacheKeyService.PrepareKeyForDefaultCache(MailChimpDefaults.OperationNumberCacheKey), () => null).HasValue;

            //prepare account info
            if (!string.IsNullOrEmpty(mailChimpSettings.ApiKey))
            {
                model.AccountInfo = await _mailChimpManager.GetAccountInfo();
            }

            //prepare available lists
            if (!string.IsNullOrEmpty(mailChimpSettings.ApiKey))
            {
                model.AvailableLists = await _mailChimpManager.GetAvailableLists() ?? new List <SelectListItem>();
            }

            var defaultListId = mailChimpSettings.ListId;

            if (!model.AvailableLists.Any())
            {
                //add the special item for 'there are no lists' with empty guid value
                model.AvailableLists.Add(new SelectListItem
                {
                    Text  = _localizationService.GetResource("Plugins.Misc.MailChimp.Fields.List.NotExist"),
                    Value = Guid.Empty.ToString()
                });
                defaultListId = Guid.Empty.ToString();
            }
            else if (string.IsNullOrEmpty(mailChimpSettings.ListId) || mailChimpSettings.ListId.Equals(Guid.Empty.ToString()))
            {
                defaultListId = model.AvailableLists.FirstOrDefault()?.Value;
            }

            //set the default list
            model.ListId             = defaultListId;
            mailChimpSettings.ListId = defaultListId;
            _settingService.SaveSettingOverridablePerStore(mailChimpSettings, settings => settings.ListId, model.ListId_OverrideForStore, storeId);

            //synchronization task
            var task = _scheduleTaskService.GetTaskByType(MailChimpDefaults.SynchronizationTask);

            if (task != null)
            {
                model.SynchronizationPeriod = task.Seconds / 60 / 60;
                model.AutoSynchronization   = task.Enabled;
            }

            return(View("~/Plugins/Misc.MailChimp/Views/Configure.cshtml", model));
        }