/// <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 ) { var rockContext = new RockContext(); GroupTypeService groupTypeService = new GroupTypeService( rockContext ); 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; } groupType.ParentGroupTypes.Clear(); groupType.ChildGroupTypes.Clear(); groupTypeService.Delete( groupType ); rockContext.SaveChanges(); GroupTypeCache.Flush( groupTypeId ); } BindGrid(); }
/// <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(); GroupTypeService groupTypeService = new GroupTypeService( rockContext ); GroupType groupType = groupTypeService.Get( hfGroupTypeId.Value.AsInteger() ); if ( groupType != null ) { string errorMessage; if ( !groupTypeService.CanDelete( groupType, out errorMessage ) ) { mdDeleteWarning.Show( errorMessage, ModalAlertType.Information ); return; } int groupTypeId = groupType.Id; groupType.ParentGroupTypes.Clear(); groupType.ChildGroupTypes.Clear(); groupTypeService.Delete( groupType ); rockContext.SaveChanges(); GroupTypeCache.Flush( groupTypeId ); Rock.CheckIn.KioskDevice.FlushAll(); } var pageRef = new PageReference( CurrentPageReference.PageId, CurrentPageReference.RouteId ); NavigateToPage( pageRef ); }
/// <summary> /// Gets the expression. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression( Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection ) { string[] options = selection.Split( '|' ); if ( options.Length < 4 ) { return null; } Guid groupTypeGuid = options[0].AsGuid(); ComparisonType comparisonType = options[1].ConvertToEnum<ComparisonType>( ComparisonType.GreaterThanOrEqualTo ); int? attended = options[2].AsIntegerOrNull(); string slidingDelimitedValues; if ( options[3].AsIntegerOrNull().HasValue ) { //// selection was from when it just simply a LastXWeeks instead of Sliding Date Range // Last X Weeks was treated as "LastXWeeks * 7" days, so we have to convert it to a SlidingDateRange of Days to keep consistent behavior int lastXWeeks = options[3].AsIntegerOrNull() ?? 1; var fakeSlidingDateRangePicker = new SlidingDateRangePicker(); fakeSlidingDateRangePicker.SlidingDateRangeMode = SlidingDateRangePicker.SlidingDateRangeType.Last; fakeSlidingDateRangePicker.TimeUnit = SlidingDateRangePicker.TimeUnitType.Day; fakeSlidingDateRangePicker.NumberOfTimeUnits = lastXWeeks * 7; slidingDelimitedValues = fakeSlidingDateRangePicker.DelimitedValues; } else { slidingDelimitedValues = options[3].Replace( ',', '|' ); } var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( slidingDelimitedValues ); bool includeChildGroupTypes = options.Length >= 5 ? options[4].AsBooleanOrNull() ?? false : false; var groupTypeService = new GroupTypeService( new RockContext() ); var groupType = groupTypeService.Get( groupTypeGuid ); List<int> groupTypeIds = new List<int>(); if ( groupType != null ) { groupTypeIds.Add( groupType.Id ); if ( includeChildGroupTypes ) { var childGroupTypes = groupTypeService.GetAllAssociatedDescendents( groupType.Guid ); if ( childGroupTypes.Any() ) { groupTypeIds.AddRange( childGroupTypes.Select( a => a.Id ) ); // get rid of any duplicates groupTypeIds = groupTypeIds.Distinct().ToList(); } } } var rockContext = serviceInstance.Context as RockContext; var attendanceQry = new AttendanceService( rockContext ).Queryable().Where( a => a.DidAttend.HasValue && a.DidAttend.Value ); if ( dateRange.Start.HasValue ) { var startDate = dateRange.Start.Value; attendanceQry = attendanceQry.Where( a => a.StartDateTime >= startDate ); } if ( dateRange.End.HasValue ) { var endDate = dateRange.End.Value; attendanceQry = attendanceQry.Where( a => a.StartDateTime < endDate ); } if ( groupTypeIds.Count == 1 ) { int groupTypeId = groupTypeIds[0]; attendanceQry = attendanceQry.Where( a => a.Group.GroupTypeId == groupTypeId ); } else if ( groupTypeIds.Count > 1 ) { attendanceQry = attendanceQry.Where( a => groupTypeIds.Contains( a.Group.GroupTypeId ) ); } else { // no group type selected, so return nothing return Expression.Constant( false ); } var qry = new PersonService( rockContext ).Queryable() .Where( p => attendanceQry.Where( xx => xx.PersonAlias.PersonId == p.Id ).Count() == attended ); BinaryExpression compareEqualExpression = FilterExpressionExtractor.Extract<Rock.Model.Person>( qry, parameterExpression, "p" ) as BinaryExpression; BinaryExpression result = FilterExpressionExtractor.AlterComparisonType( comparisonType, compareEqualExpression, null ); return result; }
/// <summary> /// Shows the detail. /// </summary> /// <param name="groupTypeId">The group type identifier.</param> public void ShowDetail( int groupTypeId ) { // hide the details panel until we verify the page params are good and that the user has edit access pnlDetails.Visible = false; var rockContext = new RockContext(); GroupTypeService groupTypeService = new GroupTypeService( rockContext ); GroupType parentGroupType = groupTypeService.Get( groupTypeId ); if ( parentGroupType == null ) { pnlDetails.Visible = false; return; } lCheckinAreasTitle.Text = parentGroupType.Name.FormatAsHtmlTitle(); hfParentGroupTypeId.Value = parentGroupType.Id.ToString(); nbEditModeMessage.Text = string.Empty; if ( !IsUserAuthorized( Authorization.EDIT ) ) { // this UI doesn't have a ReadOnly mode, so just show a message and keep the Detail panel hidden nbEditModeMessage.Heading = "Information"; nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( "check-in configuration" ); return; } pnlDetails.Visible = true; // limit to child group types that are not Templates int[] templateGroupTypes = new int[] { DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE).Id, DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_FILTER).Id }; List<GroupType> checkinGroupTypes = parentGroupType.ChildGroupTypes.Where( a => !templateGroupTypes.Contains( a.GroupTypePurposeValueId ?? 0 ) ).ToList(); // Load the Controls foreach ( GroupType groupType in checkinGroupTypes.OrderBy( a => a.Order ).ThenBy( a => a.Name ) ) { CreateGroupTypeEditorControls( groupType, phCheckinGroupTypes, rockContext ); } }
/// <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 ) { using ( var rockContext = new RockContext() ) { var groupTypeService = new GroupTypeService( rockContext ); GroupType groupType; if ( hfGroupTypeId.Value.AsInteger() == 0 ) { groupType = new GroupType(); groupTypeService.Add( groupType ); groupType.GroupTypePurposeValueId = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE ).Id; groupType.ShowInNavigation = false; groupType.ShowInGroupList = false; } else { groupType = groupTypeService.Get( hfGroupTypeId.Value.AsInteger() ); } groupType.Name = tbGroupTypeName.Text; groupType.Description = tbGroupTypeDescription.Text; rockContext.SaveChanges(); } mdAddEditCheckinGroupType.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; 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(); }
private void SortRows( string eventParam, string[] values ) { var groupTypeIds = new List<int>(); using ( var rockContext = new RockContext() ) { if ( eventParam.Equals( "re-order-area" ) ) { Guid groupTypeGuid = new Guid( values[0] ); int newIndex = int.Parse( values[1] ); var allRows = phRows.ControlsOfTypeRecursive<CheckinAreaRow>(); var sortedRow = allRows.FirstOrDefault( a => a.GroupTypeGuid.Equals( groupTypeGuid ) ); if ( sortedRow != null ) { Control parentControl = sortedRow.Parent; var siblingRows = allRows .Where( a => a.Parent.ClientID == sortedRow.Parent.ClientID ) .ToList(); int oldIndex = siblingRows.IndexOf( sortedRow ); var groupTypeService = new GroupTypeService( rockContext ); var groupTypes = new List<GroupType>(); foreach ( var siblingGuid in siblingRows.Select( a => a.GroupTypeGuid ) ) { var groupType = groupTypeService.Get( siblingGuid ); if ( groupType != null ) { groupTypes.Add( groupType ); groupTypeIds.Add( groupType.Id ); } } groupTypeService.Reorder( groupTypes, oldIndex, newIndex ); } } else if ( eventParam.Equals( "re-order-group" ) ) { Guid groupGuid = new Guid( values[0] ); int newIndex = int.Parse( values[1] ); var allRows = phRows.ControlsOfTypeRecursive<CheckinGroupRow>(); var sortedRow = allRows.FirstOrDefault( a => a.GroupGuid.Equals( groupGuid ) ); if ( sortedRow != null ) { Control parentControl = sortedRow.Parent; var siblingRows = allRows .Where( a => a.Parent.ClientID == sortedRow.Parent.ClientID ) .ToList(); int oldIndex = siblingRows.IndexOf( sortedRow ); var groupService = new GroupService( rockContext ); var groups = new List<Group>(); foreach ( var siblingGuid in siblingRows.Select( a => a.GroupGuid ) ) { var group = groupService.Get( siblingGuid ); if ( group != null ) { groups.Add( group ); } } groupService.Reorder( groups, oldIndex, newIndex ); } } rockContext.SaveChanges(); } foreach( int id in groupTypeIds ) { GroupTypeCache.Flush( id ); } Rock.CheckIn.KioskDevice.FlushAll(); BuildRows(); }
/// <summary> /// Sets the group type context. /// </summary> /// <param name="groupTypeGuid">The group type unique identifier.</param> /// <returns></returns> protected GroupType SetGroupTypeContext( Guid? groupTypeGuid ) { bool pageScope = GetAttributeValue( "ContextScope" ) == "Page"; var groupTypeService = new GroupTypeService( new RockContext() ); // check if a grouptype parameter exists to set var groupType = groupTypeService.Get( (Guid)groupTypeGuid ); if ( groupType == null ) { groupType = new GroupType() { Name = GetAttributeValue( "NoGroupText" ), Guid = Guid.Empty }; } RockPage.SetContextCookie( groupType, pageScope, false ); return groupType; }
protected void btnSave_Click( object sender, EventArgs e ) { hfAreaGroupClicked.Value = "true"; using ( var rockContext = new RockContext() ) { var attributeService = new AttributeService( rockContext ); if ( checkinArea.Visible ) { var groupTypeService = new GroupTypeService( rockContext ); var groupType = groupTypeService.Get( checkinArea.GroupTypeGuid ); if ( groupType != null ) { checkinArea.GetGroupTypeValues( groupType ); if ( groupType.IsValid ) { rockContext.SaveChanges(); groupType.SaveAttributeValues( rockContext ); bool AttributesUpdated = false; // rebuild the CheckinLabel attributes from the UI (brute-force) foreach ( var labelAttribute in CheckinArea.GetCheckinLabelAttributes( groupType.Attributes ) ) { var attribute = attributeService.Get( labelAttribute.Value.Guid ); Rock.Web.Cache.AttributeCache.Flush( attribute.Id ); attributeService.Delete( attribute ); AttributesUpdated = true; } // Make sure default role is set if ( !groupType.DefaultGroupRoleId.HasValue && groupType.Roles.Any() ) { groupType.DefaultGroupRoleId = groupType.Roles.First().Id; } rockContext.SaveChanges(); int labelOrder = 0; int binaryFileFieldTypeID = FieldTypeCache.Read( Rock.SystemGuid.FieldType.BINARY_FILE.AsGuid() ).Id; foreach ( var checkinLabelAttributeInfo in checkinArea.CheckinLabels ) { var attribute = new Rock.Model.Attribute(); attribute.AttributeQualifiers.Add( new AttributeQualifier { Key = "binaryFileType", Value = Rock.SystemGuid.BinaryFiletype.CHECKIN_LABEL } ); attribute.Guid = Guid.NewGuid(); attribute.FieldTypeId = binaryFileFieldTypeID; attribute.EntityTypeId = EntityTypeCache.GetId( typeof( GroupType ) ); attribute.EntityTypeQualifierColumn = "Id"; attribute.EntityTypeQualifierValue = groupType.Id.ToString(); attribute.DefaultValue = checkinLabelAttributeInfo.BinaryFileGuid.ToString(); attribute.Key = checkinLabelAttributeInfo.AttributeKey; attribute.Name = checkinLabelAttributeInfo.FileName; attribute.Order = labelOrder++; if ( !attribute.IsValid ) { return; } attributeService.Add( attribute ); AttributesUpdated = true; } rockContext.SaveChanges(); GroupTypeCache.Flush( groupType.Id ); Rock.CheckIn.KioskDevice.FlushAll(); if ( AttributesUpdated ) { AttributeCache.FlushEntityAttributes(); } nbSaveSuccess.Visible = true; BuildRows(); } else { ShowInvalidResults( groupType.ValidationResults ); } } } if ( checkinGroup.Visible ) { var groupService = new GroupService( rockContext ); var groupLocationService = new GroupLocationService( rockContext ); var group = groupService.Get( checkinGroup.GroupGuid ); if ( group != null ) { group.LoadAttributes( rockContext ); checkinGroup.GetGroupValues( group ); // populate groupLocations with whatever is currently in the grid, with just enough info to repopulate it and save it later var newLocationIds = checkinGroup.Locations.Select( l => l.LocationId ).ToList(); foreach ( var groupLocation in group.GroupLocations.Where( l => !newLocationIds.Contains( l.LocationId ) ).ToList() ) { groupLocationService.Delete( groupLocation ); group.GroupLocations.Remove( groupLocation ); } var existingLocationIds = group.GroupLocations.Select( g => g.LocationId ).ToList(); foreach ( var item in checkinGroup.Locations.Where( l => !existingLocationIds.Contains( l.LocationId ) ).ToList() ) { var groupLocation = new GroupLocation(); groupLocation.LocationId = item.LocationId; group.GroupLocations.Add( groupLocation ); } // Set the new order foreach ( var item in checkinGroup.Locations.OrderBy( l => l.Order ).ToList() ) { var groupLocation = group.GroupLocations.FirstOrDefault( gl => gl.LocationId == item.LocationId ); groupLocation.Order = item.Order ?? 0; } if ( group.IsValid ) { rockContext.SaveChanges(); group.SaveAttributeValues( rockContext ); Rock.CheckIn.KioskDevice.FlushAll(); nbSaveSuccess.Visible = true; BuildRows(); } else { ShowInvalidResults( group.ValidationResults ); } } } } hfIsDirty.Value = "false"; }
private void SelectArea( Guid? groupTypeGuid ) { hfIsDirty.Value = "false"; checkinArea.Visible = false; checkinGroup.Visible = false; btnSave.Visible = false; if ( groupTypeGuid.HasValue ) { using ( var rockContext = new RockContext() ) { var groupTypeService = new GroupTypeService( rockContext ); var groupType = groupTypeService.Get( groupTypeGuid.Value ); if ( groupType != null ) { _currentGroupTypeGuid = groupType.Guid; checkinArea.SetGroupType( groupType, rockContext ); checkinArea.CheckinLabels = new List<CheckinArea.CheckinLabelAttributeInfo>(); groupType.LoadAttributes( rockContext ); List<string> labelAttributeKeys = CheckinArea.GetCheckinLabelAttributes( groupType.Attributes ) .OrderBy( a => a.Value.Order ) .Select( a => a.Key ).ToList(); BinaryFileService binaryFileService = new BinaryFileService( rockContext ); foreach ( string key in labelAttributeKeys ) { var attributeValue = groupType.GetAttributeValue( key ); Guid binaryFileGuid = attributeValue.AsGuid(); var fileName = binaryFileService.Queryable().Where( a => a.Guid == binaryFileGuid ).Select( a => a.FileName ).FirstOrDefault(); if ( fileName != null ) { checkinArea.CheckinLabels.Add( new CheckinArea.CheckinLabelAttributeInfo { AttributeKey = key, BinaryFileGuid = binaryFileGuid, FileName = fileName } ); } } checkinArea.Visible = true; btnSave.Visible = true; } else { _currentGroupTypeGuid = null; } } } else { _currentGroupTypeGuid = null; } BuildRows(); }
private void CheckinAreaRow_DeleteAreaClick( object sender, EventArgs e ) { var row = sender as CheckinAreaRow; using ( var rockContext = new RockContext() ) { var groupTypeService = new GroupTypeService( rockContext ); var groupType = groupTypeService.Get( row.GroupTypeGuid ); if ( groupType != null ) { // Warn if this GroupType or any of its child grouptypes (recursive) is being used as an Inherited Group Type. Probably shouldn't happen, but just in case if ( IsInheritedGroupTypeRecursive( groupType, groupTypeService ) ) { nbDeleteWarning.Text = "WARNING - Cannot delete. This group type or one of its child group types is assigned as an inherited group type."; nbDeleteWarning.Visible = true; return; } string errorMessage; if ( !groupTypeService.CanDelete( groupType, out errorMessage ) ) { nbDeleteWarning.Text = "WARNING - Cannot Delete: " + errorMessage; nbDeleteWarning.Visible = true; return; } int id = groupType.Id; groupType.ParentGroupTypes.Clear(); groupType.ChildGroupTypes.Clear(); groupTypeService.Delete( groupType ); rockContext.SaveChanges(); GroupTypeCache.Flush( id ); Rock.CheckIn.KioskDevice.FlushAll(); SelectArea( null ); } } BuildRows(); }
private void CheckinAreaRow_AddGroupClick( object sender, EventArgs e ) { var parentRow = sender as CheckinAreaRow; parentRow.Expanded = true; using ( var rockContext = new RockContext() ) { var groupTypeService = new GroupTypeService( rockContext ); var parentArea = groupTypeService.Get( parentRow.GroupTypeGuid ); if ( parentArea != null ) { Guid newGuid = Guid.NewGuid(); var checkinGroup = new Group(); checkinGroup.Guid = newGuid; checkinGroup.Name = "New Group"; checkinGroup.IsActive = true; checkinGroup.IsPublic = true; checkinGroup.IsSystem = false; checkinGroup.Order = parentArea.Groups.Any() ? parentArea.Groups.Max( t => t.Order ) + 1 : 0; parentArea.Groups.Add( checkinGroup ); rockContext.SaveChanges(); GroupTypeCache.Flush( parentArea.Id ); Rock.CheckIn.KioskDevice.FlushAll(); SelectGroup( newGuid ); } } BuildRows(); }
private void CheckinAreaRow_AddAreaClick( object sender, EventArgs e ) { var parentRow = sender as CheckinAreaRow; parentRow.Expanded = true; using ( var rockContext = new RockContext() ) { var groupTypeService = new GroupTypeService( rockContext ); var parentArea = groupTypeService.Get( parentRow.GroupTypeGuid ); if ( parentArea != null ) { Guid newGuid = Guid.NewGuid(); var checkinArea = new GroupType(); checkinArea.Guid = newGuid; checkinArea.Name = "New Area"; checkinArea.IsSystem = false; checkinArea.ShowInNavigation = false; checkinArea.TakesAttendance = true; checkinArea.AttendanceRule = AttendanceRule.None; checkinArea.AttendancePrintTo = PrintTo.Default; checkinArea.AllowMultipleLocations = true; checkinArea.EnableLocationSchedules = true; checkinArea.Order = parentArea.ChildGroupTypes.Any() ? parentArea.ChildGroupTypes.Max( t => t.Order ) + 1 : 0; GroupTypeRole defaultRole = new GroupTypeRole(); defaultRole.Name = "Member"; checkinArea.Roles.Add( defaultRole ); parentArea.ChildGroupTypes.Add( checkinArea ); rockContext.SaveChanges(); GroupTypeCache.Flush( parentArea.Id ); Rock.CheckIn.KioskDevice.FlushAll(); SelectArea( newGuid ); } } BuildRows(); }
/// <summary> /// Handles the Click event of the btnEdit 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 btnEdit_Click( object sender, EventArgs e ) { GroupTypeService groupTypeService = new GroupTypeService( new RockContext() ); GroupType groupType = groupTypeService.Get( int.Parse( hfGroupTypeId.Value ) ); ShowEditDetails( groupType ); }
private GroupType GetGroupType( RockContext context, WorkflowAction action ) { Guid groupTypeGuid = GetAttributeValue( action, "GroupType" ).AsGuid(); if (groupTypeGuid != null) { var groupTypeService = new GroupTypeService(context); return groupTypeService.Get( groupTypeGuid ); } return null; }
/// <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 ) { if ( !Page.IsValid ) { return; } GroupType groupType = null; var rockContext = new RockContext(); GroupTypeService groupTypeService = new GroupTypeService( rockContext ); AttributeService attributeService = new AttributeService( rockContext ); AttributeQualifierService attributeQualifierService = new AttributeQualifierService( rockContext ); int? groupTypeId = hfGroupTypeId.ValueAsInt(); if ( groupTypeId.HasValue && groupTypeId.Value > 0 ) { groupType = groupTypeService.Get( groupTypeId.Value ); } bool newGroupType = false; if ( groupType == null ) { groupType = new GroupType(); groupTypeService.Add( groupType ); var templatePurpose = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE.AsGuid() ); if ( templatePurpose != null ) { groupType.GroupTypePurposeValueId = templatePurpose.Id; } newGroupType = true; } if ( groupType != null ) { groupType.Name = tbName.Text; groupType.Description = tbDescription.Text; groupType.LoadAttributes( rockContext ); Rock.Attribute.Helper.GetEditValues( phAttributeEdits, groupType ); groupType.SetAttributeValue( "core_checkin_AgeRequired", cbAgeRequired.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_GradeRequired", cbGradeRequired.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_HidePhotos", cbHidePhotos.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_PreventDuplicateCheckin", cbPreventDuplicateCheckin.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_PreventInactivePeople", cbPreventInactivePeople.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_CheckInType", ddlType.SelectedValue ); groupType.SetAttributeValue( "core_checkin_DisplayLocationCount", cbDisplayLocCount.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_EnableManagerOption", cbEnableManager.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_EnableOverride", cbEnableOverride.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_MaximumPhoneSearchLength", nbMaxPhoneLength.Text ); groupType.SetAttributeValue( "core_checkin_MaxSearchResults", nbMaxResults.Text ); groupType.SetAttributeValue( "core_checkin_MinimumPhoneSearchLength", nbMinPhoneLength.Text ); groupType.SetAttributeValue( "core_checkin_UseSameOptions", cbUseSameOptions.Checked.ToString() ); groupType.SetAttributeValue( "core_checkin_PhoneSearchType", ddlPhoneSearchType.SelectedValue ); groupType.SetAttributeValue( "core_checkin_RefreshInterval", nbRefreshInterval.Text ); groupType.SetAttributeValue( "core_checkin_RegularExpressionFilter", tbSearchRegex.Text ); groupType.SetAttributeValue( "core_checkin_ReuseSameCode", cbReuseCode.Checked.ToString() ); var searchType = DefinedValueCache.Read( ddlSearchType.SelectedValueAsInt() ?? 0 ); if ( searchType != null ) { groupType.SetAttributeValue( "core_checkin_SearchType", searchType.Guid.ToString() ); } else { groupType.SetAttributeValue( "core_checkin_SearchType", Rock.SystemGuid.DefinedValue.CHECKIN_SEARCH_TYPE_PHONE_NUMBER ); } groupType.SetAttributeValue( "core_checkin_SecurityCodeLength", nbSecurityCodeLength.Text ); groupType.SetAttributeValue( "core_checkin_AutoSelectDaysBack", nbAutoSelectDaysBack.Text ); rockContext.WrapTransaction( () => { rockContext.SaveChanges(); groupType.SaveAttributeValues( rockContext ); } ); if ( newGroupType ) { var pageRef = new PageReference( CurrentPageReference.PageId, CurrentPageReference.RouteId ); pageRef.Parameters.Add( "CheckinTypeId", groupType.Id.ToString() ); NavigateToPage( pageRef ); } else { groupType = groupTypeService.Get( groupType.Id ); ShowReadonlyDetails( groupType ); } GroupTypeCache.Flush( groupType.Id ); Rock.CheckIn.KioskDevice.FlushAll(); } }
/// <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 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 ) { using ( var rockContext = new RockContext() ) { var groupTypeService = new GroupTypeService( rockContext ); GroupType groupType; if ( hfGroupTypeId.Value.AsInteger() == 0 ) { groupType = new GroupType(); groupTypeService.Add( groupType ); groupType.GroupTypePurposeValueId = DefinedValueCache.Read( Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE ).Id; groupType.ShowInNavigation = false; groupType.ShowInGroupList = false; var defaultRole = new GroupTypeRole(); defaultRole.Name = "Member"; groupType.Roles.Add( defaultRole ); } else { groupType = groupTypeService.Get( hfGroupTypeId.Value.AsInteger() ); } groupType.Name = tbGroupTypeName.Text; groupType.Description = tbGroupTypeDescription.Text; rockContext.SaveChanges(); // Reload to check for setting default role groupType = groupTypeService.Get( groupType.Id ); if ( groupType != null && !groupType.DefaultGroupRoleId.HasValue && groupType.Roles.Any() ) { groupType.DefaultGroupRoleId = groupType.Roles.First().Id; rockContext.SaveChanges(); } } mdAddEditCheckinGroupType.Hide(); BindGrid(); }
/// <summary> /// Loads the <see cref="P:IHasAttributes.Attributes" /> and <see cref="P:IHasAttributes.AttributeValues" /> of any <see cref="IHasAttributes" /> object /// </summary> /// <param name="entity">The item.</param> /// <param name="rockContext">The rock context.</param> /// <remarks> /// If a rockContext value is included, this method will save any previous changes made to the context /// </remarks> public static void LoadAttributes( Rock.Attribute.IHasAttributes entity, RockContext rockContext = null ) { Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>(); Type entityType = entity.GetType(); if ( entityType.Namespace == "System.Data.Entity.DynamicProxies" ) entityType = entityType.BaseType; rockContext = rockContext ?? new RockContext(); // Check for group type attributes var groupTypeIds = new List<int>(); if ( entity is GroupMember || entity is Group || entity is GroupType ) { // Can't use GroupTypeCache here since it loads attributes and would result in a recursive stack overflow situation var groupTypeService = new GroupTypeService( rockContext ); GroupType groupType = null; if ( entity is GroupMember ) { var group = ( (GroupMember)entity ).Group ?? new GroupService( rockContext ).Get( ( (GroupMember)entity ).GroupId ); groupType = group.GroupType ?? groupTypeService.Get( group.GroupTypeId ); } else if ( entity is Group ) { groupType = ( (Group)entity ).GroupType ?? groupTypeService.Get( ( (Group)entity ).GroupTypeId ); } else { groupType = ( (GroupType)entity ); } while ( groupType != null ) { groupTypeIds.Insert(0, groupType.Id ); // Check for inherited group type id's groupType = groupType.InheritedGroupType ?? groupTypeService.Get( groupType.InheritedGroupTypeId ?? 0 ); } } foreach ( PropertyInfo propertyInfo in entityType.GetProperties() ) properties.Add( propertyInfo.Name.ToLower(), propertyInfo ); Rock.Model.AttributeService attributeService = new Rock.Model.AttributeService( rockContext ); Rock.Model.AttributeValueService attributeValueService = new Rock.Model.AttributeValueService( rockContext ); var inheritedAttributes = new Dictionary<int, List<Rock.Web.Cache.AttributeCache>>(); if ( groupTypeIds.Any() ) { groupTypeIds.ForEach( g => inheritedAttributes.Add( g, new List<Rock.Web.Cache.AttributeCache>() ) ); } else { inheritedAttributes.Add( 0, new List<Rock.Web.Cache.AttributeCache>() ); } var attributes = new List<Rock.Web.Cache.AttributeCache>(); // Get all the attributes that apply to this entity type and this entity's properties match any attribute qualifiers int? entityTypeId = Rock.Web.Cache.EntityTypeCache.Read( entityType ).Id; foreach ( Rock.Model.Attribute attribute in attributeService.GetByEntityTypeId( entityTypeId ) ) { // group type ids exist (entity is either GroupMember, Group, or GroupType) and qualifier is for a group type id if ( groupTypeIds.Any() && ( ( entity is GroupMember && string.Compare( attribute.EntityTypeQualifierColumn, "GroupTypeId", true ) == 0 ) || ( entity is Group && string.Compare( attribute.EntityTypeQualifierColumn, "GroupTypeId", true ) == 0 ) || ( entity is GroupType && string.Compare( attribute.EntityTypeQualifierColumn, "Id", true ) == 0 ) ) ) { int groupTypeIdValue = int.MinValue; if ( int.TryParse( attribute.EntityTypeQualifierValue, out groupTypeIdValue ) && groupTypeIds.Contains( groupTypeIdValue ) ) { inheritedAttributes[groupTypeIdValue].Add( Rock.Web.Cache.AttributeCache.Read( attribute ) ); } } else if ( string.IsNullOrEmpty( attribute.EntityTypeQualifierColumn ) || ( properties.ContainsKey( attribute.EntityTypeQualifierColumn.ToLower() ) && ( string.IsNullOrEmpty( attribute.EntityTypeQualifierValue ) || (properties[attribute.EntityTypeQualifierColumn.ToLower()].GetValue( entity, null ) ?? "").ToString() == attribute.EntityTypeQualifierValue ) ) ) { attributes.Add( Rock.Web.Cache.AttributeCache.Read( attribute ) ); } } var allAttributes = new List<Rock.Web.Cache.AttributeCache>(); foreach ( var attributeGroup in inheritedAttributes ) { foreach ( var attribute in attributeGroup.Value ) { allAttributes.Add( attribute ); } } foreach ( var attribute in attributes ) { allAttributes.Add( attribute ); } var attributeValues = new Dictionary<string, List<Rock.Model.AttributeValue>>(); if ( allAttributes.Any() ) { foreach ( var attribute in allAttributes ) { // Add a placeholder for this item's value for each attribute attributeValues.Add( attribute.Key, new List<Rock.Model.AttributeValue>() ); } // Read this item's value(s) for each attribute List<int> attributeIds = allAttributes.Select( a => a.Id ).ToList(); foreach ( var attributeValue in attributeValueService.Queryable( "Attribute" ) .Where( v => v.EntityId == entity.Id && attributeIds.Contains( v.AttributeId ) ) ) { attributeValues[attributeValue.Attribute.Key].Add( attributeValue.Clone( false ) as Rock.Model.AttributeValue ); } // Look for any attributes that don't have a value and create a default value entry foreach ( var attribute in allAttributes ) { if ( attributeValues[attribute.Key].Count == 0 ) { var attributeValue = new Rock.Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; if ( entity.AttributeValueDefaults != null && entity.AttributeValueDefaults.ContainsKey( attribute.Name ) ) { attributeValue.Value = entity.AttributeValueDefaults[attribute.Name]; } else { attributeValue.Value = attribute.DefaultValue; } attributeValues[attribute.Key].Add( attributeValue ); } else { if ( !String.IsNullOrWhiteSpace( attribute.DefaultValue ) ) { foreach ( var value in attributeValues[attribute.Key] ) { if ( String.IsNullOrWhiteSpace( value.Value ) ) { value.Value = attribute.DefaultValue; } } } } } } entity.Attributes = new Dictionary<string, Web.Cache.AttributeCache>(); allAttributes.ForEach( a => entity.Attributes.Add( a.Key, a ) ); entity.AttributeValues = attributeValues; }
/// <summary> /// Binds the inherited attributes. /// </summary> /// <param name="inheritedGroupTypeId">The inherited group type identifier.</param> /// <param name="groupTypeService">The group type service.</param> /// <param name="attributeService">The attribute service.</param> private void BindInheritedAttributes( int? inheritedGroupTypeId, GroupTypeService groupTypeService, AttributeService attributeService ) { GroupTypeAttributesInheritedState = new List<InheritedAttribute>(); GroupAttributesInheritedState = new List<InheritedAttribute>(); GroupMemberAttributesInheritedState = new List<InheritedAttribute>(); while ( inheritedGroupTypeId.HasValue ) { var inheritedGroupType = groupTypeService.Get( inheritedGroupTypeId.Value ); if ( inheritedGroupType != null ) { string qualifierValue = inheritedGroupType.Id.ToString(); foreach ( var attribute in attributeService.GetByEntityTypeId( new GroupType().TypeId ).AsQueryable() .Where( a => a.EntityTypeQualifierColumn.Equals( "Id", StringComparison.OrdinalIgnoreCase ) && a.EntityTypeQualifierValue.Equals( qualifierValue ) ) .OrderBy( a => a.Order ) .ThenBy( a => a.Name ) .ToList() ) { GroupTypeAttributesInheritedState.Add( new InheritedAttribute( attribute.Name, attribute.Key, attribute.Description, Page.ResolveUrl( "~/GroupType/" + attribute.EntityTypeQualifierValue ), inheritedGroupType.Name ) ); } foreach ( var attribute in attributeService.GetByEntityTypeId( new Group().TypeId ).AsQueryable() .Where( a => a.EntityTypeQualifierColumn.Equals( "GroupTypeId", StringComparison.OrdinalIgnoreCase ) && a.EntityTypeQualifierValue.Equals( qualifierValue ) ) .OrderBy( a => a.Order ) .ThenBy( a => a.Name ) .ToList() ) { GroupAttributesInheritedState.Add( new InheritedAttribute( attribute.Name, attribute.Key, attribute.Description, Page.ResolveUrl( "~/GroupType/" + attribute.EntityTypeQualifierValue ), inheritedGroupType.Name ) ); } foreach ( var attribute in attributeService.GetByEntityTypeId( new GroupMember().TypeId ).AsQueryable() .Where( a => a.EntityTypeQualifierColumn.Equals( "GroupTypeId", StringComparison.OrdinalIgnoreCase ) && a.EntityTypeQualifierValue.Equals( qualifierValue ) ) .OrderBy( a => a.Order ) .ThenBy( a => a.Name ) .ToList() ) { GroupMemberAttributesInheritedState.Add( new InheritedAttribute( attribute.Name, attribute.Key, attribute.Description, Page.ResolveUrl( "~/GroupType/" + attribute.EntityTypeQualifierValue ), inheritedGroupType.Name ) ); } inheritedGroupTypeId = inheritedGroupType.InheritedGroupTypeId; } else { inheritedGroupTypeId = null; } } BindGroupTypeAttributesInheritedGrid(); BindGroupAttributesInheritedGrid(); BindGroupMemberAttributesInheritedGrid(); }
/// <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; var rockContext = new RockContext(); GroupTypeService groupTypeService = new GroupTypeService( rockContext ); GroupService groupService = new GroupService( rockContext ); AttributeService attributeService = new AttributeService( rockContext ); GroupLocationService groupLocationService = new GroupLocationService( rockContext ); int parentGroupTypeId = hfParentGroupTypeId.ValueAsInt(); var groupTypeUIList = new List<GroupType>(); foreach ( var checkinGroupTypeEditor in phCheckinGroupTypes.Controls.OfType<CheckinGroupTypeEditor>().ToList() ) { var groupType = checkinGroupTypeEditor.GetCheckinGroupType( rockContext ); 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 = FieldTypeCache.Read( Rock.SystemGuid.FieldType.BINARY_FILE.AsGuid() ).Id; rockContext.WrapTransaction( () => { // delete in reverse order to get deepest child items first groupsToDelete.Reverse(); foreach ( var groupToDelete in groupsToDelete ) { groupService.Delete( groupToDelete ); } // delete in reverse order to get deepest child items first groupTypesToDelete.Reverse(); foreach ( var groupTypeToDelete in groupTypesToDelete ) { groupTypeService.Delete( groupTypeToDelete ); } rockContext.SaveChanges(); // 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.IsSystem = false; groupTypeDB.ShowInNavigation = false; groupTypeDB.ShowInGroupList = false; groupTypeDB.TakesAttendance = true; groupTypeDB.AttendanceRule = AttendanceRule.None; groupTypeDB.AttendancePrintTo = PrintTo.Default; groupTypeDB.AllowMultipleLocations = true; groupTypeDB.EnableLocationSchedules = true; } 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 ); } if ( !groupTypeDB.IsValid ) { hasValidationErrors = true; CheckinGroupTypeEditor groupTypeEditor = phCheckinGroupTypes.ControlsOfTypeRecursive<CheckinGroupTypeEditor>().First( a => a.GroupTypeGuid == groupTypeDB.Guid ); groupTypeEditor.Expanded = true; return; } rockContext.SaveChanges(); groupTypeDB.SaveAttributeValues( rockContext ); // 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.Attributes, rockContext ) ) { var attribute = attributeService.Get( labelAttributeDB.Value.Guid ); Rock.Web.Cache.AttributeCache.Flush( attribute.Id ); attributeService.Delete( attribute ); } rockContext.SaveChanges(); foreach ( var checkinLabelAttributeInfo in GroupTypeCheckinLabelAttributesState[groupTypeUI.Guid] ) { var attribute = new Rock.Model.Attribute(); attribute.AttributeQualifiers.Add( new AttributeQualifier { Key = "binaryFileType", Value = Rock.SystemGuid.BinaryFiletype.CHECKIN_LABEL } ); attribute.Guid = Guid.NewGuid(); attribute.FieldTypeId = binaryFileFieldTypeID; attribute.EntityTypeId = EntityTypeCache.GetId( typeof( GroupType ) ); attribute.EntityTypeQualifierColumn = "Id"; attribute.EntityTypeQualifierValue = groupTypeDB.Id.ToString(); attribute.DefaultValue = checkinLabelAttributeInfo.BinaryFileGuid.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.Expanded = true; return; } attributeService.Add( attribute ); } rockContext.SaveChanges(); } // 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 ) ) { groupLocationService.Delete( 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 ); } if ( !groupDB.IsValid ) { hasValidationErrors = true; hasValidationErrors = true; CheckinGroupEditor groupEditor = phCheckinGroupTypes.ControlsOfTypeRecursive<CheckinGroupEditor>().First( a => a.GroupGuid == groupDB.Guid ); groupEditor.Expanded = true; return; } rockContext.SaveChanges(); groupDB.SaveAttributeValues(); } /* 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 ); } rockContext.SaveChanges(); // 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 ); } } rockContext.SaveChanges(); } ); if ( !hasValidationErrors ) { NavigateToParentPage(); } }
/// <summary> /// Builds the group types UI. /// </summary> private void BuildGroupTypesUI() { var rockContext = new RockContext(); var groupTypeService = new GroupTypeService( rockContext ); var groupTypeTemplateGuid = this.GetAttributeValue( "GroupTypeTemplate" ).AsGuidOrNull(); nbGroupTypeWarning.Visible = !groupTypeTemplateGuid.HasValue; if ( groupTypeTemplateGuid.HasValue ) { var groupType = groupTypeService.Get( groupTypeTemplateGuid.Value ); var groupTypes = groupTypeService.GetChildGroupTypes( groupType.Id ).OrderBy( a => a.Order ).ThenBy( a => a.Name ); rptGroupTypes.DataSource = groupTypes.ToList(); rptGroupTypes.DataBind(); } }
/// <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(); }