forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgebaseController.cs
More file actions
322 lines (285 loc) · 15.8 KB
/
Copy pathKnowledgebaseController.cs
File metadata and controls
322 lines (285 loc) · 15.8 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using Grand.Core;
using Grand.Core.Caching;
using Grand.Core.Domain.Customers;
using Grand.Core.Domain.Knowledgebase;
using Grand.Core.Domain.Localization;
using Grand.Core.Domain.Media;
using Grand.Framework.Controllers;
using Grand.Framework.Mvc.Filters;
using Grand.Framework.Security;
using Grand.Framework.Security.Captcha;
using Grand.Services.Common;
using Grand.Services.Customers;
using Grand.Services.Helpers;
using Grand.Services.Knowledgebase;
using Grand.Services.Localization;
using Grand.Services.Logging;
using Grand.Services.Media;
using Grand.Services.Messages;
using Grand.Services.Security;
using Grand.Services.Seo;
using Grand.Services.Stores;
using Grand.Web.Infrastructure.Cache;
using Grand.Web.Models.Knowledgebase;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Grand.Web.Controllers
{
public class KnowledgebaseController : BasePublicController
{
private readonly KnowledgebaseSettings _knowledgebaseSettings;
private readonly IKnowledgebaseService _knowledgebaseService;
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
private readonly ICacheManager _cacheManager;
private readonly IAclService _aclService;
private readonly IStoreMappingService _storeMappingService;
private readonly ILocalizationService _localizationService;
private readonly CaptchaSettings _captchaSettings;
private readonly LocalizationSettings _localizationSettings;
private readonly IWorkflowMessageService _workflowMessageService;
private readonly ICustomerActivityService _customerActivityService;
private readonly IDateTimeHelper _dateTimeHelper;
private readonly CustomerSettings _customerSettings;
private readonly MediaSettings _mediaSettings;
private readonly IPictureService _pictureService;
public KnowledgebaseController(KnowledgebaseSettings knowledgebaseSettings, IKnowledgebaseService knowledgebaseService, IWorkContext workContext,
IStoreContext storeContext, ICacheManager cacheManager, IAclService aclService, IStoreMappingService storeMappingService, ILocalizationService localizationService,
CaptchaSettings captchaSettings, LocalizationSettings localizationSettings, IWorkflowMessageService workflowMessageService,
ICustomerActivityService customerActivityService, IDateTimeHelper dateTimeHelper, CustomerSettings customerSettings,
MediaSettings mediaSettings, IPictureService pictureService)
{
this._knowledgebaseSettings = knowledgebaseSettings;
this._knowledgebaseService = knowledgebaseService;
this._workContext = workContext;
this._storeContext = storeContext;
this._cacheManager = cacheManager;
this._aclService = aclService;
this._storeMappingService = storeMappingService;
this._localizationService = localizationService;
this._captchaSettings = captchaSettings;
this._localizationSettings = localizationSettings;
this._workflowMessageService = workflowMessageService;
this._customerActivityService = customerActivityService;
this._dateTimeHelper = dateTimeHelper;
this._customerSettings = customerSettings;
this._mediaSettings = mediaSettings;
this._pictureService = pictureService;
}
public virtual IActionResult List()
{
if (!_knowledgebaseSettings.Enabled)
return RedirectToRoute("HomePage");
var model = new KnowledgebaseHomePageModel();
return View("List", model);
}
public virtual async Task<IActionResult> ArticlesByCategory(string categoryId)
{
if (!_knowledgebaseSettings.Enabled)
return RedirectToRoute("HomePage");
var category = await _knowledgebaseService.GetPublicKnowledgebaseCategory(categoryId);
if (category == null)
return RedirectToAction("List");
var model = new KnowledgebaseHomePageModel();
var articles = await _knowledgebaseService.GetPublicKnowledgebaseArticlesByCategory(categoryId);
var allCategories = _knowledgebaseService.GetPublicKnowledgebaseCategories();
articles.ForEach(x => model.Items.Add(new KnowledgebaseItemModel
{
Name = x.GetLocalized(y => y.Name, _workContext.WorkingLanguage.Id),
Id = x.Id,
SeName = x.GetLocalized(y => y.SeName, _workContext.WorkingLanguage.Id),
IsArticle = true
}));
model.CurrentCategoryId = categoryId;
model.CurrentCategoryDescription = category.GetLocalized(y => y.Description, _workContext.WorkingLanguage.Id);
model.CurrentCategoryMetaDescription = category.GetLocalized(y => y.MetaDescription, _workContext.WorkingLanguage.Id);
model.CurrentCategoryMetaKeywords = category.GetLocalized(y => y.MetaKeywords, _workContext.WorkingLanguage.Id);
model.CurrentCategoryMetaTitle = category.GetLocalized(y => y.MetaTitle, _workContext.WorkingLanguage.Id);
model.CurrentCategoryName = category.GetLocalized(y => y.Name, _workContext.WorkingLanguage.Id);
model.CurrentCategorySeName = category.GetLocalized(y => y.SeName, _workContext.WorkingLanguage.Id);
string breadcrumbCacheKey = string.Format(ModelCacheEventConsumer.KNOWLEDGEBASE_CATEGORY_BREADCRUMB_KEY, category.Id,
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()), _storeContext.CurrentStore.Id, _workContext.WorkingLanguage.Id);
model.CategoryBreadcrumb = await _cacheManager.GetAsync(breadcrumbCacheKey, async () =>
(await category.GetCategoryBreadCrumb(_knowledgebaseService, _aclService, _storeMappingService))
.Select(catBr => new KnowledgebaseCategoryModel
{
Id = catBr.Id,
Name = catBr.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id),
SeName = catBr.GetSeName(_workContext.WorkingLanguage.Id)
})
.ToList()
);
return View("List", model);
}
public virtual async Task<IActionResult> ItemsByKeyword(string keyword)
{
if (!_knowledgebaseSettings.Enabled)
return RedirectToRoute("HomePage");
var model = new KnowledgebaseHomePageModel();
if (!string.IsNullOrEmpty(keyword))
{
var categories = await _knowledgebaseService.GetPublicKnowledgebaseCategoriesByKeyword(keyword);
var allCategories = await _knowledgebaseService.GetPublicKnowledgebaseCategories();
categories.ForEach(x => model.Items.Add(new KnowledgebaseItemModel
{
Name = x.GetLocalized(y => y.Name, _workContext.WorkingLanguage.Id),
Id = x.Id,
SeName = x.GetLocalized(y => y.SeName, _workContext.WorkingLanguage.Id),
IsArticle = false,
FormattedBreadcrumbs = x.GetFormattedBreadCrumb(allCategories, ">")
}));
var articles = await _knowledgebaseService.GetPublicKnowledgebaseArticlesByKeyword(keyword);
foreach (var item in articles)
{
var kbm = new KnowledgebaseItemModel
{
Name = item.GetLocalized(y => y.Name, _workContext.WorkingLanguage.Id),
Id = item.Id,
SeName = item.GetLocalized(y => y.SeName, _workContext.WorkingLanguage.Id),
IsArticle = true,
FormattedBreadcrumbs = (await _knowledgebaseService.GetPublicKnowledgebaseCategory(item.ParentCategoryId))?.GetFormattedBreadCrumb(allCategories, ">") +
" > " + item.GetLocalized(y => y.Name, _workContext.WorkingLanguage.Id)
};
model.Items.Add(kbm);
}
}
model.CurrentCategoryId = "[NONE]";
model.SearchKeyword = keyword;
return View("List", model);
}
public virtual async Task<IActionResult> KnowledgebaseArticle(string articleId, [FromServices] ICustomerService customerService)
{
if (!_knowledgebaseSettings.Enabled)
return RedirectToRoute("HomePage");
var customer = _workContext.CurrentCustomer;
var article = await _knowledgebaseService.GetKnowledgebaseArticle(articleId);
if (article == null)
return RedirectToAction("List");
//ACL (access control list)
if (!_aclService.Authorize(article, customer))
return InvokeHttp404();
//Store mapping
if (!_storeMappingService.Authorize(article))
return InvokeHttp404();
var model = new KnowledgebaseArticleModel();
await PrepareKnowledgebaseArticleModel(model, article, customerService);
return View("Article", model);
}
private async Task PrepareKnowledgebaseArticleModel(KnowledgebaseArticleModel model, KnowledgebaseArticle article, ICustomerService customerService)
{
model.Content = article.GetLocalized(y => y.Content, _workContext.WorkingLanguage.Id);
model.Name = article.GetLocalized(y => y.Name, _workContext.WorkingLanguage.Id);
model.Id = article.Id;
model.ParentCategoryId = article.ParentCategoryId;
model.SeName = article.GetLocalized(y => y.SeName, _workContext.WorkingLanguage.Id);
model.AllowComments = article.AllowComments;
model.AddNewComment.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnArticleCommentPage;
var articleComments = await _knowledgebaseService.GetArticleCommentsByArticleId(article.Id);
foreach (var ac in articleComments)
{
var customer = await customerService.GetCustomerById(ac.CustomerId);
var commentModel = new KnowledgebaseArticleCommentModel
{
Id = ac.Id,
CustomerId = ac.CustomerId,
CustomerName = customer.FormatUserName(_customerSettings.CustomerNameFormat),
CommentText = ac.CommentText,
CreatedOn = _dateTimeHelper.ConvertToUserTime(ac.CreatedOnUtc, DateTimeKind.Utc),
AllowViewingProfiles = _customerSettings.AllowViewingProfiles && customer != null && !customer.IsGuest(),
};
if (_customerSettings.AllowCustomersToUploadAvatars)
{
commentModel.CustomerAvatarUrl = await _pictureService.GetPictureUrl(
customer.GetAttributeFromEntity<string>(SystemCustomerAttributeNames.AvatarPictureId),
_mediaSettings.AvatarPictureSize,
_customerSettings.DefaultAvatarEnabled,
defaultPictureType: PictureType.Avatar);
}
model.Comments.Add(commentModel);
}
foreach (var id in article.RelatedArticles)
{
var a = await _knowledgebaseService.GetPublicKnowledgebaseArticle(id);
if (a != null)
model.RelatedArticles.Add(new KnowledgebaseArticleModel
{
SeName = a.SeName,
Id = a.Id,
Name = a.Name
});
}
var category = await _knowledgebaseService.GetKnowledgebaseCategory(article.ParentCategoryId);
if (category != null)
{
string breadcrumbCacheKey = string.Format(ModelCacheEventConsumer.KNOWLEDGEBASE_CATEGORY_BREADCRUMB_KEY,
article.ParentCategoryId,
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
_storeContext.CurrentStore.Id,
_workContext.WorkingLanguage.Id);
model.CategoryBreadcrumb = await _cacheManager.GetAsync(breadcrumbCacheKey, async () =>
(await category.GetCategoryBreadCrumb(_knowledgebaseService, _aclService, _storeMappingService))
.Select(catBr => new KnowledgebaseCategoryModel
{
Id = catBr.Id,
Name = catBr.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id),
SeName = catBr.GetSeName(_workContext.WorkingLanguage.Id)
})
.ToList()
);
}
}
[HttpPost, ActionName("KnowledgebaseArticle")]
[PublicAntiForgery]
[FormValueRequired("add-comment")]
[ValidateCaptcha]
public virtual async Task<IActionResult> ArticleCommentAdd(string articleId, KnowledgebaseArticleModel model, bool captchaValid,
[FromServices] IWorkContext workContext, [FromServices] ICustomerService customerService)
{
if (!_knowledgebaseSettings.Enabled)
return RedirectToRoute("HomePage");
var article = await _knowledgebaseService.GetPublicKnowledgebaseArticle(articleId);
if (article == null || !article.AllowComments)
return RedirectToRoute("HomePage");
if (workContext.CurrentCustomer.IsGuest() && !_knowledgebaseSettings.AllowNotRegisteredUsersToLeaveComments)
{
ModelState.AddModelError("", _localizationService.GetResource("Knowledgebase.Article.Comments.OnlyRegisteredUsersLeaveComments"));
}
//validate CAPTCHA
if (_captchaSettings.Enabled && _captchaSettings.ShowOnArticleCommentPage && !captchaValid)
{
ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
}
if (ModelState.IsValid)
{
var customer = _workContext.CurrentCustomer;
var comment = new KnowledgebaseArticleComment
{
ArticleId = article.Id,
CustomerId = customer.Id,
CommentText = model.AddNewComment.CommentText,
CreatedOnUtc = DateTime.UtcNow,
ArticleTitle = article.Name,
};
await _knowledgebaseService.InsertArticleComment(comment);
if (!customer.HasContributions)
{
await customerService.UpdateContributions(customer);
}
//notify a store owner
if (_knowledgebaseSettings.NotifyAboutNewArticleComments)
await _workflowMessageService.SendArticleCommentNotificationMessage(article, comment, _localizationSettings.DefaultAdminLanguageId);
//activity log
await _customerActivityService.InsertActivity("PublicStore.AddArticleComment", comment.Id, _localizationService.GetResource("ActivityLog.PublicStore.AddArticleComment"));
//The text boxes should be cleared after a comment has been posted
//That' why we reload the page
TempData["Grand.knowledgebase.addarticlecomment.result"] = _localizationService.GetResource("Knowledgebase.Article.Comments.SuccessfullyAdded");
return RedirectToRoute("KnowledgebaseArticle", new { SeName = article.GetSeName(_workContext.WorkingLanguage.Id) });
}
//If we got this far, something failed, redisplay form
await PrepareKnowledgebaseArticleModel(model, article, customerService);
return View("Article", model);
}
}
}