Exemplo n.º 1
0
        public IQueryable<PersonSearchResult> Search( string name, bool includeHtml)
        {
            int count = 20;
            bool reversed;
            IOrderedQueryable<Person> sortedPersonQry = new PersonService().Queryable().QueryByName( name, out reversed );

            var topQry = sortedPersonQry.Take( count );
            List<Person> sortedPersonList = topQry.ToList();

            var appPath = System.Web.VirtualPathUtility.ToAbsolute( "~" );
            string itemDetailFormat = @"
<div class='picker-select-item-details clearfix' style='display: none;'>
	{0}
	<div class='contents'>
        {1}
	</div>
</div>
";

            Guid activeRecord = new Guid( SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE );
 
            // figure out Family, Address, Spouse
            GroupMemberService groupMemberService = new GroupMemberService();

            List<PersonSearchResult> searchResult = new List<PersonSearchResult>();
            foreach ( var person in sortedPersonList)
            {
                PersonSearchResult personSearchResult = new PersonSearchResult();
                personSearchResult.Name = reversed ? person.FullNameReversed : person.FullName;
                personSearchResult.ImageHtmlTag = Person.GetPhotoImageTag( person.PhotoId, person.Gender, 50, 50 );
                personSearchResult.Age = person.Age.HasValue ? person.Age.Value : -1;
                personSearchResult.ConnectionStatus = person.ConnectionStatusValue != null ? person.ConnectionStatusValue.Name : string.Empty;
                personSearchResult.Gender = person.Gender.ConvertToString();

                if ( person.RecordStatusValue != null )
                {
                    personSearchResult.RecordStatus = person.RecordStatusValue.Name;
                    personSearchResult.IsActive = person.RecordStatusValue.Guid.Equals( activeRecord );
                }
                else
                {
                    personSearchResult.RecordStatus = string.Empty;
                    personSearchResult.IsActive = false;
                }

                personSearchResult.Id = person.Id;

                if ( includeHtml )
                {
                    string imageHtml = null;

                    imageHtml = Person.GetPhotoImageTag(person.PhotoId, person.Gender, 65, 65);

                    string personInfo = string.Empty;

                    var groupMemberQry = groupMemberService.Queryable().Where( a => a.PersonId.Equals( person.Id ) );
                    List<GroupMember> personGroupMember = groupMemberQry.ToList();

                    Guid familyGuid = new Guid( Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY );
                    Guid adultGuid = new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT );

                    GroupMember familyGroupMember = personGroupMember.Where( a => a.Group.GroupType.Guid.Equals( familyGuid ) ).FirstOrDefault();
                    if ( familyGroupMember != null )
                    {
                        personInfo += familyGroupMember.GroupRole.Name;
                        if ( person.Age != null )
                        {
                            personInfo += " <em>(" + person.Age.ToString() + " yrs old)</em>";
                        }

                        // Figure out spouse (Implied by "the other GROUPROLE_FAMILY_MEMBER_ADULT that is of the opposite gender")
                        if ( familyGroupMember.GroupRole.Guid.Equals( adultGuid ) )
                        {
                            person.GetSpouse();
                            GroupMember spouseMember = familyGroupMember.Group.Members.Where( a => !a.PersonId.Equals( person.Id ) && a.GroupRole.Guid.Equals( adultGuid ) ).FirstOrDefault();
                            if ( spouseMember != null )
                            {
                                if ( !familyGroupMember.Person.Gender.Equals( spouseMember.Person.Gender ) )
                                {
                                    personInfo += "<p><strong>Spouse:</strong> " + spouseMember.Person.FullName + "</p>";
                                }
                            }
                        }
                    }
                    else
                    {
                        if ( person.Age != null )
                        {
                            personInfo += person.Age.ToString() + " yrs old";
                        }
                    }

                    if ( familyGroupMember != null )
                    {
                        var groupLocation = familyGroupMember.Group.GroupLocations.FirstOrDefault();
                        if ( groupLocation != null )
                        {
                            var location = groupLocation.Location;
                            if ( location != null )
                            {
                                string streetInfo;
                                if ( !string.IsNullOrWhiteSpace( location.Street1 ) )
                                {
                                    streetInfo = location.Street1 + " " + location.Street2;
                                }
                                else
                                {
                                    streetInfo = location.Street2;
                                }

                                string addressHtml = string.Format( "<h5>Address</h5>{0} <br />{1}, {2}, {3}", streetInfo, location.City, location.State, location.Zip );
                                personInfo += addressHtml;
                            }
                        }
                    }

                    personSearchResult.PickerItemDetailsHtml = string.Format( itemDetailFormat, imageHtml, personInfo );
                }

                searchResult.Add( personSearchResult );
            }
            
            return searchResult.AsQueryable();
        }
Exemplo n.º 2
0
        public IQueryable<PersonSearchResult> Search( string name, bool includeHtml )
        {
            int count = 20;
            bool reversed;
            bool allowFirstNameOnly = false;

            var searchComponent = Rock.Search.SearchContainer.GetComponent( typeof( Rock.Search.Person.Name ) );
            if ( searchComponent != null )
            {
                allowFirstNameOnly = searchComponent.GetAttributeValue( "FirstNameSearch" ).AsBoolean();
            }

            var rockContext = new Rock.Data.RockContext();
            IOrderedQueryable<Person> sortedPersonQry = new PersonService( rockContext )
                .GetByFullNameOrdered( name, true, false, allowFirstNameOnly, out reversed );

            var topQry = sortedPersonQry.Take( count );
            List<Person> sortedPersonList = topQry.ToList();

            var appPath = System.Web.VirtualPathUtility.ToAbsolute( "~" );
            string itemDetailFormat = @"
<div class='picker-select-item-details clearfix' style='display: none;'>
	{0}
	<div class='contents'>
        {1}
	</div>
</div>
";
            Guid activeRecord = new Guid( SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE );

            // figure out Family, Address, Spouse
            GroupMemberService groupMemberService = new GroupMemberService( rockContext );

            List<PersonSearchResult> searchResult = new List<PersonSearchResult>();
            foreach ( var person in sortedPersonList )
            {
                PersonSearchResult personSearchResult = new PersonSearchResult();
                personSearchResult.Name = reversed ? person.FullNameReversed : person.FullName;
                personSearchResult.ImageHtmlTag = Person.GetPhotoImageTag( person.PhotoId, person.Gender, 50, 50 );
                personSearchResult.Age = person.Age.HasValue ? person.Age.Value : -1;
                personSearchResult.ConnectionStatus = person.ConnectionStatusValueId.HasValue ? DefinedValueCache.Read( person.ConnectionStatusValueId.Value ).Name : string.Empty;
                personSearchResult.Gender = person.Gender.ConvertToString();
                personSearchResult.Email = person.Email;

                if ( person.RecordStatusValueId.HasValue )
                {
                    var recordStatus = DefinedValueCache.Read( person.RecordStatusValueId.Value );
                    personSearchResult.RecordStatus = recordStatus.Name;
                    personSearchResult.IsActive = recordStatus.Guid.Equals( activeRecord );
                }
                else
                {
                    personSearchResult.RecordStatus = string.Empty;
                    personSearchResult.IsActive = false;
                }

                personSearchResult.Id = person.Id;

                string imageHtml = string.Format(
                    "<div class='person-image' style='background-image:url({0}&width=65);background-size:cover;background-position:50%'></div>",
                    Person.GetPhotoUrl( person.PhotoId, person.Gender ) );

                string personInfo = string.Empty;

                Guid adultGuid = new Guid( Rock.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT );

                Guid familyGuid = new Guid( Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY );
                var familyGroupMember = groupMemberService.Queryable()
                    .Where( a => a.PersonId == person.Id )
                    .Where( a => a.Group.GroupType.Guid.Equals( familyGuid ) )
                    .Select( s => new
                    {
                        s.GroupRole,
                        GroupLocation = s.Group.GroupLocations.Select( a => a.Location ).FirstOrDefault()
                    } ).FirstOrDefault();

                if ( familyGroupMember != null )
                {
                    personInfo += familyGroupMember.GroupRole.Name;
                    if ( person.Age != null )
                    {
                        personInfo += " <em>(" + person.Age.ToString() + " yrs old)</em>";
                    }

                    if ( familyGroupMember.GroupRole.Guid.Equals( adultGuid ) )
                    {
                        var spouse = person.GetSpouse();
                        if ( spouse != null )
                        {
                            personInfo += "<p><strong>Spouse:</strong> " + spouse.FullName + "</p>";
                            personSearchResult.SpouseName = spouse.FullName;
                        }
                    }
                }
                else
                {
                    if ( person.Age != null )
                    {
                        personInfo += person.Age.ToString() + " yrs old";
                    }
                }

                if ( familyGroupMember != null )
                {
                    var location = familyGroupMember.GroupLocation;

                    if ( location != null )
                    {
                        string streetInfo;
                        if ( !string.IsNullOrWhiteSpace( location.Street1 ) )
                        {
                            streetInfo = location.Street1 + " " + location.Street2;
                        }
                        else
                        {
                            streetInfo = location.Street2;
                        }

                        string addressHtml = string.Format( "<h5>Address</h5>{0} <br />{1}, {2}, {3}", streetInfo, location.City, location.State, location.Zip );
                        personSearchResult.Address = location.ToString();
                        personInfo += addressHtml;
                    }

                    if ( includeHtml )
                    {
                        personSearchResult.PickerItemDetailsHtml = string.Format( itemDetailFormat, imageHtml, personInfo );
                    }
                }

                searchResult.Add( personSearchResult );
            }

            return searchResult.AsQueryable();
        }