/// <summary> /// Renders the section contents. /// </summary> /// <param name="htmlHelper">The HTML helper.</param> /// <param name="webPage">The web page.</param> /// <param name="model">The model.</param> public static void RenderSectionContents(this HtmlHelper htmlHelper, WebPageBase webPage, RenderPageViewModel model) { foreach (var region in model.Regions) { var contentsBuilder = new StringBuilder(); var projections = model.Contents.Where(c => c.RegionId == region.RegionId).OrderBy(c => c.Order).ToList(); using (new LayoutRegionWrapper(contentsBuilder, region, model.CanManageContent)) { foreach (var projection in projections) { // Add Html using (new RegionContentWrapper(contentsBuilder, htmlHelper, projection, model.CanManageContent)) { contentsBuilder.Append(projection.GetHtml(htmlHelper)); } } } var html = contentsBuilder.ToString(); if (!string.IsNullOrWhiteSpace(html)) { RenderSectionAsLayoutRegion(webPage, html, region.RegionIdentifier); } } }
public ActionResult Container(RenderPageViewModel renderPageViewModel) { var modelToRender = renderPageViewModel.RenderingPage ?? renderPageViewModel; SidebarContainerViewModel model = new SidebarContainerViewModel(); try { model.HeaderProjections = new PageProjectionsViewModel { Page = modelToRender, Projections = modulesRegistration.GetSidebarHeaderProjections().OrderBy(f => f.Order) }; model.SideProjections = new PageProjectionsViewModel { Page = modelToRender, Projections = modulesRegistration.GetSidebarSideProjections().OrderBy(f => f.Order) }; model.BodyProjections = new PageProjectionsViewModel { Page = modelToRender, Projections = modulesRegistration.GetSidebarBodyProjections().OrderBy(f => f.Order) }; model.Version = configuration.Version; } catch (CoreException ex) { Log.Error(ex); } return PartialView(model); }
/// <summary> /// Called when page is rendering. /// </summary> /// <param name="renderPageData">The rendering page data.</param> public void OnPageRendering(RenderPageViewModel renderPageData) { if (PageRendering != null) { PageRendering(new PageRenderingEventArgs(renderPageData)); } }
/// <summary> /// Gets the replaced HTML. /// </summary> /// <param name="stringBuilder">The string builder.</param> /// <param name="model">The model.</param> /// <returns>HTML with replaced model values</returns> public override StringBuilder GetReplacedHtml(StringBuilder stringBuilder, RenderPageViewModel model) { foreach (var match in FindAllMatches(stringBuilder)) { string replaceWith = null; if (match.Parameters.Length > 0 && model.Options != null) { var optionKey = match.Parameters[0]; var option = model.Options.FirstOrDefault(o => o.Key == optionKey); if (option != null && option.Value != null) { if (option.Value is DateTime) { if (match.Parameters.Length > 1) { try { replaceWith = ((DateTime)option.Value).ToString(match.Parameters[1]); } catch { // Do nothing } } else { replaceWith = ((DateTime)option.Value).ToString(CultureInfo.InvariantCulture); } } else if (option.Value is decimal) { if (match.Parameters.Length > 1) { try { replaceWith = ((decimal)option.Value).ToString(match.Parameters[1]); } catch { // Do nothing } } else { replaceWith = ((decimal)option.Value).ToString(CultureInfo.InvariantCulture); } } else { replaceWith = option.Value.ToString(); } } } stringBuilder.Replace(match.GlobalMatch, replaceWith); } return stringBuilder; }
/// <summary> /// Renders page to string recursively - going deep to master pages and layouts. /// </summary> /// <param name="controller">The controller.</param> /// <param name="currentModel">The model.</param> /// <param name="pageModel">The page model.</param> /// <param name="htmlHelper">The HTML helper.</param> /// <returns> /// String builder with updated data /// </returns> private static StringBuilder RenderRecursively(CmsControllerBase controller, RenderPageViewModel currentModel, RenderPageViewModel pageModel, HtmlHelper htmlHelper) { if (currentModel.MasterPage != null) { var renderedMaster = RenderRecursively(controller, currentModel.MasterPage, pageModel, htmlHelper); var pageHtmlHelper = new PageHtmlRenderer.PageHtmlRenderer(renderedMaster, pageModel); foreach (var region in currentModel.Regions) { var contentsBuilder = new StringBuilder(); var projections = currentModel.Contents.Where(c => c.RegionId == region.RegionId).OrderBy(c => c.Order).ToList(); using (new LayoutRegionWrapper(contentsBuilder, region, currentModel.AreRegionsEditable)) { foreach (var projection in projections) { // Add Html using (new RegionContentWrapper(contentsBuilder, projection, currentModel.CanManageContent && currentModel.AreRegionsEditable)) { // Pass current model as view data model htmlHelper.ViewData.Model = pageModel; var content = projection.GetHtml(htmlHelper); contentsBuilder.Append(content); } } } // Insert region to master page var html = contentsBuilder.ToString(); pageHtmlHelper.ReplaceRegionHtml(region.RegionIdentifier, html); } if (currentModel.AreRegionsEditable) { pageHtmlHelper.ReplaceRegionRepresentationHtml(); } renderedMaster = pageHtmlHelper.GetReplacedHtml(); return renderedMaster; } var newModel = currentModel.Clone(); newModel.Id = pageModel.Id; newModel.PageUrl = pageModel.PageUrl; newModel.Title = pageModel.Title; newModel.MetaTitle = pageModel.MetaTitle; newModel.RenderingPage = pageModel; newModel.Metadata = pageModel.Metadata; newModel.IsReadOnly = pageModel.IsReadOnly; PopulateCollections(newModel, pageModel); var renderedView = RenderViewToString(controller, "~/Areas/bcms-Root/Views/Cms/Index.cshtml", newModel); return new StringBuilder(renderedView); }
/// <summary> /// Called when page is retrieved. /// </summary> /// <param name="renderPageData">The rendering page data.</param> /// <param name="pageData">The page data.</param> public PageRetrievedEventResult OnPageRetrieved(RenderPageViewModel renderPageData, IPage pageData) { if (PageRetrieved != null) { var args = new PageRetrievedEventArgs(renderPageData, pageData); PageRetrieved(args); return args.EventResult; } return PageRetrievedEventResult.None; }
/// <summary> /// Populates the collections. /// </summary> /// <param name="destination">The destination.</param> /// <param name="source">The source.</param> private static void PopulateCollections(RenderPageViewModel destination, RenderPageViewModel source) { if (source.MasterPage != null) { PopulateCollections(destination, source.MasterPage); } if (source.JavaScripts != null) { if (destination.JavaScripts == null) { destination.JavaScripts = new List<IJavaScriptAccessor>(); } foreach (var js in source.JavaScripts) { if (!destination.JavaScripts.Contains(js)) { destination.JavaScripts.Add(js); } } } if (source.Stylesheets != null) { if (destination.Stylesheets == null) { destination.Stylesheets = new List<IStylesheetAccessor>(); } foreach (var css in source.Stylesheets) { if (!destination.Stylesheets.Contains(css)) { destination.Stylesheets.Add(css); } } } if (source.OptionsAsDictionary != null) { if (destination.OptionsAsDictionary == null) { destination.OptionsAsDictionary = new Dictionary<string, IOptionValue>(); } foreach (var option in source.OptionsAsDictionary) { destination.OptionsAsDictionary[option.Key] = option.Value; } } }
/// <summary> /// Appends the HTML with HTML, rendered by content projection. /// </summary> /// <param name="stringBuilder">The string builder.</param> /// <param name="projection">The projection.</param> /// <param name="pageModel">The page model.</param> /// <returns></returns> public StringBuilder AppendHtml(StringBuilder stringBuilder, PageContentProjection projection, RenderPageViewModel pageModel) { var renderingPageModel = pageModel.RenderingPage ?? pageModel; var content = projection.GetHtml(htmlHelper); if (!renderingPageModel.RenderedPageContents.Contains(projection.PageContentId)) { renderingPageModel.RenderedPageContents.Add(projection.PageContentId); } var childrenContents = projection.GetChildProjections() ?? new List<ChildContentProjection>(); var parsedWidgets = ParseWidgetsFromHtml(content).Distinct(); var availableWidgets = childrenContents.Where(cc => parsedWidgets.Any(id => id.AssignmentIdentifier == cc.AssignmentIdentifier)); foreach (var childProjection in availableWidgets) { var model = parsedWidgets.First(w => w.AssignmentIdentifier == childProjection.AssignmentIdentifier); var replaceWhat = model.Match.Value; var replaceWith = AppendHtml(new StringBuilder(), childProjection, renderingPageModel).ToString(); content = content.Replace(replaceWhat, replaceWith); } // Widgets, which has no access (e.g. widgets with draft status for public users) var invisibleWidgets = parsedWidgets.Where(id => childrenContents.All(cc => cc.AssignmentIdentifier != id.AssignmentIdentifier)); foreach (var model in invisibleWidgets) { var replaceWhat = model.Match.Value; var replaceWith = string.Empty; content = content.Replace(replaceWhat, replaceWith); } // Add child contents in the master page to child region is possible only if content is widget. // If content is regular HTML content, it works as master page contents, and contents may be added only in the child page // If page is for preview, doesn't rendering children regions if ((!renderingPageModel.IsMasterPage || projection.Content is IChildRegionContainer) && !pageModel.IsPreviewing) { content = AppendHtmlWithChildRegionContens(content, projection, renderingPageModel); } stringBuilder.Append(content); return stringBuilder; }
private List<InvisibleContentProjection> CollectInvisibleRegionsRecursively( RenderPageViewModel renderingPageModel, IEnumerable<PageContentProjection> contentProjections, List<InvisibleContentProjection> invisibleContentProjections, InvisibleContentProjection parentContentProjection = null) { // Add all invisible contents to the invisible list foreach (var projection in contentProjections) { var invisibleProjection = new InvisibleContentProjection { Parent = parentContentProjection, ContentProjection = projection }; invisibleContentProjections.Add(invisibleProjection); if (!renderingPageModel.RenderedPageContents.Contains(projection.PageContentId) || (parentContentProjection != null && parentContentProjection.IsInvisible)) { invisibleProjection.IsInvisible = true; } var childContentRegionProjections = projection.GetChildRegionContentProjections(); if (childContentRegionProjections != null && childContentRegionProjections.Any()) { invisibleContentProjections = CollectInvisibleRegionsRecursively(renderingPageModel, childContentRegionProjections.Distinct(), invisibleContentProjections, invisibleProjection); } } return invisibleContentProjections; }
public StringBuilder GetReplacedInvisibleRegions(RenderPageViewModel model, StringBuilder html) { var invisibleRegionsHtml = RenderInvisibleRegions(model); return html.Replace(InvisibleRegionsPlaceholder, invisibleRegionsHtml); }
/// <summary> /// Renders the invisible regions:. /// - Layout regions: /// -- When switching from layout A to layout B, and layout B has nor regions, which were in layout A /// -- When layout regions are deleted in Site Settings -> Page Layouts -> Templates /// - Widget regions: /// -- When region was deleted from the widget, and page has a content, assigned to that region /// </summary> /// <param name="model">The model.</param> /// <returns></returns> public string RenderInvisibleRegions(RenderPageViewModel model) { var renderingModel = model; if (model.RenderingPage != null) { renderingModel = model.RenderingPage; } if (!renderingModel.CanManageContent) { return null; } var contentsBuilder = new StringBuilder(); var invisibleContentProjections = CollectInvisibleRegionsRecursively(renderingModel, renderingModel.Contents, new List<InvisibleContentProjection>()); invisibleContentProjections.Where(i => i.Parent != null && i.IsInvisible && !i.Parent.IsInvisible).ForEach(i => i.Parent = null); contentsBuilder = RenderInvisibleRegionsRecursively(contentsBuilder, invisibleContentProjections.Where(i => i.IsInvisible)); var html = contentsBuilder.ToString(); if (!string.IsNullOrWhiteSpace(html)) { return html; } return null; }
private string AppendHtmlWithChildRegionContens(string html, PageContentProjection projection, RenderPageViewModel pageModel) { // Render contents from children regions var childRegionContents = projection.GetChildRegionContentProjections() ?? new List<PageContentProjection>(); if (projection.Content is IChildRegionContainer && projection.Content.ContentRegions != null && projection.Content.ContentRegions.Any()) { var stringBuilder = new StringBuilder(html); var pageHtmlHelper = new PageHtmlRenderer(stringBuilder, pageModel); foreach (var region in projection.Content.ContentRegions.Distinct()) { var contentsBuilder = new StringBuilder(); var regionModel = new PageRegionViewModel { RegionId = region.Region.Id, RegionIdentifier = region.Region.RegionIdentifier }; var childRegionContentProjections = childRegionContents.Where(c => c.RegionId == regionModel.RegionId).OrderBy(c => c.Order).ToList(); var canEditRegion = projection.PageId == pageModel.Id && pageModel.AreRegionsEditable; using (new LayoutRegionWrapper(contentsBuilder, regionModel, canEditRegion)) { foreach (var childRegionContentProjection in childRegionContentProjections) { // Add Html using (new RegionContentWrapper(contentsBuilder, childRegionContentProjection, pageModel.CanManageContent && canEditRegion)) { // Pass current model as view data model var modelBefore = htmlHelper.ViewData.Model; htmlHelper.ViewData.Model = pageModel; contentsBuilder = AppendHtml(contentsBuilder, childRegionContentProjection, pageModel); // Restore model, which was before changes htmlHelper.ViewData.Model = modelBefore; } } } // Insert region to master page var regionHtml = contentsBuilder.ToString(); pageHtmlHelper.ReplaceRegionHtml(regionModel.RegionIdentifier, regionHtml); } return pageHtmlHelper.GetReplacedHtml().ToString(); } return html; }
/// <summary> /// Creates the test view model. /// </summary> /// <returns>Test view model</returns> private RenderPageViewModel CreateTestViewModel(bool extendModel = false) { var entity = new BlogPost { Title = "Fake Page Title", PageUrl = "/Fake/Page/Url/s", Id = new Guid("DB4C3C70-F5F3-44A1-9472-6155A9A77D89"), CreatedOn = new DateTime(2010, 11, 15), ModifiedOn = new DateTime(2012, 12, 3), CreatedByUser = "******", ModifiedByUser = "******", MetaTitle = "Fake Page Meta Title", MetaKeywords = "Fake Page Meta Keywords", MetaDescription = "Fake Page MetaDescription", ActivationDate = new DateTime(2012, 5, 12), ExpirationDate = new DateTime(2013, 4, 18) }; if (extendModel) { entity.Categories = new List<PageCategory>() { new PageCategory() { Category = new Category { Name = "Fake Category Name" }, Page = entity } }; entity.Author = new Author { Name = "Fake Author Name" }; entity.Image = new MediaImage { PublicUrl = "/Fake/Main/Image/Url/" }; entity.SecondaryImage = new MediaImage { PublicUrl = "/Fake/Secondary/Image/Url/" }; entity.FeaturedImage = new MediaImage { PublicUrl = "/Fake/Featured/Image/Url/" }; entity.ActivationDate = new DateTime(); var content = new BlogPostContent { ActivationDate = new DateTime(2012, 5, 12), ExpirationDate = new DateTime(2013, 4, 18) }; var pageContent = new PageContent { Content = content, Page = entity }; entity.PageContents = new List<PageContent> { pageContent }; } var model = new RenderPageViewModel(entity) { Options = new List<IOptionValue> { new OptionValueViewModel { OptionKey = OptionNames.Text, OptionValue = "Fake Option Value", Type = OptionType.Text }, new OptionValueViewModel { OptionKey = OptionNames.Float, OptionValue = 10.123456M, Type = OptionType.Float }, new OptionValueViewModel { OptionKey = OptionNames.Date, OptionValue = new DateTime(2009, 4, 27), Type = OptionType.DateTime } } }; if (extendModel) { model.Contents = new List<PageContentProjection>(); model.Contents.Add( new PageContentProjection( entity.PageContents[0], entity.PageContents[0].Content, new BlogPostContentAccessor((BlogPostContent)entity.PageContents[0].Content, new List<IOptionValue>()))); model.ExtendWithPageData(entity); model.ExtendWithBlogData(entity); } return model; }
/// <summary> /// Gets the view model for rendering widget preview. /// </summary> /// <param name="contentId">The content id.</param> /// <param name="user">The user.</param> /// <returns> /// View model for rendering widget preview /// </returns> public RenderPageViewModel GetContentPreviewViewModel(Guid contentId, IPrincipal user, bool allowJavaScript) { // Creating fake region. var regionGuid = new Guid(regionId); var region = new Region { Id = regionGuid, RegionIdentifier = regionIdentifier }; var regionViewModel = new PageRegionViewModel { RegionId = regionGuid, RegionIdentifier = regionIdentifier }; // Creating fake page content and loading it's children. var pageContent = new PageContent { Options = new List<PageContentOption>(), Region = region }; pageContent.Content = repository .AsQueryable<ContentEntity>(c => c.Id == contentId) .FetchMany(f => f.ContentOptions) .FetchMany(f => f.ChildContents) .ThenFetch(f => f.Child) .FetchMany(f => f.ChildContents) .ThenFetchMany(f => f.Options) .ToList() .FirstOrDefault(); if (pageContent.Content != null) { DemandAccess(user, RootModuleConstants.UserRoles.EditContent, RootModuleConstants.UserRoles.PublishContent); } childContentService.RetrieveChildrenContentsRecursively(true, new[] { pageContent.Content }); var contentProjection = contentProjectionService.CreatePageContentProjection(true, pageContent, new List<PageContent> { pageContent }, retrieveCorrectVersion: false); var pageViewModel = new RenderPageViewModel { Contents = new List<PageContentProjection> { contentProjection }, Stylesheets = new List<IStylesheetAccessor> { contentProjection }, Regions = new List<PageRegionViewModel> { regionViewModel }, AreRegionsEditable = true, IsPreviewing = true }; if (allowJavaScript) { pageViewModel.JavaScripts = new List<IJavaScriptAccessor> { contentProjection }; } return pageViewModel; }
/// <summary> /// Initializes a new instance of the <see cref="PageRenderingEventArgs" /> class. /// </summary> /// <param name="renderPageData">The render page data.</param> public PageRenderingEventArgs(RenderPageViewModel renderPageData) { RenderPageData = renderPageData; }
/// <summary> /// Initializes a new instance of the <see cref="PageRetrievedEventArgs" /> class. /// </summary> /// <param name="renderPageData">The render page data.</param> /// <param name="page">The page.</param> public PageRetrievedEventArgs(RenderPageViewModel renderPageData, IPage page) { RenderPageData = renderPageData; PageData = page; }
public CmsRequestViewModel(RenderPageViewModel renderPage) { RenderPage = renderPage; }
/// <summary> /// Gets the view model for rendering widget preview. /// </summary> /// <param name="contentId">The content id.</param> /// <param name="user">The user.</param> /// <returns> /// View model for rendering widget preview /// </returns> public RenderPageViewModel GetContentPreviewViewModel(Guid contentId, IPrincipal user, bool allowJavaScript) { // Creating fake region. var regionGuid = new Guid(regionId); var region = new Region { Id = regionGuid, RegionIdentifier = regionIdentifier }; var regionViewModel = new PageRegionViewModel { RegionId = regionGuid, RegionIdentifier = regionIdentifier }; // Creating fake page content and loading it's children. var pageContent = new PageContent { Options = new List<PageContentOption>(), Region = region }; pageContent.Content = repository .AsQueryable<ContentEntity>(c => c.Id == contentId) .FetchMany(f => f.ContentOptions) .ToList() .FirstOrDefault(); if (pageContent.Content != null) { DemandAccess(user, RootModuleConstants.UserRoles.EditContent, RootModuleConstants.UserRoles.PublishContent); } var options = optionService.GetMergedOptionValues(pageContent.Content.ContentOptions, pageContent.Options); var contentProjection = pageContentProjectionFactory.Create(pageContent, pageContent.Content, options); var pageViewModel = new RenderPageViewModel { Contents = new List<PageContentProjection> { contentProjection }, Stylesheets = new List<IStylesheetAccessor> { contentProjection }, Regions = new List<PageRegionViewModel> { regionViewModel }, AreRegionsEditable = true }; if (allowJavaScript) { pageViewModel.JavaScripts = new List<IJavaScriptAccessor> { contentProjection }; } return pageViewModel; }
private void LogAccessForbidden(RenderPageViewModel model) { log.WarnFormat("Failed to load page by URL: {0}. Access is forbidden.", model.PageUrl); // Notifying, that page access was forbidden. Events.RootEvents.Instance.OnPageAccessForbidden(model); }
/// <summary> /// Renders the page to string. /// </summary> /// <param name="controller">The controller.</param> /// <param name="renderPageViewModel">The render page view model.</param> /// <returns>Renders page to string</returns> public static string RenderPageToString(this CmsControllerBase controller, RenderPageViewModel renderPageViewModel) { var htmlHelper = GetHtmlHelper(controller); return RenderRecursively(controller, renderPageViewModel, renderPageViewModel, htmlHelper).ToString(); }
/// <summary> /// Gets the replaced HTML. /// </summary> /// <returns>HTML with replaced view model values</returns> private string GetReplacedHtml(string html, RenderPageViewModel model) { var helper = new PageHtmlRenderer(new StringBuilder(html), model); return helper.GetReplacedHtml().ToString(); }
/// <summary> /// Extends the page and master page view models with data from provided page entity. /// </summary> /// <param name="renderPageViewModel">The render page view model.</param> /// <param name="pageData">The page data.</param> private void ExtendPageWithPageData(RenderPageViewModel renderPageViewModel, IPage pageData) { if (renderPageViewModel.MasterPage != null) { ExtendPageWithPageData(renderPageViewModel.MasterPage, renderPageViewModel.PageData); } renderPageViewModel.ExtendWithBlogData(pageData); }
/// <summary> /// Gets the replaced HTML. /// </summary> /// <param name="stringBuilder">The string builder.</param> /// <param name="model">The model.</param> /// <returns>HTML with replaced model values</returns> public StringBuilder GetReplacedHtml(StringBuilder stringBuilder, RenderPageViewModel model) { return GetReplacedHtml(stringBuilder, model.Options); }
/// <summary> /// Initializes a new instance of the <see cref="PageHtmlRenderer" /> class. /// </summary> /// <param name="stringBuilder">The string builder.</param> /// <param name="model">The model.</param> public PageHtmlRenderer(StringBuilder stringBuilder, RenderPageViewModel model) { this.model = model; this.stringBuilder = stringBuilder; }