/// <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 ) { // confirmation was disabled by btnSave on client-side. So if returning without a redirect, // it should be enabled. If returning with a redirect, the control won't be updated to reflect // confirmation being enabled, so it's ok to enable it here confirmExit.Enabled = true; if ( Page.IsValid ) { confirmExit.Enabled = true; RockTransactionScope.WrapTransaction( () => { using ( new UnitOfWorkScope() ) { var familyService = new GroupService(); var familyMemberService = new GroupMemberService(); var personService = new PersonService(); var historyService = new HistoryService(); var familyChanges = new List<string>(); // SAVE FAMILY _family = familyService.Get( _family.Id ); History.EvaluateChange( familyChanges, "Name", _family.Name, tbFamilyName.Text ); _family.Name = tbFamilyName.Text; int? campusId = cpCampus.SelectedValueAsInt(); if ( _family.CampusId != campusId ) { History.EvaluateChange( familyChanges, "Campus", _family.CampusId.HasValue ? CampusCache.Read( _family.CampusId.Value ).Name : string.Empty, campusId.HasValue ? CampusCache.Read( campusId.Value ).Name : string.Empty ); _family.CampusId = campusId; } var familyGroupTypeId = _family.GroupTypeId; familyService.Save( _family, CurrentPersonId ); // SAVE FAMILY MEMBERS int? recordStatusValueID = ddlRecordStatus.SelectedValueAsInt(); int? reasonValueId = ddlReason.SelectedValueAsInt(); var newFamilies = new List<Group>(); foreach ( var familyMember in FamilyMembers ) { var memberChanges = new List<string>(); var demographicChanges = new List<string>(); var role = familyRoles.Where( r => r.Guid.Equals( familyMember.RoleGuid ) ).FirstOrDefault(); if ( role == null ) { role = familyRoles.FirstOrDefault(); } bool isChild = role != null && role.Guid.Equals( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_CHILD ) ); // People added to family (new or from other family) if ( !familyMember.ExistingFamilyMember ) { var groupMember = new GroupMember(); if ( familyMember.Id == -1 ) { // added new person demographicChanges.Add( "Created" ); var person = new Person(); person.FirstName = familyMember.FirstName; History.EvaluateChange( demographicChanges, "First Name", string.Empty, person.FirstName ); person.LastName = familyMember.LastName; History.EvaluateChange( demographicChanges, "Last Name", string.Empty, person.LastName ); person.Gender = familyMember.Gender; History.EvaluateChange( demographicChanges, "Gender", null, person.Gender ); person.BirthDate = familyMember.BirthDate; History.EvaluateChange( demographicChanges, "Birth Date", null, person.BirthDate ); if ( !isChild ) { person.GivingGroupId = _family.Id; History.EvaluateChange( demographicChanges, "Giving Group", string.Empty, _family.Name ); } groupMember.Person = person; } else { // added from other family groupMember.Person = personService.Get( familyMember.Id ); } History.EvaluateChange( demographicChanges, "Record Status", DefinedValueCache.GetName( groupMember.Person.RecordStatusValueId ), DefinedValueCache.GetName( recordStatusValueID ) ); groupMember.Person.RecordStatusValueId = recordStatusValueID; History.EvaluateChange( demographicChanges, "Record Status Reason", DefinedValueCache.GetName( groupMember.Person.RecordStatusReasonValueId ), DefinedValueCache.GetName( reasonValueId ) ); groupMember.Person.RecordStatusReasonValueId = reasonValueId; groupMember.GroupId = _family.Id; if ( role != null ) { History.EvaluateChange( memberChanges, string.Format( "Role", _family.Name ), string.Empty, role.Name ); groupMember.GroupRoleId = role.Id; } if ( groupMember.Person != null ) { familyMemberService.Add( groupMember, CurrentPersonId ); familyMemberService.Save( groupMember, CurrentPersonId ); } } else { // existing family members var groupMember = familyMemberService.Queryable( "Person" ).Where( m => m.PersonId == familyMember.Id && m.Group.GroupTypeId == familyGroupTypeId && m.GroupId == _family.Id ).FirstOrDefault(); if ( groupMember != null ) { if ( familyMember.Removed ) { var newFamilyChanges = new List<string>(); newFamilyChanges.Add( "Created" ); // Family member was removed and should be created in their own new family var newFamily = new Group(); newFamily.Name = familyMember.LastName + " Family"; History.EvaluateChange( newFamilyChanges, "Name", string.Empty, newFamily.Name ); newFamily.GroupTypeId = familyGroupTypeId; if ( _family.CampusId.HasValue ) { History.EvaluateChange( newFamilyChanges, "Campus", string.Empty, CampusCache.Read( _family.CampusId.Value ).Name ); } newFamily.CampusId = _family.CampusId; familyService.Add( newFamily, CurrentPersonId ); familyService.Save( newFamily, CurrentPersonId ); historyService.SaveChanges( typeof( Group ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), newFamily.Id, newFamilyChanges, CurrentPersonId ); // If person's previous giving group was this family, set it to their new family id if ( groupMember.Person.GivingGroup != null && groupMember.Person.GivingGroupId == _family.Id ) { History.EvaluateChange( demographicChanges, "Giving Group", groupMember.Person.GivingGroup.Name, _family.Name ); groupMember.Person.GivingGroupId = newFamily.Id; } groupMember.Group = newFamily; familyMemberService.Save( groupMember, CurrentPersonId ); var newMemberChanges = new List<string>(); History.EvaluateChange( newMemberChanges, "Role", string.Empty, groupMember.GroupRole.Name ); historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), groupMember.Person.Id, newMemberChanges, CurrentPersonId ); History.EvaluateChange( memberChanges, "Role", groupMember.GroupRole.Name, string.Empty ); newFamilies.Add( newFamily ); } else { // Existing member was not remvoved if ( role != null ) { History.EvaluateChange( memberChanges, "Role", groupMember.GroupRole != null ? groupMember.GroupRole.Name : string.Empty, role.Name ); groupMember.GroupRoleId = role.Id; History.EvaluateChange( demographicChanges, "Record Status", DefinedValueCache.GetName( groupMember.Person.RecordStatusValueId ), DefinedValueCache.GetName( recordStatusValueID ) ); groupMember.Person.RecordStatusValueId = recordStatusValueID; History.EvaluateChange( demographicChanges, "Record Status Reason", DefinedValueCache.GetName( groupMember.Person.RecordStatusReasonValueId ), DefinedValueCache.GetName( reasonValueId ) ); groupMember.Person.RecordStatusReasonValueId = reasonValueId; familyMemberService.Save( groupMember, CurrentPersonId ); } } } } // Remove anyone that was moved from another family if ( familyMember.RemoveFromOtherFamilies ) { var otherFamilies = familyMemberService.Queryable() .Where( m => m.PersonId == familyMember.Id && m.Group.GroupTypeId == familyGroupTypeId && m.GroupId != _family.Id ) .ToList(); foreach ( var otherFamilyMember in otherFamilies ) { var fm = familyMemberService.Get( otherFamilyMember.Id ); // If the person's giving group id was the family they are being removed from, update it to this new family's id if ( fm.Person.GivingGroupId == fm.GroupId ) { var person = personService.Get( fm.PersonId ); History.EvaluateChange( demographicChanges, "Giving Group", person.GivingGroup.Name, _family.Name ); person.GivingGroupId = _family.Id; personService.Save( person, CurrentPersonId ); } var oldMemberChanges = new List<string>(); History.EvaluateChange( oldMemberChanges, "Role", fm.GroupRole.Name, string.Empty ); familyMemberService.Delete( fm, CurrentPersonId ); familyMemberService.Save( fm, CurrentPersonId ); historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), fm.Person.Id, oldMemberChanges, CurrentPersonId ); var f = familyService.Queryable() .Where( g => g.Id == otherFamilyMember.GroupId && !g.Members.Any() ) .FirstOrDefault(); if ( f != null ) { var oldFamilyChanges = new List<string>(); oldFamilyChanges.Add( "Deleted" ); historyService.SaveChanges( typeof( Group ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), f.Id, oldFamilyChanges, CurrentPersonId ); familyService.Delete( f, CurrentPersonId ); familyService.Save( f, CurrentPersonId ); } } } historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(), familyMember.Id, demographicChanges, CurrentPersonId ); historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), familyMember.Id, memberChanges, _family.Name, typeof( Group ), _family.Id, CurrentPersonId ); } // SAVE LOCATIONS var groupLocationService = new GroupLocationService(); // delete any group locations that were removed var remainingLocationIds = FamilyAddresses.Where( a => a.Id > 0 ).Select( a => a.Id ).ToList(); foreach ( var removedLocation in groupLocationService.Queryable( "GroupLocationTypeValue,Location" ) .Where( l => l.GroupId == _family.Id && !remainingLocationIds.Contains( l.Id ) ) ) { History.EvaluateChange( familyChanges, removedLocation.GroupLocationTypeValue.Name + " Location", removedLocation.Location.ToString(), string.Empty ); groupLocationService.Delete( removedLocation, CurrentPersonId ); groupLocationService.Save( removedLocation, CurrentPersonId ); } foreach ( var familyAddress in FamilyAddresses ) { Location updatedAddress = null; if ( familyAddress.LocationIsDirty ) { updatedAddress = new LocationService().Get( familyAddress.Street1, familyAddress.Street2, familyAddress.City, familyAddress.State, familyAddress.Zip ); } GroupLocation groupLocation = null; if ( familyAddress.Id > 0 ) { groupLocation = groupLocationService.Get( familyAddress.Id ); } if ( groupLocation == null ) { groupLocation = new GroupLocation(); groupLocation.GroupId = _family.Id; groupLocationService.Add( groupLocation, CurrentPersonId ); } History.EvaluateChange( familyChanges, "Location Type", groupLocation.GroupLocationTypeValueId.HasValue ? DefinedValueCache.Read( groupLocation.GroupLocationTypeValueId.Value ).Name : string.Empty, familyAddress.LocationTypeName ); groupLocation.GroupLocationTypeValueId = familyAddress.LocationTypeId; History.EvaluateChange( familyChanges, familyAddress.LocationTypeName + " Location Is Mailing", groupLocation.IsMailingLocation.ToString(), familyAddress.IsMailing.ToString() ); groupLocation.IsMailingLocation = familyAddress.IsMailing; History.EvaluateChange( familyChanges, familyAddress.LocationTypeName + " Location Is Location", groupLocation.IsMappedLocation.ToString(), familyAddress.IsLocation.ToString() ); groupLocation.IsMappedLocation = familyAddress.IsLocation; if ( updatedAddress != null ) { History.EvaluateChange( familyChanges, familyAddress.LocationTypeName + " Location", groupLocation.Location.ToString(), updatedAddress.ToString() ); groupLocation.Location = updatedAddress; } groupLocationService.Save( groupLocation, CurrentPersonId ); // Add the same locations to any new families created by removing an existing family member if ( newFamilies.Any() ) { //reload grouplocation for access to child properties groupLocation = groupLocationService.Get( groupLocation.Id ); foreach ( var newFamily in newFamilies ) { var newFamilyLocation = new GroupLocation(); newFamilyLocation.GroupId = newFamily.Id; newFamilyLocation.LocationId = groupLocation.LocationId; newFamilyLocation.GroupLocationTypeValueId = groupLocation.GroupLocationTypeValueId; newFamilyLocation.IsMailingLocation = groupLocation.IsMailingLocation; newFamilyLocation.IsMappedLocation = groupLocation.IsMappedLocation; groupLocationService.Add( newFamilyLocation, CurrentPersonId ); groupLocationService.Save( newFamilyLocation, CurrentPersonId ); } } } historyService.SaveChanges( typeof( Group ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), _family.Id, familyChanges, CurrentPersonId ); _family = familyService.Get( _family.Id ); if ( _family.Members.Any( m => m.PersonId == Person.Id ) ) { Response.Redirect( string.Format( "~/Person/{0}", Person.Id ), false ); } else { var fm = _family.Members .Where( m => m.GroupRole.Guid.Equals( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ) && m.Person.Gender == Gender.Male ) .OrderByDescending( m => m.Person.Age ) .FirstOrDefault(); if ( fm == null ) { fm = _family.Members .Where( m => m.GroupRole.Guid.Equals( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ) ) .OrderByDescending( m => m.Person.Age ) .FirstOrDefault(); } if ( fm == null ) { fm = _family.Members .OrderByDescending( m => m.Person.Age ) .FirstOrDefault(); } if ( fm != null ) { Response.Redirect( string.Format( "~/Person/{0}", fm.PersonId ), false ); } else { Response.Redirect( "~", false ); } } } } ); } }
/// <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 ) { int? parentGroupId = null; // NOTE: Very similar code in GroupList.gGroups_Delete RockTransactionScope.WrapTransaction( () => { GroupService groupService = new GroupService(); AuthService authService = new AuthService(); Group group = groupService.Get( int.Parse( hfGroupId.Value ) ); if ( group != null ) { parentGroupId = group.ParentGroupId; string errorMessage; if ( !groupService.CanDelete( group, out errorMessage ) ) { mdDeleteWarning.Show( errorMessage, ModalAlertType.Information ); return; } bool isSecurityRoleGroup = group.IsSecurityRole || group.GroupType.Guid.Equals( Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid() ); if ( isSecurityRoleGroup ) { Rock.Security.Role.Flush( group.Id ); foreach ( var auth in authService.Queryable().Where( a => a.GroupId.Equals( group.Id ) ).ToList() ) { authService.Delete( auth, CurrentPersonId ); authService.Save( auth, CurrentPersonId ); } } groupService.Delete( group, CurrentPersonId ); groupService.Save( group, CurrentPersonId ); if ( isSecurityRoleGroup ) { Rock.Security.Authorization.Flush(); } } } ); // reload page, selecting the deleted group's parent var qryParams = new Dictionary<string, string>(); if ( parentGroupId != null ) { qryParams["groupId"] = parentGroupId.ToString(); } NavigateToPage( RockPage.Guid, qryParams ); }
/// <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 ) { Group group; bool wasSecurityRole = false; using ( new UnitOfWorkScope() ) { GroupService groupService = new GroupService(); GroupLocationService groupLocationService = new GroupLocationService(); AttributeService attributeService = new AttributeService(); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(); CategoryService categoryService = new CategoryService(); if ( ( ddlGroupType.SelectedValueAsInt() ?? 0 ) == 0 ) { ddlGroupType.ShowErrorMessage( Rock.Constants.WarningMessage.CannotBeBlank( GroupType.FriendlyTypeName ) ); return; } int groupId = int.Parse( hfGroupId.Value ); if ( groupId == 0 ) { group = new Group(); group.IsSystem = false; group.Name = string.Empty; } else { group = groupService.Get( groupId ); wasSecurityRole = group.IsSecurityRole; var selectedLocations = GroupLocationsState.Select( l => l.Guid ); foreach( var groupLocation in group.GroupLocations.Where( l => !selectedLocations.Contains( l.Guid)).ToList() ) { group.GroupLocations.Remove( groupLocation ); groupLocationService.Delete( groupLocation, CurrentPersonId ); } } foreach ( var groupLocationState in GroupLocationsState ) { GroupLocation groupLocation = group.GroupLocations.Where( l => l.Guid == groupLocationState.Guid).FirstOrDefault(); if (groupLocation == null) { groupLocation = new GroupLocation(); group.GroupLocations.Add( groupLocation ); } else { groupLocationState.Id = groupLocation.Id; groupLocationState.Guid = groupLocation.Guid; } groupLocation.CopyPropertiesFrom( groupLocationState ); } group.Name = tbName.Text; group.Description = tbDescription.Text; group.CampusId = ddlCampus.SelectedValue.Equals( None.IdValue ) ? (int?)null : int.Parse( ddlCampus.SelectedValue ); group.GroupTypeId = int.Parse( ddlGroupType.SelectedValue ); group.ParentGroupId = gpParentGroup.SelectedValue.Equals( None.IdValue ) ? (int?)null : int.Parse( gpParentGroup.SelectedValue ); group.IsSecurityRole = cbIsSecurityRole.Checked; group.IsActive = cbIsActive.Checked; if ( group.ParentGroupId == group.Id ) { gpParentGroup.ShowErrorMessage( "Group cannot be a Parent Group of itself." ); return; } group.LoadAttributes(); Rock.Attribute.Helper.GetEditValues( phGroupAttributes, group ); if ( !Page.IsValid ) { return; } if ( !group.IsValid ) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction( () => { if ( group.Id.Equals( 0 ) ) { groupService.Add( group, CurrentPersonId ); } groupService.Save( group, CurrentPersonId ); Rock.Attribute.Helper.SaveAttributeValues( group, CurrentPersonId ); /* Take care of Group Member Attributes */ var entityTypeId = EntityTypeCache.Read( typeof( GroupMember ) ).Id; string qualifierColumn = "GroupId"; string qualifierValue = group.Id.ToString(); // Get the existing attributes for this entity type and qualifier value var attributes = attributeService.Get( entityTypeId, qualifierColumn, qualifierValue ); // Delete any of those attributes that were removed in the UI var selectedAttributeGuids = GroupMemberAttributesState.Select( a => a.Guid ); foreach ( var attr in attributes.Where( a => !selectedAttributeGuids.Contains( a.Guid ) ) ) { Rock.Web.Cache.AttributeCache.Flush( attr.Id ); attributeService.Delete( attr, CurrentPersonId ); attributeService.Save( attr, CurrentPersonId ); } // Update the Attributes that were assigned in the UI foreach ( var attributeState in GroupMemberAttributesState ) { Rock.Attribute.Helper.SaveAttributeEdits( attributeState, attributeService, attributeQualifierService, categoryService, entityTypeId, qualifierColumn, qualifierValue, CurrentPersonId ); } } ); } if ( group != null && wasSecurityRole ) { if ( !group.IsSecurityRole ) { // if this group was a SecurityRole, but no longer is, flush Rock.Security.Role.Flush( group.Id ); Rock.Security.Authorization.Flush(); } } else { if ( group.IsSecurityRole ) { // new security role, flush Rock.Security.Authorization.Flush(); } } var qryParams = new Dictionary<string, string>(); qryParams["groupId"] = group.Id.ToString(); NavigateToPage( RockPage.Guid, qryParams ); }
/// <summary> /// Gets the person. /// </summary> /// <param name="create">if set to <c>true</c> [create].</param> /// <returns></returns> private Person GetPerson( bool create ) { Person person = null; int personId = ViewState["PersonId"] as int? ?? 0; if ( personId == 0 && TargetPerson != null ) { person = TargetPerson; } else { using ( new UnitOfWorkScope() ) { var personService = new PersonService(); if ( personId != 0 ) { person = personService.Get( personId ); } if ( person == null && create ) { // Check to see if there's only one person with same email, first name, and last name if ( !string.IsNullOrWhiteSpace( txtEmail.Text ) && !string.IsNullOrWhiteSpace( txtFirstName.Text ) && !string.IsNullOrWhiteSpace( txtLastName.Text ) ) { var personMatches = personService.GetByEmail( txtEmail.Text ).Where( p => p.LastName.Equals( txtLastName.Text, StringComparison.OrdinalIgnoreCase ) && ( p.FirstName.Equals( txtFirstName.Text, StringComparison.OrdinalIgnoreCase ) || p.NickName.Equals( txtFirstName.Text, StringComparison.OrdinalIgnoreCase ) ) ); if ( personMatches.Count() == 1 ) { person = personMatches.FirstOrDefault(); } } if ( person == null ) { // Create Person person = new Person(); person.FirstName = txtFirstName.Text; person.LastName = txtLastName.Text; person.Email = txtEmail.Text; bool displayPhone = false; if ( bool.TryParse( GetAttributeValue( "DisplayPhone" ), out displayPhone ) && displayPhone ) { var phone = new PhoneNumber(); phone.Number = txtPhone.Text.AsNumeric(); phone.NumberTypeValueId = DefinedValueCache.Read( new Guid( Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_HOME ) ).Id; person.PhoneNumbers.Add( phone ); } // Create Family Role var groupMember = new GroupMember(); groupMember.Person = person; groupMember.GroupRole = new GroupTypeRoleService().Get(new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ); // Create Family var group = new Group(); group.Members.Add( groupMember ); group.Name = person.LastName + " Family"; group.GroupTypeId = GroupTypeCache.GetFamilyGroupType().Id; var groupLocation = new GroupLocation(); var location = new LocationService().Get( txtStreet.Text, string.Empty, txtCity.Text, ddlState.SelectedValue, txtZip.Text ); if ( location != null ) { Guid addressTypeGuid = Guid.Empty; if ( !Guid.TryParse( GetAttributeValue( "AddressType" ), out addressTypeGuid ) ) { addressTypeGuid = new Guid( Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME ); } groupLocation = new GroupLocation(); groupLocation.Location = location; groupLocation.GroupLocationTypeValueId = DefinedValueCache.Read( addressTypeGuid ).Id; group.GroupLocations.Add( groupLocation ); } var groupService = new GroupService(); groupService.Add( group, CurrentPersonId ); groupService.Save( group, CurrentPersonId ); } ViewState["PersonId"] = person != null ? person.Id : 0; } } } return person; }
/// <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> /// Adds the visitor group member roles. /// </summary> /// <param name="family">The family.</param> /// <param name="personId">The person id.</param> protected void AddVisitorGroupMemberRoles( CheckInFamily family, int personId ) { GroupMemberService groupMemberService = new GroupMemberService(); GroupTypeRoleService groupRoleService = new GroupTypeRoleService(); int ownerRoleId = groupRoleService.Get( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER ) ).Id; int canCheckInId = groupRoleService.Get( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_CAN_CHECK_IN ) ).Id; foreach ( var familyMember in family.People ) { var group = groupMemberService.Queryable() .Where( m => m.PersonId == familyMember.Person.Id && m.GroupRoleId == ownerRoleId ) .Select( m => m.Group ) .FirstOrDefault(); if ( group == null ) { var role = new GroupTypeRoleService().Get( ownerRoleId ); if ( role != null && role.GroupTypeId.HasValue ) { var groupMember = new GroupMember().Clone( false ); groupMember.PersonId = familyMember.Person.Id; groupMember.GroupRoleId = role.Id; group = new Group().Clone( false ); group.Name = role.GroupType.Name; group.GroupTypeId = role.GroupTypeId.Value; group.Members.Add( groupMember ); var groupService = new GroupService(); groupService.Add( group, CurrentPersonId ); groupService.Save( group, CurrentPersonId ); } } // add the visitor to this group with CanCheckIn Person.CreateCheckinRelationship( familyMember.Person.Id, personId, CurrentPersonId ); } }
/// <summary> /// Binds the data. /// </summary> private void BindData() { if ( Person != null && Person.Id > 0 ) { if ( ownerRoleGuid != Guid.Empty ) { using ( new UnitOfWorkScope() ) { var memberService = new GroupMemberService(); var group = memberService.Queryable() .Where( m => m.PersonId == Person.Id && m.GroupRole.Guid == ownerRoleGuid ) .Select( m => m.Group ) .FirstOrDefault(); if ( group == null && bool.Parse( GetAttributeValue( "CreateGroup" ) ) ) { var role = new GroupTypeRoleService().Get( ownerRoleGuid ); if ( role != null && role.GroupTypeId.HasValue ) { var groupMember = new GroupMember(); groupMember.PersonId = Person.Id; groupMember.GroupRoleId = role.Id; group = new Group(); group.Name = role.GroupType.Name; group.GroupTypeId = role.GroupTypeId.Value; group.Members.Add( groupMember ); var groupService = new GroupService(); groupService.Add( group, CurrentPersonId ); groupService.Save( group, CurrentPersonId ); group = groupService.Get( group.Id ); } } if ( group != null ) { if ( group.IsAuthorized( "View", CurrentPerson ) ) { phGroupTypeIcon.Controls.Clear(); if ( !string.IsNullOrWhiteSpace( group.GroupType.IconCssClass ) ) { phGroupTypeIcon.Controls.Add( new LiteralControl( string.Format( "<i class='{0}'></i>", group.GroupType.IconCssClass ) ) ); } lGroupName.Text = group.Name; phEditActions.Visible = group.IsAuthorized( "Edit", CurrentPerson ); // TODO: How many implied relationships should be displayed rGroupMembers.DataSource = new GroupMemberService().GetByGroupId( group.Id ) .Where( m => m.PersonId != Person.Id ) .OrderBy( m => m.Person.LastName ) .ThenBy( m => m.Person.FirstName ) .Take( 50 ) .ToList(); rGroupMembers.DataBind(); } } } } } }
/// <summary> /// Creates the family. /// </summary> /// <param name="FamilyName">Name of the family.</param> /// <returns></returns> protected Group CreateFamily( string FamilyName ) { var familyGroup = new Group(); familyGroup.Name = FamilyName + " Family"; familyGroup.GroupTypeId = GroupTypeCache.GetFamilyGroupType().Id; familyGroup.IsSecurityRole = false; familyGroup.IsSystem = false; familyGroup.IsActive = true; Rock.Data.RockTransactionScope.WrapTransaction( () => { var gs = new GroupService(); gs.Add( familyGroup, CurrentPersonId ); gs.Save( familyGroup, CurrentPersonId ); } ); return familyGroup; }
protected void btnNext_Click( object sender, EventArgs e ) { if ( Page.IsValid ) { if ( CurrentCategoryIndex < attributeControls.Count ) { CurrentCategoryIndex++; ShowAttributeCategory( CurrentCategoryIndex ); } else { var familyMembers = GetControlData(); if ( familyMembers.Any() ) { RockTransactionScope.WrapTransaction( () => { using ( new UnitOfWorkScope() ) { var familyGroupType = GroupTypeCache.GetFamilyGroupType(); var familyChanges = new List<string>(); var familyMemberChanges = new Dictionary<Guid, List<string>>(); var familyDemographicChanges = new Dictionary<Guid, List<string>>(); if ( familyGroupType != null ) { var groupService = new GroupService(); var groupTypeRoleService = new GroupTypeRoleService(); var familyGroup = new Group(); familyGroup.GroupTypeId = familyGroupType.Id; familyChanges.Add("Created"); familyGroup.Name = familyMembers.FirstOrDefault().Person.LastName + " Family"; History.EvaluateChange( familyChanges, "Name", string.Empty, familyGroup.Name ); int? campusId = cpCampus.SelectedValueAsInt(); if (campusId.HasValue) { History.EvaluateChange( familyChanges, "Campus", string.Empty, CampusCache.Read( campusId.Value ).Name ); } familyGroup.CampusId = campusId; foreach(var familyMember in familyMembers) { var person = familyMember.Person; if ( person != null ) { familyGroup.Members.Add( familyMember ); var demographicChanges = new List<string>(); demographicChanges.Add( "Created" ); History.EvaluateChange( demographicChanges, "Record Status", string.Empty, person.RecordStatusReasonValueId.HasValue ? DefinedValueCache.GetName( person.RecordStatusReasonValueId.Value ) : string.Empty ); History.EvaluateChange( demographicChanges, "Title", string.Empty, person.TitleValueId.HasValue ? DefinedValueCache.GetName( person.TitleValueId ) : string.Empty ); History.EvaluateChange( demographicChanges, "First Name", string.Empty, person.FirstName); History.EvaluateChange( demographicChanges, "Nick Name", string.Empty, person.NickName ); History.EvaluateChange( demographicChanges, "Middle Name", string.Empty, person.MiddleName ); History.EvaluateChange( demographicChanges, "Last Name", string.Empty, person.LastName ); History.EvaluateChange( demographicChanges, "Gender", null, person.Gender ); History.EvaluateChange( demographicChanges, "Birth Date", null, person.BirthDate ); History.EvaluateChange( demographicChanges, "Connection Status", string.Empty, person.ConnectionStatusValueId.HasValue ? DefinedValueCache.GetName( person.ConnectionStatusValueId ) : string.Empty ); History.EvaluateChange( demographicChanges, "Graduation Date", null, person.GraduationDate ); familyDemographicChanges.Add( person.Guid, demographicChanges ); var memberChanges = new List<string>(); string roleName = familyGroupType.Roles[familyMember.GroupRoleId] ?? string.Empty; History.EvaluateChange( memberChanges, "Role", string.Empty, roleName ); familyMemberChanges.Add( person.Guid, memberChanges ); } } if ( !String.IsNullOrWhiteSpace( tbStreet1.Text ) || !String.IsNullOrWhiteSpace( tbStreet2.Text ) || !String.IsNullOrWhiteSpace( tbCity.Text ) || !String.IsNullOrWhiteSpace( tbZip.Text ) ) { string addressChangeField = "Address"; var groupLocation = new GroupLocation(); var location = new LocationService().Get( tbStreet1.Text, tbStreet2.Text, tbCity.Text, ddlState.SelectedValue, tbZip.Text ); groupLocation.Location = location; Guid locationTypeGuid = Guid.Empty; if ( Guid.TryParse( GetAttributeValue( "LocationType" ), out locationTypeGuid ) ) { var locationType = Rock.Web.Cache.DefinedValueCache.Read( locationTypeGuid ); if ( locationType != null ) { addressChangeField = string.Format("{0} Address", locationType.Name); groupLocation.GroupLocationTypeValueId = locationType.Id; } } familyGroup.GroupLocations.Add( groupLocation ); History.EvaluateChange( familyChanges, addressChangeField, string.Empty, groupLocation.Location.ToString() ); } groupService.Add( familyGroup, CurrentPersonId ); groupService.Save( familyGroup, CurrentPersonId ); var historyService = new HistoryService(); historyService.SaveChanges( typeof( Group ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), familyGroup.Id, familyChanges, CurrentPersonId ); var personService = new PersonService(); foreach ( var groupMember in familyMembers ) { var person = personService.Get( groupMember.PersonId ); if ( person != null ) { var changes = familyDemographicChanges[person.Guid]; if ( groupMember.GroupRoleId != _childRoleId ) { person.GivingGroupId = familyGroup.Id; personService.Save( person, CurrentPersonId ); History.EvaluateChange( changes, "Giving Group", string.Empty, familyGroup.Name ); } foreach ( var attributeControl in attributeControls ) { foreach ( var attribute in attributeControl.AttributeList ) { string attributeValue = person.GetAttributeValue( attribute.Key ); if ( !string.IsNullOrWhiteSpace( attributeValue ) ) { Rock.Attribute.Helper.SaveAttributeValue( person, attribute, attributeValue, CurrentPersonId ); attributeValue = attribute.FieldType.Field.FormatValue( null, attributeValue, attribute.QualifierValues, false ); History.EvaluateChange( changes, attribute.Name, string.Empty, attributeValue ); } } } historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_DEMOGRAPHIC_CHANGES.AsGuid(), person.Id, changes, CurrentPersonId ); historyService.SaveChanges( typeof( Person ), Rock.SystemGuid.Category.HISTORY_PERSON_FAMILY_CHANGES.AsGuid(), person.Id, familyMemberChanges[person.Guid], familyGroup.Name, typeof( Group), familyGroup.Id, CurrentPersonId ); } } } } } ); Response.Redirect( string.Format( "~/Person/{0}", familyMembers[0].Person.Id ), false ); } } } }
private void BindFamilies() { if ( Person != null && Person.Id > 0 ) { Guid familyGroupGuid = new Guid( Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY ); var memberService = new GroupMemberService(); var families = memberService.Queryable() .Where( m => m.PersonId == Person.Id && m.Group.GroupType.Guid == familyGroupGuid ) .Select( m => m.Group ) .ToList(); if ( !families.Any() ) { var role = new GroupTypeRoleService().Get( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ); if ( role != null && role.GroupTypeId.HasValue ) { var groupMember = new GroupMember(); groupMember.PersonId = Person.Id; groupMember.GroupRoleId = role.Id; var family = new Group(); family.Name = Person.LastName; family.GroupTypeId = role.GroupTypeId.Value; family.Members.Add( groupMember ); var groupService = new GroupService(); groupService.Add( family, CurrentPersonId ); groupService.Save( family, CurrentPersonId ); families.Add( groupService.Get( family.Id ) ); } } rptrFamilies.DataSource = families; rptrFamilies.DataBind(); } }
/// <summary> /// Handles the Delete event of the gGroups 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 gGroups_Delete( object sender, RowEventArgs e ) { // NOTE: Very similar code in GroupDetail.btnDelete_Click RockTransactionScope.WrapTransaction( () => { GroupService groupService = new GroupService(); AuthService authService = new AuthService(); Group group = groupService.Get( (int)e.RowKeyValue ); if ( group != null ) { string errorMessage; if ( !groupService.CanDelete( group, out errorMessage ) ) { mdGridWarning.Show( errorMessage, ModalAlertType.Information ); return; } bool isSecurityRoleGroup = group.IsSecurityRole || group.GroupType.Guid.Equals( Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid() ); if (isSecurityRoleGroup) { Rock.Security.Role.Flush( group.Id ); foreach ( var auth in authService.Queryable().Where( a => a.GroupId == group.Id ).ToList() ) { authService.Delete( auth, CurrentPersonId ); authService.Save( auth, CurrentPersonId ); } } groupService.Delete( group, CurrentPersonId ); groupService.Save( group, CurrentPersonId ); if ( isSecurityRoleGroup ) { Rock.Security.Authorization.Flush(); } } } ); BindGrid(); }
private Person GetPerson() { using ( new UnitOfWorkScope() ) { var personService = new PersonService(); var personMatches = personService.GetByEmail( txtEmail.Text ).Where( p => p.LastName.Equals( txtLastName.Text, StringComparison.OrdinalIgnoreCase ) && ( ( p.FirstName != null && p.FirstName.Equals( txtFirstName.Text, StringComparison.OrdinalIgnoreCase ) ) || ( p.NickName != null && p.NickName.Equals( txtFirstName.Text, StringComparison.OrdinalIgnoreCase ) ) ) ); if ( personMatches.Count() == 1 ) { return personMatches.FirstOrDefault(); } else { Person person = new Person(); person.FirstName = txtFirstName.Text; person.LastName = txtLastName.Text; person.Email = txtEmail.Text; // Create Family Role var groupMember = new GroupMember(); groupMember.Person = person; groupMember.GroupRole = new GroupTypeRoleService().Get( new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT ) ); // Create Family var group = new Group(); group.Members.Add( groupMember ); group.Name = person.LastName + " Family"; group.GroupTypeId = GroupTypeCache.GetFamilyGroupType().Id; // Save person/family var groupService = new GroupService(); groupService.Add( group, CurrentPersonId ); groupService.Save( group, CurrentPersonId ); return personService.Get( person.Id ); } } }