/// <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) { if (ViewMode == VIEW_MODE_EDIT) { ChangeRequest changeRequest = new ChangeRequest { EntityId = Person.Id, EntityTypeId = EntityTypeCache.Get(typeof(Person)).Id, RequestorAliasId = CurrentPersonAliasId ?? 0 }; foreach (int attributeId in AttributeList) { var attribute = AttributeCache.Get(attributeId); if (Person != null && attribute.IsAuthorized(Authorization.EDIT, CurrentPerson)) { Control attributeControl = fsAttributes.FindControl(string.Format("attribute_field_{0}", attribute.Id)); if (attributeControl != null) { Person.SetAttributeValue(attribute.Key, attribute.FieldType.Field.GetEditValue(attributeControl, attribute.QualifierValues)); } } } changeRequest.EvaluateAttributes(Person); if (changeRequest.ChangeRecords.Any()) { var rockContext = new RockContext(); ChangeRequestService changeRequestService = new ChangeRequestService(rockContext); changeRequestService.Add(changeRequest); rockContext.SaveChanges(); List <string> errors; changeRequest.CompleteChanges(rockContext, out errors); } } else if (ViewMode == VIEW_MODE_ORDER && _canAdministrate) { // Split and delineate again to remove trailing delimiter var attributeOrder = hfAttributeOrder.Value.SplitDelimitedValues().ToList().AsDelimited("|"); SetAttributeValue("AttributeOrder", attributeOrder); SaveAttributeValues(); BindData(); } ViewMode = VIEW_MODE_VIEW; CreateControls(false); } }
/// <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 (GetAttributeValue(AttributeKeys.DisplayTerms).AsBoolean() && !cbTOS.Checked) { nbTOS.Visible = true; return; } var rockContext = new RockContext(); var personService = new PersonService(rockContext); var person = GetPerson(personService); var personAliasEntityType = EntityTypeCache.Get(typeof(PersonAlias)); if (person.Id != 0) { var changeRequest = new ChangeRequest { EntityTypeId = personAliasEntityType.Id, EntityId = person.PrimaryAliasId ?? 0, RequestorAliasId = CurrentPersonAliasId ?? 0 }; if (person.PhotoId != imgPhoto.BinaryFileId) { changeRequest.EvaluatePropertyChange(person, "PhotoId", imgPhoto.BinaryFileId); if (person.Photo != null) { changeRequest.EvaluatePropertyChange(person.Photo, "IsTemporary", true, true); } } changeRequest.EvaluatePropertyChange(person, "TitleValue", DefinedValueCache.Get(ddlTitle.SelectedValueAsInt() ?? 0)); changeRequest.EvaluatePropertyChange(person, "FirstName", tbFirstName.Text); changeRequest.EvaluatePropertyChange(person, "NickName", tbNickName.Text); changeRequest.EvaluatePropertyChange(person, "LastName", tbLastName.Text); changeRequest.EvaluatePropertyChange(person, "SuffixValue", DefinedValueCache.Get(ddlSuffix.SelectedValueAsInt() ?? 0)); var birthMonth = person.BirthMonth; var birthDay = person.BirthDay; var birthYear = person.BirthYear; var birthday = bpBirthDay.SelectedDate; if (birthday.HasValue) { // If setting a future birth date, subtract a century until birth date is not greater than today. var today = RockDateTime.Today; while (birthday.Value.CompareTo(today) > 0) { birthday = birthday.Value.AddYears(-100); } changeRequest.EvaluatePropertyChange(person, "BirthMonth", birthday.Value.Month); changeRequest.EvaluatePropertyChange(person, "BirthDay", birthday.Value.Day); if (birthday.Value.Year != DateTime.MinValue.Year) { changeRequest.EvaluatePropertyChange(person, "BirthYear", birthday.Value.Year); } else { changeRequest.EvaluatePropertyChange(person, "BirthYear", ( int? )null); } } if (ddlGradePicker.Visible) { changeRequest.EvaluatePropertyChange(person, "GraduationYear", ypGraduation.SelectedYear); } changeRequest.EvaluatePropertyChange(person, "Gender", rblGender.SelectedValue.ConvertToEnum <Gender>()); var primaryFamilyMembers = person.GetFamilyMembers(true).Where(m => m.PersonId == person.Id).ToList(); foreach (var member in primaryFamilyMembers) { changeRequest.EvaluatePropertyChange(member, "GroupRoleId", rblRole.SelectedValue.AsInteger(), true); } var primaryFamily = person.GetFamily(rockContext); var familyChangeRequest = new ChangeRequest { EntityTypeId = EntityTypeCache.Get(typeof(Group)).Id, EntityId = primaryFamily.Id, RequestorAliasId = CurrentPersonAliasId ?? 0 }; // update campus bool showCampus = GetAttributeValue("ShowCampusSelector").AsBoolean(); if (showCampus) { // Only update campus for adults GroupTypeRoleService groupTypeRoleService = new GroupTypeRoleService(rockContext); var adultGuid = Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT.AsGuid(); var adultRole = groupTypeRoleService.Get(adultGuid); if (rblRole.SelectedValue.AsInteger() == adultRole.Id) { familyChangeRequest.EvaluatePropertyChange(primaryFamily, "CampusId", cpCampus.SelectedCampusId); } } //Evaluate PhoneNumbers bool showPhoneNumbers = GetAttributeValue("ShowPhoneNumbers").AsBoolean(); if (showPhoneNumbers) { var phoneNumberTypeIds = new List <int>(); var phoneNumbersScreen = new List <PhoneNumber>(); bool smsSelected = false; foreach (RepeaterItem item in rContactInfo.Items) { HiddenField hfPhoneType = item.FindControl("hfPhoneType") as HiddenField; PhoneNumberBox pnbPhone = item.FindControl("pnbPhone") as PhoneNumberBox; CheckBox cbUnlisted = item.FindControl("cbUnlisted") as CheckBox; CheckBox cbSms = item.FindControl("cbSms") as CheckBox; if (hfPhoneType != null && pnbPhone != null && cbSms != null && cbUnlisted != null) { int phoneNumberTypeId; if (int.TryParse(hfPhoneType.Value, out phoneNumberTypeId)) { var phoneNumberList = person.PhoneNumbers.Where(n => n.NumberTypeValueId == phoneNumberTypeId).ToList(); var phoneNumber = phoneNumberList.FirstOrDefault(pn => pn.Number == PhoneNumber.CleanNumber(pnbPhone.Number)); string oldPhoneNumber = string.Empty; if (phoneNumber == null && pnbPhone.Number.IsNotNullOrWhiteSpace()) //Add number { phoneNumber = new PhoneNumber { PersonId = person.Id, NumberTypeValueId = phoneNumberTypeId, CountryCode = PhoneNumber.CleanNumber(pnbPhone.CountryCode), IsMessagingEnabled = !smsSelected && cbSms.Checked, Number = PhoneNumber.CleanNumber(pnbPhone.Number) }; var phoneComment = string.Format("{0}: {1}.", DefinedValueCache.Get(phoneNumberTypeId).Value, pnbPhone.Number); changeRequest.AddEntity(phoneNumber, rockContext, true, phoneComment); phoneNumbersScreen.Add(phoneNumber); } else if (phoneNumber != null && pnbPhone.Text.IsNotNullOrWhiteSpace()) // update number { changeRequest.EvaluatePropertyChange(phoneNumber, "Number", PhoneNumber.CleanNumber(pnbPhone.Number), true); changeRequest.EvaluatePropertyChange(phoneNumber, "IsMessagingEnabled", (!smsSelected && cbSms.Checked), true); changeRequest.EvaluatePropertyChange(phoneNumber, "IsUnlisted", cbUnlisted.Checked, true); phoneNumbersScreen.Add(phoneNumber); } } } } //Remove old phone numbers or changed var phoneNumbersToRemove = person.PhoneNumbers .Where(n => !phoneNumbersScreen.Any(n2 => n2.Number == n.Number && n2.NumberTypeValueId == n.NumberTypeValueId)).ToList(); foreach (var number in phoneNumbersToRemove) { var phoneComment = string.Format("{0}: {1}.", number.NumberTypeValue.Value, number.NumberFormatted); changeRequest.DeleteEntity(number, true, phoneComment); } } changeRequest.EvaluatePropertyChange(person, "Email", tbEmail.Text.Trim()); changeRequest.EvaluatePropertyChange(person, "EmailPreference", rblEmailPreference.SelectedValueAsEnum <EmailPreference>()); changeRequest.EvaluatePropertyChange(person, "CommunicationPreference", rblCommunicationPreference.SelectedValueAsEnum <CommunicationType>()); // if they used the ImageEditor, and cropped it, the non-cropped file is still in BinaryFile. So clean it up if (imgPhoto.CropBinaryFileId.HasValue) { if (imgPhoto.CropBinaryFileId != person.PhotoId) { BinaryFileService binaryFileService = new BinaryFileService(rockContext); var binaryFile = binaryFileService.Get(imgPhoto.CropBinaryFileId.Value); if (binaryFile != null && binaryFile.IsTemporary) { string errorMessage; if (binaryFileService.CanDelete(binaryFile, out errorMessage)) { binaryFileService.Delete(binaryFile); rockContext.SaveChanges(); } } } } // save family information if (pnlAddress.Visible) { var currentLocation = person.GetHomeLocation(); Location location = new Location { Street1 = acAddress.Street1, Street2 = acAddress.Street2, City = acAddress.City, State = acAddress.State, PostalCode = acAddress.PostalCode, }; var globalAttributesCache = GlobalAttributesCache.Get(); location.Country = globalAttributesCache.OrganizationCountry; location.Country = string.IsNullOrWhiteSpace(location.Country) ? "US" : location.Country; if ((currentLocation == null && location.Street1.IsNotNullOrWhiteSpace()) || (currentLocation != null && currentLocation.Street1 != location.Street1)) { LocationService locationService = new LocationService(rockContext); locationService.Add(location); rockContext.SaveChanges(); var previousLocationType = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_PREVIOUS.AsGuid()); var homeLocationType = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid()); GroupLocation groupLocation = new GroupLocation { CreatedByPersonAliasId = CurrentPersonAliasId, ModifiedByPersonAliasId = CurrentPersonAliasId, GroupId = primaryFamily.Id, LocationId = location.Id, GroupLocationTypeValueId = homeLocationType.Id, IsMailingLocation = true, IsMappedLocation = true }; var newGroupLocation = familyChangeRequest.AddEntity(groupLocation, rockContext, true, location.ToString()); var homelocations = primaryFamily.GroupLocations.Where(gl => gl.GroupLocationTypeValueId == homeLocationType.Id); foreach (var homelocation in homelocations) { familyChangeRequest.EvaluatePropertyChange( homelocation, "GroupLocationTypeValue", previousLocationType, true, homelocation.Location.ToString()); familyChangeRequest.EvaluatePropertyChange( homelocation, "IsMailingLocation", false, true, homelocation.Location.ToString()); } } } // Handle both Child and Adult attributes together here var attributeGuids = GetAttributeValue(AttributeKeys.PersonAttributesAdult).SplitDelimitedValues().AsGuidList(); attributeGuids.AddRange(GetAttributeValue(AttributeKeys.PersonAttributesChild).SplitDelimitedValues().AsGuidList()); if (attributeGuids.Count > 0) { person.LoadAttributes(); Helper.GetEditValues(phAttributes, person); changeRequest.EvaluateAttributes(person); } List <string> errors; if (changeRequest.ChangeRecords.Any() || (!familyChangeRequest.ChangeRecords.Any() && tbComments.Text.IsNotNullOrWhiteSpace())) { changeRequest.RequestorComment = tbComments.Text; ChangeRequestService changeRequestService = new ChangeRequestService(rockContext); changeRequestService.Add(changeRequest); rockContext.SaveChanges(); changeRequest.CompleteChanges(rockContext, out errors); } if (familyChangeRequest.ChangeRecords.Any()) { familyChangeRequest.RequestorComment = tbComments.Text; ChangeRequestService changeRequestService = new ChangeRequestService(rockContext); changeRequestService.Add(familyChangeRequest); rockContext.SaveChanges(); familyChangeRequest.CompleteChanges(rockContext, out errors); } } else { var primaryFamily = CurrentPerson.GetFamily(rockContext); person.PhotoId = imgPhoto.BinaryFileId; if (imgPhoto.BinaryFileId.HasValue) { BinaryFileService binaryFileService = new BinaryFileService(rockContext); var binaryFile = binaryFileService.Get(imgPhoto.BinaryFileId.Value); binaryFile.IsTemporary = false; } person.FirstName = tbFirstName.Text; person.NickName = tbNickName.Text; person.LastName = tbLastName.Text; person.TitleValueId = ddlTitle.SelectedValue.AsIntegerOrNull(); person.SuffixValueId = ddlSuffix.SelectedValue.AsIntegerOrNull(); var birthday = bpBirthDay.SelectedDate; if (birthday.HasValue) { // If setting a future birth date, subtract a century until birth date is not greater than today. var today = RockDateTime.Today; while (birthday.Value.CompareTo(today) > 0) { birthday = birthday.Value.AddYears(-100); } person.BirthMonth = birthday.Value.Month; person.BirthDay = birthday.Value.Day; if (birthday.Value.Year != DateTime.MinValue.Year) { person.BirthYear = birthday.Value.Year; } else { person.BirthYear = null; } } person.Gender = rblGender.SelectedValue.ConvertToEnum <Gender>(); person.Email = tbEmail.Text; person.EmailPreference = rblEmailPreference.SelectedValue.ConvertToEnum <EmailPreference>(); if (ddlGradePicker.Visible) { person.GraduationYear = ypGraduation.SelectedYear; } GroupMember groupMember = new GroupMember { PersonId = person.Id, GroupId = primaryFamily.Id, GroupRoleId = rblRole.SelectedValue.AsInteger() }; PersonService.AddPersonToFamily(person, true, primaryFamily.Id, rblRole.SelectedValue.AsInteger(), rockContext); PhoneNumberService phoneNumberService = new PhoneNumberService(rockContext); foreach (RepeaterItem item in rContactInfo.Items) { HiddenField hfPhoneType = item.FindControl("hfPhoneType") as HiddenField; PhoneNumberBox pnbPhone = item.FindControl("pnbPhone") as PhoneNumberBox; CheckBox cbUnlisted = item.FindControl("cbUnlisted") as CheckBox; CheckBox cbSms = item.FindControl("cbSms") as CheckBox; if (hfPhoneType != null && pnbPhone != null && cbSms != null && cbUnlisted != null && pnbPhone.Number.IsNotNullOrWhiteSpace()) { int phoneNumberTypeId; if (int.TryParse(hfPhoneType.Value, out phoneNumberTypeId)) { var phoneNumber = new PhoneNumber { PersonId = person.Id, NumberTypeValueId = phoneNumberTypeId, CountryCode = PhoneNumber.CleanNumber(pnbPhone.CountryCode), IsMessagingEnabled = cbSms.Checked, Number = PhoneNumber.CleanNumber(pnbPhone.Number) }; phoneNumberService.Add(phoneNumber); } } } rockContext.SaveChanges(); var changeRequest = new ChangeRequest { EntityTypeId = personAliasEntityType.Id, EntityId = person.PrimaryAliasId ?? 0, RequestorAliasId = CurrentPersonAliasId ?? 0, RequestorComment = "Added as new person from My Account." }; if (tbComments.Text.IsNotNullOrWhiteSpace()) { changeRequest.RequestorComment += "<br><br>Comment: " + tbComments.Text; } ChangeRequestService changeRequestService = new ChangeRequestService(rockContext); changeRequestService.Add(changeRequest); rockContext.SaveChanges(); List <string> errors; changeRequest.CompleteChanges(rockContext, out errors); } NavigateToParentPage(); }