void rGroupMembers_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e) { int groupMemberId = int.MinValue; if (int.TryParse(e.CommandArgument.ToString(), out groupMemberId)) { var service = new GroupMemberService(); var groupMember = service.Get(groupMemberId); if (groupMember != null) { if (e.CommandName == "EditRole") { ShowModal(groupMember.Person, groupMember.GroupRoleId); } else if (e.CommandName == "RemoveRole") { if (IsKnownRelationships) { var inverseGroupMember = service.GetInverseRelationship(groupMember, false, CurrentPersonId); if (inverseGroupMember != null) { service.Delete(inverseGroupMember, CurrentPersonId); } } service.Delete(groupMember, CurrentPersonId); service.Save(groupMember, CurrentPersonId); BindData(); } } } }
/// <summary> /// Sets the owner. /// </summary> /// <param name="business">The business.</param> private void SetOwner(Person business) { var rockContext = new RockContext(); var groupMemberService = new GroupMemberService(rockContext); // Find the original owner/business relationships and remove them if they've changed var ownersKnownRelationshipGroupMember = groupMemberService.Queryable() .Where(g => g.GroupRole.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_BUSINESS)) && g.PersonId == business.Id) .FirstOrDefault(); if (ownersKnownRelationshipGroupMember != null) { var businessesKnownRelationshipGroupMember = groupMemberService.GetInverseRelationship(ownersKnownRelationshipGroupMember, false, CurrentPersonAlias); if (ppOwner.PersonId != businessesKnownRelationshipGroupMember.PersonId) { // the id of the person in the owner person picker doesn't match the id of the person in the businesses known relationship group with the principle role. var ownersKnownRelationshipGroup = ownersKnownRelationshipGroupMember.Group; ownersKnownRelationshipGroup.Members.Remove(ownersKnownRelationshipGroupMember); groupMemberService.Delete(ownersKnownRelationshipGroupMember); var businessesKnownRelationshipGroup = businessesKnownRelationshipGroupMember.Group; businessesKnownRelationshipGroup.Members.Remove(businessesKnownRelationshipGroupMember); groupMemberService.Delete(businessesKnownRelationshipGroupMember); rockContext.SaveChanges(); SetRelationships(business); } } else { SetRelationships(business); } }
void modalAddPerson_SaveClick(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(acPerson.Value)) { int personId = int.MinValue; if (int.TryParse(acPerson.Value, out personId)) { int?roleId = grpRole.GroupRoleId; if (roleId.HasValue) { 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) { var groupMember = memberService.Queryable() .Where(m => m.GroupId == group.Id && m.PersonId == personId && m.GroupRoleId == roleId.Value) .FirstOrDefault(); if (groupMember == null) { groupMember = new GroupMember(); groupMember.GroupId = group.Id; groupMember.PersonId = personId; groupMember.GroupRoleId = roleId.Value; memberService.Add(groupMember, CurrentPersonId); } memberService.Save(groupMember, CurrentPersonId); if (IsKnownRelationships) { var inverseGroupMember = memberService.GetInverseRelationship( groupMember, bool.Parse(GetAttributeValue("CreateGroup")), CurrentPersonId); if (inverseGroupMember != null) { memberService.Save(inverseGroupMember, CurrentPersonId); } } } } } } } BindData(); }
/// <summary> /// Binds the grid. /// </summary> private void BindGrid() { var rockContext = new RockContext(); var recordTypeValueId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_TYPE_BUSINESS.AsGuid()).Id; var activeRecordStatusValueId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE.AsGuid()).Id; int?businessRoleId = new GroupTypeRoleService(rockContext).Queryable() .Where(r => r.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_BUSINESS))) .Select(r => r.Id) .FirstOrDefault(); var queryable = new PersonService(rockContext).Queryable() .Where(q => q.RecordTypeValueId == recordTypeValueId && q.RecordStatusValueId == activeRecordStatusValueId); // Business Name Filter var businessName = gfBusinessFilter.GetUserPreference("Business Name"); if (!string.IsNullOrWhiteSpace(businessName)) { queryable = queryable.Where(a => a.FirstName.Contains(businessName)); } // Owner Filter int ownerId = 0; if (int.TryParse(gfBusinessFilter.GetUserPreference("Owner"), out ownerId) && ownerId != 0) { var members = queryable.SelectMany(a => a.Members).ToList(); foreach (var member in members) { if (member.GroupRoleId == businessRoleId) { var groupMemberService = new GroupMemberService(rockContext); var owner = groupMemberService.GetInverseRelationship(member, false, CurrentPersonAlias); if (owner.PersonId != ownerId) { queryable = queryable.Where(a => a.Id != member.PersonId); } } } } SortProperty sortProperty = gBusinessList.SortProperty; if (sortProperty != null) { gBusinessList.DataSource = queryable.Sort(sortProperty).ToList(); } else { gBusinessList.DataSource = queryable.OrderBy(q => q.FirstName).ToList(); } gBusinessList.DataBind(); }
/// <summary> /// Shows the edit details. /// </summary> /// <param name="business">The business.</param> private void ShowEditDetails(Person business) { if (business.Id > 0) { lTitle.Text = ActionTitle.Edit(business.FullName).FormatAsHtmlTitle(); tbBusinessName.Text = business.FirstName; tbStreet1.Text = business.GivingGroup.GroupLocations.FirstOrDefault().Location.Street1; tbStreet2.Text = business.GivingGroup.GroupLocations.FirstOrDefault().Location.Street2; tbCity.Text = business.GivingGroup.GroupLocations.FirstOrDefault().Location.City; ddlState.SelectedValue = business.GivingGroup.GroupLocations.FirstOrDefault().Location.State; tbZipCode.Text = business.GivingGroup.GroupLocations.FirstOrDefault().Location.Zip; pnbPhone.Text = business.PhoneNumbers.FirstOrDefault().ToString(); cbSms.Checked = business.PhoneNumbers.FirstOrDefault().IsMessagingEnabled; cbUnlisted.Checked = business.PhoneNumbers.FirstOrDefault().IsUnlisted; tbEmail.Text = business.Email; rblEmailPreference.SelectedValue = business.EmailPreference.ToString(); ddlCampus.SelectedValue = business.GivingGroup.CampusId.ToString(); var rockContext = new RockContext(); var groupMemberService = new GroupMemberService(rockContext); var knownRelationshipBusinessGroupMember = groupMemberService.Queryable() .Where(g => g.GroupRole.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_BUSINESS)) && g.PersonId == business.Id) .FirstOrDefault(); if (knownRelationshipBusinessGroupMember != null) { var inverseGroupMember = groupMemberService.GetInverseRelationship(knownRelationshipBusinessGroupMember, false, CurrentPersonAlias); if (inverseGroupMember != null) { ppOwner.PersonId = inverseGroupMember.Person.Id; ppOwner.PersonName = inverseGroupMember.Person.FullName; } } ddlGivingGroup.SelectedValue = business.Id.ToString(); ddlRecordStatus.SelectedValue = business.RecordStatusValueId.HasValue ? business.RecordStatusValueId.Value.ToString() : string.Empty; ddlReason.SelectedValue = business.RecordStatusReasonValueId.HasValue ? business.RecordStatusReasonValueId.Value.ToString() : string.Empty; ddlReason.Visible = business.RecordStatusReasonValueId.HasValue && business.RecordStatusValueId.Value == DefinedValueCache.Read(new Guid(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_INACTIVE)).Id; } else { lTitle.Text = ActionTitle.Add("Business").FormatAsHtmlTitle(); } SetEditMode(true); }
/// <summary> /// Handles the ItemCommand event of the rGroupMembers control. /// </summary> /// <param name="source">The source of the event.</param> /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param> protected void rGroupMembers_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e) { if (CanEdit) { int groupMemberId = e.CommandArgument.ToString().AsIntegerOrNull() ?? 0; if (groupMemberId != 0) { var rockContext = new RockContext(); var service = new GroupMemberService(rockContext); var groupMember = service.Get(groupMemberId); if (groupMember != null) { if (e.CommandName == "EditRole") { ShowModal(groupMember.Person, groupMember.GroupRoleId, groupMemberId); } else if (e.CommandName == "RemoveRole") { if (IsKnownRelationships) { var inverseGroupMember = service.GetInverseRelationship(groupMember, false); if (inverseGroupMember != null) { service.Delete(inverseGroupMember); } } service.Delete(groupMember); rockContext.SaveChanges(); BindData(); } } } } }
/// <summary> /// Handles the SaveClick event of the modalAddPerson 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 modalAddPerson_SaveClick(object sender, EventArgs e) { if (CanEdit) { if (ppPerson.PersonId.HasValue) { int?roleId = grpRole.GroupRoleId; if (roleId.HasValue) { var rockContext = new RockContext(); var memberService = new GroupMemberService(rockContext); var group = memberService.Queryable(true) .Where(m => m.PersonId == Person.Id && m.GroupRole.Guid == ownerRoleGuid) .Select(m => m.Group) .FirstOrDefault(); if (group != null) { GroupMember groupMember = null; int? groupMemberId = hfRoleId.Value.AsIntegerOrNull(); if (groupMemberId.HasValue) { groupMember = memberService.Queryable(true) .Where(m => m.Id == groupMemberId.Value) .FirstOrDefault(); } if (groupMember == null) { groupMember = new GroupMember(); groupMember.GroupId = group.Id; memberService.Add(groupMember); } GroupMember formerInverseGroupMember = null; if (IsKnownRelationships) { formerInverseGroupMember = memberService.GetInverseRelationship(groupMember, false); } groupMember.PersonId = ppPerson.PersonId.Value; groupMember.GroupRoleId = roleId.Value; rockContext.SaveChanges(); if (IsKnownRelationships) { var inverseGroupMember = memberService.GetInverseRelationship(groupMember, GetAttributeValue("CreateGroup").AsBoolean()); if (inverseGroupMember != null) { rockContext.SaveChanges(); if (formerInverseGroupMember != null && formerInverseGroupMember.Id != inverseGroupMember.Id) { memberService.Delete(formerInverseGroupMember); rockContext.SaveChanges(); } } } } } } HideDialog(); BindData(); } }
/// <summary> /// Handles the RowDataBound event of the gBusinessList control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="GridViewRowEventArgs"/> instance containing the event data.</param> protected void gBusinessList_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var rockContext = new RockContext(); var business = e.Row.DataItem as Person; int?ownerRoleId = new GroupTypeRoleService(rockContext).Queryable() .Where(r => r.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_OWNER))) .Select(r => r.Id) .FirstOrDefault(); var groupMemberService = new GroupMemberService(rockContext); var knownRelationshipBusinessGroupMember = groupMemberService.Queryable() .Where(g => g.GroupRole.Guid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_BUSINESS)) && g.PersonId == business.Id) .FirstOrDefault(); if (knownRelationshipBusinessGroupMember != null) { var inverseGroupMember = groupMemberService.GetInverseRelationship(knownRelationshipBusinessGroupMember, false, CurrentPersonAlias); if (inverseGroupMember != null) { Label lblOwner = e.Row.FindControl("lblOwner") as Label; lblOwner.Text = inverseGroupMember.Person.FullName; } } var phoneNumber = business.PhoneNumbers.FirstOrDefault().NumberFormatted; if (!string.IsNullOrWhiteSpace(phoneNumber)) { Label lblPhoneNumber = e.Row.FindControl("lblPhoneNumber") as Label; if (lblPhoneNumber != null) { lblPhoneNumber.Text = string.Format("{0}</br>", phoneNumber); } } if (!string.IsNullOrWhiteSpace(business.Email)) { Label lblEmail = e.Row.FindControl("lblEmail") as Label; if (lblEmail != null) { lblEmail.Text = string.Format("{0}", business.Email); } } var location = business.GivingGroup.GroupLocations.FirstOrDefault().Location; if (!string.IsNullOrWhiteSpace(location.Street1)) { Label lblStreet1 = e.Row.FindControl("lblStreet1") as Label; if (lblStreet1 != null) { lblStreet1.Text = string.Format("{0}</br>", location.Street1); } } if (!string.IsNullOrWhiteSpace(location.Street2)) { Label lblStreet2 = e.Row.FindControl("lblStreet2") as Label; if (lblStreet2 != null) { lblStreet2.Text = string.Format("{0}</br>", location.Street2); } } if (!string.IsNullOrWhiteSpace(location.City) || !string.IsNullOrWhiteSpace(location.State) || !string.IsNullOrWhiteSpace(location.Zip)) { Label lblCityStateZip = e.Row.FindControl("lblCityStateZip") as Label; if (lblCityStateZip != null) { lblCityStateZip.Text = string.Format("{0}, {1} {2}", location.City, location.State, location.Zip); } } } }