Repository navigation
Expand file tree
/
Copy pathHomeController.cs
More file actions
307 lines (263 loc) · 10.1 KB
/
Copy pathHomeController.cs
File metadata and controls
307 lines (263 loc) · 10.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SmartStore.Core;
using SmartStore.Core.Domain.Catalog;
using SmartStore.Core.Domain.Cms;
using SmartStore.Core.Domain.Common;
using SmartStore.Core.Domain.Messages;
using SmartStore.Core.Infrastructure;
using SmartStore.Core.Localization;
using SmartStore.Services;
using SmartStore.Services.Catalog;
using SmartStore.Services.Customers;
using SmartStore.Services.Localization;
using SmartStore.Services.Media;
using SmartStore.Services.Messages;
using SmartStore.Services.Seo;
using SmartStore.Services.Topics;
using SmartStore.Web.Framework.Controllers;
using SmartStore.Web.Framework.Security;
using SmartStore.Web.Framework.UI.Captcha;
using SmartStore.Web.Infrastructure.Cache;
using SmartStore.Web.Models.Catalog;
using SmartStore.Web.Models.Common;
using SmartStore.Web.Models.Topics;
namespace SmartStore.Web.Controllers
{
public partial class HomeController : PublicControllerBase
{
#region Fields
private readonly ICommonServices _services;
private readonly Lazy<ICategoryService> _categoryService;
private readonly Lazy<IProductService> _productService;
private readonly Lazy<IManufacturerService> _manufacturerService;
private readonly Lazy<ITopicService> _topicService;
private readonly Lazy<IQueuedEmailService> _queuedEmailService;
private readonly Lazy<IEmailAccountService> _emailAccountService;
private readonly Lazy<ISitemapGenerator> _sitemapGenerator;
private readonly Lazy<CaptchaSettings> _captchaSettings;
private readonly Lazy<CommonSettings> _commonSettings;
#endregion
#region Constructors
public HomeController(
ICommonServices services,
Lazy<ICategoryService> categoryService,
Lazy<IProductService> productService,
Lazy<IManufacturerService> manufacturerService,
Lazy<ITopicService> topicService,
Lazy<IQueuedEmailService> queuedEmailService,
Lazy<IEmailAccountService> emailAccountService,
Lazy<ISitemapGenerator> sitemapGenerator,
Lazy<CaptchaSettings> captchaSettings,
Lazy<CommonSettings> commonSettings)
{
this._services = services;
this._categoryService = categoryService;
this._productService = productService;
this._manufacturerService = manufacturerService;
this._topicService = topicService;
this._queuedEmailService = queuedEmailService;
this._emailAccountService = emailAccountService;
this._sitemapGenerator = sitemapGenerator;
this._captchaSettings = captchaSettings;
this._commonSettings = commonSettings;
}
#endregion
[RequireHttpsByConfigAttribute(SslRequirement.No)]
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult ContentSlider()
{
var pictureService = EngineContext.Current.Resolve<IPictureService>();
var settings = _services.Settings.LoadSetting<ContentSliderSettings>();
settings.BackgroundPictureUrl = pictureService.GetPictureUrl(settings.BackgroundPictureId, 0, false);
var slides = settings.Slides
.Where(s =>
s.LanguageCulture == _services.WorkContext.WorkingLanguage.LanguageCulture &&
(!s.LimitedToStores || (s.SelectedStoreIds != null && s.SelectedStoreIds.Contains(_services.StoreContext.CurrentStore.Id)))
)
.OrderBy(s => s.DisplayOrder);
foreach (var slide in slides)
{
slide.PictureUrl = pictureService.GetPictureUrl(slide.PictureId, 0, false);
slide.Button1.Url = CheckButtonUrl(slide.Button1.Url);
slide.Button2.Url = CheckButtonUrl(slide.Button2.Url);
slide.Button3.Url = CheckButtonUrl(slide.Button3.Url);
}
settings.Slides = slides.ToList();
return PartialView(settings);
}
public ActionResult StoreClosed()
{
return View();
}
[RequireHttpsByConfigAttribute(SslRequirement.No)]
public ActionResult ContactUs()
{
var model = new ContactUsModel()
{
Email = _services.WorkContext.CurrentCustomer.Email,
FullName = _services.WorkContext.CurrentCustomer.GetFullName(),
DisplayCaptcha = _captchaSettings.Value.Enabled && _captchaSettings.Value.ShowOnContactUsPage
};
return View(model);
}
[HttpPost, ActionName("ContactUs")]
[CaptchaValidator]
public ActionResult ContactUsSend(ContactUsModel model, bool captchaValid)
{
//validate CAPTCHA
if (_captchaSettings.Value.Enabled && _captchaSettings.Value.ShowOnContactUsPage && !captchaValid)
{
ModelState.AddModelError("", T("Common.WrongCaptcha"));
}
if (ModelState.IsValid)
{
string email = model.Email.Trim();
string fullName = model.FullName;
string subject = T("ContactUs.EmailSubject", _services.StoreContext.CurrentStore.Name);
var emailAccount = _emailAccountService.Value.GetDefaultEmailAccount();
string from = null;
string fromName = null;
string body = Core.Html.HtmlUtils.FormatText(model.Enquiry, false, true, false, false, false, false);
//required for some SMTP servers
if (_commonSettings.Value.UseSystemEmailForContactUsForm)
{
from = emailAccount.Email;
fromName = emailAccount.DisplayName;
body = string.Format("<strong>From</strong>: {0} - {1}<br /><br />{2}",
Server.HtmlEncode(fullName),
Server.HtmlEncode(email), body);
}
else
{
from = email;
fromName = fullName;
}
_queuedEmailService.Value.InsertQueuedEmail(new QueuedEmail
{
From = from,
FromName = fromName,
To = emailAccount.Email,
ToName = emailAccount.DisplayName,
Priority = 5,
Subject = subject,
Body = body,
CreatedOnUtc = DateTime.UtcNow,
EmailAccountId = emailAccount.Id,
ReplyTo = email,
ReplyToName = fullName
});
model.SuccessfullySent = true;
model.Result = T("ContactUs.YourEnquiryHasBeenSent");
//activity log
_services.CustomerActivity.InsertActivity("PublicStore.ContactUs", T("ActivityLog.PublicStore.ContactUs"));
return View(model);
}
model.DisplayCaptcha = _captchaSettings.Value.Enabled && _captchaSettings.Value.ShowOnContactUsPage;
return View(model);
}
[RequireHttpsByConfigAttribute(SslRequirement.No)]
public ActionResult SitemapSeo()
{
if (!_commonSettings.Value.SitemapEnabled)
return HttpNotFound();
var roleIds = _services.WorkContext.CurrentCustomer.CustomerRoles.Where(x => x.Active).Select(x => x.Id).ToList();
string cacheKey = ModelCacheEventConsumer.SITEMAP_XML_MODEL_KEY.FormatInvariant(_services.WorkContext.WorkingLanguage.Id, string.Join(",", roleIds), _services.StoreContext.CurrentStore.Id);
var sitemap = _services.Cache.Get(cacheKey, () =>
{
return _sitemapGenerator.Value.Generate(this.Url);
}, 120);
return Content(sitemap, "text/xml");
}
[RequireHttpsByConfigAttribute(SslRequirement.No)]
public ActionResult Sitemap()
{
if (!_commonSettings.Value.SitemapEnabled)
return HttpNotFound();
var roleIds = _services.WorkContext.CurrentCustomer.CustomerRoles.Where(x => x.Active).Select(x => x.Id).ToList();
string cacheKey = ModelCacheEventConsumer.SITEMAP_PAGE_MODEL_KEY.FormatInvariant(_services.WorkContext.WorkingLanguage.Id, string.Join(",", roleIds), _services.StoreContext.CurrentStore.Id);
var result = _services.Cache.Get(cacheKey, () =>
{
var model = new SitemapModel();
if (_commonSettings.Value.SitemapIncludeCategories)
{
var categories = _categoryService.Value.GetAllCategories();
model.Categories = categories.Select(x => x.ToModel()).ToList();
}
if (_commonSettings.Value.SitemapIncludeManufacturers)
{
var manufacturers = _manufacturerService.Value.GetAllManufacturers();
model.Manufacturers = manufacturers.Select(x => x.ToModel()).ToList();
}
if (_commonSettings.Value.SitemapIncludeProducts)
{
//limit product to 200 until paging is supported on this page
IList<int> filterableSpecificationAttributeOptionIds = null;
var productSearchContext = new ProductSearchContext();
productSearchContext.OrderBy = ProductSortingEnum.Position;
productSearchContext.PageSize = 200;
productSearchContext.FilterableSpecificationAttributeOptionIds = filterableSpecificationAttributeOptionIds;
productSearchContext.StoreId = _services.StoreContext.CurrentStoreIdIfMultiStoreMode;
productSearchContext.VisibleIndividuallyOnly = true;
var products = _productService.Value.SearchProducts(productSearchContext);
model.Products = products.Select(product => new ProductOverviewModel()
{
Id = product.Id,
Name = product.GetLocalized(x => x.Name).EmptyNull(),
ShortDescription = product.GetLocalized(x => x.ShortDescription),
SeName = product.GetSeName(),
}).ToList();
}
if (_commonSettings.Value.SitemapIncludeTopics)
{
var topics = _topicService.Value.GetAllTopics(_services.StoreContext.CurrentStore.Id)
.ToList()
.FindAll(t => t.IncludeInSitemap);
model.Topics = topics.Select(topic => new TopicModel()
{
Id = topic.Id,
SystemName = topic.SystemName,
IncludeInSitemap = topic.IncludeInSitemap,
IsPasswordProtected = topic.IsPasswordProtected,
Title = topic.GetLocalized(x => x.Title),
})
.ToList();
}
return model;
});
return View(result);
}
#region helper functions
private string CheckButtonUrl(string url)
{
if (!String.IsNullOrEmpty(url))
{
if (url.StartsWith("//") || url.StartsWith("/") || url.StartsWith("http://") || url.StartsWith("https://"))
{
// //www.domain.de/dir
// http://www.domain.de/dir
// nothing needs to be done
return url;
}
else if (url.StartsWith("~/"))
{
// ~/directory
return Url.Content(url);
}
else
{
// directory
return Url.Content("~/" + url);
}
}
return url.EmptyNull();
}
#endregion helper functions
}
}