Inheritance: Rock.Web.UI.PersonBlock
        private void AddFamilyMember()
        {
            var familyGroupType = GroupTypeCache.GetFamilyGroupType();

            if (familyGroupType != null && familyGroupType.DefaultGroupRoleId != null)
            {
                int recordTypePersonId    = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_TYPE_PERSON.AsGuid()).Id;
                int recordStatusActiveId  = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE.AsGuid()).Id;
                var ConnectionStatusValue = DefinedValueCache.Read(GetAttributeValue("DefaultConnectionStatus").AsGuid());

                var person = new Person();
                person.Guid = Guid.NewGuid();
                person.RecordTypeValueId   = recordTypePersonId;
                person.RecordStatusValueId = recordStatusActiveId;
                person.Gender = Gender.Unknown;
                person.ConnectionStatusValueId = (ConnectionStatusValue != null) ? ConnectionStatusValue.Id : (int?)null;

                var familyMember = new GroupMember();
                familyMember.GroupMemberStatus = GroupMemberStatus.Active;
                familyMember.GroupRoleId       = familyGroupType.DefaultGroupRoleId.Value;
                familyMember.Person            = person;

                FamilyMembers.Add(familyMember);
            }
        }
        private List <FamilyMember> GetMembersOrdered()
        {
            var orderedMembers = new List <FamilyMember>();

            // Add adult males
            orderedMembers.AddRange(FamilyMembers
                                    .Where(m =>
                                           !m.Removed &&
                                           m.RoleGuid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT)) &&
                                           m.Gender == Gender.Male)
                                    .OrderByDescending(m => m.Age));

            // Add adult females
            orderedMembers.AddRange(FamilyMembers
                                    .Where(m =>
                                           !m.Removed &&
                                           m.RoleGuid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT)) &&
                                           m.Gender != Gender.Male)
                                    .OrderByDescending(m => m.Age));

            // Add non-adults
            orderedMembers.AddRange(FamilyMembers
                                    .Where(m =>
                                           !m.Removed &&
                                           !m.RoleGuid.Equals(new Guid(Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT)))
                                    .OrderByDescending(m => m.Age));

            return(orderedMembers);
        }
        /// <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>
        void modalAddPerson_SaveClick(object sender, EventArgs e)
        {
            if (hfActiveTab.Value == "Existing")
            {
                int?personId = ppExistingPerson.PersonId;
                if (personId.HasValue)
                {
                    using (new UnitOfWorkScope())
                    {
                        var person = new PersonService().Get(personId.Value);
                        if (person != null)
                        {
                            var familyMember = new FamilyMember();
                            familyMember.SetValuesFromPerson(person);

                            var familyRoleIds = familyRoles.Select(r => r.Id).ToList();

                            var existingFamilyRoles = new GroupMemberService().Queryable("GroupRole")
                                                      .Where(m => m.PersonId == person.Id && familyRoleIds.Contains(m.GroupRoleId))
                                                      .OrderBy(m => m.GroupRole.SortOrder)
                                                      .ToList();

                            var existingRole = existingFamilyRoles.Select(m => m.GroupRole).FirstOrDefault();
                            if (existingRole != null)
                            {
                                familyMember.RoleGuid = existingRole.Guid;
                                familyMember.RoleName = existingRole.Name;
                            }

                            familyMember.ExistingFamilyMember    = existingFamilyRoles.Any(r => r.GroupId == _family.Id);
                            familyMember.RemoveFromOtherFamilies = cbRemoveOtherFamilies.Checked;

                            FamilyMembers.Add(familyMember);
                        }
                    }
                }
            }
            else
            {
                var familyMember = new FamilyMember();
                familyMember.FirstName = tbNewPersonFirstName.Text;
                familyMember.LastName  = tbNewPersonLastName.Text;
                familyMember.Gender    = ddlNewPersonGender.SelectedValueAsEnum <Gender>();
                familyMember.BirthDate = dpNewPersonBirthDate.SelectedDate;
                var role = familyRoles.Where(r => r.Id == (rblNewPersonRole.SelectedValueAsInt() ?? 0)).FirstOrDefault();
                if (role != null)
                {
                    familyMember.RoleGuid = role.Guid;
                    familyMember.RoleName = role.Name;
                }
                FamilyMembers.Add(familyMember);
            }

            tbNewPersonFirstName.Required = false;
            tbNewPersonLastName.Required  = false;

            confirmExit.Enabled = true;

            BindMembers();
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            var adults = FamilyMembers.Where(m => m.GroupRoleId != _childRoleId).ToList();

            ddlMaritalStatus.Visible = adults.Any();

            base.OnPreRender(e);
        }
        private void BindMembers()
        {
            int i = 0;

            FamilyMembers.ForEach(m => m.Index = i++);

            lvMembers.DataSource = GetMembersOrdered();
            lvMembers.DataBind();
        }
        /// <summary>
        /// Handles the DeleteClick event of the familyMemberRow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void familyMemberRow_DeleteClick(object sender, EventArgs e)
        {
            NewFamilyMembersRow row = sender as NewFamilyMembersRow;
            var familyMember        = FamilyMembers.FirstOrDefault(m => m.Person.Guid.Equals(row.PersonGuid));

            if (familyMember != null)
            {
                FamilyMembers.Remove(familyMember);
            }

            CreateControls(true);
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (CurrentPageIndex == 0)
                {
                    string locationKey = GetLocationKey();
                    if (!string.IsNullOrEmpty(locationKey) && !_verifiedLocations.ContainsKey(locationKey))
                    {
                        using (var rockContext = new RockContext())
                        {
                            var location = new LocationService(rockContext).Get(acAddress.Street1, acAddress.Street2, acAddress.City, acAddress.State, acAddress.PostalCode, acAddress.Country);
                            _verifiedLocations.AddOrIgnore(locationKey, (location != null ? location.Id : (int?)null));
                        }
                    }
                }

                if (CurrentPageIndex < (attributeControls.Count + 1))
                {
                    CurrentPageIndex++;
                    CreateControls(true);
                }
                else
                {
                    if (FamilyMembers.Any())
                    {
                        if (CurrentPageIndex == (attributeControls.Count + 1) && FindDuplicates())
                        {
                            CurrentPageIndex++;
                            CreateControls(true);
                        }
                        else
                        {
                            var rockContext = new RockContext();
                            rockContext.WrapTransaction(() =>
                            {
                                var familyGroup = GroupService.SaveNewFamily(rockContext, FamilyMembers, cpCampus.SelectedValueAsInt(), true);
                                if (familyGroup != null)
                                {
                                    string locationKey = GetLocationKey();
                                    if (!string.IsNullOrEmpty(locationKey) && _verifiedLocations.ContainsKey(locationKey))
                                    {
                                        GroupService.AddNewFamilyAddress(rockContext, familyGroup, GetAttributeValue("LocationType"), _verifiedLocations[locationKey]);
                                    }
                                }
                            });

                            Response.Redirect(string.Format("~/Person/{0}", FamilyMembers[0].Person.Id), false);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load" /> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Page.IsPostBack)
            {
                foreach (var item in lvMembers.Items)
                {
                    var rblRole = item.FindControl("rblRole") as RadioButtonList;
                    if (rblRole != null)
                    {
                        int?roleId = rblRole.SelectedValueAsInt();
                        if (roleId.HasValue)
                        {
                            var role = familyRoles.Where(r => r.Id == roleId.Value).FirstOrDefault();
                            if (role != null)
                            {
                                int index        = (int)lvMembers.DataKeys[item.DataItemIndex]["Index"];
                                var familyMember = FamilyMembers.Where(m => m.Index == index).FirstOrDefault();
                                if (familyMember != null)
                                {
                                    familyMember.RoleGuid = role.Guid;
                                    familyMember.RoleName = role.Name;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (_family != null)
                {
                    tbFamilyName.Text         = _family.Name;
                    cpCampus.SelectedCampusId = _family.CampusId;

                    FamilyMembers = new List <FamilyMember>();
                    foreach (var familyMember in _family.Members)
                    {
                        FamilyMembers.Add(new FamilyMember(familyMember, true));
                    }
                    BindMembers();

                    FamilyAddresses = new List <FamilyAddress>();
                    foreach (var groupLocation in _family.GroupLocations)
                    {
                        FamilyAddresses.Add(new FamilyAddress(groupLocation));
                    }
                    BindLocations();
                }
            }
        }
        /// <summary>
        /// Handles the ItemDataBound event of the lvMembers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ListViewItemEventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        void lvMembers_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var familyMember = e.Item.DataItem as FamilyMember;
                if (familyMember != null)
                {
                    System.Web.UI.WebControls.Image imgPerson = e.Item.FindControl("imgPerson") as System.Web.UI.WebControls.Image;
                    if (imgPerson != null)
                    {
                        imgPerson.Visible = familyMember.PhotoId.HasValue;
                        if (familyMember.PhotoId.HasValue)
                        {
                            imgPerson.ImageUrl = string.Format("~/GetImage.ashx?id={0}", familyMember.PhotoId);
                        }
                    }

                    var rblRole = e.Item.FindControl("rblRole") as RadioButtonList;
                    if (rblRole != null)
                    {
                        rblRole.DataSource = familyRoles;
                        rblRole.DataBind();

                        var role = familyRoles.Where(r => r.Guid.Equals(familyMember.RoleGuid)).FirstOrDefault();
                        if (role != null)
                        {
                            rblRole.SelectedValue = role.Id.ToString();
                        }
                    }

                    int members = FamilyMembers.Where(m => !m.Removed).Count();

                    var lbNewFamily = e.Item.FindControl("lbNewFamily") as LinkButton;
                    if (lbNewFamily != null)
                    {
                        lbNewFamily.Visible = familyMember.ExistingFamilyMember && members > 1;
                    }

                    var lbRemoveMember = e.Item.FindControl("lbRemoveMember") as LinkButton;
                    if (lbRemoveMember != null)
                    {
                        lbRemoveMember.Visible = !familyMember.ExistingFamilyMember && members > 1;
                    }
                }
            }
        }
Exemple #10
0
        void lbRemoveMember_Click(object sender, EventArgs e)
        {
            Guid personGuid   = ((LinkButton)sender).ID.Substring(15).Replace("_", "-").AsGuid();
            var  familyMember = FamilyMembers.Where(f => f.Person.Guid.Equals(personGuid)).FirstOrDefault();

            if (familyMember != null)
            {
                FamilyMembers.Remove(familyMember);
                Duplicates.Remove(personGuid);
                if (!FamilyMembers.Any())
                {
                    AddFamilyMember();
                    CurrentPageIndex = 0;
                }
                CreateControls(true);
            }
        }
        /// <summary>
        /// Handles the ItemCommand event of the lvMembers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ListViewCommandEventArgs"/> instance containing the event data.</param>
        void lvMembers_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            int index        = (int)lvMembers.DataKeys[e.Item.DataItemIndex]["Index"];
            var familyMember = FamilyMembers.Where(m => m.Index == index).FirstOrDefault();

            if (familyMember != null)
            {
                if (e.CommandName == "Move")
                {
                    familyMember.Removed = true;
                }
                else if (e.CommandName == "Remove")
                {
                    FamilyMembers.RemoveAt(index);
                }

                confirmExit.Enabled = true;

                BindMembers();
            }
        }
Exemple #12
0
        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (CurrentPageIndex < (attributeControls.Count + 1))
                {
                    CurrentPageIndex++;
                    CreateControls(true);
                }
                else
                {
                    if (FamilyMembers.Any())
                    {
                        if (CurrentPageIndex == (attributeControls.Count + 1) && FindDuplicates())
                        {
                            CurrentPageIndex++;
                            CreateControls(true);
                        }
                        else
                        {
                            var rockContext = new RockContext();
                            rockContext.WrapTransaction(() =>
                            {
                                var familyGroup = GroupService.SaveNewFamily(rockContext, FamilyMembers, cpCampus.SelectedValueAsInt(), true);
                                if (familyGroup != null)
                                {
                                    GroupService.AddNewFamilyAddress(rockContext, familyGroup, GetAttributeValue("LocationType"),
                                                                     acAddress.Street1, acAddress.Street2, acAddress.City, acAddress.State, acAddress.PostalCode, acAddress.Country);
                                }
                            });

                            Response.Redirect(string.Format("~/Person/{0}", FamilyMembers[0].Person.Id), false);
                        }
                    }
                }
            }
        }
Exemple #13
0
        public bool FindDuplicates()
        {
            Duplicates = new Dictionary <Guid, List <Person> >();

            var rockContext     = new RockContext();
            var locationService = new LocationService(rockContext);
            var groupService    = new GroupService(rockContext);
            var personService   = new PersonService(rockContext);

            // Find any other family members (any family) that have same location
            var othersAtAddress = new List <int>();
            var familyGroupType = GroupTypeCache.GetFamilyGroupType();
            var location        = locationService.Get(acAddress.Street1, acAddress.Street2, acAddress.City, acAddress.State, acAddress.PostalCode, acAddress.Country);

            if (location != null)
            {
                othersAtAddress = groupService
                                  .Queryable().AsNoTracking()
                                  .Where(g =>
                                         g.GroupTypeId == familyGroupType.Id &&
                                         g.GroupLocations.Any(l => l.LocationId == location.Id))
                                  .SelectMany(g => g.Members)
                                  .Select(m => m.PersonId)
                                  .ToList();
            }

            foreach (var person in FamilyMembers
                     .Where(m =>
                            m.Person != null &&
                            m.Person.FirstName != "")
                     .Select(m => m.Person))
            {
                bool otherCriteria = false;
                var  personQry     = personService
                                     .Queryable().AsNoTracking()
                                     .Where(p =>
                                            p.FirstName == person.FirstName ||
                                            p.NickName == person.FirstName);

                if (othersAtAddress.Any())
                {
                    personQry = personQry
                                .Where(p => othersAtAddress.Contains(p.Id));
                }

                if (person.BirthDate.HasValue)
                {
                    otherCriteria = true;
                    personQry     = personQry
                                    .Where(p =>
                                           p.BirthDate.HasValue &&
                                           p.BirthDate.Value == person.BirthDate.Value);
                }

                if (_homePhone != null)
                {
                    var homePhoneNumber = person.PhoneNumbers.Where(p => p.NumberTypeValueId == _homePhone.Id).FirstOrDefault();
                    if (homePhoneNumber != null)
                    {
                        otherCriteria = true;
                        personQry     = personQry
                                        .Where(p =>
                                               p.PhoneNumbers.Any(n =>
                                                                  n.NumberTypeValueId == _homePhone.Id &&
                                                                  n.Number == homePhoneNumber.Number));
                    }
                }

                if (_cellPhone != null)
                {
                    var cellPhoneNumber = person.PhoneNumbers.Where(p => p.NumberTypeValueId == _cellPhone.Id).FirstOrDefault();
                    if (cellPhoneNumber != null)
                    {
                        otherCriteria = true;
                        personQry     = personQry
                                        .Where(p =>
                                               p.PhoneNumbers.Any(n =>
                                                                  n.NumberTypeValueId == _cellPhone.Id &&
                                                                  n.Number == cellPhoneNumber.Number));
                    }
                }

                if (!string.IsNullOrWhiteSpace(person.Email))
                {
                    otherCriteria = true;
                    personQry     = personQry
                                    .Where(p => p.Email == person.Email);
                }

                var dups = new List <Person>();
                if (otherCriteria)
                {
                    // If a birthday, email, phone, or address was entered, find anyone with same info and same first name
                    dups = personQry.ToList();
                }
                else
                {
                    // otherwise find people with same first and last name
                    dups = personQry
                           .Where(p => p.LastName == person.LastName)
                           .ToList();
                }
                if (dups.Any())
                {
                    Duplicates.Add(person.Guid, dups);
                }
            }

            return(Duplicates.Any());
        }