Пример #1
0
        public virtual IActionResult ProductReviewsAdd(int productId, ProductReviewsModel model, bool captchaValid)
        {
            var product = _productService.GetProductById(productId);

            if (product == null || product.Deleted || !product.Published || !product.AllowCustomerReviews)
            {
                return(RedirectToRoute("Homepage"));
            }

            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnProductReviewPage && !captchaValid)
            {
                ModelState.AddModelError("", _localizationService.GetResource("Common.WrongCaptchaMessage"));
            }

            if (_customerService.IsGuest(_workContext.CurrentCustomer) && !_catalogSettings.AllowAnonymousUsersToReviewProduct)
            {
                ModelState.AddModelError("", _localizationService.GetResource("Reviews.OnlyRegisteredUsersCanWriteReviews"));
            }

            if (_catalogSettings.ProductReviewPossibleOnlyAfterPurchasing)
            {
                var hasCompletedOrders = _orderService.SearchOrders(customerId: _workContext.CurrentCustomer.Id,
                                                                    productId: productId,
                                                                    osIds: new List <int> {
                    (int)OrderStatus.Complete
                },
                                                                    pageSize: 1).Any();
                if (!hasCompletedOrders)
                {
                    ModelState.AddModelError(string.Empty, _localizationService.GetResource("Reviews.ProductReviewPossibleOnlyAfterPurchasing"));
                }
            }

            if (ModelState.IsValid)
            {
                //save review
                var rating = model.AddProductReview.Rating;
                if (rating < 1 || rating > 5)
                {
                    rating = _catalogSettings.DefaultProductRatingValue;
                }
                var isApproved = !_catalogSettings.ProductReviewsMustBeApproved;

                var productReview = new ProductReview
                {
                    ProductId       = product.Id,
                    CustomerId      = _workContext.CurrentCustomer.Id,
                    Title           = model.AddProductReview.Title,
                    ReviewText      = model.AddProductReview.ReviewText,
                    Rating          = rating,
                    HelpfulYesTotal = 0,
                    HelpfulNoTotal  = 0,
                    IsApproved      = isApproved,
                    CreatedOnUtc    = DateTime.UtcNow,
                    StoreId         = _storeContext.CurrentStore.Id,
                };

                _productService.InsertProductReview(productReview);

                //add product review and review type mapping
                foreach (var additionalReview in model.AddAdditionalProductReviewList)
                {
                    var additionalProductReview = new ProductReviewReviewTypeMapping
                    {
                        ProductReviewId = productReview.Id,
                        ReviewTypeId    = additionalReview.ReviewTypeId,
                        Rating          = additionalReview.Rating
                    };

                    _reviewTypeService.InsertProductReviewReviewTypeMappings(additionalProductReview);
                }

                //update product totals
                _productService.UpdateProductReviewTotals(product);

                //notify store owner
                if (_catalogSettings.NotifyStoreOwnerAboutNewProductReviews)
                {
                    _workflowMessageService.SendProductReviewNotificationMessage(productReview, _localizationSettings.DefaultAdminLanguageId);
                }

                //activity log
                _customerActivityService.InsertActivity("PublicStore.AddProductReview",
                                                        string.Format(_localizationService.GetResource("ActivityLog.PublicStore.AddProductReview"), product.Name), product);

                //raise event
                if (productReview.IsApproved)
                {
                    _eventPublisher.Publish(new ProductReviewApprovedEvent(productReview));
                }

                model = _productModelFactory.PrepareProductReviewsModel(model, product);
                model.AddProductReview.Title      = null;
                model.AddProductReview.ReviewText = null;

                model.AddProductReview.SuccessfullyAdded = true;
                if (!isApproved)
                {
                    model.AddProductReview.Result = _localizationService.GetResource("Reviews.SeeAfterApproving");
                }
                else
                {
                    model.AddProductReview.Result = _localizationService.GetResource("Reviews.SuccessfullyAdded");
                }

                return(View(model));
            }

            //If we got this far, something failed, redisplay form
            model = _productModelFactory.PrepareProductReviewsModel(model, product);
            return(View(model));
        }
Пример #2
0
        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));
        }