/// <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 hasValidationErrors = false; using ( new UnitOfWorkScope() ) { GroupTypeService groupTypeService = new GroupTypeService(); GroupService groupService = new GroupService(); AttributeService attributeService = new AttributeService(); int parentGroupTypeId = hfParentGroupTypeId.ValueAsInt(); var groupTypeUIList = new List<GroupType>(); foreach ( var checkinGroupTypeEditor in phCheckinGroupTypes.Controls.OfType<CheckinGroupTypeEditor>().ToList() ) { var groupType = checkinGroupTypeEditor.GetCheckinGroupType(); groupTypeUIList.Add( groupType ); } var groupTypeDBList = new List<GroupType>(); var groupTypesToDelete = new List<GroupType>(); var groupsToDelete = new List<Group>(); var groupTypesToAddUpdate = new List<GroupType>(); var groupsToAddUpdate = new List<Group>(); GroupType parentGroupTypeDB = groupTypeService.Get( parentGroupTypeId ); GroupType parentGroupTypeUI = parentGroupTypeDB.Clone( false ); parentGroupTypeUI.ChildGroupTypes = groupTypeUIList; PopulateDeleteLists( groupTypesToDelete, groupsToDelete, parentGroupTypeDB, parentGroupTypeUI ); PopulateAddUpdateLists( groupTypesToAddUpdate, groupsToAddUpdate, parentGroupTypeUI ); int binaryFileFieldTypeID = new FieldTypeService().Get( new Guid( Rock.SystemGuid.FieldType.BINARY_FILE ) ).Id; int binaryFileTypeId = new BinaryFileTypeService().Get( new Guid( Rock.SystemGuid.BinaryFiletype.CHECKIN_LABEL ) ).Id; RockTransactionScope.WrapTransaction( () => { // delete in reverse order to get deepest child items first groupsToDelete.Reverse(); foreach ( var groupToDelete in groupsToDelete ) { groupService.Delete( groupToDelete, this.CurrentPersonId ); groupService.Save( groupToDelete, this.CurrentPersonId ); } // delete in reverse order to get deepest child items first groupTypesToDelete.Reverse(); foreach ( var groupTypeToDelete in groupTypesToDelete ) { groupTypeService.Delete( groupTypeToDelete, this.CurrentPersonId ); groupTypeService.Save( groupTypeToDelete, this.CurrentPersonId ); } // Add/Update grouptypes and groups that are in the UI // Note: We'll have to save all the groupTypes without changing the DB value of ChildGroupTypes, then come around again and save the ChildGroupTypes // since the ChildGroupTypes may not exist in the database yet foreach ( GroupType groupTypeUI in groupTypesToAddUpdate ) { GroupType groupTypeDB = groupTypeService.Get( groupTypeUI.Guid ); if ( groupTypeDB == null ) { groupTypeDB = new GroupType(); groupTypeDB.Id = 0; groupTypeDB.Guid = groupTypeUI.Guid; } groupTypeDB.Name = groupTypeUI.Name; groupTypeDB.Order = groupTypeUI.Order; groupTypeDB.InheritedGroupTypeId = groupTypeUI.InheritedGroupTypeId; groupTypeDB.Attributes = groupTypeUI.Attributes; groupTypeDB.AttributeValues = groupTypeUI.AttributeValues; if ( groupTypeDB.Id == 0 ) { groupTypeService.Add( groupTypeDB, this.CurrentPersonId ); } if ( !groupTypeDB.IsValid ) { hasValidationErrors = true; CheckinGroupTypeEditor groupTypeEditor = phCheckinGroupTypes.ControlsOfTypeRecursive<CheckinGroupTypeEditor>().First( a => a.GroupTypeGuid == groupTypeDB.Guid ); groupTypeEditor.ForceContentVisible = true; return; } groupTypeService.Save( groupTypeDB, this.CurrentPersonId ); Rock.Attribute.Helper.SaveAttributeValues( groupTypeDB, this.CurrentPersonId ); // get fresh from database to make sure we have Id so we can update the CheckinLabel Attributes groupTypeDB = groupTypeService.Get( groupTypeDB.Guid ); // rebuild the CheckinLabel attributes from the UI (brute-force) foreach ( var labelAttributeDB in CheckinGroupTypeEditor.GetCheckinLabelAttributes( groupTypeDB ) ) { var attribute = attributeService.Get( labelAttributeDB.Value.Guid ); Rock.Web.Cache.AttributeCache.Flush( attribute.Id ); attributeService.Delete( attribute, this.CurrentPersonId ); } foreach ( var checkinLabelAttributeInfo in GroupTypeCheckinLabelAttributesState[groupTypeUI.Guid] ) { var attribute = new Rock.Model.Attribute(); attribute.AttributeQualifiers.Add( new AttributeQualifier { Key = "binaryFileType", Value = binaryFileTypeId.ToString() } ); attribute.Guid = Guid.NewGuid(); attribute.FieldTypeId = binaryFileFieldTypeID; attribute.EntityTypeId = EntityTypeCache.GetId( typeof( GroupType ) ); attribute.EntityTypeQualifierColumn = "Id"; attribute.EntityTypeQualifierValue = groupTypeDB.Id.ToString(); attribute.DefaultValue = checkinLabelAttributeInfo.BinaryFileId.ToString(); attribute.Key = checkinLabelAttributeInfo.AttributeKey; attribute.Name = checkinLabelAttributeInfo.FileName; if ( !attribute.IsValid ) { hasValidationErrors = true; CheckinGroupTypeEditor groupTypeEditor = phCheckinGroupTypes.ControlsOfTypeRecursive<CheckinGroupTypeEditor>().First( a => a.GroupTypeGuid == groupTypeDB.Guid ); groupTypeEditor.ForceContentVisible = true; return; } attributeService.Add( attribute, this.CurrentPersonId ); attributeService.Save( attribute, this.CurrentPersonId ); } } // Add/Update Groups foreach ( var groupUI in groupsToAddUpdate ) { Group groupDB = groupService.Get( groupUI.Guid ); if ( groupDB == null ) { groupDB = new Group(); groupDB.Guid = groupUI.Guid; } groupDB.Name = groupUI.Name; // delete any GroupLocations that were removed in the UI foreach ( var groupLocationDB in groupDB.GroupLocations.ToList() ) { if ( !groupUI.GroupLocations.Select( a => a.LocationId ).Contains( groupLocationDB.LocationId ) ) { groupDB.GroupLocations.Remove( groupLocationDB ); } } // add any GroupLocations that were added in the UI foreach ( var groupLocationUI in groupUI.GroupLocations ) { if ( !groupDB.GroupLocations.Select( a => a.LocationId ).Contains( groupLocationUI.LocationId ) ) { GroupLocation groupLocationDB = new GroupLocation { LocationId = groupLocationUI.LocationId }; groupDB.GroupLocations.Add( groupLocationDB ); } } groupDB.Order = groupUI.Order; // get GroupTypeId from database in case the groupType is new groupDB.GroupTypeId = groupTypeService.Get( groupUI.GroupType.Guid ).Id; groupDB.Attributes = groupUI.Attributes; groupDB.AttributeValues = groupUI.AttributeValues; if ( groupDB.Id == 0 ) { groupService.Add( groupDB, this.CurrentPersonId ); } if ( !groupDB.IsValid ) { hasValidationErrors = true; hasValidationErrors = true; CheckinGroupEditor groupEditor = phCheckinGroupTypes.ControlsOfTypeRecursive<CheckinGroupEditor>().First( a => a.GroupGuid == groupDB.Guid ); groupEditor.ForceContentVisible = true; return; } groupService.Save( groupDB, this.CurrentPersonId ); Rock.Attribute.Helper.SaveAttributeValues( groupDB, this.CurrentPersonId ); } /* now that we have all the grouptypes saved, now lets go back and save them again with the current UI ChildGroupTypes */ // save main parentGroupType with current UI ChildGroupTypes parentGroupTypeDB.ChildGroupTypes = new List<GroupType>(); parentGroupTypeDB.ChildGroupTypes.Clear(); foreach ( var childGroupTypeUI in parentGroupTypeUI.ChildGroupTypes ) { var childGroupTypeDB = groupTypeService.Get( childGroupTypeUI.Guid ); parentGroupTypeDB.ChildGroupTypes.Add( childGroupTypeDB ); } groupTypeService.Save( parentGroupTypeDB, this.CurrentPersonId ); // loop thru all the other GroupTypes in the UI and save their childgrouptypes foreach ( var groupTypeUI in groupTypesToAddUpdate ) { var groupTypeDB = groupTypeService.Get( groupTypeUI.Guid ); groupTypeDB.ChildGroupTypes = new List<GroupType>(); groupTypeDB.ChildGroupTypes.Clear(); foreach ( var childGroupTypeUI in groupTypeUI.ChildGroupTypes ) { var childGroupTypeDB = groupTypeService.Get( childGroupTypeUI.Guid ); groupTypeDB.ChildGroupTypes.Add( childGroupTypeDB ); } groupTypeService.Save( groupTypeDB, this.CurrentPersonId ); } } ); } if ( !hasValidationErrors ) { NavigateToParentPage(); } }
/// <summary> /// Handles the SaveClick event of the mdAddCheckinGroupType 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 mdAddCheckinGroupType_SaveClick( object sender, EventArgs e ) { var groupTypeService = new GroupTypeService(); GroupType groupType = new GroupType(); groupType.Name = tbGroupTypeName.Text; groupType.GroupTypePurposeValueId = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE ).Id; groupType.ShowInNavigation = false; groupType.ShowInGroupList = false; groupTypeService.Add( groupType, this.CurrentPersonId ); groupTypeService.Save( groupType, this.CurrentPersonId ); mdAddCheckinGroupType.Hide(); 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 ) { GroupType groupType; using ( new UnitOfWorkScope() ) { GroupTypeService groupTypeService = new GroupTypeService(); GroupTypeRoleService groupTypeRoleService = new GroupTypeRoleService(); AttributeService attributeService = new AttributeService(); AttributeQualifierService qualifierService = new AttributeQualifierService(); CategoryService categoryService = new CategoryService(); int groupTypeId = int.Parse( hfGroupTypeId.Value ); if ( groupTypeId == 0 ) { groupType = new GroupType(); groupTypeService.Add( groupType, CurrentPersonId ); } else { groupType = groupTypeService.Get( groupTypeId ); // selected roles var selectedRoleGuids = GroupTypeRolesState.Select( r => r.Guid ); foreach ( var role in groupType.Roles.Where( r => !selectedRoleGuids.Contains( r.Guid ) ).ToList() ) { groupType.Roles.Remove( role ); groupTypeRoleService.Delete( role, CurrentPersonId ); } } foreach ( var roleState in GroupTypeRolesState ) { GroupTypeRole role = groupType.Roles.Where( r => r.Guid == roleState.Guid ).FirstOrDefault(); if ( role == null ) { role = new GroupTypeRole(); groupType.Roles.Add( role ); } else { roleState.Id = role.Id; roleState.Guid = role.Guid; } role.CopyPropertiesFrom( roleState ); } GroupLocationPickerMode locationSelectionMode = GroupLocationPickerMode.None; foreach(ListItem li in cblLocationSelectionModes.Items) { if ( li.Selected ) { locationSelectionMode = locationSelectionMode | (GroupLocationPickerMode)li.Value.AsInteger().Value; } } groupType.Name = tbName.Text; groupType.Description = tbDescription.Text; groupType.GroupTerm = tbGroupTerm.Text; groupType.GroupMemberTerm = tbGroupMemberTerm.Text; groupType.ShowInGroupList = cbShowInGroupList.Checked; groupType.ShowInNavigation = cbShowInNavigation.Checked; groupType.IconCssClass = tbIconCssClass.Text; groupType.TakesAttendance = cbTakesAttendance.Checked; groupType.AttendanceRule = ddlAttendanceRule.SelectedValueAsEnum<AttendanceRule>(); groupType.AttendancePrintTo = ddlAttendancePrintTo.SelectedValueAsEnum<PrintTo>(); groupType.LocationSelectionMode = locationSelectionMode; groupType.GroupTypePurposeValueId = ddlGroupTypePurpose.SelectedValueAsInt(); groupType.AllowMultipleLocations = cbAllowMultipleLocations.Checked; groupType.InheritedGroupTypeId = gtpInheritedGroupType.SelectedGroupTypeId; groupType.ChildGroupTypes = new List<GroupType>(); groupType.ChildGroupTypes.Clear(); foreach ( var item in ChildGroupTypesDictionary ) { var childGroupType = groupTypeService.Get( item.Key ); if ( childGroupType != null ) { groupType.ChildGroupTypes.Add( childGroupType ); } } DefinedValueService definedValueService = new DefinedValueService(); groupType.LocationTypes = new List<GroupTypeLocationType>(); groupType.LocationTypes.Clear(); foreach ( var item in LocationTypesDictionary ) { var locationType = definedValueService.Get( item.Key ); if ( locationType != null ) { groupType.LocationTypes.Add( new GroupTypeLocationType { LocationTypeValueId = locationType.Id } ); } } if ( !groupType.IsValid ) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction( () => { groupTypeService.Save( groupType, CurrentPersonId ); /* Save Attributes */ string qualifierValue = groupType.Id.ToString(); SaveAttributes( new GroupType().TypeId, "Id", qualifierValue, GroupTypeAttributesState, attributeService, qualifierService, categoryService ); SaveAttributes( new Group().TypeId, "GroupTypeId", qualifierValue, GroupAttributesState, attributeService, qualifierService, categoryService ); SaveAttributes( new GroupMember().TypeId, "GroupTypeId", qualifierValue, GroupMemberAttributesState, attributeService, qualifierService, categoryService ); // Reload to save default role groupType = groupTypeService.Get( groupType.Id ); groupType.DefaultGroupRole = groupType.Roles.FirstOrDefault( r => r.Guid.Equals( DefaultRoleGuid ) ); if ( groupType.DefaultGroupRole == null ) { groupType.DefaultGroupRole = groupType.Roles.FirstOrDefault(); } groupTypeService.Save( groupType, CurrentPersonId ); // Reload the roles and apply their attribute values foreach ( var role in groupTypeRoleService.GetByGroupTypeId( groupType.Id ) ) { role.LoadAttributes(); var roleState = GroupTypeRolesState.Where( r => r.Guid.Equals( role.Guid ) ).FirstOrDefault(); if ( roleState != null && roleState.AttributeValues != null ) { foreach ( var attributeValue in roleState.AttributeValues ) { role.SetAttributeValue( attributeValue.Key, roleState.GetAttributeValue( attributeValue.Key ) ); } Helper.SaveAttributeValues( role, CurrentPersonId ); } } } ); GroupTypeCache.Flush( groupType.Id ); } NavigateToParentPage(); }
/// <summary> /// Handles the Delete event of the gGroupType 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 gGroupType_Delete( object sender, RowEventArgs e ) { RockTransactionScope.WrapTransaction( () => { GroupTypeService groupTypeService = new GroupTypeService(); GroupType groupType = groupTypeService.Get( e.RowKeyId ); if ( groupType != null ) { int groupTypeId = groupType.Id; if ( !groupType.IsAuthorized("Administrate", CurrentPerson)) { mdGridWarning.Show( "Sorry, you're not authorized to delete this group type.", ModalAlertType.Alert ); return; } string errorMessage; if ( !groupTypeService.CanDelete( groupType, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Alert ); return; } groupTypeService.Delete( groupType, CurrentPersonId ); groupTypeService.Save( groupType, CurrentPersonId ); GroupTypeCache.Flush( groupTypeId ); } } ); BindGrid(); }