/// <summary> /// Handles the SaveClick event of the mdAddBlock 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 mdAddBlock_SaveClick(object sender, EventArgs e) { using (var rockContext = new RockContext()) { BlockService blockService = new BlockService(rockContext); var page = PageCache.Get(hfPageId.Value.AsInteger()); Block block = new Rock.Model.Block(); block.Zone = ddlZones.SelectedValue; block.Name = tbNewBlockName.Text; block.BlockTypeId = ddlBlockType.SelectedValue.AsInteger(); if (rblAddBlockLocation.SelectedValue == "Site") { block.PageId = null; block.LayoutId = null; block.SiteId = page.SiteId; } else if (rblAddBlockLocation.SelectedValue == "Layout") { block.PageId = null; block.LayoutId = page.LayoutId; block.SiteId = null; } else if (rblAddBlockLocation.SelectedValue == "Page") { block.PageId = page.Id; block.LayoutId = null; block.SiteId = null; } block.Order = blockService.GetMaxOrder(block); blockService.Add(block); rockContext.SaveChanges(); Rock.Security.Authorization.CopyAuthorization(page, block, rockContext); if (block.LayoutId.HasValue) { PageCache.FlushPagesForLayout(page.LayoutId); } else { //page.RemoveBlocks(); PageCache.Remove(page.Id); } mdAddBlock.Hide(); ShowDetailForZone(ddlZones.SelectedValue); } }
/// <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>(); switch (location) { case BlockLocation.Site: block.LayoutId = null; block.PageId = null; block.SiteId = _Page.SiteId; break; case BlockLocation.Layout: block.LayoutId = _Page.LayoutId; block.PageId = null; block.SiteId = null; break; case BlockLocation.Page: block.LayoutId = null; block.PageId = _Page.Id; block.SiteId = null; break; } block.Zone = _ZoneName; Rock.Model.Block lastBlock = blockService.GetByPageAndZone(_Page.Id, _ZoneName).OrderByDescending(b => b.Order).FirstOrDefault(); var maxOrder = blockService.GetMaxOrder(block); 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> /// Processes the drag events. /// </summary> private void ProcessDragEvents() { string argument = Request["__EVENTARGUMENT"].ToStringSafe(); var segments = argument.Split(new[] { '|' }); // // Check for the event to add a new block. // if (segments.Length == 4 && segments[0] == "add-block") { var blockType = ComponentItemState.Where(c => c.Id == segments[1].AsInteger()).Single(); var zoneName = segments[2]; var order = segments[3].AsInteger(); int pageId = hfPageId.Value.AsInteger(); using (var rockContext = new RockContext()) { var blockService = new BlockService(rockContext); // // Generate the new block. // var block = new Block { PageId = pageId, BlockTypeId = blockType.Id, Name = blockType.Name, Zone = zoneName, Order = order }; blockService.Add(block); // // Get the current list of blocks in the zone. // var blocks = blockService.Queryable() .Where(b => b.PageId == pageId && b.Zone == zoneName) .OrderBy(a => a.Order) .ThenBy(a => a.Id) .ToList(); // // Insert the new block and then fixup all the Order values. // blocks.Insert(order, block); int index = 0; blocks.ForEach(b => b.Order = index++); rockContext.SaveChanges(); } BindZones(); } // // Check for the event to drag-reorder block. // else if (segments.Length == 4 && segments[0] == "reorder-block") { int pageId = hfPageId.Value.AsInteger(); var zoneName = segments[1]; int blockId = segments[2].AsInteger(); int newIndex = segments[3].AsInteger(); using (var rockContext = new RockContext()) { var blockService = new BlockService(rockContext); var block = blockService.Get(blockId); // // Get all blocks for this page and the destination zone except the current block. // var blocks = blockService.Queryable() .Where(b => b.PageId == pageId && b.Zone == zoneName) .Where(b => b.Id != block.Id) .OrderBy(a => a.Order) .ThenBy(a => a.Id) .ToList(); // // Insert the block in the right position and update the zone name. // blocks.Insert(newIndex, block); block.Zone = zoneName; // // Fixup the Order values of all the blocks. // int index = 0; blocks.ForEach(b => b.Order = index++); rockContext.SaveChanges(); } BindZones(); } }
/// <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) { int pageId = PageParameter("EditPage").AsInteger() ?? 0; 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; }