/// <summary> /// Handles the Click event of the btnNewBlockQuickSetting 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 btnNewBlockQuickSetting_Click(object sender, EventArgs e) { LinkButton btnNewBlockQuickSetting = sender as LinkButton; BlockTypeCache quickSettingBlockType = BlockTypeCache.Read(btnNewBlockQuickSetting.CommandArgument.AsInteger()); if (quickSettingBlockType != null) { ddlBlockType.SetValue(quickSettingBlockType.Id); ddlBlockType_SelectedIndexChanged(sender, e); } }
/// <summary> /// Flushes the block type attributes. /// </summary> private void FlushBlockTypeAttributes() { // Flush BlockType, Block and Entity Attributes AttributeCache.FlushEntityAttributes(); BlockTypeCache.Flush(hfBlockTypeId.Value.AsInteger()); var blockTypeCache = BlockTypeCache.Read(hfBlockTypeId.Value.AsInteger()); foreach (var blockId in new BlockService(new RockContext()).GetByBlockTypeId(hfBlockTypeId.Value.AsInteger()).Select(a => a.Id).ToList()) { BlockCache.Flush(blockId); } }
/// <summary> /// Handles the Click event of the btnLoadBlockTypesAndPages 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 btnLoadBlockTypesAndPages_Click(object sender, EventArgs e) { var rockContext = new RockContext(); // ensure update attributes is called on every blocktype foreach (var blockType in new BlockTypeService(rockContext).Queryable().AsNoTracking().ToList()) { System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); var blockTypeCache = BlockTypeCache.Read(blockType.Guid); if (!blockTypeCache.IsInstancePropertiesVerified) { var blockControl = this.Page.LoadControl(blockTypeCache.Path) as RockBlock; int?blockEntityTypeId = EntityTypeCache.Read(typeof(Block)).Id; Rock.Attribute.Helper.UpdateAttributes(blockControl.GetType(), blockEntityTypeId, "BlockTypeId", blockType.Id.ToString(), rockContext); blockTypeCache.IsInstancePropertiesVerified = true; System.Diagnostics.Debug.WriteLine(string.Format("[{1}ms] BlockType {0}", blockTypeCache.Path, stopwatch.Elapsed.TotalMilliseconds)); } stopwatch.Stop(); } foreach (var page in new PageService(rockContext).Queryable()) { string url = string.Empty; try { System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); url = string.Format("http{0}://{1}:{2}{3}", (Request.IsSecureConnection) ? "s" : "", Request.Url.Host, Request.Url.Port, ResolveRockUrl(new PageReference(page.Id).BuildUrl())); WebRequest request = WebRequest.Create(url); request.Timeout = 10000; WebResponse response = request.GetResponse(); stopwatch.Stop(); System.Diagnostics.Debug.WriteLine(string.Format("[{2}ms] Loaded {0} {1} ", page.InternalName, url, stopwatch.Elapsed.TotalMilliseconds)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("Exception Loading {0} {1}, {2}", page.InternalName, url, ex)); } } }
/// <summary> /// Gets the block type attributes that are defined in code in the blocktype /// </summary> /// <returns></returns> private List <string> GetBlockTypeStaticAttributeKeys() { var blockTypeCache = BlockTypeCache.Read(hfBlockTypeId.Value.AsInteger()); List <FieldAttribute> blockProperties = new List <FieldAttribute>();; try { var blockControlType = System.Web.Compilation.BuildManager.GetCompiledType(blockTypeCache.Path); foreach (var customAttribute in blockControlType.GetCustomAttributes(typeof(FieldAttribute), true)) { blockProperties.Add((FieldAttribute)customAttribute); } } catch { // ignore if the block can't compile } var blockStaticAttributeKeys = blockProperties.Select(a => a.Key).ToList(); return(blockStaticAttributeKeys); }
/// <summary> /// Raises the <see cref="E:Init" /> event. /// </summary> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected override void OnInit(EventArgs e) { Rock.Web.UI.DialogPage dialogPage = this.Page as Rock.Web.UI.DialogPage; if (dialogPage != null) { dialogPage.OnSave += new EventHandler <EventArgs>(masterPage_OnSave); } try { int blockId = Convert.ToInt32(PageParameter("BlockId")); Block _block = new BlockService(new RockContext()).Get(blockId); if (_block.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson)) { var blockType = BlockTypeCache.Read(_block.BlockTypeId); if (blockType != null && !blockType.IsInstancePropertiesVerified) { System.Web.UI.Control control = Page.LoadControl(blockType.Path); if (control is RockBlock) { using (var rockContext = new RockContext()) { var rockBlock = control as RockBlock; int?blockEntityTypeId = EntityTypeCache.Read(typeof(Block)).Id; Rock.Attribute.Helper.UpdateAttributes(rockBlock.GetType(), blockEntityTypeId, "BlockTypeId", blockType.Id.ToString(), rockContext); } blockType.IsInstancePropertiesVerified = true; } } phAttributes.Controls.Clear(); phAdvancedAttributes.Controls.Clear(); _block.LoadAttributes(); if (_block.Attributes != null) { foreach (var attributeCategory in Rock.Attribute.Helper.GetAttributeCategories(_block)) { if (attributeCategory.Category != null && attributeCategory.Category.Name.Equals("customsetting", StringComparison.OrdinalIgnoreCase)) { } else if (attributeCategory.Category != null && attributeCategory.Category.Name.Equals("advanced", StringComparison.OrdinalIgnoreCase)) { Rock.Attribute.Helper.AddEditControls( string.Empty, attributeCategory.Attributes.Select(a => a.Key).ToList(), _block, phAdvancedAttributes, string.Empty, !Page.IsPostBack, new List <string>()); } else { Rock.Attribute.Helper.AddEditControls( attributeCategory.Category != null ? attributeCategory.Category.Name : string.Empty, attributeCategory.Attributes.Select(a => a.Key).ToList(), _block, phAttributes, string.Empty, !Page.IsPostBack, new List <string>()); } } } } else { DisplayError("You are not authorized to edit this block", null); } } catch (SystemException ex) { DisplayError(ex.Message, "<pre>" + HttpUtility.HtmlEncode(ex.StackTrace) + "</pre>"); } base.OnInit(e); }
/// <summary> /// Handles the Click event of the btnAddBlock 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 btnAddBlock_Click(object sender, EventArgs e) { tbNewBlockName.Text = string.Empty; // Load the block types using (var rockContext = new RockContext()) { try { BlockTypeService.RegisterBlockTypes(Request.MapPath("~"), Page); } catch { // ignore } Rock.Model.BlockTypeService blockTypeService = new Rock.Model.BlockTypeService(rockContext); var blockTypes = blockTypeService.Queryable().AsNoTracking() .Select(b => new { b.Id, b.Name, b.Category, b.Description }) .ToList(); ddlBlockType.Items.Clear(); // Add the categorized block types foreach (var blockType in blockTypes .Where(b => b.Category != string.Empty) .OrderBy(b => b.Category) .ThenBy(b => b.Name)) { var li = new ListItem(blockType.Name, blockType.Id.ToString()); li.Attributes.Add("optiongroup", blockType.Category); li.Attributes.Add("title", blockType.Description); ddlBlockType.Items.Add(li); } // Add the uncategorized block types foreach (var blockType in blockTypes .Where(b => b.Category == null || b.Category == string.Empty) .OrderBy(b => b.Name)) { var li = new ListItem(blockType.Name, blockType.Id.ToString()); li.Attributes.Add("optiongroup", "Other (not categorized)"); li.Attributes.Add("title", blockType.Description); ddlBlockType.Items.Add(li); } } var htmlContentBlockType = BlockTypeCache.Read(Rock.SystemGuid.BlockType.HTML_CONTENT.AsGuid()); ddlBlockType.SetValue(htmlContentBlockType.Id); rblAddBlockLocation.Items.Clear(); var page = PageCache.Read(hfPageId.Value.AsInteger()); var listItemPage = new ListItem(); listItemPage.Text = string.Format("Page ({0})", page.ToString()); listItemPage.Value = "Page"; listItemPage.Selected = true; var listItemLayout = new ListItem(); listItemLayout.Text = string.Format("Layout ({0})", page.Layout); listItemLayout.Value = "Layout"; listItemLayout.Selected = false; var listItemSite = new ListItem(); listItemSite.Text = string.Format("Site ({0})", page.Layout.Site); listItemSite.Value = "Site"; listItemSite.Selected = false; rblAddBlockLocation.Items.Add(listItemPage); rblAddBlockLocation.Items.Add(listItemLayout); rblAddBlockLocation.Items.Add(listItemSite); mdAddBlock.Title = "Add Block to " + ddlZones.SelectedValue + " Zone"; mdAddBlock.Show(); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { var rockContext = new RockContext(); int entityTypeIdBlock = EntityTypeCache.Read(typeof(Rock.Model.Block), true, rockContext).Id; string entityTypeQualifier = BlockTypeCache.Read(Rock.SystemGuid.BlockType.HTML_CONTENT.AsGuid(), rockContext).Id.ToString(); var htmlContentService = new HtmlContentService(rockContext); var attributeValueQry = new AttributeValueService(rockContext).Queryable() .Where(a => a.Attribute.Key == "RequireApproval" && a.Attribute.EntityTypeId == entityTypeIdBlock) .Where(a => a.Attribute.EntityTypeQualifierColumn == "BlockTypeId" && a.Attribute.EntityTypeQualifierValue == entityTypeQualifier) .Where(a => a.Value == "True") .Select(a => a.EntityId); var qry = htmlContentService.Queryable().Where(a => attributeValueQry.Contains(a.BlockId)); // Filter by approved/unapproved if (ddlApprovedFilter.SelectedIndex > -1) { if (ddlApprovedFilter.SelectedValue.ToLower() == "unapproved") { qry = qry.Where(a => a.IsApproved == false); } else if (ddlApprovedFilter.SelectedValue.ToLower() == "approved") { qry = qry.Where(a => a.IsApproved == true); } } // Filter by the person that approved the content if (_canApprove) { int?personId = gContentListFilter.GetUserPreference("Approved By").AsIntegerOrNull(); if (personId.HasValue) { qry = qry.Where(a => a.ApprovedByPersonAliasId.HasValue && a.ApprovedByPersonAlias.PersonId == personId); } } SortProperty sortProperty = gContentList.SortProperty; if (sortProperty != null) { qry = qry.Sort(sortProperty); } else { qry = qry.OrderByDescending(a => a.ModifiedDateTime); } var selectQry = qry.Select(a => new { a.Id, SiteName = a.Block.PageId.HasValue ? a.Block.Page.Layout.Site.Name : a.Block.Layout.Site.Name, PageName = a.Block.Page.InternalName, a.ModifiedDateTime, a.IsApproved, ApprovedDateTime = a.IsApproved ? a.ApprovedDateTime : null, ApprovedByPerson = a.IsApproved ? a.ApprovedByPersonAlias.Person : null, BlockPageId = a.Block.PageId, BlockLayoutId = a.Block.LayoutId, }); // Filter by Site if (ddlSiteFilter.SelectedIndex > 0) { if (ddlSiteFilter.SelectedValue.ToLower() != "all") { selectQry = selectQry.Where(h => h.SiteName == ddlSiteFilter.SelectedValue); } } gContentList.DataSource = selectQry.ToList(); gContentList.DataBind(); }
/// <summary> /// Adds the page nodes. /// </summary> /// <param name="page">The page.</param> /// <param name="expandedPageIdList">The expanded page identifier list.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> protected string PageNode(Page page, List <int> expandedPageIdList, RockContext rockContext) { var sb = new StringBuilder(); string pageSearch = this.PageParameter("pageSearch"); bool isSelected = false; if (!string.IsNullOrWhiteSpace(pageSearch)) { isSelected = page.InternalName.IndexOf(pageSearch, StringComparison.OrdinalIgnoreCase) >= 0; } bool isExpanded = expandedPageIdList.Contains(page.Id); sb.AppendFormat("<li data-expanded='{4}' data-model='Page' data-id='p{0}'><span><i class=\"fa fa-file-o\"> </i> <a href='{1}'>{2}</a></span>{3}", page.Id, new PageReference(page.Id).BuildUrl(), isSelected ? "<strong>" + page.InternalName + "</strong>" : page.InternalName, Environment.NewLine, isExpanded.ToString().ToLower()); if (page.Pages.Any() || page.Blocks.Any()) { sb.AppendLine("<ul>"); foreach (var childPage in page.Pages.OrderBy(a => a.Order).ThenBy(a => a.InternalName)) { sb.Append(PageNode(childPage, expandedPageIdList, rockContext)); } foreach (var block in page.Blocks.OrderBy(b => b.Order)) { sb.AppendFormat("<li data-expanded='false' data-model='Block' data-id='b{0}'><span>{1}{2}:{3}</span></li>{4}", block.Id, CreateConfigIcon(block), block.Name, BlockTypeCache.Read(block.BlockTypeId, rockContext).Name, Environment.NewLine); } sb.AppendLine("</ul>"); } sb.AppendLine("</li>"); return(sb.ToString()); }
/// <summary> /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event. /// </summary> /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); List <int> expandedPageIds = new List <int>(); RockContext rockContext = new RockContext(); PageService pageService = new PageService(rockContext); var allPages = pageService.Queryable("PageContexts, PageRoutes"); foreach (var page in allPages) { PageCache.Read(page); } foreach (var block in new BlockService(rockContext).Queryable()) { BlockCache.Read(block); } foreach (var blockType in new BlockTypeService(rockContext).Queryable()) { BlockTypeCache.Read(blockType); } if (Page.IsPostBack) { foreach (string expandedId in hfExpandedIds.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { int id = 0; if (expandedId.StartsWith("p") && expandedId.Length > 1) { if (int.TryParse(expandedId.Substring(1), out id)) { expandedPageIds.Add(id); } } } } else { string pageSearch = this.PageParameter("pageSearch"); if (!string.IsNullOrWhiteSpace(pageSearch)) { foreach (Page page in pageService.Queryable().Where(a => a.InternalName.IndexOf(pageSearch) >= 0)) { Page selectedPage = page; while (selectedPage != null) { selectedPage = selectedPage.ParentPage; if (selectedPage != null) { expandedPageIds.Add(selectedPage.Id); } } } } } var sb = new StringBuilder(); sb.AppendLine("<ul id=\"treeview\">"); string rootPage = GetAttributeValue("RootPage"); if (!string.IsNullOrEmpty(rootPage)) { Guid pageGuid = rootPage.AsGuid(); allPages = allPages.Where(a => a.ParentPage.Guid == pageGuid); } else { allPages = allPages.Where(a => a.ParentPageId == null); } foreach (var page in allPages.OrderBy(a => a.Order).ThenBy(a => a.InternalName).Include(a => a.Blocks).ToList()) { sb.Append(PageNode(PageCache.Read(page), expandedPageIds, rockContext)); } sb.AppendLine("</ul>"); lPages.Text = sb.ToString(); }
/// <summary> /// Adds the page nodes. /// </summary> /// <param name="page">The page.</param> /// <param name="expandedPageIdList">The expanded page identifier list.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> protected string PageNode(PageCache page, List <int> expandedPageIdList, RockContext rockContext) { var sb = new StringBuilder(); string pageSearch = this.PageParameter("pageSearch"); bool isSelected = false; if (!string.IsNullOrWhiteSpace(pageSearch)) { isSelected = page.InternalName.IndexOf(pageSearch, StringComparison.OrdinalIgnoreCase) >= 0; } bool isExpanded = expandedPageIdList.Contains(page.Id); var authRoles = Authorization.AuthRules(EntityTypeCache.Read <Rock.Model.Page>().Id, page.Id, Authorization.VIEW); string authHtml = string.Empty; if (authRoles.Any()) { authHtml += string.Format( " <i class=\"fa fa-lock\"> </i>{0}", authRoles.Select(a => "<span class=\"badge badge-" + (a.AllowOrDeny == 'A' ? "success" : "danger") + "\">" + a.DisplayName + "</span>").ToList().AsDelimited(" ")); } int pageEntityTypeId = EntityTypeCache.Read("Rock.Model.Page").Id; sb.AppendFormat( "<li data-expanded='{4}' data-model='Page' data-id='p{0}'><span><span class='rollover-container'><i class=\"fa fa-file-o\"> </i><a href='{1}'>{2}</a> {6} {7}<span class='js-auth-roles hidden'>{5}</span></span></span>{3}", page.Id, // 0 new PageReference(page.Id).BuildUrl(), // 1 isSelected ? "<strong>" + page.InternalName + "</strong>" : page.InternalName, // 2 Environment.NewLine, // 3 isExpanded.ToString().ToLower(), // 4 authHtml, // 5 CreatePageConfigIcon(page), // 6 CreateSecurityIcon(pageEntityTypeId, page.Id, page.InternalName) // 7 ); var childPages = page.GetPages(rockContext); if (childPages.Any() || page.Blocks.Any()) { sb.AppendLine("<ul>"); foreach (var childPage in childPages.OrderBy(a => a.Order).ThenBy(a => a.InternalName)) { sb.Append(PageNode(childPage, expandedPageIdList, rockContext)); } foreach (var block in page.Blocks.OrderBy(b => b.Order)) { string blockName = block.Name; string blockCacheName = BlockTypeCache.Read(block.BlockTypeId, rockContext).Name; if (blockName != blockCacheName) { blockName = blockName + " (" + blockCacheName + ")"; } int blockEntityTypeId = EntityTypeCache.Read("Rock.Model.Block").Id; sb.AppendFormat("<li data-expanded='false' data-model='Block' data-id='b{0}'><span><span class='rollover-container'> <i class='fa fa-th-large'> </i> {2} {1} {4}</span></span></li>{3}", block.Id, // 0 CreateConfigIcon(block), // 1 blockName, // 2 Environment.NewLine, //3 CreateSecurityIcon(blockEntityTypeId, block.Id, block.Name) // 4 ); } sb.AppendLine("</ul>"); } sb.AppendLine("</li>"); return(sb.ToString()); }