/// <summary> /// Saves the group member. /// </summary> /// <param name="parameters">The parameters.</param> /// <returns>The response to send back to the client.</returns> private CallbackResponse SaveGroupMember(Dictionary <string, object> parameters) { using (var rockContext = new RockContext()) { var groupMemberGuid = RequestContext.GetPageParameter(PageParameterKeys.GroupMemberGuid).AsGuid(); var member = new GroupMemberService(rockContext).Get(groupMemberGuid); if (member == null || (!member.Group.IsAuthorized(Authorization.EDIT, RequestContext.CurrentPerson) && !member.Group.IsAuthorized(Authorization.MANAGE_MEMBERS, RequestContext.CurrentPerson))) { return(new CallbackResponse { Error = "You are not authorized to edit members of this group." }); } member.LoadAttributes(rockContext); // // Verify and save all the property values. // if (AllowRoleChange) { member.GroupRoleId = (( string )parameters["role"]).AsInteger(); } if (AllowMemberStatusChange) { member.GroupMemberStatus = (( string )parameters["memberstatus"]).ConvertToEnum <GroupMemberStatus>(); } if (AllowNoteEdit) { member.Note = ( string )parameters["note"]; } // // Save all the attribute values. // MobileHelper.UpdateEditAttributeValues(member, parameters, GetEditableAttributes(member)); // // Save all changes to database. // rockContext.WrapTransaction(() => { rockContext.SaveChanges(); member.SaveAttributeValues(rockContext); }); } if (MemberDetailPage.HasValue) { return(new CallbackResponse { Command = "ReplacePage", CommandParameter = $"{MemberDetailPage}?GroupMemberGuid={RequestContext.GetPageParameter( PageParameterKeys.GroupMemberGuid )}" }); } else { return(new CallbackResponse { Command = "PopPage", CommandParameter = "true" }); } }
/// <summary> /// Handles the Click event of the btnSaveEditPreferences 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 btnSaveEditPreferences_Click(object sender, EventArgs e) { var rockContext = new RockContext(); var groupMember = new GroupMemberService(rockContext).Get(hfGroupMemberId.Value.AsInteger()); var personService = new PersonService(rockContext); var person = personService.Get(groupMember.PersonId); int?orphanedPhotoId = null; if (person.PhotoId != imgProfilePhoto.BinaryFileId) { orphanedPhotoId = person.PhotoId; person.PhotoId = imgProfilePhoto.BinaryFileId; // add or update the Photo Verify group to have this person as Pending since the photo was changed or deleted using (var photoRequestRockContext = new RockContext()) { GroupMemberService groupMemberService = new GroupMemberService(photoRequestRockContext); Group photoRequestGroup = new GroupService(photoRequestRockContext).Get(Rock.SystemGuid.Group.GROUP_PHOTO_REQUEST.AsGuid()); var photoRequestGroupMember = groupMemberService.Queryable().Where(a => a.GroupId == photoRequestGroup.Id && a.PersonId == person.Id).FirstOrDefault(); if (photoRequestGroupMember == null) { photoRequestGroupMember = new GroupMember(); photoRequestGroupMember.GroupId = photoRequestGroup.Id; photoRequestGroupMember.PersonId = person.Id; photoRequestGroupMember.GroupRoleId = photoRequestGroup.GroupType.DefaultGroupRoleId ?? -1; groupMemberService.Add(photoRequestGroupMember); } photoRequestGroupMember.GroupMemberStatus = GroupMemberStatus.Pending; photoRequestRockContext.SaveChanges(); } } // Save the GroupMember Attributes groupMember.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phGroupMemberAttributes, groupMember); // Save selected Person Attributes (The ones picked in Block Settings) var personAttributes = this.GetAttributeValue("PersonAttributes").SplitDelimitedValues().AsGuidList().Select(a => AttributeCache.Get(a)); if (personAttributes.Any()) { person.LoadAttributes(rockContext); foreach (var personAttribute in personAttributes) { Control attributeControl = phPersonAttributes.FindControl(string.Format("attribute_field_{0}", personAttribute.Id)); if (attributeControl != null) { // Save Attribute value to the database string newValue = personAttribute.FieldType.Field.GetEditValue(attributeControl, personAttribute.QualifierValues); Rock.Attribute.Helper.SaveAttributeValue(person, personAttribute, newValue, rockContext); } } } // Save everything else to the database in a db transaction rockContext.WrapTransaction(() => { rockContext.SaveChanges(); groupMember.SaveAttributeValues(rockContext); if (orphanedPhotoId.HasValue) { BinaryFileService binaryFileService = new BinaryFileService(rockContext); var binaryFile = binaryFileService.Get(orphanedPhotoId.Value); if (binaryFile != null) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; rockContext.SaveChanges(); } } // if they used the ImageEditor, and cropped it, the uncropped file is still in BinaryFile. So clean it up if (imgProfilePhoto.CropBinaryFileId.HasValue) { if (imgProfilePhoto.CropBinaryFileId != person.PhotoId) { BinaryFileService binaryFileService = new BinaryFileService(rockContext); var binaryFile = binaryFileService.Get(imgProfilePhoto.CropBinaryFileId.Value); if (binaryFile != null && binaryFile.IsTemporary) { string errorMessage; if (binaryFileService.CanDelete(binaryFile, out errorMessage)) { binaryFileService.Delete(binaryFile); rockContext.SaveChanges(); } } } } }); ShowView(hfGroupId.Value.AsInteger(), hfGroupMemberId.Value.AsInteger()); }
public BlockActionResult SaveMember(Guid groupMemberGuid, MemberDataViewModel groupMemberData) { using (var rockContext = new RockContext()) { var member = new GroupMemberService(rockContext).Get(groupMemberGuid); if (member == null || (!member.Group.IsAuthorized(Authorization.EDIT, RequestContext.CurrentPerson) && !member.Group.IsAuthorized(Authorization.MANAGE_MEMBERS, RequestContext.CurrentPerson))) { return(ActionBadRequest("You are not authorized to edit members of this group.")); } member.LoadAttributes(rockContext); // Verify and save the member role. if (AllowRoleChange) { if (!groupMemberData.RoleGuid.HasValue) { return(ActionBadRequest("Invalid data.")); } var groupRole = GroupTypeCache.Get(member.Group.GroupTypeId) .Roles .FirstOrDefault(r => r.Guid == groupMemberData.RoleGuid.Value); if (groupRole == null) { return(ActionBadRequest("Invalid data.")); } member.GroupRoleId = groupRole.Id; } // Verify and save the member status. if (AllowMemberStatusChange) { if (!groupMemberData.MemberStatus.HasValue) { return(ActionBadRequest("Invalid data.")); } member.GroupMemberStatus = groupMemberData.MemberStatus.Value; } // Verify and save the note. if (AllowNoteEdit) { if (groupMemberData.Note == null) { return(ActionBadRequest("Invalid data.")); } member.Note = groupMemberData.Note; } // Save all the attribute values. if (groupMemberData.FieldValues != null) { member.LoadAttributes(); var attributes = GetEditableAttributes(member); foreach (var attribute in attributes) { if (!groupMemberData.FieldValues.TryGetValue(attribute.Key, out var value)) { continue; } member.SetAttributeValue(attribute.Key, value); } } // Save all changes to database. rockContext.WrapTransaction(() => { rockContext.SaveChanges(); member.SaveAttributeValues(rockContext); }); return(ActionOk()); } }