/// <summary> /// Prepare message template model /// </summary> /// <param name="model">Message template model</param> /// <param name="messageTemplate">Message template</param> /// <param name="excludeProperties">Whether to exclude populating of some properties of model</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the message template model /// </returns> public virtual async Task <MessageTemplateModel> PrepareMessageTemplateModelAsync(MessageTemplateModel model, MessageTemplate messageTemplate, bool excludeProperties = false) { Action <MessageTemplateLocalizedModel, int> localizedModelConfiguration = null; if (messageTemplate != null) { //fill in model values from the entity model ??= messageTemplate.ToModel <MessageTemplateModel>(); //define localized model configuration action localizedModelConfiguration = async(locale, languageId) => { locale.BccEmailAddresses = await _localizationService.GetLocalizedAsync(messageTemplate, entity => entity.BccEmailAddresses, languageId, false, false); locale.Subject = await _localizationService.GetLocalizedAsync(messageTemplate, entity => entity.Subject, languageId, false, false); locale.Body = await _localizationService.GetLocalizedAsync(messageTemplate, entity => entity.Body, languageId, false, false); locale.EmailAccountId = await _localizationService.GetLocalizedAsync(messageTemplate, entity => entity.EmailAccountId, languageId, false, false); //prepare available email accounts await _baseAdminModelFactory.PrepareEmailAccountsAsync(locale.AvailableEmailAccounts, defaultItemText : await _localizationService.GetResourceAsync("Admin.ContentManagement.MessageTemplates.Fields.EmailAccount.Standard")); //PrepareEmailAccounts only gets available accounts, we need to set the item as selected manually if (locale.AvailableEmailAccounts?.FirstOrDefault(x => x.Value == locale.EmailAccountId.ToString()) is SelectListItem emailAccountListItem) { emailAccountListItem.Selected = true; } }; } model.SendImmediately = !model.DelayBeforeSend.HasValue; model.HasAttachedDownload = model.AttachedDownloadId > 0; var allowedTokens = string.Join(", ", await _messageTokenProvider.GetListOfAllowedTokensAsync(_messageTokenProvider.GetTokenGroups(messageTemplate))); model.AllowedTokens = $"{allowedTokens}{Environment.NewLine}{Environment.NewLine}" + $"{await _localizationService.GetResourceAsync("Admin.ContentManagement.MessageTemplates.Tokens.ConditionalStatement")}{Environment.NewLine}"; //prepare localized models if (!excludeProperties) { model.Locales = await _localizedModelFactory.PrepareLocalizedModelsAsync(localizedModelConfiguration); } //prepare available email accounts await _baseAdminModelFactory.PrepareEmailAccountsAsync(model.AvailableEmailAccounts); //prepare available stores await _storeMappingSupportedModelFactory.PrepareModelStoresAsync(model, messageTemplate, excludeProperties); return(model); }
/// <summary> /// Prepare campaign model /// </summary> /// <param name="model">Campaign model</param> /// <param name="campaign">Campaign</param> /// <param name="excludeProperties">Whether to exclude populating of some properties of model</param> /// <returns>Campaign model</returns> public virtual async Task<CampaignModel> PrepareCampaignModelAsync(CampaignModel model, Campaign campaign, bool excludeProperties = false) { //fill in model values from the entity if (campaign != null) { model ??= campaign.ToModel<CampaignModel>(); if (campaign.DontSendBeforeDateUtc.HasValue) model.DontSendBeforeDate = await _dateTimeHelper.ConvertToUserTimeAsync(campaign.DontSendBeforeDateUtc.Value, DateTimeKind.Utc); } model.AllowedTokens = string.Join(", ", await _messageTokenProvider.GetListOfCampaignAllowedTokensAsync()); //whether to fill in some of properties if (!excludeProperties) model.EmailAccountId = _emailAccountSettings.DefaultEmailAccountId; //prepare available stores await _baseAdminModelFactory.PrepareStoresAsync(model.AvailableStores); //prepare available customer roles await _baseAdminModelFactory.PrepareCustomerRolesAsync(model.AvailableCustomerRoles); //prepare available email accounts await _baseAdminModelFactory.PrepareEmailAccountsAsync(model.AvailableEmailAccounts, false); return model; }