Repository navigation
Expand file tree
/
Copy pathNewsletterController.cs
More file actions
135 lines (119 loc) · 3.87 KB
/
Copy pathNewsletterController.cs
File metadata and controls
135 lines (119 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Web.Mvc;
using SmartStore.Core;
using SmartStore.Core.Domain.Customers;
using SmartStore.Core.Domain.Messages;
using SmartStore.Services.Messages;
using SmartStore.Web.Framework.Controllers;
using SmartStore.Web.Models.Newsletter;
namespace SmartStore.Web.Controllers
{
public partial class NewsletterController : PublicControllerBase
{
private readonly IWorkContext _workContext;
private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
private readonly IWorkflowMessageService _workflowMessageService;
private readonly IStoreContext _storeContext;
private readonly CustomerSettings _customerSettings;
public NewsletterController(
IWorkContext workContext,
INewsLetterSubscriptionService newsLetterSubscriptionService,
IWorkflowMessageService workflowMessageService,
CustomerSettings customerSettings,
IStoreContext storeContext)
{
this._workContext = workContext;
this._newsLetterSubscriptionService = newsLetterSubscriptionService;
this._workflowMessageService = workflowMessageService;
this._customerSettings = customerSettings;
this._storeContext = storeContext;
}
[ChildActionOnly]
public ActionResult NewsletterBox()
{
if (_customerSettings.HideNewsletterBlock)
return Content("");
return PartialView(new NewsletterBoxModel());
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Subscribe(bool subscribe, string email)
{
string result;
var success = false;
if (!email.IsEmail())
{
result = T("Newsletter.Email.Wrong");
}
else
{
//subscribe/unsubscribe
email = email.Trim();
var subscription = _newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmail(email, _storeContext.CurrentStore.Id);
if (subscription != null)
{
if (subscribe)
{
if (!subscription.Active)
{
_workflowMessageService.SendNewsLetterSubscriptionActivationMessage(subscription, _workContext.WorkingLanguage.Id);
}
result = T("Newsletter.SubscribeEmailSent");
}
else
{
if (subscription.Active)
{
_workflowMessageService.SendNewsLetterSubscriptionDeactivationMessage(subscription, _workContext.WorkingLanguage.Id);
}
result = T("Newsletter.UnsubscribeEmailSent");
}
}
else if (subscribe)
{
subscription = new NewsLetterSubscription
{
NewsLetterSubscriptionGuid = Guid.NewGuid(),
Email = email,
Active = false,
CreatedOnUtc = DateTime.UtcNow,
StoreId = _storeContext.CurrentStore.Id
};
_newsLetterSubscriptionService.InsertNewsLetterSubscription(subscription);
_workflowMessageService.SendNewsLetterSubscriptionActivationMessage(subscription, _workContext.WorkingLanguage.Id);
result = T("Newsletter.SubscribeEmailSent");
}
else
{
result = T("Newsletter.UnsubscribeEmailSent");
}
success = true;
}
return Json(new
{
Success = success,
Result = result,
});
}
public ActionResult SubscriptionActivation(Guid token, bool active)
{
var subscription = _newsLetterSubscriptionService.GetNewsLetterSubscriptionByGuid(token);
if (subscription == null)
{
return HttpNotFound();
}
var model = new SubscriptionActivationModel();
if (active)
{
subscription.Active = active;
_newsLetterSubscriptionService.UpdateNewsLetterSubscription(subscription);
}
else
{
_newsLetterSubscriptionService.DeleteNewsLetterSubscription(subscription);
}
model.Result = T(active ? "Newsletter.ResultActivated" : "Newsletter.ResultDeactivated");
return View(model);
}
}
}