public virtual IActionResult Edit(int id) { if (!_permissionService.Authorize(StandardPermissionProvider.ManageSettings)) { return(AccessDeniedView()); } //try to get an product review type with the specified id var reviewType = _reviewTypeService.GetReviewTypeById(id); if (reviewType == null) { return(RedirectToAction("List")); } //prepare model var model = _reviewTypeModelFactory.PrepareReviewTypeModel(null, reviewType); return(View(model)); }
/// <summary> /// Prepare paged product reviews mapping list model /// </summary> /// <param name="searchModel">Product review and review type mapping search model</param> /// <param name="productReview">Product review</param> /// <returns>Product review and review type mapping list model</returns> public virtual ProductReviewReviewTypeMappingListModel PrepareProductReviewReviewTypeMappingListModel(ProductReviewReviewTypeMappingSearchModel searchModel, ProductReview productReview) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } if (productReview == null) { throw new ArgumentNullException(nameof(productReview)); } //get product review and review type mappings var productReviewReviewTypeMappings = _reviewTypeService.GetProductReviewReviewTypeMappingsByProductReviewId(productReview.Id); //prepare grid model var model = new ProductReviewReviewTypeMappingListModel { Data = productReviewReviewTypeMappings.PaginationByRequestModel(searchModel).Select(productReviewReviewTypeMapping => { //fill in model values from the entity var productReviewReviewTypeMappingModel = new ProductReviewReviewTypeMappingModel { Id = productReviewReviewTypeMapping.Id, ProductReviewId = productReviewReviewTypeMapping.ProductReviewId, Rating = productReviewReviewTypeMapping.Rating }; //fill in additional values (not existing in the entity) var reviewType = _reviewTypeService.GetReviewTypeById(productReviewReviewTypeMapping.ReviewTypeId); productReviewReviewTypeMappingModel.Name = reviewType.GetLocalized(entity => entity.Name); productReviewReviewTypeMappingModel.Description = reviewType.GetLocalized(entity => entity.Description); productReviewReviewTypeMappingModel.VisibleToAllCustomers = reviewType.VisibleToAllCustomers; return(productReviewReviewTypeMappingModel); }), Total = productReviewReviewTypeMappings.Count }; return(model); }
/// <summary> /// Prepare paged product reviews mapping list model /// </summary> /// <param name="searchModel">Product review and review type mapping search model</param> /// <param name="productReview">Product review</param> /// <returns>Product review and review type mapping list model</returns> public virtual ProductReviewReviewTypeMappingListModel PrepareProductReviewReviewTypeMappingListModel(ProductReviewReviewTypeMappingSearchModel searchModel, ProductReview productReview) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } if (productReview == null) { throw new ArgumentNullException(nameof(productReview)); } //get product review and review type mappings var productReviewReviewTypeMappings = _reviewTypeService .GetProductReviewReviewTypeMappingsByProductReviewId(productReview.Id).ToPagedList(searchModel); //prepare grid model var model = new ProductReviewReviewTypeMappingListModel().PrepareToGrid(searchModel, productReviewReviewTypeMappings, () => { return(productReviewReviewTypeMappings.Select(productReviewReviewTypeMapping => { //fill in model values from the entity var productReviewReviewTypeMappingModel = productReviewReviewTypeMapping .ToModel <ProductReviewReviewTypeMappingModel>(); //fill in additional values (not existing in the entity) var reviewType = _reviewTypeService.GetReviewTypeById(productReviewReviewTypeMapping.ReviewTypeId); productReviewReviewTypeMappingModel.Name = _localizationService.GetLocalized(reviewType, entity => entity.Name); productReviewReviewTypeMappingModel.Description = _localizationService.GetLocalized(reviewType, entity => entity.Description); productReviewReviewTypeMappingModel.VisibleToAllCustomers = reviewType.VisibleToAllCustomers; return productReviewReviewTypeMappingModel; })); }); return(model); }
public IActionResult CreateProductReview([ModelBinder(typeof(JsonModelBinder <ProductReviewDto>))] Delta <ProductReviewDto> productReviewDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } // We doesn't have to check for value because this is done by the review validator. var customer = CustomerService.GetCustomerById(productReviewDelta.Dto.CustomerId); if (customer == null) { return(Error(HttpStatusCode.NotFound, "customer", "not found")); } var product = _productService.GetProductById(productReviewDelta.Dto.ProductId); if (product == null || product.Deleted || !product.Published || !product.AllowCustomerReviews) { return(Error(HttpStatusCode.NotFound, "product", "not found")); } if (_customerService.IsGuest(customer) && !_catalogSettings.AllowAnonymousUsersToReviewProduct) { return(Error(HttpStatusCode.BadRequest, "customer", _localizationService.GetResource("Reviews.OnlyRegisteredUsersCanWriteReviews"))); } if (_catalogSettings.ProductReviewPossibleOnlyAfterPurchasing) { var hasCompletedOrders = _orderService.SearchOrders(customerId: customer.Id, productId: product.Id, osIds: new List <int> { (int)OrderStatus.Complete }, pageSize: 1).Any(); if (!hasCompletedOrders) { return(Error(HttpStatusCode.BadRequest, "error", _localizationService.GetResource("Reviews.ProductReviewPossibleOnlyAfterPurchasing"))); } } var store = _storeService.GetStoreById(productReviewDelta.Dto.StoreId); //save review var newReview = new ProductReview() { ProductId = product.Id, CustomerId = customer.Id, StoreId = store == null ? _storeContext.CurrentStore.Id : store.Id, Title = productReviewDelta.Dto.Title, ReviewText = productReviewDelta.Dto.ReviewText, IsApproved = !_catalogSettings.ProductReviewsMustBeApproved, CreatedOnUtc = DateTime.UtcNow, HelpfulNoTotal = 0, HelpfulYesTotal = 0, CustomerNotifiedOfReply = _catalogSettings.NotifyCustomerAboutProductReviewReply }; // must be rating betweent 1 - 5 or set default rating var rating = productReviewDelta.Dto.Rating; newReview.Rating = rating < 1 || rating > 5 ? _catalogSettings.DefaultProductRatingValue : rating; _productService.InsertProductReview(newReview); //add product review and review type mapping foreach (var additionalReview in productReviewDelta.Dto.ReviewTypeMappingsDto) { // must be rating betweent 1 - 5 or set default rating var reviewTypeMappingRating = additionalReview.Rating; reviewTypeMappingRating = reviewTypeMappingRating < 1 || reviewTypeMappingRating > 5 ? _catalogSettings.DefaultProductRatingValue : reviewTypeMappingRating; var reviewType = _reviewTypeService.GetReviewTypeById(additionalReview.ReviewTypeId); if (reviewType == null) { // remove new Review after insert _productService.DeleteProductReview(newReview); return(Error(HttpStatusCode.NotFound, "review_type", "not found id = " + additionalReview.ReviewTypeId)); } var additionalProductReview = new ProductReviewReviewTypeMapping { ProductReviewId = newReview.Id, ReviewTypeId = reviewType.Id, Rating = reviewTypeMappingRating }; _reviewTypeService.InsertProductReviewReviewTypeMappings(additionalProductReview); } //update product totals _productService.UpdateProductReviewTotals(product); //notify store owner if (_catalogSettings.NotifyStoreOwnerAboutNewProductReviews) { _workflowMessageService.SendProductReviewNotificationMessage(newReview, _localizationSettings.DefaultAdminLanguageId); } //activity log _customerActivityService.InsertActivity(customer, "PublicStore.AddProductReview", string.Format(_localizationService.GetResource("ActivityLog.PublicStore.AddProductReview"), product.Name), product); //raise event if (newReview.IsApproved) { _eventPublisher.Publish(new ProductReviewApprovedEvent(newReview)); } if (!newReview.IsApproved) { return(Ok(_localizationService.GetResource("Reviews.SeeAfterApproving"))); } var productReviewsRootObject = new ProductReviewsRootObject(); productReviewsRootObject.ProductReviews.Add(_dtoHelper.PrepareProductReviewDTO(newReview)); var json = JsonFieldsSerializer.Serialize(productReviewsRootObject, string.Empty); return(new RawJsonActionResult(json)); }
/// <summary> /// Prepare the product reviews model /// </summary> /// <param name="model">Product reviews model</param> /// <param name="product">Product</param> /// <returns>Product reviews model</returns> public virtual ProductReviewsModel PrepareProductReviewsModel(ProductReviewsModel model, Product product) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (product == null) { throw new ArgumentNullException(nameof(product)); } model.ProductId = product.Id; model.ProductName = _localizationService.GetLocalized(product, x => x.Name); model.ProductSeName = _urlRecordService.GetSeName(product); var productReviews = _catalogSettings.ShowProductReviewsPerStore ? product.ProductReviews.Where(pr => pr.IsApproved && pr.StoreId == _storeContext.CurrentStore.Id) : product.ProductReviews.Where(pr => pr.IsApproved); productReviews = _catalogSettings.ProductReviewsSortByCreatedDateAscending ? productReviews.OrderBy(pr => pr.CreatedOnUtc) : productReviews.OrderByDescending(pr => pr.CreatedOnUtc); //get all review types foreach (var reviewType in _reviewTypeService.GetAllReviewTypes()) { model.ReviewTypeList.Add(new ReviewTypeModel { Id = reviewType.Id, Name = _localizationService.GetLocalized(reviewType, entity => entity.Name), Description = _localizationService.GetLocalized(reviewType, entity => entity.Description), VisibleToAllCustomers = reviewType.VisibleToAllCustomers, DisplayOrder = reviewType.DisplayOrder, IsRequired = reviewType.IsRequired, }); } //filling data from db foreach (var pr in productReviews) { var customer = pr.Customer; var productReviewModel = new ProductReviewModel { Id = pr.Id, CustomerId = pr.CustomerId, CustomerName = _customerService.FormatUserName(customer), AllowViewingProfiles = _customerSettings.AllowViewingProfiles && customer != null && !customer.IsGuest(), Title = pr.Title, ReviewText = pr.ReviewText, ReplyText = pr.ReplyText, Rating = pr.Rating, Helpfulness = new ProductReviewHelpfulnessModel { ProductReviewId = pr.Id, HelpfulYesTotal = pr.HelpfulYesTotal, HelpfulNoTotal = pr.HelpfulNoTotal, }, WrittenOnStr = _dateTimeHelper.ConvertToUserTime(pr.CreatedOnUtc, DateTimeKind.Utc).ToString("g"), }; foreach (var q in _reviewTypeService.GetProductReviewReviewTypeMappingsByProductReviewId(pr.Id)) { productReviewModel.AdditionalProductReviewList.Add(new ProductReviewReviewTypeMappingModel { ReviewTypeId = q.ReviewTypeId, ProductReviewId = pr.Id, Rating = q.Rating, Name = _localizationService.GetLocalized(q.ReviewType, x => x.Name), VisibleToAllCustomers = q.ReviewType.VisibleToAllCustomers || _workContext.CurrentCustomer.Id == pr.CustomerId, }); } model.Items.Add(productReviewModel); } foreach (var rt in model.ReviewTypeList) { if (model.ReviewTypeList.Count <= model.AddAdditionalProductReviewList.Count) { continue; } var reviewType = _reviewTypeService.GetReviewTypeById(rt.Id); var reviewTypeMappingModel = new AddProductReviewReviewTypeMappingModel { ReviewTypeId = rt.Id, Name = _localizationService.GetLocalized(reviewType, entity => entity.Name), Description = _localizationService.GetLocalized(reviewType, entity => entity.Description), DisplayOrder = rt.DisplayOrder, IsRequired = rt.IsRequired, }; model.AddAdditionalProductReviewList.Add(reviewTypeMappingModel); } //Average rating foreach (var rtm in model.ReviewTypeList) { var totalRating = 0; var totalCount = 0; foreach (var item in model.Items) { foreach (var q in item.AdditionalProductReviewList.Where(w => w.ReviewTypeId == rtm.Id)) { totalRating += q.Rating; totalCount = ++totalCount; } } rtm.AverageRating = (double)totalRating / (totalCount > 0 ? totalCount : 1); } model.AddProductReview.CanCurrentCustomerLeaveReview = _catalogSettings.AllowAnonymousUsersToReviewProduct || !_workContext.CurrentCustomer.IsGuest(); model.AddProductReview.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnProductReviewPage; return(model); }