/// <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="htmlHelper">The HTML helper.</param> /// <param name="model">The model.</param> /// <returns></returns> public static MvcHtmlString RenderInvisibleRegions(this HtmlHelper htmlHelper, RenderPageViewModel model) { var childContentRenderHelper = new PageContentRenderHelper(htmlHelper); var html = childContentRenderHelper.RenderInvisibleRegions(model); return(new MvcHtmlString(html)); }
/// <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) { var contentHtmlHelper = new PageContentRenderHelper(htmlHelper); 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.AreRegionsEditable)) { foreach (var projection in projections) { // Add content Html using (new RegionContentWrapper(contentsBuilder, projection, model.CanManageContent && model.AreRegionsEditable)) { contentsBuilder = contentHtmlHelper.AppendHtml(contentsBuilder, projection, model); } } } var pageHtmlHelper = new PageHtmlRenderer.PageHtmlRenderer(contentsBuilder, model); if (model.AreRegionsEditable) { pageHtmlHelper.ReplaceRegionRepresentationHtml(); } var html = pageHtmlHelper.GetReplacedHtml().ToString(); RenderSectionAsLayoutRegion(webPage, html, region.RegionIdentifier); } }
public void CollectChildContents(string html, Models.Content content) { var widgetModels = PageContentRenderHelper.ParseWidgetsFromHtml(html, true); if (widgetModels != null && widgetModels.Count > 0) { // Validate widget ids var widgetIds = widgetModels.Select(w => w.WidgetId).Distinct().ToArray(); var widgets = repository.AsQueryable <Models.Content>(c => widgetIds.Contains(c.Id)).Select(c => c.Id).ToList(); widgetIds.Where(id => widgets.All(dbId => dbId != id)).ToList().ForEach( id => { var message = RootGlobalization.ChildContent_WidgetNotFound_ById; var logMessage = string.Format("{0} Id: {1}", message, id); throw new ValidationException(() => message, logMessage); }); // Validate child content var group = widgetModels.GroupBy(w => w.AssignmentIdentifier).FirstOrDefault(g => g.Count() > 1); if (group != null) { var message = string.Format(RootGlobalization.ChildContent_AssignmentAlreadyAdded, group.First().AssignmentIdentifier); throw new ValidationException(() => message, message); } foreach (var model in widgetModels) { // Add child content only if it's not added yet (for example, it may be added when restoring / cloning a content) if (content.ChildContents.All(cc => cc.AssignmentIdentifier != model.AssignmentIdentifier)) { // Create child content var childContent = new ChildContent { Id = Guid.NewGuid(), Child = repository.AsProxy <Models.Content>(model.WidgetId), Parent = content, AssignmentIdentifier = model.AssignmentIdentifier }; content.ChildContents.Add(childContent); } } } }
private void CheckIfContentHasDeletingWidgetsWithDynamicRegions(Widget widget, string targetHtml) { var sourceHtml = ((IDynamicContentContainer)widget).Html; var sourceWidgets = PageContentRenderHelper.ParseWidgetsFromHtml(sourceHtml); var targetWidgets = PageContentRenderHelper.ParseWidgetsFromHtml(targetHtml); // Get ids of child widgets, which are being removed from the content var removingWidgetIds = sourceWidgets .Where(sw => targetWidgets.All(tw => tw.WidgetId != sw.WidgetId)) .Select(sw => sw.WidgetId) .Distinct() .ToArray(); if (removingWidgetIds.Any()) { var childContents = childContentService.RetrieveChildrenContentsRecursively(true, removingWidgetIds); var childContentIds = childContents.Select(cc => cc.Child.Id).Distinct().ToList(); PageContent pcAlias = null; ContentRegion contentRegionAlias = null; var subQuery = QueryOver.Of(() => contentRegionAlias) .Where(() => !contentRegionAlias.IsDeleted) .And(Restrictions.In(Projections.Property(() => contentRegionAlias.Content.Id), childContentIds)) .And(() => contentRegionAlias.Region == pcAlias.Region) .Select(cr => cr.Id); var areAnyContentUsages = repository.AsQueryOver(() => pcAlias) .WithSubquery.WhereExists(subQuery) .And(() => !pcAlias.IsDeleted) .RowCount() > 0; if (areAnyContentUsages) { var message = PagesGlobalization.SaveWidget_ContentHasChildrenWidgetWithDynamicContents_ConfirmationMessage; var logMessage = string.Format("User is trying to remove child widget which has children with dynamic regions and assigned contents. Confirmation is required."); throw new ConfirmationRequestException(() => message, logMessage); } } }
/// <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); var contentHtmlHelper = new PageContentRenderHelper(htmlHelper); 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; contentsBuilder = contentHtmlHelper.AppendHtml(contentsBuilder, projection, currentModel); } } } // Insert region to master page var html = contentsBuilder.ToString(); pageHtmlHelper.ReplaceRegionHtml(region.RegionIdentifier, html); } if (currentModel.AreRegionsEditable) { pageHtmlHelper.ReplaceRegionRepresentationHtml(); } renderedMaster = pageHtmlHelper.GetReplacedHtml(); if (pageModel == currentModel) { renderedMaster = contentHtmlHelper.GetReplacedInvisibleRegions(pageModel, renderedMaster); } return(renderedMaster); } var newModel = currentModel.Clone(); newModel.Id = pageModel.Id; newModel.PageUrl = pageModel.PageUrl; newModel.Title = pageModel.Title; newModel.MetaTitle = pageModel.MetaTitle; newModel.MetaKeywords = pageModel.MetaKeywords; newModel.MetaDescription = pageModel.MetaDescription; newModel.RenderingPage = pageModel; newModel.Metadata = pageModel.Metadata; newModel.IsReadOnly = pageModel.IsReadOnly; newModel.CreatedOn = pageModel.CreatedOn; newModel.ModifiedOn = pageModel.ModifiedOn; newModel.CreatedByUser = pageModel.CreatedByUser; newModel.ModifiedByUser = pageModel.ModifiedByUser; newModel.IsMasterPage = pageModel.IsMasterPage; PopulateCollections(newModel, pageModel); var renderedView = RenderViewToString(controller, "~/Areas/bcms-Root/Views/Cms/Index.cshtml", newModel); return(new StringBuilder(renderedView)); }