/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { PageRoute pageRoute; var rockContext = new RockContext(); PageRouteService pageRouteService = new PageRouteService(rockContext); int pageRouteId = int.Parse(hfPageRouteId.Value); if (pageRouteId == 0) { pageRoute = new PageRoute(); pageRouteService.Add(pageRoute); } else { pageRoute = pageRouteService.Get(pageRouteId); } pageRoute.Route = tbRoute.Text.Trim(); int selectedPageId = int.Parse(ppPage.SelectedValue); pageRoute.PageId = selectedPageId; if (!pageRoute.IsValid) { // Controls will render the error messages return; } if (pageRouteService.Queryable().Any(r => r.Route == pageRoute.Route && r.Id != pageRoute.Id)) { // Duplicate nbErrorMessage.Title = "Duplicate Route"; nbErrorMessage.Text = "<p>There is already an existing route with this name and route names must be unique. Please choose a different route name.</p>"; nbErrorMessage.Visible = true; } else { rockContext.SaveChanges(); // new or updated route var existingRoute = RouteTable.Routes.OfType <Route>().FirstOrDefault(a => a.RouteId() == pageRoute.Id); if (existingRoute != null) { RouteTable.Routes.Remove(existingRoute); } RouteTable.Routes.AddPageRoute(pageRoute); NavigateToParentPage(); } }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { PageRoute pageRoute; PageRouteService pageRouteService = new PageRouteService(); int pageRouteId = int.Parse(hfPageRouteId.Value); if (pageRouteId == 0) { pageRoute = new PageRoute(); pageRouteService.Add(pageRoute, CurrentPersonId); } else { pageRoute = pageRouteService.Get(pageRouteId); } pageRoute.Route = tbRoute.Text.Trim(); int selectedPageId = int.Parse(ddlPageName.SelectedValue); pageRoute.PageId = selectedPageId; // check for duplicates if (pageRouteService.Queryable().Count(a => a.Route.Equals(pageRoute.Route, StringComparison.OrdinalIgnoreCase) && !a.Id.Equals(pageRoute.Id)) > 0) { nbMessage.Text = WarningMessage.DuplicateFoundMessage("route", Rock.Model.Page.FriendlyTypeName); nbMessage.Visible = true; return; } if (!pageRoute.IsValid) { // Controls will render the error messages return; } pageRouteService.Save(pageRoute, CurrentPersonId); RemovePageRoute(pageRoute); // new or updated route RouteTable.Routes.AddPageRoute(pageRoute); BindGrid(); pnlDetails.Visible = false; pnlList.Visible = true; }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { PageRoute pageRoute; var rockContext = new RockContext(); PageRouteService pageRouteService = new PageRouteService(rockContext); int pageRouteId = int.Parse(hfPageRouteId.Value); if (pageRouteId == 0) { pageRoute = new PageRoute(); pageRouteService.Add(pageRoute); } else { pageRoute = pageRouteService.Get(pageRouteId); } pageRoute.Route = tbRoute.Text.Trim(); int selectedPageId = int.Parse(ppPage.SelectedValue); pageRoute.PageId = selectedPageId; if (!pageRoute.IsValid) { // Controls will render the error messages return; } rockContext.SaveChanges(); // new or updated route var existingRoute = RouteTable.Routes.OfType <Route>().FirstOrDefault(a => a.RouteId() == pageRoute.Id); if (existingRoute != null) { RouteTable.Routes.Remove(existingRoute); } RouteTable.Routes.AddPageRoute(pageRoute); NavigateToParentPage(); }
/// <summary> /// Recursively saves Pages and associated Blocks, PageRoutes and PageContexts. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="page">The current Page to save</param> /// <param name="newBlockTypes">List of BlockTypes not currently installed</param> /// <param name="parentPageId">Id of the current Page's parent</param> /// <param name="siteId">Id of the site the current Page is being imported into</param> private void SavePages(RockContext rockContext, Page page, IEnumerable <BlockType> newBlockTypes, int parentPageId, int siteId) { rockContext = rockContext ?? new RockContext(); // find layout var layoutService = new LayoutService(rockContext); Layout layout = new Layout(); if (page.Layout != null) { layout = layoutService.GetBySiteId(siteId).Where(l => l.Name == page.Layout.Name && l.FileName == page.Layout.FileName).FirstOrDefault(); if (layout == null) { layout = new Layout(); layout.FileName = page.Layout.FileName; layout.Name = page.Layout.Name; layout.SiteId = siteId; layoutService.Add(layout); rockContext.SaveChanges(); } } else { layout = layoutService.GetBySiteId(siteId).Where(l => l.Name.Contains("Full") || l.Name.Contains("Home")).First(); } int layoutId = layout.Id; // Force shallow copies on entities so save operations are more atomic and don't get hosed // by nested object references. var pg = page.Clone(deepCopy: false); var blockTypes = newBlockTypes.ToList(); pg.ParentPageId = parentPageId; pg.LayoutId = layoutId; var pageService = new PageService(rockContext); pageService.Add(pg); rockContext.SaveChanges(); var blockService = new BlockService(rockContext); foreach (var block in page.Blocks ?? new List <Block>()) { var blockType = blockTypes.FirstOrDefault(bt => block.BlockType.Path == bt.Path); var b = block.Clone(deepCopy: false); b.PageId = pg.Id; if (blockType != null) { b.BlockTypeId = blockType.Id; } blockService.Add(b); } rockContext.SaveChanges(); var pageRouteService = new PageRouteService(rockContext); foreach (var pageRoute in page.PageRoutes ?? new List <PageRoute>()) { var pr = pageRoute.Clone(deepCopy: false); pr.PageId = pg.Id; pageRouteService.Add(pr); } rockContext.SaveChanges(); var pageContextService = new PageContextService(rockContext); foreach (var pageContext in page.PageContexts ?? new List <PageContext>()) { var pc = pageContext.Clone(deepCopy: false); pc.PageId = pg.Id; pageContextService.Add(pc); } rockContext.SaveChanges(); foreach (var p in page.Pages ?? new List <Page>()) { SavePages(rockContext, p, blockTypes, pg.Id, siteId); } }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { PageRoute pageRoute; var rockContext = new RockContext(); PageRouteService pageRouteService = new PageRouteService(rockContext); int pageRouteId = int.Parse(hfPageRouteId.Value); if (pageRouteId == 0) { pageRoute = new PageRoute(); pageRouteService.Add(pageRoute); } else { pageRoute = pageRouteService.Get(pageRouteId); } pageRoute.Route = tbRoute.Text.Trim(); int selectedPageId = int.Parse(ppPage.SelectedValue); pageRoute.PageId = selectedPageId; if (!pageRoute.IsValid) { // Controls will render the error messages return; } int?siteId = null; var pageCache = PageCache.Read(selectedPageId); if (pageCache != null && pageCache.Layout != null) { siteId = pageCache.Layout.SiteId; } var duplicateRoutes = pageRouteService .Queryable().AsNoTracking() .Where(r => r.Route == pageRoute.Route && r.Id != pageRoute.Id); if (siteId.HasValue) { duplicateRoutes = duplicateRoutes .Where(r => r.Page != null && r.Page.Layout != null && r.Page.Layout.SiteId == siteId.Value); } if (duplicateRoutes.Any()) { // Duplicate nbErrorMessage.Title = "Duplicate Route"; nbErrorMessage.Text = "<p>There is already an existing route with this name for the selected page's site. Route names must be unique per site. Please choose a different route name.</p>"; nbErrorMessage.Visible = true; } else { rockContext.SaveChanges(); // Remove previous route var oldRoute = RouteTable.Routes.OfType <Route>().FirstOrDefault(a => a.RouteIds().Contains(pageRoute.Id)); if (oldRoute != null) { var pageAndRouteIds = oldRoute.DataTokens["PageRoutes"] as List <Rock.Web.PageAndRouteId>; pageAndRouteIds = pageAndRouteIds.Where(p => p.RouteId != pageRoute.Id).ToList(); if (pageAndRouteIds.Any()) { oldRoute.DataTokens["PageRoutes"] = pageAndRouteIds; } else { RouteTable.Routes.Remove(oldRoute); } } // Remove the '{shortlink}' route (will be added back after specific routes) var shortLinkRoute = RouteTable.Routes.OfType <Route>().Where(r => r.Url == "{shortlink}").FirstOrDefault(); if (shortLinkRoute != null) { RouteTable.Routes.Remove(shortLinkRoute); } // Add new route var pageAndRouteId = new Rock.Web.PageAndRouteId { PageId = pageRoute.PageId, RouteId = pageRoute.Id }; var existingRoute = RouteTable.Routes.OfType <Route>().FirstOrDefault(r => r.Url == pageRoute.Route); if (existingRoute != null) { var pageAndRouteIds = existingRoute.DataTokens["PageRoutes"] as List <Rock.Web.PageAndRouteId>; pageAndRouteIds.Add(pageAndRouteId); existingRoute.DataTokens["PageRoutes"] = pageAndRouteIds; } else { var pageAndRouteIds = new List <Rock.Web.PageAndRouteId>(); pageAndRouteIds.Add(pageAndRouteId); RouteTable.Routes.AddPageRoute(pageRoute.Route, pageAndRouteIds); } RouteTable.Routes.Add(new Route("{shortlink}", new Rock.Web.RockRouteHandler())); NavigateToParentPage(); } }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { PageRoute pageRoute; var rockContext = new RockContext(); PageRouteService pageRouteService = new PageRouteService(rockContext); int pageRouteId = int.Parse(hfPageRouteId.Value); if (pageRouteId == 0) { pageRoute = new PageRoute(); pageRouteService.Add(pageRoute); } else { pageRoute = pageRouteService.Get(pageRouteId); } pageRoute.Route = tbRoute.Text.Trim(); pageRoute.IsGlobal = cbIsGlobal.Checked; int selectedPageId = int.Parse(ppPage.SelectedValue); pageRoute.PageId = selectedPageId; if (!pageRoute.IsValid) { // Controls will render the error messages return; } int?siteId = null; var pageCache = PageCache.Get(selectedPageId); if (pageCache != null && pageCache.Layout != null) { siteId = pageCache.Layout.SiteId; } var duplicateRoutes = pageRouteService .Queryable().AsNoTracking() .Where(r => r.Route == pageRoute.Route && r.Id != pageRoute.Id); if (siteId.HasValue) { duplicateRoutes = duplicateRoutes .Where(r => r.Page != null && r.Page.Layout != null && r.Page.Layout.SiteId == siteId.Value); } if (duplicateRoutes.Any()) { // Duplicate nbErrorMessage.Title = "Duplicate Route"; nbErrorMessage.Text = "<p>There is already an existing route with this name for the selected page's site. Route names must be unique per site. Please choose a different route name.</p>"; nbErrorMessage.Visible = true; } else { pageRoute.LoadAttributes(rockContext); rockContext.WrapTransaction(() => { rockContext.SaveChanges(); if (!pageRoute.IsSystem) { Rock.Attribute.Helper.GetEditValues(phAttributes, pageRoute); pageRoute.SaveAttributeValues(rockContext); } }); PageCache.FlushPage(pageCache.Id); Rock.Web.RockRouteHandler.ReregisterRoutes(); NavigateToParentPage(); } }