Esempio n. 1
0
        private async Task PrepareBlogPostModel(BlogPostModel model, BlogPost blogPost)
        {
            if (blogPost == null)
            {
                throw new ArgumentNullException("blogPost");
            }

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

            model.Id                = blogPost.Id;
            model.MetaTitle         = blogPost.GetLocalized(x => x.MetaTitle, _workContext.WorkingLanguage.Id);
            model.MetaDescription   = blogPost.GetLocalized(x => x.MetaDescription, _workContext.WorkingLanguage.Id);
            model.MetaKeywords      = blogPost.GetLocalized(x => x.MetaKeywords, _workContext.WorkingLanguage.Id);
            model.SeName            = blogPost.GetSeName(_workContext.WorkingLanguage.Id);
            model.Title             = blogPost.GetLocalized(x => x.Title, _workContext.WorkingLanguage.Id);
            model.Body              = blogPost.GetLocalized(x => x.Body, _workContext.WorkingLanguage.Id);
            model.BodyOverview      = blogPost.GetLocalized(x => x.BodyOverview, _workContext.WorkingLanguage.Id);
            model.AllowComments     = blogPost.AllowComments;
            model.CreatedOn         = _dateTimeHelper.ConvertToUserTime(blogPost.StartDateUtc ?? blogPost.CreatedOnUtc, DateTimeKind.Utc);
            model.Tags              = blogPost.ParseTags().ToList();
            model.NumberOfComments  = blogPost.CommentCount;
            model.GenericAttributes = blogPost.GenericAttributes;

            //prepare picture model
            await PrepareBlogPostPictureModel(model, blogPost);
        }
Esempio n. 2
0
        public void PrepareBlogPostModel(BlogPostModel model, BlogPost blogPost, bool prepareComments)
        {
            if (blogPost == null)
            {
                throw new ArgumentNullException("blogPost");
            }

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

            model.Id                           = blogPost.Id;
            model.MetaTitle                    = blogPost.GetLocalized(x => x.MetaTitle);
            model.MetaDescription              = blogPost.GetLocalized(x => x.MetaDescription);
            model.MetaKeywords                 = blogPost.GetLocalized(x => x.MetaKeywords);
            model.SeName                       = blogPost.GetSeName();
            model.Title                        = blogPost.GetLocalized(x => x.Title);
            model.Body                         = blogPost.GetLocalized(x => x.Body);
            model.BodyOverview                 = blogPost.GetLocalized(x => x.BodyOverview);
            model.AllowComments                = blogPost.AllowComments;
            model.CreatedOn                    = _dateTimeHelper.ConvertToUserTime(blogPost.StartDateUtc ?? blogPost.CreatedOnUtc, DateTimeKind.Utc);
            model.Tags                         = blogPost.ParseTags().ToList();
            model.NumberOfComments             = blogPost.CommentCount;
            model.AddNewComment.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnBlogCommentPage;
            if (prepareComments)
            {
                var blogComments = _blogService.GetBlogCommentsByBlogPostId(blogPost.Id);
                foreach (var bc in blogComments)
                {
                    var commentModel = PrepareBlogPostCommentModel(bc);
                    model.Comments.Add(commentModel);
                }
            }
        }
Esempio n. 3
0
        public async Task PrepareBlogPostModel(BlogPostModel model, BlogPost blogPost, bool prepareComments)
        {
            if (blogPost == null)
            {
                throw new ArgumentNullException("blogPost");
            }

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

            model.Id                           = blogPost.Id;
            model.MetaTitle                    = blogPost.GetLocalized(x => x.MetaTitle, _workContext.WorkingLanguage.Id);
            model.MetaDescription              = blogPost.GetLocalized(x => x.MetaDescription, _workContext.WorkingLanguage.Id);
            model.MetaKeywords                 = blogPost.GetLocalized(x => x.MetaKeywords, _workContext.WorkingLanguage.Id);
            model.SeName                       = blogPost.GetSeName(_workContext.WorkingLanguage.Id);
            model.Title                        = blogPost.GetLocalized(x => x.Title, _workContext.WorkingLanguage.Id);
            model.Body                         = blogPost.GetLocalized(x => x.Body, _workContext.WorkingLanguage.Id);
            model.BodyOverview                 = blogPost.GetLocalized(x => x.BodyOverview, _workContext.WorkingLanguage.Id);
            model.AllowComments                = blogPost.AllowComments;
            model.CreatedOn                    = _dateTimeHelper.ConvertToUserTime(blogPost.StartDateUtc ?? blogPost.CreatedOnUtc, DateTimeKind.Utc);
            model.Tags                         = blogPost.ParseTags().ToList();
            model.NumberOfComments             = blogPost.CommentCount;
            model.AddNewComment.DisplayCaptcha = _captchaSettings.Enabled && _captchaSettings.ShowOnBlogCommentPage;
            model.GenericAttributes            = blogPost.GenericAttributes;

            if (prepareComments)
            {
                var blogComments = await _blogService.GetBlogCommentsByBlogPostId(blogPost.Id);

                foreach (var bc in blogComments)
                {
                    var commentModel = await PrepareBlogPostCommentModel(bc);

                    model.Comments.Add(commentModel);
                }
            }

            //prepare picture model
            if (!string.IsNullOrEmpty(blogPost.PictureId))
            {
                int pictureSize             = _mediaSettings.BlogThumbPictureSize;
                var categoryPictureCacheKey = string.Format(ModelCacheEventConsumer.BLOG_PICTURE_MODEL_KEY, blogPost.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(), _storeContext.CurrentStore.Id);
                model.PictureModel = await _cacheManager.GetAsync(categoryPictureCacheKey, async() =>
                {
                    var picture      = await _pictureService.GetPictureById(blogPost.PictureId);
                    var pictureModel = new PictureModel {
                        Id = blogPost.PictureId,
                        FullSizeImageUrl = await _pictureService.GetPictureUrl(picture),
                        ImageUrl         = await _pictureService.GetPictureUrl(picture, pictureSize),
                        Title            = string.Format(_localizationService.GetResource("Media.Blog.ImageLinkTitleFormat"), blogPost.Title),
                        AlternateText    = string.Format(_localizationService.GetResource("Media.Blog.ImageAlternateTextFormat"), blogPost.Title)
                    };
                    return(pictureModel);
                });
            }
        }
Esempio n. 4
0
        protected void PrepareBlogPostModel(BlogPostModel model, BlogPost blogPost, bool prepareComments)
        {
            Guard.NotNull(blogPost, nameof(blogPost));
            Guard.NotNull(model, nameof(model));

            MiniMapper.Map(blogPost, model);

            model.Title           = blogPost.GetLocalized(x => x.Title);
            model.Intro           = blogPost.GetLocalized(x => x.Intro);
            model.Body            = blogPost.GetLocalized(x => x.Body, true);
            model.MetaTitle       = blogPost.GetLocalized(x => x.MetaTitle);
            model.MetaDescription = blogPost.GetLocalized(x => x.MetaDescription);
            model.MetaKeywords    = blogPost.GetLocalized(x => x.MetaKeywords);
            model.SeName          = blogPost.GetSeName(ensureTwoPublishedLanguages: false);
            model.CreatedOn       = _dateTimeHelper.ConvertToUserTime(blogPost.CreatedOnUtc, DateTimeKind.Utc);
            model.CreatedOnUTC    = blogPost.CreatedOnUtc;
            model.AddNewComment.DisplayCaptcha           = _captchaSettings.CanDisplayCaptcha && _captchaSettings.ShowOnBlogCommentPage;
            model.Comments.AllowComments                 = blogPost.AllowComments;
            model.Comments.NumberOfComments              = blogPost.ApprovedCommentCount;
            model.Comments.AllowCustomersToUploadAvatars = _customerSettings.AllowCustomersToUploadAvatars;
            model.DisplayAdminLink = _services.Permissions.Authorize(Permissions.System.AccessBackend, _services.WorkContext.CurrentCustomer);

            model.HasBgImage = blogPost.PreviewDisplayType == PreviewDisplayType.DefaultSectionBg || blogPost.PreviewDisplayType == PreviewDisplayType.PreviewSectionBg;

            model.PictureModel = PrepareBlogPostPictureModel(blogPost, blogPost.MediaFileId);

            if (blogPost.PreviewDisplayType == PreviewDisplayType.Default || blogPost.PreviewDisplayType == PreviewDisplayType.DefaultSectionBg)
            {
                model.PreviewPictureModel = PrepareBlogPostPictureModel(blogPost, blogPost.MediaFileId);
            }
            else if (blogPost.PreviewDisplayType == PreviewDisplayType.Preview || blogPost.PreviewDisplayType == PreviewDisplayType.PreviewSectionBg)
            {
                model.PreviewPictureModel = PrepareBlogPostPictureModel(blogPost, blogPost.PreviewMediaFileId);
            }

            if (blogPost.PreviewDisplayType == PreviewDisplayType.Preview ||
                blogPost.PreviewDisplayType == PreviewDisplayType.Default ||
                blogPost.PreviewDisplayType == PreviewDisplayType.Bare)
            {
                model.SectionBg = string.Empty;
            }

            model.Tags = blogPost.ParseTags().Select(x => new BlogPostTagModel
            {
                Name   = x,
                SeName = SeoHelper.GetSeName(x,
                                             _seoSettings.ConvertNonWesternChars,
                                             _seoSettings.AllowUnicodeCharsInUrls,
                                             true,
                                             _seoSettings.SeoNameCharConversion)
            }).ToList();

            if (prepareComments)
            {
                var blogComments = blogPost.BlogComments
                                   .Where(pr => pr.IsApproved)
                                   .OrderBy(pr => pr.CreatedOnUtc);

                foreach (var bc in blogComments)
                {
                    var isGuest = bc.Customer.IsGuest();

                    var commentModel = new CommentModel(model.Comments)
                    {
                        Id                   = bc.Id,
                        CustomerId           = bc.CustomerId,
                        CustomerName         = bc.Customer.FormatUserName(_customerSettings, T, false),
                        CommentText          = bc.CommentText,
                        CreatedOn            = _dateTimeHelper.ConvertToUserTime(bc.CreatedOnUtc, DateTimeKind.Utc),
                        CreatedOnPretty      = bc.CreatedOnUtc.RelativeFormat(true, "f"),
                        AllowViewingProfiles = _customerSettings.AllowViewingProfiles && !isGuest
                    };

                    commentModel.Avatar = bc.Customer.ToAvatarModel(_genericAttributeService, _customerSettings, _mediaSettings, commentModel.CustomerName);

                    model.Comments.Comments.Add(commentModel);
                }
            }

            Services.DisplayControl.Announce(blogPost);
        }
Esempio n. 5
0
        protected PictureModel PrepareBlogPostPictureModel(BlogPost blogPost, int?fileId)
        {
            var file = _mediaService.GetFileById(fileId ?? 0, MediaLoadFlags.AsNoTracking);

            var pictureModel = new PictureModel
            {
                PictureId           = blogPost.MediaFileId.GetValueOrDefault(),
                Size                = MediaSettings.ThumbnailSizeLg,
                ImageUrl            = _mediaService.GetUrl(file, MediaSettings.ThumbnailSizeLg, null, false),
                FullSizeImageUrl    = _mediaService.GetUrl(file, 0, null, false),
                FullSizeImageWidth  = file?.Dimensions.Width,
                FullSizeImageHeight = file?.Dimensions.Height,
                Title               = file?.File?.GetLocalized(x => x.Title)?.Value.NullEmpty() ?? blogPost.GetLocalized(x => x.Title),
                AlternateText       = file?.File?.GetLocalized(x => x.Alt)?.Value.NullEmpty() ?? blogPost.GetLocalized(x => x.Title),
                File                = file
            };

            _services.DisplayControl.Announce(file?.File);

            return(pictureModel);
        }
Esempio n. 6
0
        /// <summary>
        /// Gets blog post SE (search engine) name
        /// </summary>
        /// <param name="blogPost">Blog post</param>
        /// <param name="languageId">Language identifier</param>
        /// <returns>Blog post SE (search engine) name</returns>
        public static string GetSeName(this BlogPost blogPost, int languageId)
        {
            Guard.NotNull(blogPost, nameof(blogPost));

            return(GetSeName(blogPost.GetLocalized(x => x.Title, languageId)));
        }