Ejemplo n.º 1
0
        public ActionResult ReviewsAdd(int id, ProductReviewsModel model, bool captchaValid)
        {
            var product = _productService.GetProductById(id);

            if (product == null || product.Deleted || product.IsSystemProduct || !product.Published || !product.AllowCustomerReviews)
            {
                return(HttpNotFound());
            }

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

            if (_services.WorkContext.CurrentCustomer.IsGuest() && !_catalogSettings.AllowAnonymousUsersToReviewProduct)
            {
                ModelState.AddModelError("", T("Reviews.OnlyRegisteredUsersCanWriteReviews"));
            }

            if (ModelState.IsValid)
            {
                //save review
                int rating = model.Rating;
                if (rating < 1 || rating > 5)
                {
                    rating = _catalogSettings.DefaultProductRatingValue;
                }

                bool isApproved = !_catalogSettings.ProductReviewsMustBeApproved;
                var  customer   = _services.WorkContext.CurrentCustomer;

                var productReview = new ProductReview
                {
                    ProductId       = product.Id,
                    CustomerId      = customer.Id,
                    IpAddress       = _services.WebHelper.GetCurrentIpAddress(),
                    Title           = model.Title,
                    ReviewText      = model.ReviewText,
                    Rating          = rating,
                    HelpfulYesTotal = 0,
                    HelpfulNoTotal  = 0,
                    IsApproved      = isApproved,
                };
                _customerContentService.InsertCustomerContent(productReview);

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

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

                // activity log
                _services.CustomerActivity.InsertActivity("PublicStore.AddProductReview", T("ActivityLog.PublicStore.AddProductReview"), product.Name);

                if (isApproved)
                {
                    _customerService.RewardPointsForProductReview(customer, product, true);
                }

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

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

                return(View(model));
            }

            // If we got this far, something failed, redisplay form
            _helper.PrepareProductReviewsModel(model, product);
            return(View(model));
        }
        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("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
            }

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

            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,
                };

                product.ProductReviews.Add(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
                    };
                    productReview.ProductReviewReviewTypeMappingEntries.Add(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));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> ReviewsAdd(int id, ProductReviewsModel model, string captchaError)
        {
            // INFO: Entitity is being loaded tracked because else navigation properties can't be loaded in PrepareProductReviewsModelAsync.
            var product = await _db.Products
                          .IncludeReviews()
                          .FindByIdAsync(id);

            if (product == null || product.IsSystemProduct || !product.Published || !product.AllowCustomerReviews)
            {
                return(NotFound());
            }

            if (_captchaSettings.ShowOnProductReviewPage && captchaError.HasValue())
            {
                ModelState.AddModelError("", captchaError);
            }

            var customer = Services.WorkContext.CurrentCustomer;

            if (customer.IsGuest() && !_catalogSettings.AllowAnonymousUsersToReviewProduct)
            {
                ModelState.AddModelError("", T("Reviews.OnlyRegisteredUsersCanWriteReviews"));
            }

            if (ModelState.IsValid)
            {
                var rating = model.Rating;
                if (rating < 1 || rating > 5)
                {
                    rating = _catalogSettings.DefaultProductRatingValue;
                }

                var isApproved    = !_catalogSettings.ProductReviewsMustBeApproved;
                var productReview = new ProductReview
                {
                    ProductId       = product.Id,
                    CustomerId      = customer.Id,
                    IpAddress       = _webHelper.GetClientIpAddress().ToString(),
                    Title           = model.Title,
                    ReviewText      = model.ReviewText,
                    Rating          = rating,
                    HelpfulYesTotal = 0,
                    HelpfulNoTotal  = 0,
                    IsApproved      = isApproved,
                };

                product.ProductReviews.Add(productReview);
                _productService.ApplyProductReviewTotals(product);

                if (_catalogSettings.NotifyStoreOwnerAboutNewProductReviews)
                {
                    await _messageFactory.Value.SendProductReviewNotificationMessageAsync(productReview, _localizationSettings.DefaultAdminLanguageId);
                }

                Services.ActivityLogger.LogActivity("PublicStore.AddProductReview", T("ActivityLog.PublicStore.AddProductReview"), product.Name);

                if (isApproved)
                {
                    _customerService.ApplyRewardPointsForProductReview(customer, product, true);
                }

                TempData["SuccessfullyAdded"] = true;

                return(RedirectToAction("Reviews"));
            }

            // If we got this far something failed. Redisplay form.
            await _helper.PrepareProductReviewsModelAsync(model, product);

            return(View(model));
        }
Ejemplo n.º 4
0
        public ActionResult ReviewsAdd(int id, ProductReviewsModel model, string captchaError)
        {
            var product = _productService.GetProductById(id);

            if (product == null || product.Deleted || product.IsSystemProduct || !product.Published || !product.AllowCustomerReviews)
            {
                return(HttpNotFound());
            }

            if (_captchaSettings.ShowOnProductReviewPage && captchaError.HasValue())
            {
                ModelState.AddModelError("", captchaError);
            }

            if (_services.WorkContext.CurrentCustomer.IsGuest() && !_catalogSettings.AllowAnonymousUsersToReviewProduct)
            {
                ModelState.AddModelError("", T("Reviews.OnlyRegisteredUsersCanWriteReviews"));
            }

            if (ModelState.IsValid)
            {
                var rating = model.Rating;
                if (rating < 1 || rating > 5)
                {
                    rating = _catalogSettings.DefaultProductRatingValue;
                }

                var isApproved = !_catalogSettings.ProductReviewsMustBeApproved;
                var customer   = _services.WorkContext.CurrentCustomer;

                var productReview = new ProductReview
                {
                    ProductId       = product.Id,
                    CustomerId      = customer.Id,
                    IpAddress       = _services.WebHelper.GetCurrentIpAddress(),
                    Title           = model.Title,
                    ReviewText      = model.ReviewText,
                    Rating          = rating,
                    HelpfulYesTotal = 0,
                    HelpfulNoTotal  = 0,
                    IsApproved      = isApproved,
                };
                _customerContentService.InsertCustomerContent(productReview);

                _productService.UpdateProductReviewTotals(product);

                if (_catalogSettings.NotifyStoreOwnerAboutNewProductReviews)
                {
                    Services.MessageFactory.SendProductReviewNotificationMessage(productReview, _localizationSettings.DefaultAdminLanguageId);
                }

                _services.CustomerActivity.InsertActivity("PublicStore.AddProductReview", T("ActivityLog.PublicStore.AddProductReview"), product.Name);

                if (isApproved)
                {
                    _customerService.RewardPointsForProductReview(customer, product, true);
                }

                _helper.PrepareProductReviewsModel(model, product);

                TempData["SuccessfullyAdded"] = true;

                return(RedirectToAction("Reviews"));
            }

            // If we got this far, something failed, redisplay form.
            _helper.PrepareProductReviewsModel(model, product);
            return(View(model));
        }
        public virtual IActionResult ProductDetails(int productId, int updatecartitemid = 0)
        {
            var product = _productService.GetProductById(productId);

            if (product == null || product.Deleted)
            {
                return(InvokeHttp404());
            }

            var notAvailable =
                //published?
                (!product.Published && !_catalogSettings.AllowViewUnpublishedProductPage) ||
                //ACL (access control list)
                !_aclService.Authorize(product) ||
                //Store mapping
                !_storeMappingService.Authorize(product) ||
                //availability dates
                !product.IsAvailable();

            //Check whether the current user has a "Manage products" permission (usually a store owner)
            //We should allows him (her) to use "Preview" functionality
            if (notAvailable && !_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            {
                return(InvokeHttp404());
            }

            //visible individually?
            if (!product.VisibleIndividually)
            {
                //is this one an associated products?
                var parentGroupedProduct = _productService.GetProductById(product.ParentGroupedProductId);
                if (parentGroupedProduct == null)
                {
                    return(RedirectToRoute("HomePage"));
                }

                return(RedirectToRoute("Product", new { SeName = parentGroupedProduct.GetSeName() }));
            }

            //update existing shopping cart or wishlist  item?
            ShoppingCartItem updatecartitem = null;

            if (_shoppingCartSettings.AllowCartItemEditing && updatecartitemid > 0)
            {
                var cart = _workContext.CurrentCustomer.ShoppingCartItems
                           .LimitPerStore(_storeContext.CurrentStore.Id)
                           .ToList();
                updatecartitem = cart.FirstOrDefault(x => x.Id == updatecartitemid);
                //not found?
                if (updatecartitem == null)
                {
                    return(RedirectToRoute("Product", new { SeName = product.GetSeName() }));
                }
                //is it this product?
                if (product.Id != updatecartitem.ProductId)
                {
                    return(RedirectToRoute("Product", new { SeName = product.GetSeName() }));
                }
            }

            //save as recently viewed
            _recentlyViewedProductsService.AddProductToRecentlyViewedList(product.Id);

            //display "edit" (manage) link
            if (_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel) &&
                _permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            {
                //a vendor should have access only to his products
                if (_workContext.CurrentVendor == null || _workContext.CurrentVendor.Id == product.VendorId)
                {
                    DisplayEditLink(Url.Action("Edit", "Product", new { id = product.Id, area = AreaNames.Admin }));
                }
            }

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

            //model
            var model   = _productModelFactory.PrepareProductDetailsModel(product, updatecartitem, false);
            var reviews = new ProductReviewsModel();

            model.ProductReviewsModel = _productModelFactory.PrepareProductReviewsModel(reviews, product);
            //template
            var productTemplateViewPath = _productModelFactory.PrepareProductTemplateViewPath(product);

            return(View(productTemplateViewPath, model));
        }
Ejemplo n.º 6
0
        public virtual async Task <IActionResult> ProductReviews(string productId, ProductReviewsModel model, bool captchaValid,
                                                                 [FromServices] IGroupService groupService,
                                                                 [FromServices] IOrderService orderService)
        {
            var product = await _productService.GetProductById(productId);

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

            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnProductReviewPage && !captchaValid)
            {
                ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_translationService));
            }

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

            if (_catalogSettings.ProductReviewPossibleOnlyAfterPurchasing &&
                !(await orderService.SearchOrders(customerId: _workContext.CurrentCustomer.Id, productId: productId, os: (int)OrderStatusSystem.Complete)).Any())
            {
                ModelState.AddModelError(string.Empty, _translationService.GetResource("Reviews.ProductReviewPossibleOnlyAfterPurchasing"));
            }

            if (ModelState.IsValid)
            {
                var productReview = await _mediator.Send(new InsertProductReviewCommand()
                {
                    Customer = _workContext.CurrentCustomer,
                    Store    = _workContext.CurrentStore,
                    Model    = model,
                    Product  = product
                });

                //notification
                await _mediator.Publish(new ProductReviewEvent(product, model.AddProductReview));

                await _customerActivityService.InsertActivity("PublicStore.AddProductReview", product.Id, _translationService.GetResource("ActivityLog.PublicStore.AddProductReview"), product.Name);

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

                model = await _mediator.Send(new GetProductReviews()
                {
                    Customer = _workContext.CurrentCustomer,
                    Language = _workContext.WorkingLanguage,
                    Product  = product,
                    Store    = _workContext.CurrentStore,
                    Size     = _catalogSettings.NumberOfReview
                });

                model.AddProductReview.Title      = null;
                model.AddProductReview.ReviewText = null;

                model.AddProductReview.SuccessfullyAdded = true;
                if (!productReview.IsApproved)
                {
                    model.AddProductReview.Result = _translationService.GetResource("Reviews.SeeAfterApproving");
                }
                else
                {
                    model.AddProductReview.Result = _translationService.GetResource("Reviews.SuccessfullyAdded");
                }

                return(View(model));
            }

            //If we got this far, something failed, redisplay form
            var newmodel = await _mediator.Send(new GetProductReviews()
            {
                Customer = _workContext.CurrentCustomer,
                Language = _workContext.WorkingLanguage,
                Product  = product,
                Store    = _workContext.CurrentStore,
                Size     = _catalogSettings.NumberOfReview
            });

            newmodel.AddProductReview.Rating     = model.AddProductReview.Rating;
            newmodel.AddProductReview.ReviewText = model.AddProductReview.ReviewText;
            newmodel.AddProductReview.Title      = model.AddProductReview.Title;

            return(View(newmodel));
        }
Ejemplo n.º 7
0
        public virtual async Task <IActionResult> ProductReviewsAdd(string productId, ProductReviewsModel model, bool captchaValid,
                                                                    [FromServices] IOrderService orderService, [FromServices] IMediator mediator)
        {
            var product = await _productService.GetProductById(productId);

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

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

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

            if (_catalogSettings.ProductReviewPossibleOnlyAfterPurchasing &&
                !(await orderService.SearchOrders(customerId: _workContext.CurrentCustomer.Id, productId: productId, os: OrderStatus.Complete)).Any())
            {
                ModelState.AddModelError(string.Empty, _localizationService.GetResource("Reviews.ProductReviewPossibleOnlyAfterPurchasing"));
            }

            if (ModelState.IsValid)
            {
                var productReview = await _productViewModelService.InsertProductReview(product, model);

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

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

                await _productViewModelService.PrepareProductReviewsModel(model, product);

                model.AddProductReview.Title      = null;
                model.AddProductReview.ReviewText = null;

                model.AddProductReview.SuccessfullyAdded = true;
                if (!productReview.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
            await _productViewModelService.PrepareProductReviewsModel(model, product);

            return(View(model));
        }
Ejemplo n.º 8
0
        public virtual async Task <IActionResult> ProductReviewsAdd(int productId, ProductReviewsModel model, bool captchaValid)
        {
            var product = await _productService.GetProductByIdAsync(productId);

            var currentStore = await _storeContext.GetCurrentStoreAsync();

            if (product == null || product.Deleted || !product.Published || !product.AllowCustomerReviews ||
                !await _productService.CanAddReviewAsync(product.Id, _catalogSettings.ShowProductReviewsPerStore ? currentStore.Id : 0))
            {
                return(RedirectToRoute("Homepage"));
            }

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

            await ValidateProductReviewAvailabilityAsync(product);

            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      = (await _workContext.GetCurrentCustomerAsync()).Id,
                    Title           = model.AddProductReview.Title,
                    ReviewText      = model.AddProductReview.ReviewText,
                    Rating          = rating,
                    HelpfulYesTotal = 0,
                    HelpfulNoTotal  = 0,
                    IsApproved      = isApproved,
                    CreatedOnUtc    = DateTime.UtcNow,
                    StoreId         = currentStore.Id,
                };

                await _productService.InsertProductReviewAsync(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
                    };

                    await _reviewTypeService.InsertProductReviewReviewTypeMappingsAsync(additionalProductReview);
                }

                //update product totals
                await _productService.UpdateProductReviewTotalsAsync(product);

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

                //activity log
                await _customerActivityService.InsertActivityAsync("PublicStore.AddProductReview",
                                                                   string.Format(await _localizationService.GetResourceAsync("ActivityLog.PublicStore.AddProductReview"), product.Name), product);

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

                model = await _productModelFactory.PrepareProductReviewsModelAsync(model, product);

                model.AddProductReview.Title      = null;
                model.AddProductReview.ReviewText = null;

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

                return(View(model));
            }

            //if we got this far, something failed, redisplay form
            model = await _productModelFactory.PrepareProductReviewsModelAsync(model, product);

            return(View(model));
        }