Inheritance: Rock.Web.UI.PersonBlock
        private void AddGroupMember()
        {
            int defaultRoleId         = _groupType.DefaultGroupRoleId ?? _groupType.Roles.Select(r => r.Id).FirstOrDefault();
            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 groupMember = new GroupMember();

            groupMember.GroupMemberStatus = GroupMemberStatus.Active;
            groupMember.GroupRoleId       = defaultRoleId;
            groupMember.Person            = person;

            if (GetAttributeValue("EnableCommonLastName").AsBoolean())
            {
                if (GroupMembers.Count > 0)
                {
                    person.LastName = GroupMembers.FirstOrDefault().Person.LastName;
                }
            }

            GroupMembers.Add(groupMember);
        }
        /// <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)
        {
            if (_isFamilyGroupType)
            {
                var adults = GroupMembers.Where(m => m.GroupRoleId != _childRoleId).ToList();
                ddlMaritalStatus.Visible = adults.Any();
            }

            base.OnPreRender(e);
        }
        /// <summary>
        /// Handles the DeleteClick event of the groupMemberRow 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 groupMemberRow_DeleteClick(object sender, EventArgs e)
        {
            NewGroupMembersRow row = sender as NewGroupMembersRow;
            var groupMember        = GroupMembers.FirstOrDefault(m => m.Person.Guid.Equals(row.PersonGuid));

            if (groupMember != null)
            {
                GroupMembers.Remove(groupMember);
            }

            CreateControls(true);
        }
        void lbRemoveMember_Click(object sender, EventArgs e)
        {
            Guid personGuid  = ((LinkButton)sender).ID.Substring(15).Replace("_", "-").AsGuid();
            var  groupMember = GroupMembers.Where(f => f.Person.Guid.Equals(personGuid)).FirstOrDefault();

            if (groupMember != null)
            {
                GroupMembers.Remove(groupMember);
                Duplicates.Remove(personGuid);
                if (!GroupMembers.Any())
                {
                    AddGroupMember();
                    CurrentPageIndex = 0;
                }
                CreateControls(true);
            }
        }
        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 group members (any group) that have same location
            var othersAtAddress = new List <int>();

            string locationKey = GetLocationKey();

            if (!string.IsNullOrWhiteSpace(locationKey) && _verifiedLocations.ContainsKey(locationKey))
            {
                int?locationId = _verifiedLocations[locationKey];
                if (locationId.HasValue)
                {
                    var location = locationService.Get(locationId.Value);
                    if (location != null)
                    {
                        othersAtAddress = groupService
                                          .Queryable().AsNoTracking()
                                          .Where(g =>
                                                 g.GroupTypeId == _locationType.Id &&
                                                 g.GroupLocations.Any(l => l.LocationId == location.Id))
                                          .SelectMany(g => g.Members)
                                          .Select(m => m.PersonId)
                                          .ToList();
                    }
                }
            }

            foreach (var person in GroupMembers
                     .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());
        }
        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 (GroupMembers.Any())
                    {
                        if (CurrentPageIndex == (attributeControls.Count + 1) && FindDuplicates())
                        {
                            CurrentPageIndex++;
                            CreateControls(true);
                        }
                        else
                        {
                            var rockContext = new RockContext();

                            Guid?parentGroupGuid = GetAttributeValue("ParentGroup").AsGuidOrNull();

                            rockContext.WrapTransaction(() =>
                            {
                                Group group = null;
                                if (_isFamilyGroupType)
                                {
                                    group = GroupService.SaveNewFamily(rockContext, GroupMembers, cpCampus.SelectedValueAsInt(), true);
                                }
                                else
                                {
                                    group = GroupService.SaveNewGroup(rockContext, _groupType.Id, parentGroupGuid, tbGroupName.Text, GroupMembers, null, true);
                                }

                                if (group != null)
                                {
                                    string locationKey = GetLocationKey();
                                    if (!string.IsNullOrEmpty(locationKey) && _verifiedLocations.ContainsKey(locationKey))
                                    {
                                        GroupService.AddNewGroupAddress(rockContext, group, _locationType.Guid.ToString(), _verifiedLocations[locationKey]);
                                    }
                                }
                            });

                            Response.Redirect(string.Format("~/Person/{0}", GroupMembers[0].Person.Id), false);
                        }
                    }
                }
            }
        }