private void Map() { string mapStylingFormat = @" <style> #map_wrapper {{ height: {0}px; }} #map_canvas {{ width: 100%; height: 100%; border-radius: 8px; }} </style>"; lMapStyling.Text = string.Format( mapStylingFormat, GetAttributeValue( "MapHeight" ) ); string settingGroupTypeId = GetAttributeValue( "GroupType" ); string queryStringGroupTypeId = PageParameter( "GroupTypeId" ); if ( ( string.IsNullOrWhiteSpace(settingGroupTypeId) && string.IsNullOrWhiteSpace(queryStringGroupTypeId) ) ) { pnlMap.Visible = false; lMessages.Text = "<div class='alert alert-warning'><strong>Group Mapper</strong> Please configure a group type to display as a block setting or pass a GroupTypeId as a query parameter.</div>"; } else { var rockContext = new RockContext(); pnlMap.Visible = true; int groupsMapped = 0; int groupsWithNoGeo = 0; StringBuilder sbGroupJson = new StringBuilder(); StringBuilder sbGroupsWithNoGeo = new StringBuilder(); Guid? groupType = null; int groupTypeId = -1; if ( !string.IsNullOrWhiteSpace( settingGroupTypeId ) ) { groupType = new Guid( settingGroupTypeId ); } else { if ( !string.IsNullOrWhiteSpace( queryStringGroupTypeId ) && Int32.TryParse( queryStringGroupTypeId, out groupTypeId ) ) { groupType = new GroupTypeService( rockContext ).Get( groupTypeId ).Guid; } } if ( groupType != null ) { Template template = null; if ( GetAttributeValue( "ShowMapInfoWindow" ).AsBoolean() ) { template = Template.Parse( GetAttributeValue( "InfoWindowContents" ).Trim() ); } else { template = Template.Parse( string.Empty ); } var groupPageRef = new PageReference( GetAttributeValue( "GroupDetailPage" ) ); // create group detail link for use in map's info window var personPageParams = new Dictionary<string, string>(); personPageParams.Add( "PersonId", string.Empty ); var personProfilePage = LinkedPageUrl( "PersonProfilePage", personPageParams ); var groupEntityType = EntityTypeCache.Read( typeof( Group ) ); var dynamicGroups = new List<dynamic>(); // Create query to get attribute values for selected attribute keys. var attributeKeys = GetAttributeValue( "Attributes" ).SplitDelimitedValues().ToList(); var attributeValues = new AttributeValueService( rockContext ).Queryable( "Attribute" ) .Where( v => v.Attribute.EntityTypeId == groupEntityType.Id && attributeKeys.Contains( v.Attribute.Key ) ); GroupService groupService = new GroupService( rockContext ); var groups = groupService.Queryable() .Where( g => g.GroupType.Guid == groupType ) .Select( g => new { Group = g, GroupId = g.Id, GroupName = g.Name, GroupGuid = g.Guid, GroupMemberTerm = g.GroupType.GroupMemberTerm, GroupCampus = g.Campus.Name, IsActive = g.IsActive, GroupLocation = g.GroupLocations .Where( l => l.Location.GeoPoint != null ) .Select( l => new { l.Location.Street1, l.Location.Street2, l.Location.City, l.Location.State, PostalCode = l.Location.PostalCode, Latitude = l.Location.GeoPoint.Latitude, Longitude = l.Location.GeoPoint.Longitude, Name = l.GroupLocationTypeValue.Value } ).FirstOrDefault(), GroupMembers = g.Members, AttributeValues = attributeValues .Where( v => v.EntityId == g.Id ) } ); if ( GetAttributeValue( "IncludeInactiveGroups" ).AsBoolean() == false ) { groups = groups.Where( g => g.IsActive == true ); } // Create dynamic object to include attribute values foreach ( var group in groups ) { dynamic dynGroup = new ExpandoObject(); dynGroup.GroupId = group.GroupId; dynGroup.GroupName = group.GroupName; // create group detail link for use in map's info window if ( groupPageRef.PageId > 0 ) { var groupPageParams = new Dictionary<string, string>(); groupPageParams.Add( "GroupId", group.GroupId.ToString() ); groupPageRef.Parameters = groupPageParams; dynGroup.GroupDetailPage = groupPageRef.BuildUrl(); } else { dynGroup.GroupDetailPage = string.Empty; } dynGroup.PersonProfilePage = personProfilePage; dynGroup.GroupMemberTerm = group.GroupMemberTerm; dynGroup.GroupCampus = group.GroupCampus; dynGroup.GroupLocation = group.GroupLocation; var groupAttributes = new List<dynamic>(); foreach ( AttributeValue value in group.AttributeValues ) { var attrCache = AttributeCache.Read( value.AttributeId ); var dictAttribute = new Dictionary<string, object>(); dictAttribute.Add( "Key", attrCache.Key ); dictAttribute.Add( "Name", attrCache.Name ); if ( attrCache != null ) { dictAttribute.Add( "Value", attrCache.FieldType.Field.FormatValueAsHtml( null, value.Value, attrCache.QualifierValues, false ) ); } else { dictAttribute.Add( "Value", value.Value ); } groupAttributes.Add( dictAttribute ); } dynGroup.Attributes = groupAttributes; var groupMembers = new List<dynamic>(); foreach ( GroupMember member in group.GroupMembers ) { var dictMember = new Dictionary<string, object>(); dictMember.Add( "Id", member.Person.Id ); dictMember.Add( "GuidP", member.Person.Guid ); dictMember.Add( "NickName", member.Person.NickName ); dictMember.Add( "LastName", member.Person.LastName ); dictMember.Add( "RoleName", member.GroupRole.Name ); dictMember.Add( "Email", member.Person.Email ); dictMember.Add( "PhotoGuid", member.Person.Photo != null ? member.Person.Photo.Guid : Guid.Empty ); var phoneTypes = new List<dynamic>(); foreach ( PhoneNumber p in member.Person.PhoneNumbers ) { var dictPhoneNumber = new Dictionary<string, object>(); dictPhoneNumber.Add( "Name", p.NumberTypeValue.Value ); dictPhoneNumber.Add( "Number", p.ToString() ); phoneTypes.Add( dictPhoneNumber ); } dictMember.Add( "PhoneTypes", phoneTypes ); groupMembers.Add( dictMember ); } dynGroup.GroupMembers = groupMembers; dynamicGroups.Add( dynGroup ); } // enable showing debug info if ( GetAttributeValue( "EnableDebug" ).AsBoolean() && IsUserAuthorized( Authorization.EDIT ) ) { lDebug.Visible = true; lDebug.Text = dynamicGroups.Take( 5 ).lavaDebugInfo(); } else { lDebug.Visible = false; lDebug.Text = string.Empty; } foreach ( var group in dynamicGroups ) { if ( group.GroupLocation != null && group.GroupLocation.Latitude != null ) { groupsMapped++; var groupDict = group as IDictionary<string, object>; string infoWindow = template.Render( Hash.FromDictionary( groupDict ) ).Replace( "\n", string.Empty ); sbGroupJson.Append( string.Format( @"{{ ""name"":""{0}"" , ""latitude"":""{1}"", ""longitude"":""{2}"", ""infowindow"":""{3}"" }},", HttpUtility.HtmlEncode( group.GroupName ), group.GroupLocation.Latitude, group.GroupLocation.Longitude, HttpUtility.HtmlEncode( infoWindow ) ) ); } else { groupsWithNoGeo++; if ( !string.IsNullOrWhiteSpace( group.GroupDetailPage ) ) { sbGroupsWithNoGeo.Append( string.Format( @"<li><a href='{0}'>{1}</a></li>", group.GroupDetailPage, group.GroupName ) ); } else { sbGroupsWithNoGeo.Append( string.Format( @"<li>{0}</li>", group.GroupName ) ); } } } string groupJson = sbGroupJson.ToString(); // remove last comma if ( groupJson.Length > 0 ) { groupJson = groupJson.Substring( 0, groupJson.Length - 1 ); } // add styling to map string styleCode = "null"; string markerColor = "FE7569"; DefinedValueCache dvcMapStyle = DefinedValueCache.Read( GetAttributeValue( "MapStyle" ).AsGuid() ); if ( dvcMapStyle != null ) { styleCode = dvcMapStyle.GetAttributeValue( "DynamicMapStyle" ); var colors = dvcMapStyle.GetAttributeValue( "Colors" ).Split( new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries ).ToList(); if ( colors.Any() ) { markerColor = colors.First().Replace( "#", "" ); } } // write script to page string mapScriptFormat = @" <script> Sys.Application.add_load(function () {{ var groupData = JSON.parse('{{ ""groups"" : [ {0} ]}}'); var showInfoWindow = {1}; var mapStyle = {2}; var pinColor = '{3}'; var pinImage = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|' + pinColor, new google.maps.Size(21, 34), new google.maps.Point(0,0), new google.maps.Point(10, 34)); var pinShadow = new google.maps.MarkerImage('http://chart.apis.google.com/chart?chst=d_map_pin_shadow', new google.maps.Size(40, 37), new google.maps.Point(0, 0), new google.maps.Point(12, 35)); initializeMap(); function initializeMap() {{ console.log(mapStyle); var map; var bounds = new google.maps.LatLngBounds(); var mapOptions = {{ mapTypeId: 'roadmap', styles: mapStyle }}; // Display a map on the page map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); map.setTilt(45); // Display multiple markers on a map if (showInfoWindow) {{ var infoWindow = new google.maps.InfoWindow(), marker, i; }} // Loop through our array of markers & place each one on the map $.each(groupData.groups, function (i, group) {{ var position = new google.maps.LatLng(group.latitude, group.longitude); bounds.extend(position); marker = new google.maps.Marker({{ position: position, map: map, title: htmlDecode(group.name), icon: pinImage, shadow: pinShadow }}); // Allow each marker to have an info window if (showInfoWindow) {{ google.maps.event.addListener(marker, 'click', (function (marker, i) {{ return function () {{ infoWindow.setContent(htmlDecode(groupData.groups[i].infowindow)); infoWindow.open(map, marker); }} }})(marker, i)); }} map.fitBounds(bounds); }}); // Override our map zoom level once our fitBounds function runs (Make sure it only runs once) var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {{ google.maps.event.removeListener(boundsListener); }}); }} function htmlDecode(input) {{ var e = document.createElement('div'); e.innerHTML = input; return e.childNodes.length === 0 ? """" : e.childNodes[0].nodeValue; }} }}); </script>"; string mapScript = string.Format( mapScriptFormat, groupJson, GetAttributeValue( "ShowMapInfoWindow" ).AsBoolean().ToString().ToLower(), styleCode, markerColor ); ScriptManager.RegisterStartupScript( pnlMap, pnlMap.GetType(), "group-mapper-script", mapScript, false ); if ( groupsMapped == 0 ) { pnlMap.Visible = false; lMessages.Text = @" <p> <div class='alert alert-warning fade in'>No groups were able to be mapped. You may want to check your configuration.</div> </p>"; } else { // output any warnings if ( groupsWithNoGeo > 0 ) { string messagesFormat = @" <p> <div class='alert alert-warning fade in'>Some groups could not be mapped. <button type='button' class='close' data-dismiss='alert' aria-hidden='true'><i class='fa fa-times'></i></button> <small><a data-toggle='collapse' data-parent='#accordion' href='#map-error-details'>Show Details</a></small> <div id='map-error-details' class='collapse'> <p class='margin-t-sm'> <strong>Groups That Could Not Be Mapped</strong> <ul> {0} </ul> </p> </div> </div> </p>"; lMessages.Text = string.Format( messagesFormat, sbGroupsWithNoGeo.ToString() ); } } } else { pnlMap.Visible = false; lMessages.Text = "<div class='alert alert-warning'><strong>Group Mapper</strong> Please configure a group type to display and a location type to use.</div>"; } } }
/// <summary> /// Binds the group placement grid. /// </summary> /// <param name="isExporting">if set to <c>true</c> [is exporting].</param> private void BindGroupPlacementGrid( bool isExporting = false ) { int? groupId = gpGroupPlacementParentGroup.SelectedValueAsInt(); int? instanceId = hfRegistrationInstanceId.Value.AsIntegerOrNull(); if ( instanceId.HasValue ) { using ( var rockContext = new RockContext() ) { // Start query for registrants var qry = new RegistrationRegistrantService( rockContext ) .Queryable( "PersonAlias.Person.PhoneNumbers.NumberTypeValue,Fees.RegistrationTemplateFee,GroupMember.Group" ).AsNoTracking() .Where( r => r.Registration.RegistrationInstanceId == instanceId.Value && r.PersonAlias != null && r.PersonAlias.Person != null ); if ( groupId.HasValue ) { var validGroupIds = new GroupService( rockContext ).GetAllDescendents( groupId.Value ) .Select( g => g.Id ) .ToList(); var existingPeopleInGroups = new GroupMemberService( rockContext ) .Queryable().AsNoTracking() .Where( m => validGroupIds.Contains( m.GroupId ) ) .Select( m => m.PersonId ) .ToList(); qry = qry.Where( r => !existingPeopleInGroups.Contains( r.PersonAlias.PersonId ) ); } bool preloadCampusValues = false; var registrantAttributeIds = new List<int>(); var personAttributesIds = new List<int>(); var groupMemberAttributesIds = new List<int>(); if ( RegistrantFields != null ) { // Check if campus is used preloadCampusValues = RegistrantFields .Any( f => f.FieldSource == RegistrationFieldSource.PersonField && f.PersonFieldType.HasValue && f.PersonFieldType.Value == RegistrationPersonFieldType.Campus ); // Get all the registrant attributes selected var registrantAttributes = RegistrantFields .Where( f => f.Attribute != null && f.FieldSource == RegistrationFieldSource.RegistrationAttribute ) .Select( f => f.Attribute ) .ToList(); registrantAttributeIds = registrantAttributes.Select( a => a.Id ).Distinct().ToList(); // Get all the person attributes selected var personAttributes = RegistrantFields .Where( f => f.Attribute != null && f.FieldSource == RegistrationFieldSource.PersonAttribute ) .Select( f => f.Attribute ) .ToList(); personAttributesIds = personAttributes.Select( a => a.Id ).Distinct().ToList(); // Get all the group member attributes selected to be on grid var groupMemberAttributes = RegistrantFields .Where( f => f.Attribute != null && f.FieldSource == RegistrationFieldSource.GroupMemberAttribute ) .Select( f => f.Attribute ) .ToList(); groupMemberAttributesIds = groupMemberAttributes.Select( a => a.Id ).Distinct().ToList(); } // Sort the query IOrderedQueryable<RegistrationRegistrant> orderedQry = null; SortProperty sortProperty = gGroupPlacements.SortProperty; if ( sortProperty != null ) { orderedQry = qry.Sort( sortProperty ); } else { orderedQry = qry .OrderBy( r => r.PersonAlias.Person.LastName ) .ThenBy( r => r.PersonAlias.Person.NickName ); } // Set the grids LinqDataSource which will run query and set results for current page gGroupPlacements.SetLinqDataSource<RegistrationRegistrant>( orderedQry ); if ( RegistrantFields != null ) { // Get the query results for the current page var currentPageRegistrants = gGroupPlacements.DataSource as List<RegistrationRegistrant>; if ( currentPageRegistrants != null ) { // Get all the registrant ids in current page of query results var registrantIds = currentPageRegistrants .Select( r => r.Id ) .Distinct() .ToList(); // Get all the person ids in current page of query results var personIds = currentPageRegistrants .Select( r => r.PersonAlias.PersonId ) .Distinct() .ToList(); // Get all the group member ids and the group id in current page of query results var groupMemberIds = new List<int>(); GroupLinks = new Dictionary<int, string>(); foreach ( var groupMember in currentPageRegistrants .Where( m => m.GroupMember != null && m.GroupMember.Group != null ) .Select( m => m.GroupMember ) ) { groupMemberIds.Add( groupMember.Id ); GroupLinks.AddOrIgnore( groupMember.GroupId, isExporting ? groupMember.Group.Name : string.Format( "<a href='{0}'>{1}</a>", LinkedPageUrl( "GroupDetailPage", new Dictionary<string, string> { { "GroupId", groupMember.GroupId.ToString() } } ), groupMember.Group.Name ) ); } // If the campus column was selected to be displayed on grid, preload all the people's // campuses so that the databind does not need to query each row if ( preloadCampusValues ) { PersonCampusIds = new Dictionary<int, List<int>>(); Guid familyGroupTypeGuid = Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid(); foreach ( var personCampusList in new GroupMemberService( rockContext ) .Queryable().AsNoTracking() .Where( m => m.Group.GroupType.Guid == familyGroupTypeGuid && personIds.Contains( m.PersonId ) ) .GroupBy( m => m.PersonId ) .Select( m => new { PersonId = m.Key, CampusIds = m .Where( g => g.Group.CampusId.HasValue ) .Select( g => g.Group.CampusId.Value ) .ToList() } ) ) { PersonCampusIds.Add( personCampusList.PersonId, personCampusList.CampusIds ); } } // If there are any attributes that were selected to be displayed, we're going // to try and read all attribute values in one query and then put them into a // custom grid ObjectList property so that the AttributeField columns don't need // to do the LoadAttributes and querying of values for each row/column if ( personAttributesIds.Any() || groupMemberAttributesIds.Any() || registrantAttributeIds.Any() ) { // Query the attribute values for all rows and attributes var attributeValues = new AttributeValueService( rockContext ) .Queryable( "Attribute" ).AsNoTracking() .Where( v => v.EntityId.HasValue && ( ( personAttributesIds.Contains( v.AttributeId ) && personIds.Contains( v.EntityId.Value ) ) || ( groupMemberAttributesIds.Contains( v.AttributeId ) && groupMemberIds.Contains( v.EntityId.Value ) ) || ( registrantAttributeIds.Contains( v.AttributeId ) && registrantIds.Contains( v.EntityId.Value ) ) ) ) .ToList(); // Get the attributes to add to each row's object var attributes = new Dictionary<string, AttributeCache>(); RegistrantFields .Where( f => f.Attribute != null ) .Select( f => f.Attribute ) .ToList() .ForEach( a => attributes .Add( a.Id.ToString() + a.Key, a ) ); // Initialize the grid's object list gGroupPlacements.ObjectList = new Dictionary<string, object>(); // Loop through each of the current page's registrants and build an attribute // field object for storing attributes and the values for each of the registrants foreach ( var registrant in currentPageRegistrants ) { // Create a row attribute object var attributeFieldObject = new AttributeFieldObject(); // Add the attributes to the attribute object attributeFieldObject.Attributes = attributes; // Add any person attribute values to object attributeValues .Where( v => personAttributesIds.Contains( v.AttributeId ) && v.EntityId.Value == registrant.PersonAlias.PersonId ) .ToList() .ForEach( v => attributeFieldObject.AttributeValues .Add( v.AttributeId.ToString() + v.Attribute.Key, new AttributeValueCache( v ) ) ); // Add any group member attribute values to object if ( registrant.GroupMemberId.HasValue ) { attributeValues .Where( v => groupMemberAttributesIds.Contains( v.AttributeId ) && v.EntityId.Value == registrant.GroupMemberId.Value ) .ToList() .ForEach( v => attributeFieldObject.AttributeValues .Add( v.AttributeId.ToString() + v.Attribute.Key, new AttributeValueCache( v ) ) ); } // Add any registrant attribute values to object attributeValues .Where( v => registrantAttributeIds.Contains( v.AttributeId ) && v.EntityId.Value == registrant.Id ) .ToList() .ForEach( v => attributeFieldObject.AttributeValues .Add( v.AttributeId.ToString() + v.Attribute.Key, new AttributeValueCache( v ) ) ); // Add row attribute object to grid's object list gGroupPlacements.ObjectList.Add( registrant.Id.ToString(), attributeFieldObject ); } } } } gGroupPlacements.DataBind(); } } }
/// <summary> /// Binds the registrants grid. /// </summary> private void BindRegistrantsGrid() { int? instanceId = hfRegistrationInstanceId.Value.AsIntegerOrNull(); if ( instanceId.HasValue ) { using ( var rockContext = new RockContext() ) { // Start query for registrants var qry = new RegistrationRegistrantService( rockContext ) .Queryable( "PersonAlias.Person.PhoneNumbers.NumberTypeValue,Fees.RegistrationTemplateFee,GroupMember.Group" ).AsNoTracking() .Where( r => r.Registration.RegistrationInstanceId == instanceId.Value && r.PersonAlias != null && r.PersonAlias.Person != null ); // Filter by daterange if ( drpRegistrantDateRange.LowerValue.HasValue ) { qry = qry.Where( r => r.CreatedDateTime.HasValue && r.CreatedDateTime.Value >= drpRegistrantDateRange.LowerValue.Value ); } if ( drpRegistrantDateRange.UpperValue.HasValue ) { qry = qry.Where( r => r.CreatedDateTime.HasValue && r.CreatedDateTime.Value <= drpRegistrantDateRange.UpperValue.Value ); } // Filter by first name if ( !string.IsNullOrWhiteSpace( tbRegistrantFirstName.Text ) ) { string rfname = tbRegistrantFirstName.Text; qry = qry.Where( r => r.PersonAlias.Person.NickName.StartsWith( rfname ) || r.PersonAlias.Person.FirstName.StartsWith( rfname ) ); } // Filter by last name if ( !string.IsNullOrWhiteSpace( tbRegistrantLastName.Text ) ) { string rlname = tbRegistrantLastName.Text; qry = qry.Where( r => r.PersonAlias.Person.LastName.StartsWith( rlname ) ); } bool preloadCampusValues = false; var registrantAttributes = new List<AttributeCache>(); var personAttributes = new List<AttributeCache>(); var groupMemberAttributes = new List<AttributeCache>(); var registrantAttributeIds = new List<int>(); var personAttributesIds = new List<int>(); var groupMemberAttributesIds = new List<int>(); if ( RegistrantFields != null ) { // Filter by any selected foreach ( var personFieldType in RegistrantFields .Where( f => f.FieldSource == RegistrationFieldSource.PersonField && f.PersonFieldType.HasValue ) .Select( f => f.PersonFieldType.Value ) ) { switch ( personFieldType ) { case RegistrationPersonFieldType.Campus: { preloadCampusValues = true; var ddlCampus = phRegistrantFormFieldFilters.FindControl( "ddlCampus" ) as RockDropDownList; if ( ddlCampus != null ) { var campusId = ddlCampus.SelectedValue.AsIntegerOrNull(); if ( campusId.HasValue ) { var familyGroupTypeGuid = Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid(); qry = qry.Where( r => r.PersonAlias.Person.Members.Any( m => m.Group.GroupType.Guid == familyGroupTypeGuid && m.Group.CampusId.HasValue && m.Group.CampusId.Value == campusId ) ); } } break; } case RegistrationPersonFieldType.Email: { var tbEmailFilter = phRegistrantFormFieldFilters.FindControl( "tbEmailFilter" ) as RockTextBox; if ( tbEmailFilter != null && !string.IsNullOrWhiteSpace( tbEmailFilter.Text ) ) { qry = qry.Where( r => r.PersonAlias.Person.Email != null && r.PersonAlias.Person.Email.Contains( tbEmailFilter.Text ) ); } break; } case RegistrationPersonFieldType.Birthdate: { var drpBirthdateFilter = phRegistrantFormFieldFilters.FindControl( "drpBirthdateFilter" ) as DateRangePicker; if ( drpBirthdateFilter != null ) { if ( drpBirthdateFilter.LowerValue.HasValue ) { qry = qry.Where( r => r.PersonAlias.Person.BirthDate.HasValue && r.PersonAlias.Person.BirthDate.Value >= drpBirthdateFilter.LowerValue.Value ); } if ( drpBirthdateFilter.UpperValue.HasValue ) { qry = qry.Where( r => r.PersonAlias.Person.BirthDate.HasValue && r.PersonAlias.Person.BirthDate.Value <= drpBirthdateFilter.UpperValue.Value ); } } break; } case RegistrationPersonFieldType.Gender: { var ddlGenderFilter = phRegistrantFormFieldFilters.FindControl( "ddlGenderFilter" ) as RockDropDownList; if ( ddlGenderFilter != null ) { var gender = ddlGenderFilter.SelectedValue.ConvertToEnumOrNull<Gender>(); if ( gender.HasValue ) { qry = qry.Where( r => r.PersonAlias.Person.Gender == gender ); } } break; } case RegistrationPersonFieldType.MaritalStatus: { var ddlMaritalStatusFilter = phRegistrantFormFieldFilters.FindControl( "ddlMaritalStatusFilter" ) as RockDropDownList; if ( ddlMaritalStatusFilter != null ) { var maritalStatusId = ddlMaritalStatusFilter.SelectedValue.AsIntegerOrNull(); if ( maritalStatusId.HasValue ) { qry = qry.Where( r => r.PersonAlias.Person.MaritalStatusValueId.HasValue && r.PersonAlias.Person.MaritalStatusValueId.Value == maritalStatusId.Value ); } } break; } case RegistrationPersonFieldType.MobilePhone: { var tbPhoneFilter = phRegistrantFormFieldFilters.FindControl( "tbPhoneFilter" ) as RockTextBox; if ( tbPhoneFilter != null && !string.IsNullOrWhiteSpace( tbPhoneFilter.Text ) ) { string numericPhone = tbPhoneFilter.Text.AsNumeric(); qry = qry.Where( r => r.PersonAlias.Person.PhoneNumbers != null && r.PersonAlias.Person.PhoneNumbers.Any( n => n.Number.Contains( numericPhone ) ) ); } break; } } } // Get all the registrant attributes selected to be on grid registrantAttributes = RegistrantFields .Where( f => f.Attribute != null && f.FieldSource == RegistrationFieldSource.RegistrationAttribute ) .Select( f => f.Attribute ) .ToList(); registrantAttributeIds = registrantAttributes.Select( a => a.Id ).Distinct().ToList(); // Filter query by any configured registrant attribute filters if ( registrantAttributes != null && registrantAttributes.Any() ) { var attributeValueService = new AttributeValueService( rockContext ); var parameterExpression = attributeValueService.ParameterExpression; foreach ( var attribute in registrantAttributes ) { var filterControl = phRegistrantFormFieldFilters.FindControl( "filter_" + attribute.Id.ToString() ); if ( filterControl != null ) { var filterValues = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter ); var expression = attribute.FieldType.Field.AttributeFilterExpression( attribute.QualifierValues, filterValues, parameterExpression ); if ( expression != null ) { var attributeValues = attributeValueService .Queryable() .Where( v => v.Attribute.Id == attribute.Id ); attributeValues = attributeValues.Where( parameterExpression, expression, null ); qry = qry .Where( r => attributeValues.Select( v => v.EntityId ).Contains( r.Id ) ); } } } } // Get all the person attributes selected to be on grid personAttributes = RegistrantFields .Where( f => f.Attribute != null && f.FieldSource == RegistrationFieldSource.PersonAttribute ) .Select( f => f.Attribute ) .ToList(); personAttributesIds = personAttributes.Select( a => a.Id ).Distinct().ToList(); // Filter query by any configured person attribute filters if ( personAttributes != null && personAttributes.Any() ) { var attributeValueService = new AttributeValueService( rockContext ); var parameterExpression = attributeValueService.ParameterExpression; foreach ( var attribute in personAttributes ) { var filterControl = phRegistrantFormFieldFilters.FindControl( "filter_" + attribute.Id.ToString() ); if ( filterControl != null ) { var filterValues = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter ); var expression = attribute.FieldType.Field.AttributeFilterExpression( attribute.QualifierValues, filterValues, parameterExpression ); if ( expression != null ) { var attributeValues = attributeValueService .Queryable() .Where( v => v.Attribute.Id == attribute.Id ); attributeValues = attributeValues.Where( parameterExpression, expression, null ); qry = qry .Where( r => attributeValues.Select( v => v.EntityId ).Contains( r.PersonAlias.PersonId ) ); } } } } // Get all the group member attributes selected to be on grid groupMemberAttributes = RegistrantFields .Where( f => f.Attribute != null && f.FieldSource == RegistrationFieldSource.GroupMemberAttribute ) .Select( f => f.Attribute ) .ToList(); groupMemberAttributesIds = groupMemberAttributes.Select( a => a.Id ).Distinct().ToList(); // Filter query by any configured person attribute filters if ( groupMemberAttributes != null && groupMemberAttributes.Any() ) { var attributeValueService = new AttributeValueService( rockContext ); var parameterExpression = attributeValueService.ParameterExpression; foreach ( var attribute in groupMemberAttributes ) { var filterControl = phRegistrantFormFieldFilters.FindControl( "filter_" + attribute.Id.ToString() ); if ( filterControl != null ) { var filterValues = attribute.FieldType.Field.GetFilterValues( filterControl, attribute.QualifierValues, Rock.Reporting.FilterMode.SimpleFilter ); var expression = attribute.FieldType.Field.AttributeFilterExpression( attribute.QualifierValues, filterValues, parameterExpression ); if ( expression != null ) { var attributeValues = attributeValueService .Queryable() .Where( v => v.Attribute.Id == attribute.Id ); attributeValues = attributeValues.Where( parameterExpression, expression, null ); qry = qry .Where( r => r.GroupMemberId.HasValue && attributeValues.Select( v => v.EntityId ).Contains( r.GroupMemberId.Value ) ); } } } } } // Sort the query IOrderedQueryable<RegistrationRegistrant> orderedQry = null; SortProperty sortProperty = gRegistrants.SortProperty; if ( sortProperty != null ) { orderedQry = qry.Sort( sortProperty ); } else { orderedQry = qry .OrderBy( r => r.PersonAlias.Person.LastName ) .ThenBy( r => r.PersonAlias.Person.NickName ); } // Set the grids LinqDataSource which will run query and set results for current page gRegistrants.SetLinqDataSource<RegistrationRegistrant>( orderedQry ); if ( RegistrantFields != null ) { // Get the query results for the current page var currentPageRegistrants = gRegistrants.DataSource as List<RegistrationRegistrant>; if ( currentPageRegistrants != null ) { // Get all the registrant ids in current page of query results var registrantIds = currentPageRegistrants .Select( r => r.Id ) .Distinct() .ToList(); // Get all the person ids in current page of query results var personIds = currentPageRegistrants .Select( r => r.PersonAlias.PersonId ) .Distinct() .ToList(); // Get all the group member ids and the group id in current page of query results var groupMemberIds = new List<int>(); GroupLinks = new Dictionary<int,string>(); foreach( var groupMember in currentPageRegistrants .Where( m => m.GroupMember != null && m.GroupMember.Group != null ) .Select( m => m.GroupMember ) ) { groupMemberIds.Add( groupMember.Id ); GroupLinks.AddOrIgnore( groupMember.GroupId, string.Format( "<a href='{0}'>{1}</a>", LinkedPageUrl( "GroupDetailPage", new Dictionary<string, string> { { "GroupId", groupMember.GroupId.ToString() } } ), groupMember.Group.Name ) ); } // If the campus column was selected to be displayed on grid, preload all the people's // campuses so that the databind does not need to query each row if ( preloadCampusValues ) { PersonCampusIds = new Dictionary<int, List<int>>(); Guid familyGroupTypeGuid = Rock.SystemGuid.GroupType.GROUPTYPE_FAMILY.AsGuid(); foreach ( var personCampusList in new GroupMemberService( rockContext ) .Queryable().AsNoTracking() .Where( m => m.Group.GroupType.Guid == familyGroupTypeGuid && personIds.Contains( m.PersonId ) ) .GroupBy( m => m.PersonId ) .Select( m => new { PersonId = m.Key, CampusIds = m .Where( g => g.Group.CampusId.HasValue ) .Select( g => g.Group.CampusId.Value ) .ToList() } ) ) { PersonCampusIds.Add( personCampusList.PersonId, personCampusList.CampusIds ); } } // If there are any attributes that were selected to be displayed, we're going // to try and read all attribute values in one query and then put them into a // custom grid ObjectList property so that the AttributeField columns don't need // to do the LoadAttributes and querying of values for each row/column if ( personAttributesIds.Any() || groupMemberAttributesIds.Any() || registrantAttributeIds.Any() ) { // Query the attribute values for all rows and attributes var attributeValues = new AttributeValueService( rockContext ) .Queryable( "Attribute" ).AsNoTracking() .Where( v => v.EntityId.HasValue && ( ( personAttributesIds.Contains( v.AttributeId ) && personIds.Contains( v.EntityId.Value ) ) || ( groupMemberAttributesIds.Contains( v.AttributeId ) && groupMemberIds.Contains( v.EntityId.Value ) ) || ( registrantAttributeIds.Contains( v.AttributeId ) && registrantIds.Contains( v.EntityId.Value ) ) ) ) .ToList(); // Get the attributes to add to each row's object var attributes = new Dictionary<string, AttributeCache>(); RegistrantFields .Where( f => f.Attribute != null ) .Select( f => f.Attribute ) .ToList() .ForEach( a => attributes .Add( a.Id.ToString() + a.Key, a ) ); // Initialize the grid's object list gRegistrants.ObjectList = new Dictionary<string, object>(); // Loop through each of the current page's registrants and build an attribute // field object for storing attributes and the values for each of the registrants foreach ( var registrant in currentPageRegistrants ) { // Create a row attribute object var attributeFieldObject = new AttributeFieldObject(); // Add the attributes to the attribute object attributeFieldObject.Attributes = attributes; // Add any person attribute values to object attributeValues .Where( v => personAttributesIds.Contains( v.AttributeId ) && v.EntityId.Value == registrant.PersonAlias.PersonId ) .ToList() .ForEach( v => attributeFieldObject.AttributeValues .Add( v.AttributeId.ToString() + v.Attribute.Key, v ) ); // Add any group member attribute values to object if ( registrant.GroupMemberId.HasValue ) { attributeValues .Where( v => groupMemberAttributesIds.Contains( v.AttributeId ) && v.EntityId.Value == registrant.GroupMemberId.Value ) .ToList() .ForEach( v => attributeFieldObject.AttributeValues .Add( v.AttributeId.ToString() + v.Attribute.Key, v ) ); } // Add any registrant attribute values to object attributeValues .Where( v => registrantAttributeIds.Contains( v.AttributeId ) && v.EntityId.Value == registrant.PersonAlias.PersonId ) .ToList() .ForEach( v => attributeFieldObject.AttributeValues .Add( v.AttributeId.ToString() + v.Attribute.Key, v ) ); // Add row attribute object to grid's object list gRegistrants.ObjectList.Add( registrant.Id.ToString(), attributeFieldObject ); } } } } gRegistrants.DataBind(); } } }
/// <summary> /// Maps the authorizations to an attribute. /// </summary> /// <param name="tableData">The table data.</param> private void MapAuthorizations( IQueryable<Row> tableData ) { var lookupContext = new RockContext(); var rockContext = new RockContext(); var categoryService = new CategoryService( lookupContext ); var personService = new PersonService( lookupContext ); var noteList = new List<Note>(); int completed = 0; int totalRows = tableData.Count(); int percentage = ( totalRows - 1 ) / 100 + 1; ReportProgress( 0, string.Format( "Verifying Authorization import ({0:N0} found).", totalRows ) ); var attributeList = new AttributeService( lookupContext ).Queryable().ToList(); int authorizationAttributeId = 0; if ( attributeList.Find( a => a.Key == "PickupAuthorization" ) != null ) { authorizationAttributeId = attributeList.Find( a => a.Key == "PickupAuthorization" ).Id; } var authorizationAttributeValueList = new List<AttributeValue>(); authorizationAttributeValueList = new AttributeValueService( rockContext ).Queryable().Where( av => av.AttributeId == authorizationAttributeId ).ToList(); int f1HouseholdIdAttributeId = attributeList.Find( a => a.Key == "F1HouseholdId" ).Id; //places all household attributes in a dictionary where the key is the personId and the houshold is the value. var householdDictionary = new AttributeValueService( lookupContext ).Queryable() .Where( av => av.AttributeId == f1HouseholdIdAttributeId ) .Select( av => new { PersonId = av.EntityId, HouseholdId = av.Value } ) .ToDictionary( t => t.PersonId, t => t.HouseholdId ); foreach ( var row in tableData ) { //var rockContext = new RockContext(); var categoryList = new CategoryService( rockContext ).Queryable().ToList(); //check if category exists //If it doesn't (though it should), it will create a new category called Authorization if ( categoryList.Find( c => c.Name == "Childhood Information" ) == null ) { var entityType = new EntityTypeService( rockContext ); //creates if category doesn't exist var newCategory = new Category(); newCategory.IsSystem = false; newCategory.EntityTypeId = entityType.Queryable().Where( e => e.Name == "Rock.Model.Attribute" ).Select( e => e.Id ).FirstOrDefault(); newCategory.EntityTypeQualifierColumn = "EntityTypeId"; newCategory.EntityTypeQualifierValue = Convert.ToString( PersonEntityTypeId ); newCategory.Name = "Authorization"; newCategory.Description = "Contains the pickup authorization"; //var newCategoryContext = new RockContext(); //newCategoryContext.WrapTransaction( () => //{ // newCategoryContext.Configuration.AutoDetectChangesEnabled = false; // newCategoryContext.Categories.Add( newCategory ); // newCategoryContext.SaveChanges( DisableAudit ); //} ); rockContext.WrapTransaction( () => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.Categories.Add( newCategory ); rockContext.SaveChanges( DisableAudit ); } ); } attributeList = new AttributeService( lookupContext ).Queryable().ToList(); //Check if Attribute exists //If it doesn't it creates the attribute if ( attributeList.Find( a => a.Key == "PickupAuthorization" ) == null ) { var fieldType = new FieldTypeService( rockContext ); //var newAttributeList = new List<Rock.Model.Attribute>(); var fieldTypeId = fieldType.Queryable().Where( e => e.Name == "Memo" ).FirstOrDefault().Id; var category2 = new CategoryService( rockContext ).Queryable().Where( gt => gt.Name == "Childhood Information" ).FirstOrDefault(); var category3 = new CategoryService( rockContext ).Queryable().Where( gt => gt.Name == "Authorization" ).FirstOrDefault(); //Creates if attribute doesn't exist //The attribute is a memo attribute var newAttribute = new Rock.Model.Attribute(); newAttribute.Key = "PickupAuthorization"; newAttribute.Name = "Pickup Authorization"; newAttribute.FieldTypeId = fieldTypeId; newAttribute.EntityTypeId = PersonEntityTypeId; newAttribute.EntityTypeQualifierValue = string.Empty; newAttribute.EntityTypeQualifierColumn = string.Empty; newAttribute.Description = "Lists who is authorized to pickup this child along with their current phone number."; newAttribute.DefaultValue = string.Empty; newAttribute.IsMultiValue = false; newAttribute.IsRequired = false; if ( categoryList.Find( c => c.Name == "Childhood Information" ) != null ) { newAttribute.Categories = new List<Category>(); newAttribute.Categories.Add( category2 ); } //If authorization category was create, this is where the attribute is set to that category. else { newAttribute.Categories = new List<Category>(); newAttribute.Categories.Add( category3 ); //Sets to Authorization Attribute Category. } //saves the attribute rockContext.WrapTransaction( () => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.Attributes.Add( newAttribute ); rockContext.SaveChanges( DisableAudit ); } ); } //Adding to the person's attributes int? householdId = row["HouseholdID"] as int?; string personName = row["PersonName"] as string; DateTime? authorizationDate = row["AuthorizationDate"] as DateTime?; attributeList = new AttributeService( rockContext ).Queryable().ToList(); //Gets the Attribute Id for Pickup Authorization. authorizationAttributeId = attributeList.Find(a => a.Key == "PickupAuthorization").Id; //since F1 authorization applies to the household id and not individual I have to apply it to all members in that household. //Value in F1 is a text entry and not a person select. Discuss with staff that we need to break away from this and start using known relationships for authorization foreach ( var household in householdDictionary.Where( h => h.Value == Convert.ToString( householdId ) ) ) { //checks if a record already exists in the list if ( authorizationAttributeValueList.Find( a => a.AttributeId == authorizationAttributeId && a.EntityId == household.Key ) == null ) { var person = new PersonService( rockContext ).Queryable().Where( p => p.Id == household.Key ).FirstOrDefault(); //trying to keep from adding this attribute to adult records if ( person != null && (person.Age <= 18 || person.Age == null) ) { //creates new attribute record if it does not exist. var newAuthorizationAttribute = new AttributeValue(); newAuthorizationAttribute.IsSystem = false; newAuthorizationAttribute.AttributeId = authorizationAttributeId; newAuthorizationAttribute.EntityId = household.Key; //the key is the person ID newAuthorizationAttribute.Value = personName + ", "; //text field newAuthorizationAttribute.CreatedDateTime = authorizationDate; //adds the new record to the list authorizationAttributeValueList.Add( newAuthorizationAttribute ); } } //if a record already exists else { //adds to the current value. authorizationAttributeValueList.Find( a => a.AttributeId == authorizationAttributeId && a.EntityId == household.Key ).Value = personName + ", "; } } completed++; if ( completed % percentage < 1 ) { int percentComplete = completed / percentage; ReportProgress( percentComplete, string.Format( "{0:N0} authorizations imported ({1}% complete).", completed, percentComplete ) ); } else if ( completed % ReportingNumber < 1 ) { //rockContext.WrapTransaction( () => // { // rockContext.Configuration.AutoDetectChangesEnabled = false; // rockContext.AttributeValues.AddRange( authorizationAttributeValueList ); // rockContext.SaveChanges( DisableAudit ); //I get can't insert duplicate entry error // } ); ReportPartialProgress(); } } if ( authorizationAttributeValueList.Any() ) { //var rockContext = new RockContext(); rockContext.WrapTransaction( () => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.AttributeValues.AddRange( authorizationAttributeValueList ); rockContext.SaveChanges( DisableAudit ); } ); } ReportProgress( 100, string.Format( "Finished authorization import: {0:N0} notes imported.", completed ) ); }