Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/nopSolutions/nopCommerce
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
AndreiMaz committed Apr 29, 2019
2 parents 40ab9c7 + 3af8268 commit 79cd018
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ public IActionResult MessageList(SendinBlueMessageTemplateSearchModel searchMode
return messageTemplates.Select(messageTemplate =>
{
//standard template of message is edited in the admin area, SendinBlue template is edited in the SendinBlue account
var isStandardTemplate = !_genericAttributeService.GetAttribute<bool>(messageTemplate, SendinBlueDefaults.SendinBlueTemplateAttribute);
var templateId = _genericAttributeService.GetAttribute<int>(messageTemplate, SendinBlueDefaults.TemplateIdAttribute);
var templateId = _genericAttributeService.GetAttribute<int?>(messageTemplate, SendinBlueDefaults.TemplateIdAttribute);
var stores = _storeService.GetAllStores()
.Where(store => !messageTemplate.LimitedToStores || _storeMappingService.GetStoresIdsWithAccess(messageTemplate).Contains(store.Id))
.Aggregate(string.Empty, (current, next) => $"{current}, {next.Name}").Trim(',');
Expand All @@ -384,13 +383,10 @@ public IActionResult MessageList(SendinBlueMessageTemplateSearchModel searchMode
Name = messageTemplate.Name,
IsActive = messageTemplate.IsActive,
ListOfStores = stores,
TemplateTypeId = isStandardTemplate ? 0 : 1,
TemplateType = isStandardTemplate
? _localizationService.GetResource("Plugins.Misc.SendinBlue.StandardTemplate")
: _localizationService.GetResource("Plugins.Misc.SendinBlue.SendinBlueTemplate"),
EditLink = isStandardTemplate
? Url.Action("Edit", "MessageTemplate", new { id = messageTemplate.Id, area = AreaNames.Admin })
: $"{string.Format(SendinBlueDefaults.EditMessageTemplateUrl, templateId)}"
UseSendinBlueTemplate = templateId.HasValue,
EditLink = templateId.HasValue
? $"{string.Format(SendinBlueDefaults.EditMessageTemplateUrl, templateId.Value)}"
: Url.Action("Edit", "MessageTemplate", new { id = messageTemplate.Id, area = AreaNames.Admin })
};
});
});
Expand All @@ -408,30 +404,25 @@ public IActionResult MessageUpdate(SendinBlueMessageTemplateModel model)

var message = _messageTemplateService.GetMessageTemplateById(model.Id);

//standard message template
if (model.TemplateTypeId == 0)
{
_genericAttributeService.SaveAttribute(message, SendinBlueDefaults.SendinBlueTemplateAttribute, false);
model.TemplateType = _localizationService.GetResource("Plugins.Misc.SendinBlue.StandardTemplate");
model.EditLink = Url.Action("Edit", "MessageTemplate", new { id = model.Id, area = AreaNames.Admin });
}

//SendinBlue message template
if (model.TemplateTypeId == 1)
if (model.UseSendinBlueTemplate)
{
var storeId = _storeContext.ActiveStoreScopeConfiguration;
var sendinBlueSettings = _settingService.LoadSetting<SendinBlueSettings>(storeId);

//get template or create new one
var currentTemplateId = _genericAttributeService.GetAttribute<int>(message, SendinBlueDefaults.TemplateIdAttribute);
var currentTemplateId = _genericAttributeService.GetAttribute<int?>(message, SendinBlueDefaults.TemplateIdAttribute);
var templateId = _sendinBlueEmailManager.GetTemplateId(currentTemplateId, message,
_emailAccountService.GetEmailAccountById(sendinBlueSettings.EmailAccountId));

_genericAttributeService.SaveAttribute(message, SendinBlueDefaults.SendinBlueTemplateAttribute, true);
_genericAttributeService.SaveAttribute(message, SendinBlueDefaults.TemplateIdAttribute, templateId);
model.TemplateType = _localizationService.GetResource("Plugins.Misc.SendinBlue.SendinBlueTemplate");
model.EditLink = $"{string.Format(SendinBlueDefaults.EditMessageTemplateUrl, templateId)}";
}
else
{
//standard message template
_genericAttributeService.SaveAttribute<int?>(message, SendinBlueDefaults.TemplateIdAttribute, null);
model.EditLink = Url.Action("Edit", "MessageTemplate", new { id = model.Id, area = AreaNames.Admin });
}

//update message template
if (model.IsActive == message.IsActive)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public class SendinBlueMessageTemplateModel : BaseNopEntityModel

public string ListOfStores { get; set; }

public string TemplateType { get; set; }

public int TemplateTypeId { get; set; }
public bool UseSendinBlueTemplate { get; set; }

public string EditLink { get; set; }
}
Expand Down
5 changes: 0 additions & 5 deletions src/Plugins/Nop.Plugin.Misc.SendinBlue/SendinBlueDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,6 @@ public static class SendinBlueDefaults
/// </summary>
public static string MarketingAutomationKeyHeader => "ma-key";

/// <summary>
/// Gets a key of the attribute to store a value indicating whether non-standard template used
/// </summary>
public static string SendinBlueTemplateAttribute => "SendinBlueTemplate";

/// <summary>
/// Gets a key of the attribute to store template identifier
/// </summary>
Expand Down
17 changes: 3 additions & 14 deletions src/Plugins/Nop.Plugin.Misc.SendinBlue/SendinBluePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Nop.Core;
using Nop.Core.Domain.Tasks;
using Nop.Data.Extensions;
using Nop.Services.Cms;
using Nop.Services.Common;
using Nop.Services.Configuration;
Expand Down Expand Up @@ -162,17 +160,15 @@ public override void Install()
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.MyPhone", "Store owner phone number");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.PhoneType", "Type of phone number");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.PhoneType.Hint", "Specify the type of phone number to send SMS.");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.SendinBlueTemplate", "SendinBlue email template");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.SMS", "SMS");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.SMS.Campaigns", "SMS campaigns");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.SMS.Campaigns.Sent", "Campaign successfully sent");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.SMS.Campaigns.Submit", "Send campaign");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.SMSText", "Text");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.SMSText.Hint", "Enter SMS text to send.");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.StandardTemplate", "Standard message template");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.Synchronization", "Contacts");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.TemplateType", "Template type");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.Transactional", "Transactional emails");
_localizationService.AddOrUpdatePluginLocaleResource("Plugins.Misc.SendinBlue.UseSendinBlueTemplate", "SendinBlue template");

base.Install();
}
Expand Down Expand Up @@ -201,12 +197,7 @@ public override void Uninstall()
var messageTemplates = _messageTemplateService.GetAllMessageTemplates(store.Id);
foreach (var messageTemplate in messageTemplates)
{
var genericAttributes = _genericAttributeService
.GetAttributesForEntity(messageTemplate.Id, messageTemplate.GetUnproxiedEntityType().Name)
.Where(attribute => attribute.Key.Equals(SendinBlueDefaults.TemplateIdAttribute)
|| attribute.Key.Equals(SendinBlueDefaults.SendinBlueTemplateAttribute))
.ToList();
_genericAttributeService.DeleteAttributes(genericAttributes);
_genericAttributeService.SaveAttribute<int?>(messageTemplate, SendinBlueDefaults.TemplateIdAttribute, null);
}
}

Expand Down Expand Up @@ -275,11 +266,9 @@ public override void Uninstall()
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.SMSText");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.SMSText.Hint");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.StandardTemplate");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.StandardTemplate");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.StandardTemplate");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.Synchronization");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.TemplateType");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.Transactional");
_localizationService.DeletePluginLocaleResource("Plugins.Misc.SendinBlue.UseSendinBlueTemplate");

base.Uninstall();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ public string PrepareTransactionalAttributes(IList<string> tokens)
/// <param name="message">Message template</param>
/// <param name="emailAccount">Email account</param>
/// <returns>Email template identifier</returns>
public int GetTemplateId(int templateId, MessageTemplate message, EmailAccount emailAccount)
public int? GetTemplateId(int? templateId, MessageTemplate message, EmailAccount emailAccount)
{
try
{
Expand Down Expand Up @@ -1161,13 +1161,13 @@ public int GetTemplateId(int templateId, MessageTemplate message, EmailAccount e
templateName: message.Name, htmlContent: body, subject: subject, isActive: true);
var emailTemplate = client.CreateSmtpTemplate(createSmtpTemplate);

return (int)emailTemplate.Id;
return (int?)emailTemplate.Id;
}
catch (Exception exception)
{
//log full error
_logger.Error($"SendinBlue error: {exception.Message}.", exception, _workContext.CurrentCustomer);
return 0;
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,16 @@ private void SendSmsNotification(MessageTemplate messageTemplate, IEnumerable<To
return null;

//whether to send email by the passed message template
var sendEmailForThisMessageTemplate = _genericAttributeService
.GetAttribute<bool>(messageTemplate, SendinBlueDefaults.SendinBlueTemplateAttribute);
var templateId = _genericAttributeService.GetAttribute<int?>(messageTemplate, SendinBlueDefaults.TemplateIdAttribute);
var sendEmailForThisMessageTemplate = templateId.HasValue;
if (!sendEmailForThisMessageTemplate)
return null;

//get the specified email account from settings
emailAccount = _emailAccountService.GetEmailAccountById(sendinBlueSettings.EmailAccountId) ?? emailAccount;

//get an email from the template
var templateId = _genericAttributeService.GetAttribute<int>(messageTemplate, SendinBlueDefaults.TemplateIdAttribute);
var email = _sendinBlueEmailManager.GetQueuedEmailFromTemplate(templateId)
var email = _sendinBlueEmailManager.GetQueuedEmailFromTemplate(templateId.Value)
?? throw new NopException($"There is no template with id {templateId}");

//replace body and subject tokens
Expand Down
Loading

0 comments on commit 79cd018

Please sign in to comment.