/// <summary> /// Handles the Click event of the btnDelete 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 btnDelete_Click( object sender, EventArgs e ) { var rockContext = new RockContext(); var service = new RegistrationTemplateService( rockContext ); var registrationTemplate = service.Get( hfRegistrationTemplateId.Value.AsInteger() ); if ( registrationTemplate != null ) { if ( !UserCanEdit && !registrationTemplate.IsAuthorized( Authorization.ADMINISTRATE, this.CurrentPerson ) ) { mdDeleteWarning.Show( "You are not authorized to delete this registration template.", ModalAlertType.Information ); return; } rockContext.WrapTransaction( () => { new RegistrationService( rockContext ).DeleteRange( registrationTemplate.Instances.SelectMany( i => i.Registrations ) ); new RegistrationInstanceService( rockContext ).DeleteRange( registrationTemplate.Instances ); service.Delete( registrationTemplate ); rockContext.SaveChanges(); } ); } // reload page var qryParams = new Dictionary<string, string>(); NavigateToPage( RockPage.Guid, qryParams ); }
/// <summary> /// Deletes the registration templates. /// </summary> /// <param name="elemRegistrationTemplate">The elem registration template.</param> /// <param name="rockContext">The rock context.</param> private void DeleteRegistrationTemplates( XElement elemRegistrationTemplates, RockContext rockContext ) { if ( elemRegistrationTemplates == null ) { return; } var service = new RegistrationTemplateService( rockContext ); foreach ( var elemRegistrationTemplate in elemRegistrationTemplates.Elements( "registrationTemplate" ) ) { Guid guid = elemRegistrationTemplate.Attribute( "guid" ).Value.Trim().AsGuid(); var registrationTemplate = service.Get( guid ); rockContext.WrapTransaction( () => { if ( registrationTemplate != null ) { if ( registrationTemplate.Instances != null ) { AttributeService attributeService = new AttributeService( rockContext ); if ( registrationTemplate.Forms != null ) { foreach ( var id in registrationTemplate.Forms.SelectMany( f => f.Fields ).Where( ff => ff.FieldSource == RegistrationFieldSource.RegistrationAttribute ).Select( f => f.AttributeId ) ) { if ( id != null ) { Rock.Model.Attribute attribute = attributeService.Get( id ?? -1 ); if ( attribute != null ) { attributeService.Delete( attribute ); } } } } var registrations = registrationTemplate.Instances.SelectMany( i => i.Registrations ); new RegistrationService( rockContext ).DeleteRange( registrations ); new RegistrationInstanceService( rockContext ).DeleteRange( registrationTemplate.Instances ); } service.Delete( registrationTemplate ); rockContext.SaveChanges(); } } ); } }