private void LoadBlockTypes() { using (new Rock.Data.UnitOfWorkScope()) { Rock.Model.BlockTypeService blockTypeService = new Rock.Model.BlockTypeService(); // Add any unregistered blocks foreach (Rock.Model.BlockType blockType in blockTypeService.GetUnregisteredBlocks(Request.MapPath("~"))) { try { Control control = LoadControl(blockType.Path); if (control is Rock.Web.UI.RockBlock) { blockType.Name = Path.GetFileNameWithoutExtension(blockType.Path); // Split the name on intercapped changes (ie, "HelloWorld" becomes "Hello World") blockType.Name = System.Text.RegularExpressions.Regex.Replace(blockType.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); blockType.Description = blockType.Path; blockTypeService.Add(blockType, CurrentPersonId); blockTypeService.Save(blockType, CurrentPersonId); } } catch { } } ddlBlockType.DataSource = blockTypeService.Queryable().OrderBy(b => b.Name).ToList(); ddlBlockType.DataTextField = "Name"; ddlBlockType.DataValueField = "Id"; ddlBlockType.DataBind(); } }
/// <summary> /// Loads the block types. /// </summary> private void LoadBlockTypes(bool registerBlockTypes) { if (registerBlockTypes) { // Add any unregistered blocks try { BlockTypeService.RegisterBlockTypes(Request.MapPath("~"), Page); } catch (Exception ex) { nbMessage.Text = "Error registering one or more block types"; nbMessage.Details = ex.Message + "<code>" + HttpUtility.HtmlEncode(ex.StackTrace) + "</code>"; nbMessage.Visible = true; } } // Load the block types using (var rockContext = new RockContext()) { 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 != "") .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 == "") .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); } } }
/// <summary> /// Loads the block types. /// </summary> private void LoadBlockTypes() { using (new Rock.Data.UnitOfWorkScope()) { Rock.Model.BlockTypeService blockTypeService = new Rock.Model.BlockTypeService(); // Add any unregistered blocks blockTypeService.RegisterBlockTypes(Request.MapPath("~"), Page, CurrentPersonId); ddlBlockType.DataSource = blockTypeService.Queryable().OrderBy(b => b.Name).ToList(); ddlBlockType.DataTextField = "Name"; ddlBlockType.DataValueField = "Id"; ddlBlockType.DataBind(); } }
/// <summary> /// Loads the block types. /// </summary> private void LoadBlockTypes() { using (new Rock.Data.UnitOfWorkScope()) { Rock.Model.BlockTypeService blockTypeService = new Rock.Model.BlockTypeService(); // Add any unregistered blocks blockTypeService.RegisterBlockTypes(Request.MapPath("~"), Page, CurrentPersonId); // Load the block types var blockTypes = blockTypeService.Queryable() .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 != "") .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 == "") .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); } } }
/// <summary> /// Loads the block types. /// </summary> private void LoadBlockTypes( bool registerBlockTypes ) { if ( registerBlockTypes ) { // Add any unregistered blocks try { BlockTypeService.RegisterBlockTypes( Request.MapPath( "~" ), Page ); } catch ( Exception ex ) { nbMessage.Text = "Error registering one or more block types"; nbMessage.Details = ex.Message + "<code>" + HttpUtility.HtmlEncode( ex.StackTrace ) + "</code>"; nbMessage.Visible = true; } } // Load the block types using ( var rockContext = new RockContext() ) { 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 != "" ) .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 == "" ) .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 ); } } }
/// <summary> /// Registers any block types that are not currently registered in Rock. /// </summary> /// <param name="physWebAppPath">A <see cref="System.String" /> containing the physical path to Rock on the server.</param> /// <param name="page">The <see cref="System.Web.UI.Page" />.</param> /// <param name="refreshAll">if set to <c>true</c> will refresh name, category, and description for all block types (not just the new ones)</param> public static void RegisterBlockTypes( string physWebAppPath, System.Web.UI.Page page, bool refreshAll = false) { // Dictionary for block types. Key is path, value is friendly name var list = new Dictionary<string, string>(); // Find all the blocks in the Blocks folder... FindAllBlocksInPath( physWebAppPath, list, "Blocks" ); // Now do the exact same thing for the Plugins folder... FindAllBlocksInPath( physWebAppPath, list, "Plugins" ); // Get a list of the BlockTypes already registered (via the path) var rockContext = new RockContext(); var blockTypeService = new BlockTypeService( rockContext ); var registered = blockTypeService.Queryable().ToList(); // for each BlockType foreach ( string path in list.Keys) { if ( refreshAll || !registered.Any( b => b.Path.Equals( path, StringComparison.OrdinalIgnoreCase ) ) ) { // Attempt to load the control try { System.Web.UI.Control control = page.LoadControl( path ); if ( control is Rock.Web.UI.RockBlock ) { var blockType = registered.FirstOrDefault( b => b.Path.Equals( path, StringComparison.OrdinalIgnoreCase ) ); if ( blockType == null ) { // Create new BlockType record and save it blockType = new BlockType(); blockType.Path = path; blockTypeService.Add( blockType ); } Type controlType = control.GetType(); // Update Name, Category, and Description based on block's attribute definitions blockType.Name = Rock.Reflection.GetDisplayName( controlType ) ?? string.Empty; if ( string.IsNullOrWhiteSpace( blockType.Name ) ) { // Parse the relative path to get the name var nameParts = list[path].Split( '/' ); for ( int i = 0; i < nameParts.Length; i++ ) { if ( i == nameParts.Length - 1 ) { nameParts[i] = Path.GetFileNameWithoutExtension( nameParts[i] ); } nameParts[i] = nameParts[i].SplitCase(); } blockType.Name = string.Join( " > ", nameParts ); } if ( blockType.Name.Length > 100 ) { blockType.Name = blockType.Name.Truncate( 100 ); } blockType.Category = Rock.Reflection.GetCategory( controlType ) ?? string.Empty; blockType.Description = Rock.Reflection.GetDescription( controlType ) ?? string.Empty; } } catch ( Exception ex ) { ExceptionLogService.LogException( new Exception( string.Format("Problem processing block with path '{0}'.", path ), ex ), null ); } } } rockContext.SaveChanges(); }
/// <summary> /// Registers any block types that are not currently registered in Rock. /// </summary> /// <param name="physWebAppPath">A <see cref="System.String" /> containing the physical path to Rock on the server.</param> /// <param name="page">The <see cref="System.Web.UI.Page" />.</param> /// <param name="refreshAll">if set to <c>true</c> will refresh name, category, and description for all block types (not just the new ones)</param> public static void RegisterBlockTypes(string physWebAppPath, System.Web.UI.Page page, bool refreshAll = false) { // Dictionary for block types. Key is path, value is friendly name var list = new Dictionary <string, string>(); RegisterEntityBlockTypes(refreshAll); // Find all the blocks in the Blocks folder... FindAllBlocksInPath(physWebAppPath, list, "Blocks"); // Now do the exact same thing for the Plugins folder... FindAllBlocksInPath(physWebAppPath, list, "Plugins"); // Get a list of the BlockTypes already registered (via the path) List <string> registeredPaths; if (refreshAll) { FlushRegistrationCache(); registeredPaths = new List <string>(); } else { using (var rockContext = new RockContext()) { registeredPaths = new BlockTypeService(rockContext) .Queryable().AsNoTracking() .Where(b => !string.IsNullOrEmpty(b.Path)) .Select(b => b.Path) .ToList(); } } // Get the Block Entity Type int?blockEntityTypeId = EntityTypeCache.Get(typeof(Block)).Id; // for each BlockType foreach (string path in list.Keys) { // If the block has been previously processed or successfully registered, ignore it. if (_processedBlockPaths.Any(b => b.Equals(path, StringComparison.OrdinalIgnoreCase)) || registeredPaths.Any(b => b.Equals(path, StringComparison.OrdinalIgnoreCase))) { continue; } // Store the block path in the list of processed blocks to avoid re-processing if the registration fails. lock ( _processedBlockPathsLock ) { _processedBlockPaths.Add(path); } // Attempt to load the control try { var blockCompiledType = System.Web.Compilation.BuildManager.GetCompiledType(path); if (blockCompiledType != null && typeof(Web.UI.RockBlock).IsAssignableFrom(blockCompiledType)) { using (var rockContext = new RockContext()) { var blockTypeService = new BlockTypeService(rockContext); var blockType = blockTypeService.Queryable() .FirstOrDefault(b => b.Path == path); if (blockType == null) { // Create new BlockType record and save it blockType = new BlockType(); blockType.Path = path; blockTypeService.Add(blockType); } Type controlType = blockCompiledType; // Update Name, Category, and Description based on block's attribute definitions blockType.Name = Reflection.GetDisplayName(controlType) ?? string.Empty; if (string.IsNullOrWhiteSpace(blockType.Name)) { // Parse the relative path to get the name var nameParts = list[path].Split('/'); for (int i = 0; i < nameParts.Length; i++) { if (i == nameParts.Length - 1) { nameParts[i] = Path.GetFileNameWithoutExtension(nameParts[i]); } nameParts[i] = nameParts[i].SplitCase(); } blockType.Name = string.Join(" > ", nameParts); } if (blockType.Name.Length > 100) { blockType.Name = blockType.Name.Truncate(100); } blockType.Category = Rock.Reflection.GetCategory(controlType) ?? string.Empty; blockType.Description = Rock.Reflection.GetDescription(controlType) ?? string.Empty; rockContext.SaveChanges(); // Update the attributes used by the block Rock.Attribute.Helper.UpdateAttributes(controlType, blockEntityTypeId, "BlockTypeId", blockType.Id.ToString(), rockContext); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"RegisterBlockTypes failed for {path} with exception: {ex.Message}"); ExceptionLogService.LogException(new Exception(string.Format("Problem processing block with path '{0}'.", path), ex), null); } } }
/// <summary> /// Registers any entity-based block types that are not currently registered in Rock. /// </summary> /// <param name="refreshAll">if set to <c>true</c> will refresh name, category, and description for all block types (not just the new ones)</param> private static void RegisterEntityBlockTypes(bool refreshAll = false) { var rockBlockTypes = Reflection.FindTypes(typeof(Blocks.IRockBlockType)); List <Type> registeredTypes; using (var rockContext = new RockContext()) { registeredTypes = new BlockTypeService(rockContext) .Queryable().AsNoTracking() .Where(b => b.EntityTypeId.HasValue && !string.IsNullOrEmpty(b.EntityType.AssemblyName)) .ToList() .Select(b => Type.GetType(b.EntityType.AssemblyName, false)) .Where(b => b != null) .ToList(); } // Get the Block Entity Type int?blockEntityTypeId = EntityTypeCache.Get(typeof(Block)).Id; // for each BlockType foreach (var type in rockBlockTypes.Values) { if (refreshAll || !registeredTypes.Any(t => t == type)) { // Attempt to load the control try { using (var rockContext = new RockContext()) { var entityTypeId = EntityTypeCache.Get(type, true, rockContext).Id; var blockTypeService = new BlockTypeService(rockContext); var blockType = blockTypeService.Queryable() .FirstOrDefault(b => b.EntityTypeId == entityTypeId); if (blockType == null) { // Create new BlockType record and save it blockType = new BlockType(); blockType.EntityTypeId = entityTypeId; blockTypeService.Add(blockType); } // Update Name, Category, and Description based on block's attribute definitions blockType.Name = Reflection.GetDisplayName(type) ?? string.Empty; if (string.IsNullOrWhiteSpace(blockType.Name)) { blockType.Name = type.FullName; } if (blockType.Name.Length > 100) { blockType.Name = blockType.Name.Truncate(100); } blockType.Category = Rock.Reflection.GetCategory(type) ?? string.Empty; blockType.Description = Rock.Reflection.GetDescription(type) ?? string.Empty; rockContext.SaveChanges(); // Update the attributes used by the block Rock.Attribute.Helper.UpdateAttributes(type, blockEntityTypeId, "BlockTypeId", blockType.Id.ToString(), rockContext); } } catch (Exception ex) { Debug.WriteLine($"RegisterEntityBlockTypes failed for {type.FullName} with exception: {ex.Message}"); ExceptionLogService.LogException(new Exception(string.Format("Problem processing block with path '{0}'.", type.FullName), ex), null); } } } }
/// <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> /// Expands the files. /// </summary> /// <param name="packageFiles">The package files.</param> private void ExpandFiles( IEnumerable<IPackageFile> packageFiles ) { // Remove export.json file from the list of files to be unzipped var filesToUnzip = packageFiles.Where( f => !f.Path.Contains( "export.json" ) ).ToList(); var blockTypeService = new BlockTypeService(); var installedBlockTypes = blockTypeService.Queryable(); var webRoot = HttpContext.Current.Server.MapPath( "~" ); // Compare the packages files with currently installed block types, removing anything that already exists foreach ( var blockType in installedBlockTypes ) { var blockFileName = blockType.Path.Substring( blockType.Path.LastIndexOf( "/", StringComparison.InvariantCultureIgnoreCase ) ); blockFileName = blockFileName.Replace( '/', Path.DirectorySeparatorChar ); filesToUnzip.RemoveAll( f => f.Path.Contains( blockFileName ) ); } foreach ( var packageFile in filesToUnzip ) { var path = Path.Combine( webRoot, packageFile.EffectivePath ); var file = new FileInfo( path ); // Err on the side of not being destructive for now. Consider refactoring to give user a choice // on whether or not to overwrite these files. if ( file.Exists ) { WarningMessages.Add( string.Format( "Skipping '{0}', found duplicate file at '{1}'.", file.Name, path ) ); continue; } // Write each file out to disk using ( var fileStream = new FileStream( path, FileMode.Create ) ) { var stream = packageFile.GetStream(); var bytes = stream.ReadAllBytes(); fileStream.Write( bytes, 0, bytes.Length ); stream.Close(); } } }
/// <summary> /// Registers any block types that are not currently registered in Rock. /// </summary> /// <param name="physWebAppPath">A <see cref="System.String" /> containing the physical path to Rock on the server.</param> /// <param name="page">The <see cref="System.Web.UI.Page" />.</param> /// <param name="refreshAll">if set to <c>true</c> will refresh name, category, and description for all block types (not just the new ones)</param> public static void RegisterBlockTypes( string physWebAppPath, System.Web.UI.Page page, bool refreshAll = false) { // Dictionary for block types. Key is path, value is friendly name var list = new Dictionary<string, string>(); // Find all the blocks in the Blocks folder... FindAllBlocksInPath( physWebAppPath, list, "Blocks" ); // Now do the exact same thing for the Plugins folder... FindAllBlocksInPath( physWebAppPath, list, "Plugins" ); // Get a list of the BlockTypes already registered (via the path) var rockContext = new RockContext(); var blockTypeService = new BlockTypeService( rockContext ); var registered = blockTypeService.Queryable().ToList(); // for each BlockType foreach ( string path in list.Keys) { if ( refreshAll || !registered.Any( b => b.Path.Equals( path, StringComparison.OrdinalIgnoreCase ) ) ) { // Attempt to load the control try { System.Web.UI.Control control = page.LoadControl( path ); if ( control is Rock.Web.UI.RockBlock ) { var blockType = registered.FirstOrDefault( b => b.Path.Equals( path, StringComparison.OrdinalIgnoreCase ) ); if ( blockType == null ) { // Create new BlockType record and save it blockType = new BlockType(); blockType.Path = path; blockTypeService.Add( blockType ); } Type controlType = control.GetType(); // Update Name, Category, and Description based on block's attribute definitions blockType.Name = Rock.Reflection.GetDisplayName( controlType ) ?? string.Empty; if ( string.IsNullOrWhiteSpace( blockType.Name ) ) { // Parse the relative path to get the name var nameParts = list[path].Split( '/' ); for ( int i = 0; i < nameParts.Length; i++ ) { if ( i == nameParts.Length - 1 ) { nameParts[i] = Path.GetFileNameWithoutExtension( nameParts[i] ); } nameParts[i] = nameParts[i].SplitCase(); } blockType.Name = string.Join( " > ", nameParts ); } if ( blockType.Name.Length > 100 ) { blockType.Name = blockType.Name.Truncate( 100 ); } blockType.Category = Rock.Reflection.GetCategory( controlType ) ?? string.Empty; blockType.Description = Rock.Reflection.GetDescription( controlType ) ?? string.Empty; } } catch ( Exception ex ) { ExceptionLogService.LogException( new Exception( string.Format("Problem processing block with path '{0}'.", path ), ex ), null ); } } } rockContext.SaveChanges(); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { BlockTypeService blockTypeService = new BlockTypeService(); SortProperty sortProperty = gBlockTypes.SortProperty; if ( sortProperty != null ) { gBlockTypes.DataSource = blockTypeService.Queryable().Sort( sortProperty ).ToList(); } else { gBlockTypes.DataSource = blockTypeService.Queryable().OrderBy( b => b.Name ).ToList(); } gBlockTypes.DataBind(); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { BlockTypeService blockTypeService = new BlockTypeService( new RockContext() ); SortProperty sortProperty = gBlockTypes.SortProperty; var blockTypes = blockTypeService.Queryable(); // Exclude system blocks if checked. if ( !string.IsNullOrWhiteSpace( gfSettings.GetUserPreference("Exclude System") ) ) { blockTypes = blockTypes.Where( b => b.IsSystem == false ); } // Filter by Name string nameFilter = gfSettings.GetUserPreference( "Name" ); if ( !string.IsNullOrEmpty( nameFilter.Trim() ) ) { blockTypes = blockTypes.Where( b => b.Name.Contains( nameFilter.Trim() ) ); } // Filter by Path string path = gfSettings.GetUserPreference( "Path" ); if ( !string.IsNullOrEmpty( path.Trim() ) ) { blockTypes = blockTypes.Where( b => b.Path.Contains( path.Trim() ) ); } string category = gfSettings.GetUserPreference( "Category" ); if (!string.IsNullOrWhiteSpace(category)) { blockTypes = blockTypes.Where( b => b.Category == category ); } var selectQry = blockTypes.Select( a => new { a.Id, a.Name, a.Category, a.Description, a.Path, BlocksCount = a.Blocks.Count(), a.IsSystem } ); if ( sortProperty != null ) { if ( sortProperty.Property == "Status" ) { // special case: See if the file exists and sort by that if ( sortProperty.Direction == System.Web.UI.WebControls.SortDirection.Ascending ) { gBlockTypes.DataSource = selectQry.ToList().OrderBy( a => System.IO.File.Exists( Request.MapPath( a.Path ) ) ).ToList(); } else { gBlockTypes.DataSource = selectQry.ToList().OrderBy( a => !System.IO.File.Exists( Request.MapPath( a.Path ) ) ).ToList(); } } else { gBlockTypes.DataSource = selectQry.Sort( sortProperty ).ToList(); } } else { gBlockTypes.DataSource = selectQry.OrderBy( b => b.Name ).ToList(); } gBlockTypes.DataBind(); }
/// <summary> /// Loads the block types. /// </summary> private void LoadBlockTypes() { // Add any unregistered blocks BlockTypeService.RegisterBlockTypes( Request.MapPath( "~" ), Page ); // Load the block types Rock.Model.BlockTypeService blockTypeService = new Rock.Model.BlockTypeService( new RockContext() ); var blockTypes = blockTypeService.Queryable() .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 != "" ) .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 == "" ) .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 ); } }
private void LoadBlockTypes() { using ( new Rock.Data.UnitOfWorkScope() ) { Rock.Model.BlockTypeService blockTypeService = new Rock.Model.BlockTypeService(); // Add any unregistered blocks foreach ( Rock.Model.BlockType blockType in blockTypeService.GetUnregisteredBlocks( Request.MapPath( "~" ) ) ) { try { Control control = LoadControl( blockType.Path ); if ( control is Rock.Web.UI.RockBlock ) { blockType.Name = Path.GetFileNameWithoutExtension( blockType.Path ); // Split the name on intercapped changes (ie, "HelloWorld" becomes "Hello World") blockType.Name = System.Text.RegularExpressions.Regex.Replace( blockType.Name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 " ); blockType.Description = blockType.Path; blockTypeService.Add( blockType, CurrentPersonId ); blockTypeService.Save( blockType, CurrentPersonId ); } } catch { } } ddlBlockType.DataSource = blockTypeService.Queryable().OrderBy( b => b.Name).ToList(); ddlBlockType.DataTextField = "Name"; ddlBlockType.DataValueField = "Id"; ddlBlockType.DataBind(); } }