Beispiel #1
0
        public ActionResult ProductComments(string widgetZone, object additionalData = null)
        {
            if (additionalData == null)
            {
                return(Content(""));
            }

            var product = _productService.GetProductById((int)additionalData);

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

            if (!_productCommentsSetting.EnablePlugin)
            {
                return(Content(""));
            }

            var model = new ProductCommentsModel();

            _productCommentModelFactory.PrepareProductCommentsModel(model, product);
            //only registered users can leave comments
            if (_workContext.CurrentCustomer.IsGuest() && !_productCommentsSetting.AllowAnonymousUsersToCommentProduct)
            {
                ModelState.AddModelError("", _localizationService.GetResource("Comments.OnlyRegisteredUsersCanWriteComments"));
            }

            return(View("~/Plugins/Resanehlab.ProductComments/Views/_ProductComments.cshtml", model));
        }
Beispiel #2
0
        private void InsertProductComment(ProductCommentsModel productCommentsModel, Product product)
        {
            bool           productCommentsMustBeApproved = !_productCommentsSetting.ProductCommentsMustBeApproved;
            ProductComment productComment = new ProductComment()
            {
                ProductId       = product.Id,
                CustomerId      = _workContext.CurrentCustomer.Id,
                CommentText     = productCommentsModel.AddProductComment.CommentText,
                HelpfulYesTotal = 0,
                HelpfulNoTotal  = 0,
                IsApproved      = productCommentsMustBeApproved,
                CreatedOnUtc    = DateTime.UtcNow,
                StoreId         = _storeContext.CurrentStore.Id,
                Visited         = false
            };

            _productCommentService.InsertProductComment(productComment);

            _productCommentModelFactory.PrepareProductCommentsModel(productCommentsModel, product);
            productCommentsModel.AddProductComment.CommentText       = null;
            productCommentsModel.AddProductComment.SuccessfullyAdded = true;
            if (!productCommentsMustBeApproved)
            {
                productCommentsModel.AddProductComment.Result = _localizationService.GetResource("Comments.SeeAfterApproving");
                return;
            }
            productCommentsModel.AddProductComment.Result = _localizationService.GetResource("Comments.SuccessfullyAdded");
        }
Beispiel #3
0
        public virtual void PrepareProductCommentsModel(ProductCommentsModel model, Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            model.ProductId     = product.Id;
            model.ProductName   = product.GetLocalized(t => t.Name);
            model.ProductSeName = product.GetSeName();

            var productComments = _productCommentService.GetAllProductComments(isApproved: true, productId: product.Id, storeId: _storeContext.CurrentStore.Id);

            foreach (var pr in productComments)
            {
                var customer = pr.CustomerId.HasValue ? _customerService.GetCustomerById(pr.CustomerId.Value) : null;
                model.Items.Add(new ProductCommentModel
                {
                    Id                   = pr.Id,
                    CustomerId           = pr.CustomerId,
                    CustomerName         = customer != null ? customer.FormatUserName() : _localizationService.GetResource("Customer.Guest"),
                    AllowViewingProfiles = customer != null && !customer.IsGuest() && _customerSettings.AllowViewingProfiles,
                    CommentText          = pr.CommentText,
                    ReplyText            = pr.ReplyText,
                    Helpfulness          = new ProductCommentHelpfulnessModel
                    {
                        ProductCommentId = pr.Id,
                        HelpfulYesTotal  = pr.HelpfulYesTotal,
                        HelpfulNoTotal   = pr.HelpfulNoTotal,
                    },
                    CreatedOnStr = _dateTimeHelper.ConvertToUserTime(pr.CreatedOnUtc, DateTimeKind.Utc).ToString("g"),
                });
            }

            model.AddProductComment.CanCurrentCustomerLeaveComment = _productCommentsSetting.AllowAnonymousUsersToCommentProduct || !_workContext.CurrentCustomer.IsGuest();
        }
Beispiel #4
0
        public ActionResult ProductCommentsAddNew(int id, ProductCommentsModel model)
        {
            Product productById = _productService.GetProductById(id);

            if (productById == null || productById.Deleted || !productById.Published || !_productCommentsSetting.EnablePlugin)
            {
                return(Content(""));
            }
            if (base.ModelState.IsValid)
            {
                if (!_workContext.CurrentCustomer.IsGuest() || _productCommentsSetting.AllowAnonymousUsersToCommentProduct)
                {
                    InsertProductComment(model, productById);
                    return(PartialView("~/Plugins/Resanehlab.ProductComments/Views/_ProductComments.cshtml", model));
                }
                base.ModelState.AddModelError("", _localizationService.GetResource("Comments.OnlyRegisteredUsersCanWriteComments"));
            }

            _productCommentModelFactory.PrepareProductCommentsModel(model, productById);
            return(PartialView("~/Plugins/Resanehlab.ProductComments/Views/_ProductComments.cshtml", model));
        }