/// <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;
var rockContext = new RockContext();
GroupTypeService groupTypeService = new GroupTypeService( rockContext );
GroupTypeRoleService groupTypeRoleService = new GroupTypeRoleService( rockContext );
AttributeService attributeService = new AttributeService( rockContext );
AttributeQualifierService qualifierService = new AttributeQualifierService( rockContext );
CategoryService categoryService = new CategoryService( rockContext );
GroupScheduleExclusionService scheduleExclusionService = new GroupScheduleExclusionService( rockContext );
int groupTypeId = int.Parse( hfGroupTypeId.Value );
if ( groupTypeId == 0 )
{
groupType = new GroupType();
groupTypeService.Add( groupType );
}
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 );
}
}
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 );
}
ScheduleType allowedScheduleTypes = ScheduleType.None;
foreach( ListItem li in cblScheduleTypes.Items )
{
if ( li.Selected )
{
allowedScheduleTypes = allowedScheduleTypes | (ScheduleType)li.Value.AsInteger();
}
}
GroupLocationPickerMode locationSelectionMode = GroupLocationPickerMode.None;
foreach ( ListItem li in cblLocationSelectionModes.Items )
{
if ( li.Selected )
{
locationSelectionMode = locationSelectionMode | (GroupLocationPickerMode)li.Value.AsInteger();
}
}
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.SendAttendanceReminder = cbSendAttendanceReminder.Checked;
groupType.AttendanceRule = ddlAttendanceRule.SelectedValueAsEnum<AttendanceRule>();
groupType.AttendancePrintTo = ddlPrintTo.SelectedValueAsEnum<PrintTo>();
groupType.AllowedScheduleTypes = allowedScheduleTypes;
groupType.LocationSelectionMode = locationSelectionMode;
groupType.GroupTypePurposeValueId = ddlGroupTypePurpose.SelectedValueAsInt();
groupType.AllowMultipleLocations = cbAllowMultipleLocations.Checked;
groupType.InheritedGroupTypeId = gtpInheritedGroupType.SelectedGroupTypeId;
groupType.EnableLocationSchedules = cbEnableLocationSchedules.Checked;
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 );
}
}
// Delete any removed exclusions
foreach ( var exclusion in groupType.GroupScheduleExclusions.Where( s => !ScheduleExclusionDictionary.Keys.Contains( s.Guid ) ).ToList() )
{
groupType.GroupScheduleExclusions.Remove( exclusion );
scheduleExclusionService.Delete( exclusion );
}
// Update exclusions
foreach( var keyVal in ScheduleExclusionDictionary )
{
var scheduleExclusion = groupType.GroupScheduleExclusions
.FirstOrDefault( s => s.Guid.Equals( keyVal.Key));
if ( scheduleExclusion == null )
{
scheduleExclusion = new GroupScheduleExclusion();
groupType.GroupScheduleExclusions.Add( scheduleExclusion);
}
scheduleExclusion.StartDate = keyVal.Value.Start;
scheduleExclusion.EndDate = keyVal.Value.End;
}
DefinedValueService definedValueService = new DefinedValueService( rockContext );
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;
}
// need WrapTransaction due to Attribute saves
rockContext.WrapTransaction( () =>
{
rockContext.SaveChanges();
/* Save Attributes */
string qualifierValue = groupType.Id.ToString();
SaveAttributes( new GroupType().TypeId, "Id", qualifierValue, GroupTypeAttributesState, rockContext );
SaveAttributes( new Group().TypeId, "GroupTypeId", qualifierValue, GroupAttributesState, rockContext );
SaveAttributes( new GroupMember().TypeId, "GroupTypeId", qualifierValue, GroupMemberAttributesState, rockContext );
// 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();
}
rockContext.SaveChanges();
// Reload the roles and apply their attribute values
foreach ( var role in groupTypeRoleService.GetByGroupTypeId( groupType.Id ).ToList() )
{
role.LoadAttributes( rockContext );
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 ) );
}
role.SaveAttributeValues( rockContext );
}
}
} );
GroupTypeCache.Flush( groupType.Id );
NavigateToParentPage();
}