/// <summary> /// Binds the grid. /// </summary> private void ShowResults() { // Get the group types that we're interested in Guid? groupTypeGuid = GetAttributeValue( "GroupType" ).AsGuidOrNull(); if ( !groupTypeGuid.HasValue ) { ShowError( "A valid Group Type is required." ); return; } gGroups.Columns[1].Visible = GetAttributeValue( "ShowDescription" ).AsBoolean(); gGroups.Columns[2].Visible = GetAttributeValue( "ShowSchedule" ).AsBoolean(); gGroups.Columns[3].Visible = GetAttributeValue( "ShowCount" ).AsBoolean(); gGroups.Columns[4].Visible = GetAttributeValue( "ShowAge" ).AsBoolean(); bool showProximity = GetAttributeValue( "ShowProximity" ).AsBoolean(); gGroups.Columns[5].Visible = showProximity; // Distance // Get query of groups of the selected group type var rockContext = new RockContext(); var groupService = new GroupService( rockContext ); var groupQry = groupService .Queryable( "GroupLocations.Location" ) .Where( g => g.IsActive && g.GroupType.Guid.Equals( groupTypeGuid.Value ) && g.IsPublic ); var groupParameterExpression = groupService.ParameterExpression; var schedulePropertyExpression = Expression.Property( groupParameterExpression, "Schedule" ); var dowFilterControl = phFilterControls.FindControl( "filter_dow" ); if ( dowFilterControl != null ) { var field = FieldTypeCache.Read( Rock.SystemGuid.FieldType.DAY_OF_WEEK ).Field; var filterValues = field.GetFilterValues( dowFilterControl, null, Rock.Reporting.FilterMode.SimpleFilter ); var expression = field.PropertyFilterExpression( null, filterValues, schedulePropertyExpression, "WeeklyDayOfWeek", typeof( DayOfWeek? ) ); groupQry = groupQry.Where( groupParameterExpression, expression, null ); } var timeFilterControl = phFilterControls.FindControl( "filter_time" ); if ( timeFilterControl != null ) { var field = FieldTypeCache.Read( Rock.SystemGuid.FieldType.TIME ).Field; var filterValues = field.GetFilterValues( timeFilterControl, null, Rock.Reporting.FilterMode.SimpleFilter ); var expression = field.PropertyFilterExpression( null, filterValues, schedulePropertyExpression, "WeeklyTimeOfDay", typeof( TimeSpan? ) ); groupQry = groupQry.Where( groupParameterExpression, expression, null ); } // Filter query by any configured attribute filters if ( AttributeFilters != null && AttributeFilters.Any() ) { var attributeValueService = new AttributeValueService( rockContext ); var parameterExpression = attributeValueService.ParameterExpression; foreach ( var attribute in AttributeFilters ) { var filterControl = phFilterControls.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 ); groupQry = groupQry.Where( w => attributeValues.Select( v => v.EntityId ).Contains( w.Id ) ); } } } } List<GroupLocation> fences = null; List<Group> groups = null; // Run query to get list of matching groups SortProperty sortProperty = gGroups.SortProperty; if ( sortProperty != null ) { groups = groupQry.Sort( sortProperty ).ToList(); } else { groups = groupQry.OrderBy( g => g.Name ).ToList(); } int? fenceGroupTypeId = GetGroupTypeId( GetAttributeValue( "GeofencedGroupType" ).AsGuidOrNull() ); bool showMap = GetAttributeValue( "ShowMap" ).AsBoolean(); bool showFences = showMap && GetAttributeValue( "ShowFence" ).AsBoolean(); var distances = new Dictionary<int, double>(); // If we care where these groups are located... if ( fenceGroupTypeId.HasValue || showMap || showProximity ) { // Get the location for the address entered Location personLocation = null; if ( fenceGroupTypeId.HasValue || showProximity ) { personLocation = new LocationService( rockContext ) .Get( acAddress.Street1, acAddress.Street2, acAddress.City, acAddress.State, acAddress.PostalCode, acAddress.Country ); } // If showing a map, and person's location was found, save a mapitem for this location FinderMapItem personMapItem = null; if ( showMap && personLocation != null && personLocation.GeoPoint != null ) { var infoWindow = string.Format( @" <div style='width:250px'> <div class='clearfix'> <strong>Your Location</strong> <br/>{0} </div> </div> ", personLocation.FormattedHtmlAddress ); personMapItem = new FinderMapItem( personLocation ); personMapItem.Name = "Your Location"; personMapItem.InfoWindow = HttpUtility.HtmlEncode( infoWindow.Replace( Environment.NewLine, string.Empty ).Replace( "\n", string.Empty ).Replace( "\t", string.Empty ) ); } // Get the locations, and optionally calculate the distance for each of the groups var groupLocations = new List<GroupLocation>(); foreach ( var group in groups ) { foreach ( var groupLocation in group.GroupLocations .Where( gl => gl.Location.GeoPoint != null ) ) { groupLocations.Add( groupLocation ); if ( showProximity && personLocation != null && personLocation.GeoPoint != null ) { double meters = groupLocation.Location.GeoPoint.Distance( personLocation.GeoPoint ) ?? 0.0D; double miles = meters * Location.MilesPerMeter; // If this group already has a distance calculated, see if this location is closer and if so, use it instead if ( distances.ContainsKey( group.Id ) ) { if ( distances[group.Id] < miles ) { distances[group.Id] = miles; } } else { distances.Add( group.Id, miles ); } } } } // If groups should be limited by a geofence var fenceMapItems = new List<MapItem>(); if ( fenceGroupTypeId.HasValue ) { fences = new List<GroupLocation>(); if ( personLocation != null && personLocation.GeoPoint != null ) { fences = new GroupLocationService( rockContext ) .Queryable( "Group,Location" ) .Where( gl => gl.Group.GroupTypeId == fenceGroupTypeId && gl.Location.GeoFence != null && personLocation.GeoPoint.Intersects( gl.Location.GeoFence ) ) .ToList(); } // Limit the group locations to only those locations inside one of the fences groupLocations = groupLocations .Where( gl => fences.Any( f => gl.Location.GeoPoint.Intersects( f.Location.GeoFence ) ) ) .ToList(); // Limit the groups to the those that still contain a valid location groups = groups .Where( g => groupLocations.Any( gl => gl.GroupId == g.Id ) ) .ToList(); // If the map and fences should be displayed, create a map item for each fence if ( showMap && showFences ) { foreach ( var fence in fences ) { var mapItem = new FinderMapItem( fence.Location ); mapItem.EntityTypeId = EntityTypeCache.Read( "Rock.Model.Group" ).Id; mapItem.EntityId = fence.GroupId; mapItem.Name = fence.Group.Name; fenceMapItems.Add( mapItem ); } } } // if not sorting by ColumnClick and SortByDistance, then sort the groups by distance if ( gGroups.SortProperty == null && GetAttributeValue( "SortByDistance" ).AsBoolean() ) { // only show groups with a known location, and sort those by distance groups = groups.Where( a => distances.Select( b => b.Key ).Contains( a.Id ) ).ToList(); groups = groups.OrderBy( a => distances[a.Id] ).ThenBy( a => a.Name ).ToList(); } // if limiting by PageSize, limit to the top X groups int? pageSize = ddlPageSize.SelectedValue.AsIntegerOrNull(); if ( pageSize.HasValue && pageSize > 0 ) { groups = groups.Take( pageSize.Value ).ToList(); } // If a map is to be shown if ( showMap && groups.Any() ) { Template template = Template.Parse( GetAttributeValue( "MapInfo" ) ); bool showDebug = UserCanEdit && GetAttributeValue( "MapInfoDebug" ).AsBoolean(); lMapInfoDebug.Visible = showDebug; // Add mapitems for all the remaining valid group locations var groupMapItems = new List<MapItem>(); foreach ( var gl in groupLocations ) { var group = groups.Where( g => g.Id == gl.GroupId ).FirstOrDefault(); if ( group != null ) { // Resolve info window lava template var linkedPageParams = new Dictionary<string, string> { { "GroupId", group.Id.ToString() } }; var mergeFields = new Dictionary<string, object>(); mergeFields.Add( "Group", gl.Group ); mergeFields.Add( "Location", gl.Location ); Dictionary<string, object> linkedPages = new Dictionary<string, object>(); linkedPages.Add( "GroupDetailPage", LinkedPageRoute( "GroupDetailPage" ) ); if ( _targetPersonGuid != Guid.Empty ) { linkedPages.Add( "RegisterPage", LinkedPageUrl( "RegisterPage", _urlParms ) ); } else { linkedPages.Add( "RegisterPage", LinkedPageUrl( "RegisterPage", null ) ); } mergeFields.Add( "LinkedPages", linkedPages ); // add collection of allowed security actions Dictionary<string, object> securityActions = new Dictionary<string, object>(); securityActions.Add( "View", group.IsAuthorized( Authorization.VIEW, CurrentPerson ) ); securityActions.Add( "Edit", group.IsAuthorized( Authorization.EDIT, CurrentPerson ) ); securityActions.Add( "Administrate", group.IsAuthorized( Authorization.ADMINISTRATE, CurrentPerson ) ); mergeFields.Add( "AllowedActions", securityActions ); string infoWindow = template.Render( Hash.FromDictionary( mergeFields ) ); if ( showDebug ) { lMapInfoDebug.Text = mergeFields.lavaDebugInfo( null, "<span class='label label-info'>Lava used for the map window.</span>", "" ); showDebug = false; } // Add a map item for group var mapItem = new FinderMapItem( gl.Location ); mapItem.EntityTypeId = EntityTypeCache.Read( "Rock.Model.Group" ).Id; mapItem.EntityId = group.Id; mapItem.Name = group.Name; mapItem.InfoWindow = HttpUtility.HtmlEncode( infoWindow.Replace( Environment.NewLine, string.Empty ).Replace( "\n", string.Empty ).Replace( "\t", string.Empty ) ); groupMapItems.Add( mapItem ); } } // Show the map Map( personMapItem, fenceMapItems, groupMapItems ); pnlMap.Visible = true; } else { pnlMap.Visible = false; } } else { pnlMap.Visible = false; } // Should a lava output be displayed if ( GetAttributeValue( "ShowLavaOutput" ).AsBoolean() ) { string template = GetAttributeValue( "LavaOutput" ); var mergeFields = new Dictionary<string, object>(); if ( fences != null ) { mergeFields.Add( "Fences", fences.Select( f => f.Group ).ToList() ); } else { mergeFields.Add( "Fences", new Dictionary<string, object>() ); } mergeFields.Add( "Groups", groups ); Dictionary<string, object> linkedPages = new Dictionary<string, object>(); linkedPages.Add( "GroupDetailPage", LinkedPageUrl( "GroupDetailPage", null ) ); if ( _targetPersonGuid != Guid.Empty ) { linkedPages.Add( "RegisterPage", LinkedPageUrl( "RegisterPage", _urlParms ) ); } else { linkedPages.Add( "RegisterPage", LinkedPageUrl( "RegisterPage", null ) ); } mergeFields.Add( "LinkedPages", linkedPages ); lLavaOverview.Text = template.ResolveMergeFields( mergeFields ); bool showDebug = UserCanEdit && GetAttributeValue( "LavaOutputDebug" ).AsBoolean(); lLavaOutputDebug.Visible = showDebug; if ( showDebug ) { lLavaOutputDebug.Text = mergeFields.lavaDebugInfo( null, "<span class='label label-info'>Lava used for the summary info.</span>" ); } pnlLavaOutput.Visible = true; } else { pnlLavaOutput.Visible = false; } // Should a grid be displayed if ( GetAttributeValue( "ShowGrid" ).AsBoolean() ) { pnlGrid.Visible = true; // Save the groups into the grid's object list since it is not being bound to actual group objects gGroups.ObjectList = new Dictionary<string, object>(); groups.ForEach( g => gGroups.ObjectList.Add( g.Id.ToString(), g ) ); // Bind the grid gGroups.DataSource = groups.Select( g => { var qryMembers = new GroupMemberService( rockContext ).Queryable().Where( a => a.GroupId == g.Id ); var groupType = GroupTypeCache.Read( g.GroupTypeId ); return new { Id = g.Id, Name = g.Name, GroupTypeName = groupType.Name, GroupOrder = g.Order, GroupTypeOrder = groupType.Order, Description = g.Description, IsSystem = g.IsSystem, IsActive = g.IsActive, GroupRole = string.Empty, DateAdded = DateTime.MinValue, Schedule = g.Schedule, MemberCount = qryMembers.Count(), AverageAge = Math.Round( qryMembers.Select( m => m.Person.BirthDate ).ToList().Select( a => Person.GetAge( a ) ).Average() ?? 0.0D ), Distance = distances.Where( d => d.Key == g.Id ) .Select( d => d.Value ).FirstOrDefault() }; } ).ToList(); gGroups.DataBind(); } else { pnlGrid.Visible = false; } // Show the results pnlResults.Visible = true; }
/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> public override bool Execute( RockContext rockContext, WorkflowAction action, Object entity, out List<string> errorMessages ) { errorMessages = new List<string>(); Guid? groupGuid = null; Person person = null; Group group = null; string noteValue = string.Empty; string captionValue = string.Empty; bool isAlert = false; // get the group attribute Guid groupAttributeGuid = GetAttributeValue( action, "Group" ).AsGuid(); if ( !groupAttributeGuid.IsEmpty() ) { groupGuid = action.GetWorklowAttributeValue( groupAttributeGuid ).AsGuidOrNull(); if ( groupGuid.HasValue ) { group = new GroupService( rockContext ).Get( groupGuid.Value ); if ( group == null ) { errorMessages.Add( "The group provided does not exist." ); } } else { errorMessages.Add( "Invalid group provided." ); } } // get person alias guid Guid personAliasGuid = Guid.Empty; string personAttribute = GetAttributeValue( action, "Person" ); Guid guid = personAttribute.AsGuid(); if ( !guid.IsEmpty() ) { var attribute = AttributeCache.Read( guid, rockContext ); if ( attribute != null ) { string value = action.GetWorklowAttributeValue( guid ); personAliasGuid = value.AsGuid(); } if ( personAliasGuid != Guid.Empty ) { person = new PersonAliasService( rockContext ).Queryable() .Where( p => p.Guid.Equals( personAliasGuid ) ) .Select( p => p.Person ) .FirstOrDefault(); } else { errorMessages.Add( "The person could not be found!" ); } } // get caption captionValue = GetAttributeValue( action, "Caption" ); guid = captionValue.AsGuid(); if ( guid.IsEmpty() ) { captionValue = captionValue.ResolveMergeFields( GetMergeFields( action ) ); } else { var workflowAttributeValue = action.GetWorklowAttributeValue( guid ); if ( workflowAttributeValue != null ) { captionValue = workflowAttributeValue; } } // get group member note noteValue = GetAttributeValue( action, "Note" ); guid = noteValue.AsGuid(); if ( guid.IsEmpty() ) { noteValue = noteValue.ResolveMergeFields( GetMergeFields( action ) ); } else { var workflowAttributeValue = action.GetWorklowAttributeValue( guid ); if ( workflowAttributeValue != null ) { noteValue = workflowAttributeValue; } } // get alert type string isAlertString = GetAttributeValue( action, "IsAlert" ); guid = isAlertString.AsGuid(); if ( guid.IsEmpty() ) { isAlert = isAlertString.AsBoolean(); } else { var workflowAttributeValue = action.GetWorklowAttributeValue( guid ); if ( workflowAttributeValue != null ) { isAlert = workflowAttributeValue.AsBoolean(); } } // get note type NoteTypeCache noteType = null; Guid noteTypeGuid = GetAttributeValue( action, "NoteType" ).AsGuid(); if ( !noteTypeGuid.IsEmpty() ) { noteType = NoteTypeCache.Read( noteTypeGuid, rockContext ); if (noteType == null ) { errorMessages.Add( "The note type provided does not exist." ); } } else { errorMessages.Add( "Invalid note type provided." ); } // set note if ( group != null && person != null && noteType != null ) { var groupMembers = new GroupMemberService( rockContext ).Queryable() .Where( m => m.Group.Guid == groupGuid && m.PersonId == person.Id ).ToList(); if ( groupMembers.Count() > 0 ) { foreach ( var groupMember in groupMembers ) { NoteService noteservice = new NoteService( rockContext ); Note note = new Note(); noteservice.Add( note ); note.NoteTypeId = noteType.Id; note.Text = noteValue; note.IsAlert = isAlert; note.Caption = captionValue; note.EntityId = groupMember.Id; rockContext.SaveChanges(); } } else { errorMessages.Add( string.Format("{0} is not a member of the group {1}.", person.FullName, group.Name )); } } errorMessages.ForEach( m => action.AddLogEntry( m, true ) ); return true; }