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> /// 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 ) { BlockType blockType; BlockTypeService blockTypeService = new BlockTypeService(); int blockTypeId = int.Parse( hfBlockTypeId.Value ); if ( blockTypeId == 0 ) { blockType = new BlockType(); blockTypeService.Add( blockType, CurrentPersonId ); } else { BlockTypeCache.Flush( blockTypeId ); blockType = blockTypeService.Get( blockTypeId ); } blockType.Name = tbName.Text; blockType.Path = tbPath.Text; blockType.Description = tbDescription.Text; if ( !blockType.IsValid ) { // Controls will render the error messages return; } blockTypeService.Save( blockType, CurrentPersonId ); BindGrid(); pnlDetails.Visible = false; pnlList.Visible = true; }
/// <summary> /// Imports the page. /// </summary> /// <param name="uploadedPackage">Byte array of the uploaded package</param> /// <param name="fileName">File name of uploaded package</param> /// <param name="personId">Id of the Person performing the import</param> /// <param name="pageId">The Id of the Page to save new data underneath</param> /// <param name="siteId">The Id of the Site tha the Page is being imported into</param> public bool ImportPage( byte[] uploadedPackage, string fileName, int personId, int pageId, int siteId ) { // Write .nupkg file to the PackageStaging folder... var path = Path.Combine( HttpContext.Current.Server.MapPath( "~/App_Data/PackageStaging" ), fileName ); using ( var file = new FileStream( path, FileMode.Create ) ) { file.Write( uploadedPackage, 0, uploadedPackage.Length ); } var package = new ZipPackage( path ); var packageFiles = package.GetFiles().ToList(); var exportFile = packageFiles.FirstOrDefault( f => f.Path.Contains( "export.json" ) ); Page page = null; // If export.json is present, deserialize data // * Are there any new BlockTypes to register? If so, save them first. // * Scrub out any `Id` and `Guid` fields that came over from export // * Save page data via PageService if ( exportFile != null ) { string json; using ( var stream = exportFile.GetStream() ) { json = stream.ReadToEnd(); } page = Page.FromJson( json ); } // Validate package... // + Does it have any executable .dll files? Should those go to the bin folder, or into a plugins directory to be loaded via MEF? // - Does it have code or asset files that need to go on the file system? (Done) // - Does it have an export.json file? Should that be a requirement? (Done) // + Does it have any corresponding SQL, migrations, seed methods to run, etc. if ( page != null ) { // Find new block types and save them prior to scrubbing data... var newBlockTypes = FindNewBlockTypes( page, new BlockTypeService().Queryable() ).ToList(); RockTransactionScope.WrapTransaction( () => { try { var blockTypeService = new BlockTypeService(); foreach ( var blockType in newBlockTypes ) { blockTypeService.Add( blockType, personId ); blockTypeService.Save( blockType, personId ); } ValidateImportData( page, newBlockTypes ); SavePages( page, newBlockTypes, personId, pageId, siteId ); ExpandFiles( packageFiles ); } catch ( Exception e ) { ErrorMessages.Add( e.Message ); } }); // Clean up PackageStaging folder on successful import. var file = new FileInfo( path ); file.Delete(); return ErrorMessages.Count <= 0; } ErrorMessages.Add( "The export package uploaded does not appear to have any data associated with it." ); return false; }
/// <summary> /// Handles the Delete event of the gBlockTypes control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gBlockTypes_Delete( object sender, RowEventArgs e ) { BlockTypeService blockTypeService = new BlockTypeService(); BlockType blockType = blockTypeService.Get( (int)gBlockTypes.DataKeys[e.RowIndex]["id"] ); if ( CurrentBlock != null ) { blockTypeService.Delete( blockType, CurrentPersonId ); blockTypeService.Save( blockType, CurrentPersonId ); Rock.Web.Cache.BlockTypeCache.Flush( blockType.Id ); } BindGrid(); }
/// <summary> /// Scans for unregistered blocks. /// </summary> private void ScanForUnregisteredBlocks() { BlockTypeService blockTypeService = new BlockTypeService(); 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 ).SplitCase(); blockType.Description = Rock.Reflection.GetDescription( control.GetType() ) ?? string.Empty; blockTypeService.Add( blockType, CurrentPersonId ); blockTypeService.Save( blockType, CurrentPersonId ); } } catch { // } } }
/// <summary> /// Handles the Delete event of the gBlockTypes control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gBlockTypes_Delete( object sender, RowEventArgs e ) { RockTransactionScope.WrapTransaction( () => { BlockTypeService blockTypeService = new BlockTypeService(); BlockType blockType = blockTypeService.Get( (int)e.RowKeyValue ); if ( blockType != null ) { string errorMessage; if ( !blockTypeService.CanDelete( blockType, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } blockTypeService.Delete( blockType, CurrentPersonId ); blockTypeService.Save( blockType, CurrentPersonId ); Rock.Web.Cache.BlockTypeCache.Flush( blockType.Id ); } } ); BindGrid(); }
/// <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 ) { BlockType blockType; BlockTypeService blockTypeService = new BlockTypeService(); int blockTypeId = int.Parse( hfBlockTypeId.Value ); if ( blockTypeId == 0 ) { blockType = new BlockType(); blockTypeService.Add( blockType, CurrentPersonId ); } else { BlockTypeCache.Flush( blockTypeId ); blockType = blockTypeService.Get( blockTypeId ); } blockType.Name = tbName.Text; blockType.Path = tbPath.Text; blockType.Description = tbDescription.Text; if ( !blockType.IsValid ) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction( () => { blockTypeService.Save( blockType, CurrentPersonId ); } ); NavigateToParentPage(); }
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(); } }