public void Can_clone() { //create var model1 = new HomePageNewsItemsModel() { WorkingLanguageId = 1, }; var newsItemModel1 = new NewsItemModel() { Id = 1, SeName = "SeName 1", Title = "Title 1", Short = "Short 1", Full = "Full 1", AllowComments = true, NumberOfComments = 2, CreatedOn = new DateTime(2010, 01, 01), AddNewComment = new AddNewsCommentModel() { CommentTitle = "CommentTitle 1", CommentText = "CommentText 1", DisplayCaptcha = true } }; newsItemModel1.Comments.Add(new NewsCommentModel() { Id = 3, CustomerId = 4, CustomerName = "CustomerName 1", CustomerAvatarUrl = "CustomerAvatarUrl 1", CommentTitle = "CommentTitle 1", CommentText = "CommentText 1", CreatedOn = new DateTime(2010, 01, 02), AllowViewingProfiles = true }); model1.NewsItems.Add(newsItemModel1); //clone var model2 = (HomePageNewsItemsModel)model1.Clone(); model2.WorkingLanguageId.ShouldEqual(1); model2.NewsItems.ShouldNotBeNull(); model2.NewsItems.Count.ShouldEqual(1); var newsItemModel2 = model2.NewsItems[0]; newsItemModel2.Id.ShouldEqual(1); newsItemModel2.SeName.ShouldEqual("SeName 1"); newsItemModel2.Title.ShouldEqual("Title 1"); newsItemModel2.Short.ShouldEqual("Short 1"); newsItemModel2.Full.ShouldEqual("Full 1"); newsItemModel2.AllowComments.ShouldEqual(true); newsItemModel2.NumberOfComments.ShouldEqual(2); newsItemModel2.CreatedOn.ShouldEqual(new DateTime(2010, 01, 01)); newsItemModel2.Comments.ShouldNotBeNull(); newsItemModel2.Comments.Count.ShouldEqual(1); newsItemModel2.Comments[0].Id.ShouldEqual(3); newsItemModel2.Comments[0].CustomerId.ShouldEqual(4); newsItemModel2.Comments[0].CustomerName.ShouldEqual("CustomerName 1"); newsItemModel2.Comments[0].CustomerAvatarUrl.ShouldEqual("CustomerAvatarUrl 1"); newsItemModel2.Comments[0].CommentTitle.ShouldEqual("CommentTitle 1"); newsItemModel2.Comments[0].CommentText.ShouldEqual("CommentText 1"); newsItemModel2.Comments[0].CreatedOn.ShouldEqual(new DateTime(2010, 01, 02)); newsItemModel2.Comments[0].AllowViewingProfiles.ShouldEqual(true); newsItemModel2.AddNewComment.ShouldNotBeNull(); newsItemModel2.AddNewComment.CommentTitle.ShouldEqual("CommentTitle 1"); newsItemModel2.AddNewComment.CommentText.ShouldEqual("CommentText 1"); newsItemModel2.AddNewComment.DisplayCaptcha.ShouldEqual(true); }
public ActionResult HomePageNews() { if (!_newsSettings.Enabled || !_newsSettings.ShowNewsOnMainPage) return Content(""); var cacheKey = string.Format(ModelCacheEventConsumer.HOMEPAGE_NEWSMODEL_KEY, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); var cachedModel = _cacheManager.Get(cacheKey, () => { var newsItems = _newsService.GetAllNews(_workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id, 0, _newsSettings.MainPageNewsCount); return new HomePageNewsItemsModel() { WorkingLanguageId = _workContext.WorkingLanguage.Id, NewsItems = newsItems .Select(x => { var newsModel = new NewsItemModel(); PrepareNewsItemModel(newsModel, x, false); return newsModel; }) .ToList() }; }); //"Comments" property of "NewsItemModel" object depends on the current customer. //Furthermore, we just don't need it for home page news. So let's reset it. //But first we need to clone the cached model (the updated one should not be cached) var model = (HomePageNewsItemsModel)cachedModel.Clone(); foreach (var newsItemModel in model.NewsItems) newsItemModel.Comments.Clear(); return PartialView(model); }
protected void PrepareNewsItemModel(NewsItemModel model, NewsItem newsItem, bool prepareComments) { if (newsItem == null) throw new ArgumentNullException("newsItem"); if (model == null) throw new ArgumentNullException("model"); model.Id = newsItem.Id; model.MetaTitle = newsItem.MetaTitle; model.MetaDescription = newsItem.MetaDescription; model.MetaKeywords = newsItem.MetaKeywords; model.SeName = newsItem.GetSeName(newsItem.LanguageId, ensureTwoPublishedLanguages: false); model.Title = newsItem.Title; model.Short = newsItem.Short; model.Full = newsItem.Full; model.AllowComments = newsItem.AllowComments; model.CreatedOn = _dateTimeHelper.ConvertToUserTime(newsItem.CreatedOnUtc, DateTimeKind.Utc); model.NumberOfComments = newsItem.CommentCount; model.AddNewComment.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnNewsCommentPage; if (prepareComments) { var newsComments = newsItem.NewsComments.OrderBy(pr => pr.CreatedOnUtc); foreach (var nc in newsComments) { var commentModel = new NewsCommentModel() { Id = nc.Id, CustomerId = nc.CustomerId, CustomerName = nc.Customer.FormatUserName(), CommentTitle = nc.CommentTitle, CommentText = nc.CommentText, CreatedOn = _dateTimeHelper.ConvertToUserTime(nc.CreatedOnUtc, DateTimeKind.Utc), AllowViewingProfiles = _customerSettings.AllowViewingProfiles && nc.Customer != null && !nc.Customer.IsGuest(), }; if (_customerSettings.AllowCustomersToUploadAvatars) { commentModel.CustomerAvatarUrl = _pictureService.GetPictureUrl( nc.Customer.GetAttribute<int>(SystemCustomerAttributeNames.AvatarPictureId), _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType:PictureType.Avatar); } model.Comments.Add(commentModel); } } }
public ActionResult NewsItem(int newsItemId) { if (!_newsSettings.Enabled) return RedirectToRoute("HomePage"); var newsItem = _newsService.GetNewsById(newsItemId); if (newsItem == null || !newsItem.Published || (newsItem.StartDateUtc.HasValue && newsItem.StartDateUtc.Value >= DateTime.UtcNow) || (newsItem.EndDateUtc.HasValue && newsItem.EndDateUtc.Value <= DateTime.UtcNow) || //Store mapping !_storeMappingService.Authorize(newsItem)) return RedirectToRoute("HomePage"); var model = new NewsItemModel(); PrepareNewsItemModel(model, newsItem, true); return View(model); }
public ActionResult NewsCommentAdd(int newsItemId, NewsItemModel model, bool captchaValid) { if (!_newsSettings.Enabled) return RedirectToRoute("HomePage"); var newsItem = _newsService.GetNewsById(newsItemId); if (newsItem == null || !newsItem.Published || !newsItem.AllowComments) return RedirectToRoute("HomePage"); //validate CAPTCHA if (_captchaSettings.Enabled && _captchaSettings.ShowOnNewsCommentPage && !captchaValid) { ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptcha")); } if (_workContext.CurrentCustomer.IsGuest() && !_newsSettings.AllowNotRegisteredUsersToLeaveComments) { ModelState.AddModelError("", _localizationService.GetResource("News.Comments.OnlyRegisteredUsersLeaveComments")); } if (ModelState.IsValid) { var comment = new NewsComment() { NewsItemId = newsItem.Id, CustomerId = _workContext.CurrentCustomer.Id, CommentTitle = model.AddNewComment.CommentTitle, CommentText = model.AddNewComment.CommentText, CreatedOnUtc = DateTime.UtcNow, }; newsItem.NewsComments.Add(comment); //update totals newsItem.CommentCount = newsItem.NewsComments.Count; _newsService.UpdateNews(newsItem); //notify a store owner; if (_newsSettings.NotifyAboutNewNewsComments) _workflowMessageService.SendNewsCommentNotificationMessage(comment, _localizationSettings.DefaultAdminLanguageId); //activity log _customerActivityService.InsertActivity("PublicStore.AddNewsComment", _localizationService.GetResource("ActivityLog.PublicStore.AddNewsComment")); //The text boxes should be cleared after a comment has been posted //That' why we reload the page TempData["Nas.news.addcomment.result"] = _localizationService.GetResource("News.Comments.SuccessfullyAdded"); return RedirectToRoute("NewsItem", new {SeName = newsItem.GetSeName(newsItem.LanguageId, ensureTwoPublishedLanguages: false) }); } //If we got this far, something failed, redisplay form PrepareNewsItemModel(model, newsItem, true); return View(model); }
public ActionResult List(NewsPagingFilteringModel command) { if (!_newsSettings.Enabled) return RedirectToRoute("HomePage"); var model = new NewsItemListModel(); model.WorkingLanguageId = _workContext.WorkingLanguage.Id; if (command.PageSize <= 0) command.PageSize = _newsSettings.NewsArchivePageSize; if (command.PageNumber <= 0) command.PageNumber = 1; var newsItems = _newsService.GetAllNews(_workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id, command.PageNumber - 1, command.PageSize); model.PagingFilteringContext.LoadPagedList(newsItems); model.NewsItems = newsItems .Select(x => { var newsModel = new NewsItemModel(); PrepareNewsItemModel(newsModel, x, false); return newsModel; }) .ToList(); return View(model); }