Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
0
        public ProductReviewDto PrepareProductReviewDTO(ProductReview productReview)
        {
            var productReviewModel = productReview.ToDto();


            // load the customer who wrote a comment
            var customer = _customerService.GetCustomerById(productReview.CustomerId);

            productReviewModel.CustomerName = _customerService.GetCustomerFullName(customer);

            productReviewModel.CustomerAvatarUrl = _pictureService.GetPictureUrl(
                _genericAttributeService.GetAttribute <int>(customer, NopCustomerDefaults.AvatarPictureIdAttribute),
                _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType: PictureType.Avatar);


            // load a review type mappings

            productReviewModel.ReviewTypeMappingsDto = _reviewTypeService
                                                       .GetProductReviewReviewTypeMappingsByProductReviewId(productReview.Id)
                                                       .Select(rtm => rtm.ToDto())
                                                       .ToList();

            return(productReviewModel);
        }
        /// <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);
        }