PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Presentation/Nop.Web/Administration/Controllers/NewsController.cs

#
C# | 264 lines | 218 code | 41 blank | 5 comment | 19 complexity | 64a13298613d09313f7752ddcbad592e MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Nop.Admin.Models.News;
  7. using Nop.Core.Domain.News;
  8. using Nop.Services.Customers;
  9. using Nop.Services.Helpers;
  10. using Nop.Services.Localization;
  11. using Nop.Services.News;
  12. using Nop.Web.Framework;
  13. using Nop.Web.Framework.Controllers;
  14. using Telerik.Web.Mvc;
  15. using Nop.Services.Security;
  16. using Nop.Core.Domain.Common;
  17. namespace Nop.Admin.Controllers
  18. {
  19. [AdminAuthorize]
  20. public class NewsController : BaseNopController
  21. {
  22. #region Fields
  23. private readonly INewsService _newsService;
  24. private readonly ILanguageService _languageService;
  25. private readonly IDateTimeHelper _dateTimeHelper;
  26. private readonly ICustomerContentService _customerContentService;
  27. private readonly ILocalizationService _localizationService;
  28. private readonly IPermissionService _permissionService;
  29. private readonly AdminAreaSettings _adminAreaSettings;
  30. #endregion
  31. #region Constructors
  32. public NewsController(INewsService newsService, ILanguageService languageService,
  33. IDateTimeHelper dateTimeHelper, ICustomerContentService customerContentService,
  34. ILocalizationService localizationService, IPermissionService permissionService,
  35. AdminAreaSettings adminAreaSettings)
  36. {
  37. this._newsService = newsService;
  38. this._languageService = languageService;
  39. this._dateTimeHelper = dateTimeHelper;
  40. this._customerContentService = customerContentService;
  41. this._localizationService = localizationService;
  42. this._permissionService = permissionService;
  43. this._adminAreaSettings = adminAreaSettings;
  44. }
  45. #endregion 
  46. #region News items
  47. public ActionResult Index()
  48. {
  49. return RedirectToAction("List");
  50. }
  51. public ActionResult List()
  52. {
  53. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  54. return AccessDeniedView();
  55. var news = _newsService.GetAllNews(0, null, null, 0, _adminAreaSettings.GridPageSize, true);
  56. var gridModel = new GridModel<NewsItemModel>
  57. {
  58. Data = news.Select(x =>
  59. {
  60. var m = x.ToModel();
  61. m.CreatedOn = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
  62. m.LanguageName = x.Language.Name;
  63. m.Comments = x.NewsComments.Count;
  64. return m;
  65. }),
  66. Total = news.TotalCount
  67. };
  68. return View(gridModel);
  69. }
  70. [HttpPost, GridAction(EnableCustomBinding = true)]
  71. public ActionResult List(GridCommand command)
  72. {
  73. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  74. return AccessDeniedView();
  75. var news = _newsService.GetAllNews(0, null, null, command.Page - 1, command.PageSize, true);
  76. var gridModel = new GridModel<NewsItemModel>
  77. {
  78. Data = news.Select(x =>
  79. {
  80. var m = x.ToModel();
  81. m.CreatedOn = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
  82. m.LanguageName = x.Language.Name;
  83. m.Comments = x.NewsComments.Count;
  84. return m;
  85. }),
  86. Total = news.TotalCount
  87. };
  88. return new JsonResult
  89. {
  90. Data = gridModel
  91. };
  92. }
  93. public ActionResult Create()
  94. {
  95. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  96. return AccessDeniedView();
  97. ViewBag.AllLanguages = _languageService.GetAllLanguages(true);
  98. var model = new NewsItemModel();
  99. //default values
  100. model.Published = true;
  101. model.AllowComments = true;
  102. return View(model);
  103. }
  104. [HttpPost, FormValueExists("save", "save-continue", "continueEditing")]
  105. public ActionResult Create(NewsItemModel model, bool continueEditing)
  106. {
  107. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  108. return AccessDeniedView();
  109. if (ModelState.IsValid)
  110. {
  111. var newsItem = model.ToEntity();
  112. newsItem.CreatedOnUtc = DateTime.UtcNow;
  113. _newsService.InsertNews(newsItem);
  114. SuccessNotification(_localizationService.GetResource("Admin.ContentManagement.News.NewsItems.Added"));
  115. return continueEditing ? RedirectToAction("Edit", new { id = newsItem.Id }) : RedirectToAction("List");
  116. }
  117. //If we got this far, something failed, redisplay form
  118. ViewBag.AllLanguages = _languageService.GetAllLanguages(true);
  119. return View(model);
  120. }
  121. public ActionResult Edit(int id)
  122. {
  123. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  124. return AccessDeniedView();
  125. var newsItem = _newsService.GetNewsById(id);
  126. if (newsItem == null)
  127. throw new ArgumentException("No news item found with the specified id", "id");
  128. ViewBag.AllLanguages = _languageService.GetAllLanguages(true);
  129. var model = newsItem.ToModel();
  130. return View(model);
  131. }
  132. [HttpPost, FormValueExists("save", "save-continue", "continueEditing")]
  133. public ActionResult Edit(NewsItemModel model, bool continueEditing)
  134. {
  135. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  136. return AccessDeniedView();
  137. var newsItem = _newsService.GetNewsById(model.Id);
  138. if (newsItem == null)
  139. throw new ArgumentException("No news item found with the specified id");
  140. if (ModelState.IsValid)
  141. {
  142. newsItem = model.ToEntity(newsItem);
  143. _newsService.UpdateNews(newsItem);
  144. SuccessNotification(_localizationService.GetResource("Admin.ContentManagement.News.NewsItems.Updated"));
  145. return continueEditing ? RedirectToAction("Edit", new { id = newsItem.Id }) : RedirectToAction("List");
  146. }
  147. //If we got this far, something failed, redisplay form
  148. ViewBag.AllLanguages = _languageService.GetAllLanguages(true);
  149. return View(model);
  150. }
  151. [HttpPost, ActionName("Delete")]
  152. public ActionResult DeleteConfirmed(int id)
  153. {
  154. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  155. return AccessDeniedView();
  156. var newsItem = _newsService.GetNewsById(id);
  157. if (newsItem == null)
  158. throw new ArgumentException("No news item found with the specified id", "id");
  159. _newsService.DeleteNews(newsItem);
  160. SuccessNotification(_localizationService.GetResource("Admin.ContentManagement.News.NewsItems.Deleted"));
  161. return RedirectToAction("List");
  162. }
  163. #endregion
  164. #region Comments
  165. public ActionResult Comments(int? filterByNewsItemId)
  166. {
  167. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  168. return AccessDeniedView();
  169. ViewBag.FilterByNewsItemId = filterByNewsItemId;
  170. var model = new GridModel<NewsCommentModel>();
  171. return View(model);
  172. }
  173. [HttpPost, GridAction(EnableCustomBinding = true)]
  174. public ActionResult Comments(int? filterByNewsItemId, GridCommand command)
  175. {
  176. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  177. return AccessDeniedView();
  178. IList<NewsComment> comments;
  179. if (filterByNewsItemId.HasValue)
  180. {
  181. //filter comments by news item
  182. var newsItem = _newsService.GetNewsById(filterByNewsItemId.Value);
  183. comments = newsItem.NewsComments.OrderBy(bc => bc.CreatedOnUtc).ToList();
  184. }
  185. else
  186. {
  187. //load all news comments
  188. comments = _customerContentService.GetAllCustomerContent<NewsComment>(0, null);
  189. }
  190. var gridModel = new GridModel<NewsCommentModel>
  191. {
  192. Data = comments.PagedForCommand(command).Select(newsComment =>
  193. {
  194. var commentModel = new NewsCommentModel();
  195. commentModel.Id = newsComment.Id;
  196. commentModel.NewsItemId = newsComment.NewsItemId;
  197. commentModel.NewsItemTitle = newsComment.NewsItem.Title;
  198. commentModel.CustomerId = newsComment.CustomerId;
  199. commentModel.IpAddress = newsComment.IpAddress;
  200. commentModel.CreatedOn = _dateTimeHelper.ConvertToUserTime(newsComment.CreatedOnUtc, DateTimeKind.Utc);
  201. commentModel.CommentTitle = newsComment.CommentTitle;
  202. commentModel.CommentText = Core.Html.HtmlHelper.FormatText(newsComment.CommentText, false, true, false, false, false, false);
  203. return commentModel;
  204. }),
  205. Total = comments.Count,
  206. };
  207. return new JsonResult
  208. {
  209. Data = gridModel
  210. };
  211. }
  212. [GridAction(EnableCustomBinding = true)]
  213. public ActionResult CommentDelete(int? filterByNewsItemId, int id, GridCommand command)
  214. {
  215. if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
  216. return AccessDeniedView();
  217. var comment = _customerContentService.GetCustomerContentById(id);
  218. _customerContentService.DeleteCustomerContent(comment);
  219. return Comments(filterByNewsItemId, command);
  220. }
  221. #endregion
  222. }
  223. }