Repository navigation
Expand file tree
/
Copy pathPollController.cs
More file actions
190 lines (154 loc) · 6.24 KB
/
Copy pathPollController.cs
File metadata and controls
190 lines (154 loc) · 6.24 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SmartStore.Core;
using SmartStore.Core.Caching;
using SmartStore.Core.Domain.Polls;
using SmartStore.Services.Customers;
using SmartStore.Services.Polls;
using SmartStore.Web.Framework.Controllers;
using SmartStore.Web.Infrastructure.Cache;
using SmartStore.Web.Models.Polls;
namespace SmartStore.Web.Controllers
{
public partial class PollController : PublicControllerBase
{
#region Fields
private readonly IWorkContext _workContext;
private readonly IPollService _pollService;
private readonly IWebHelper _webHelper;
private readonly ICacheManager _cacheManager;
private readonly IStoreContext _storeContext;
#endregion
#region Constructors
public PollController(
IWorkContext workContext,
IPollService pollService,
IWebHelper webHelper,
ICacheManager cacheManager,
IStoreContext storeContext)
{
this._workContext = workContext;
this._pollService = pollService;
this._webHelper = webHelper;
this._cacheManager = cacheManager;
this._storeContext = storeContext;
}
#endregion
#region Utilities
[NonAction]
protected PollModel PreparePollModel(Poll poll, bool setAlreadyVotedProperty)
{
var model = new PollModel
{
Id = poll.Id,
AlreadyVoted = setAlreadyVotedProperty && _pollService.AlreadyVoted(poll.Id, _workContext.CurrentCustomer.Id),
Name = poll.Name
};
var answers = poll.PollAnswers.OrderBy(x => x.DisplayOrder);
foreach (var answer in answers)
{
model.TotalVotes += answer.NumberOfVotes;
}
foreach (var pa in answers)
{
model.Answers.Add(new PollAnswerModel
{
Id = pa.Id,
Name = pa.Name,
NumberOfVotes = pa.NumberOfVotes,
PercentOfTotalVotes = model.TotalVotes > 0 ? ((Convert.ToDouble(pa.NumberOfVotes) / Convert.ToDouble(model.TotalVotes)) * Convert.ToDouble(100)) : 0,
});
}
return model;
}
#endregion
#region Methods
[ChildActionOnly]
public ActionResult PollBlock(string systemKeyword)
{
if (String.IsNullOrWhiteSpace(systemKeyword))
return Content("");
var cacheKey = string.Format(ModelCacheEventConsumer.POLL_BY_SYSTEMNAME_MODEL_KEY, systemKeyword, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id);
var cachedModel = _cacheManager.Get(cacheKey, () =>
{
var poll = _pollService.GetPollBySystemKeyword(systemKeyword, _workContext.WorkingLanguage.Id);
if (poll == null)
return new PollModel { Id = 0 }; //we do not cache nulls. that's why let's return an empty record (ID = 0)
return PreparePollModel(poll, false);
});
if (cachedModel == null || cachedModel.Id == 0)
return Content("");
//"AlreadyVoted" property of "PollModel" object depends on the current customer. Let's update it.
//But first we need to clone the cached model (the updated one should not be cached)
var model = (PollModel)cachedModel.Clone();
model.AlreadyVoted = _pollService.AlreadyVoted(model.Id, _workContext.CurrentCustomer.Id);
return PartialView(model);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Vote(int pollAnswerId)
{
var pollAnswer = _pollService.GetPollAnswerById(pollAnswerId);
if (pollAnswer == null)
{
return Json(new { error = T("Polls.AnswerNotFound", pollAnswerId).Text });
}
var poll = pollAnswer.Poll;
if (!poll.Published)
{
return Json(new { error = T("Polls.NotAvailable").Text });
}
if (_workContext.CurrentCustomer.IsGuest() && !poll.AllowGuestsToVote)
{
return Json(new { error = T("Polls.OnlyRegisteredUsersVote").Text });
}
bool alreadyVoted = _pollService.AlreadyVoted(poll.Id, _workContext.CurrentCustomer.Id);
if (!alreadyVoted)
{
//vote
pollAnswer.PollVotingRecords.Add(new PollVotingRecord
{
PollAnswerId = pollAnswer.Id,
CustomerId = _workContext.CurrentCustomer.Id,
IpAddress = _webHelper.GetCurrentIpAddress(),
IsApproved = true,
CreatedOnUtc = DateTime.UtcNow,
UpdatedOnUtc = DateTime.UtcNow,
});
//update totals
pollAnswer.NumberOfVotes = pollAnswer.PollVotingRecords.Count;
_pollService.UpdatePoll(poll);
}
return Json(new
{
html = this.RenderPartialViewToString("_Poll", PreparePollModel(poll, true)),
});
}
[ChildActionOnly]
public ActionResult HomePagePolls()
{
var cacheKey = string.Format(ModelCacheEventConsumer.HOMEPAGE_POLLS_MODEL_KEY, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id);
var cachedModel = _cacheManager.Get(cacheKey, () =>
{
return _pollService.GetPolls(_workContext.WorkingLanguage.Id, true, 0, int.MaxValue)
.Select(x => PreparePollModel(x, false))
.ToList();
});
//"AlreadyVoted" property of "PollModel" object depends on the current customer. Let's update it.
//But first we need to clone the cached model (the updated one should not be cached)
var model = new List<PollModel>();
foreach (var p in cachedModel)
{
var pollModel = (PollModel)p.Clone();
pollModel.AlreadyVoted = _pollService.AlreadyVoted(pollModel.Id, _workContext.CurrentCustomer.Id);
model.Add(pollModel);
}
if (model.Count == 0)
return Content("");
return PartialView(model);
}
#endregion
}
}