protected void btnSave_Click(object sender, EventArgs e) { Rock.Model.Block block; int blockId = 0; if (!Int32.TryParse(hfBlockId.Value, out blockId)) { blockId = 0; } if (blockId == 0) { block = new Rock.Model.Block(); BlockLocation location = hfBlockLocation.Value.ConvertToEnum <BlockLocation>(); if (location == BlockLocation.Layout) { block.Layout = _page.Layout; block.PageId = null; } else { block.Layout = null; block.PageId = _page.Id; } block.Zone = _zoneName; Rock.Model.Block lastBlock = blockService.GetByLayoutAndPageIdAndZone(null, _page.Id, _zoneName). OrderByDescending(b => b.Order).FirstOrDefault(); if (lastBlock != null) { block.Order = lastBlock.Order + 1; } else { block.Order = 0; } blockService.Add(block, CurrentPersonId); } else { block = blockService.Get(blockId); } block.Name = tbBlockName.Text; block.BlockTypeId = Convert.ToInt32(ddlBlockType.SelectedValue); blockService.Save(block, CurrentPersonId); Rock.Security.Authorization.CopyAuthorization(_page, block, CurrentPersonId); if (block.Layout != null) { Rock.Web.Cache.PageCache.FlushLayoutBlocks(_page.Layout); } else { _page.FlushBlocks(); } BindGrids(); pnlDetails.Visible = false; pnlLists.Visible = true; }
/// <summary> /// Recursively saves Pages and associated Blocks, PageRoutes and PageContexts. /// </summary> /// <param name="page">The current Page to save</param> /// <param name="newBlockTypes">List of BlockTypes not currently installed</param> /// <param name="personId">Id of the Person performing the "Import" operation</param> /// <param name="parentPageId">Id of the the current Page's parent</param> /// <param name="siteId">Id of the site the current Page is being imported into</param> private void SavePages( Page page, IEnumerable<BlockType> newBlockTypes, int personId, int parentPageId, int siteId ) { // find layout var layoutService = new LayoutService(); var 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, null ); layoutService.Save( layout, null ); } 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(); pageService.Add( pg, personId ); pageService.Save( pg, personId ); var blockService = new BlockService(); 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, personId ); blockService.Save( b, personId ); } var pageRouteService = new PageRouteService(); foreach ( var pageRoute in page.PageRoutes ?? new List<PageRoute>() ) { var pr = pageRoute.Clone(deepCopy: false); pr.PageId = pg.Id; pageRouteService.Add( pr, personId ); pageRouteService.Save( pr, personId ); } var pageContextService = new PageContextService(); foreach ( var pageContext in page.PageContexts ?? new List<PageContext>() ) { var pc = pageContext.Clone(deepCopy: false); pc.PageId = pg.Id; pageContextService.Add( pc, personId ); pageContextService.Save( pc, personId ); } foreach ( var p in page.Pages ?? new List<Page>() ) { SavePages( p, blockTypes, personId, 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 ) { bool newBlock = false; Rock.Model.Block block = null; using ( var rockContext = new RockContext() ) { BlockService blockService = new BlockService( rockContext ); int blockId = hfBlockId.ValueAsInt(); if ( blockId != 0 ) { block = blockService.Get( blockId ); } if ( block == null ) { newBlock = true; block = new Rock.Model.Block(); blockService.Add( block ); BlockLocation location = hfBlockLocation.Value.ConvertToEnum<BlockLocation>(); if ( location == BlockLocation.Layout ) { block.LayoutId = _Page.LayoutId; block.PageId = null; } else { block.LayoutId = null; block.PageId = _Page.Id; } block.Zone = _ZoneName; Rock.Model.Block lastBlock = blockService.GetByPageAndZone( _Page.Id, _ZoneName ).OrderByDescending( b => b.Order ).FirstOrDefault(); if ( lastBlock != null ) { block.Order = lastBlock.Order + 1; } else { block.Order = 0; } } block.Name = tbBlockName.Text; block.BlockTypeId = Convert.ToInt32( ddlBlockType.SelectedValue ); rockContext.SaveChanges(); if ( newBlock ) { Rock.Security.Authorization.CopyAuthorization( _Page, block, rockContext ); } if ( block.Layout != null ) { Rock.Web.Cache.PageCache.FlushLayoutBlocks( _Page.LayoutId ); } else { _Page.FlushBlocks(); } } PageUpdated = true; BindGrids(); pnlDetails.Visible = false; pnlLists.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 ) { int pageId = PageParameter( "EditPage" ).AsInteger(); Rock.Web.Cache.PageCache page = Rock.Web.Cache.PageCache.Read( pageId ); string zoneName = this.PageParameter( "ZoneName" ); Rock.Model.Block block; var rockContext = new RockContext(); BlockService blockService = new BlockService( rockContext ); int blockId = hfBlockId.ValueAsInt(); if ( blockId == 0 ) { block = new Rock.Model.Block(); BlockLocation location = hfBlockLocation.Value.ConvertToEnum<BlockLocation>(); if ( location == BlockLocation.Layout ) { block.LayoutId = page.LayoutId; block.PageId = null; } else { block.LayoutId = null; block.PageId = page.Id; } block.Zone = zoneName; Rock.Model.Block lastBlock = blockService.GetByPageAndZone( page.Id, zoneName ).OrderByDescending( b => b.Order ).FirstOrDefault(); if ( lastBlock != null ) { block.Order = lastBlock.Order + 1; } else { block.Order = 0; } blockService.Add( block ); } else { block = blockService.Get( blockId ); } block.Name = tbBlockName.Text; block.BlockTypeId = Convert.ToInt32( ddlBlockType.SelectedValue ); rockContext.SaveChanges(); Rock.Security.Authorization.CopyAuthorization( page, block ); if ( block.Layout != null ) { Rock.Web.Cache.PageCache.FlushLayoutBlocks( page.LayoutId ); } else { page.FlushBlocks(); } BindGrids(); pnlDetails.Visible = false; pnlLists.Visible = true; }