/// <summary> /// Saves the changes. /// </summary> /// <param name="item">The item.</param> protected void SaveChanges(RepeaterItem item) { var hfGroupId = item.FindControl("hfGroupId") as HiddenField; var cbCommunicationListIsSubscribed = item.FindControl("cbCommunicationListIsSubscribed") as RockCheckBox; var tglCommunicationPreference = item.FindControl("tglCommunicationPreference") as Toggle; var nbGroupNotification = item.FindControl("nbGroupNotification") as NotificationBox; nbGroupNotification.Visible = false; using (var rockContext = new RockContext()) { int groupId = hfGroupId.Value.AsInteger(); var groupMemberService = new GroupMemberService(rockContext); var group = new GroupService(rockContext).Get(groupId); var groupMemberRecordsForPerson = groupMemberService.Queryable().Where(a => a.GroupId == groupId && a.PersonId == this.CurrentPersonId).ToList(); if (groupMemberRecordsForPerson.Any()) { // normally there would be at most 1 group member record for the person, but just in case, mark them all foreach (var groupMember in groupMemberRecordsForPerson) { if (cbCommunicationListIsSubscribed.Checked) { if (groupMember.GroupMemberStatus == GroupMemberStatus.Inactive) { groupMember.GroupMemberStatus = GroupMemberStatus.Active; if (groupMember.Note == "Unsubscribed") { groupMember.Note = string.Empty; } } } else { if (groupMember.GroupMemberStatus == GroupMemberStatus.Active) { groupMember.GroupMemberStatus = GroupMemberStatus.Inactive; if (groupMember.Note.IsNullOrWhiteSpace()) { groupMember.Note = "Unsubscribed"; } } } groupMember.LoadAttributes(); CommunicationType communicationType = tglCommunicationPreference.Checked ? CommunicationType.Email : CommunicationType.SMS; groupMember.SetAttributeValue("PreferredCommunicationMedium", communicationType.ConvertToInt().ToString()); groupMember.SaveAttributeValue("PreferredCommunicationMedium", rockContext); } } else { // they are not currently in the Group if (cbCommunicationListIsSubscribed.Checked) { var groupMember = new GroupMember(); groupMember.PersonId = this.CurrentPersonId.Value; groupMember.GroupId = group.Id; int?defaultGroupRoleId = GroupTypeCache.Get(group.GroupTypeId).DefaultGroupRoleId; if (defaultGroupRoleId.HasValue) { groupMember.GroupRoleId = defaultGroupRoleId.Value; } else { nbGroupNotification.Text = "Unable to add to group."; nbGroupNotification.Details = "Group has no default group role"; nbGroupNotification.NotificationBoxType = NotificationBoxType.Danger; nbGroupNotification.Visible = true; } groupMember.GroupMemberStatus = GroupMemberStatus.Active; groupMember.LoadAttributes(); CommunicationType communicationType = tglCommunicationPreference.Checked ? CommunicationType.Email : CommunicationType.SMS; groupMember.SetAttributeValue("PreferredCommunicationMedium", communicationType.ConvertToInt().ToString()); if (groupMember.IsValidGroupMember(rockContext)) { groupMemberService.Add(groupMember); rockContext.SaveChanges(); groupMember.SaveAttributeValue("PreferredCommunicationMedium", rockContext); } else { // if the group member couldn't be added (for example, one of the group membership rules didn't pass), add the validation messages to the errormessages nbGroupNotification.Text = "Unable to add to group."; nbGroupNotification.Details = groupMember.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />"); nbGroupNotification.NotificationBoxType = NotificationBoxType.Danger; nbGroupNotification.Visible = true; } } } rockContext.SaveChanges(); } }