/// <summary> /// Gets the connection opportunities queryable that will provide the results. /// This method returns a queryable of <see cref="ConnectionType"/> objects /// that can then have additional custom filters applied before the results are /// materialized from the database. If no filters are applied then all /// connection types are returned. /// </summary> /// <param name="options">The options that describe the filters to apply to the query.</param> /// <returns>A queryable of <see cref="ConnectionType"/> objects.</returns> /// <exception cref="System.InvalidOperationException">Context is not a RockContext.</exception> public IQueryable <ConnectionType> GetConnectionTypesQuery(ConnectionTypeQueryOptions options = null) { if (!(Context is RockContext rockContext)) { throw new InvalidOperationException("Context is not a RockContext."); } options = options ?? DefaultGetConnectionTypesOptions; var qry = Queryable(); if (options.ConnectorPersonIds != null && options.ConnectorPersonIds.Any()) { var connectorRequestsQry = new ConnectionRequestService(rockContext).Queryable() .Where(r => r.ConnectionState != ConnectionState.Connected && r.ConnectorPersonAliasId.HasValue && options.ConnectorPersonIds.Contains(r.ConnectorPersonAlias.PersonId)) .Select(r => r.Id); qry = qry.Where(t => t.ConnectionOpportunities.SelectMany(o => o.ConnectionRequests).Any(r => connectorRequestsQry.Contains(r.Id))); } if (!options.IncludeInactive) { qry = qry.Where(t => t.IsActive && t.IsActive); } return(qry); }
/// <summary> /// Gets the connection types view model that can be sent to the client. /// </summary> /// <returns>The <see cref="GetContentViewModel"/> that contains the information about the response.</returns> private GetContentViewModel GetConnectionTypes(GetConnectionTypesFilterViewModel filterViewModel) { using (var rockContext = new RockContext()) { var connectionTypeService = new ConnectionTypeService(rockContext); var clientTypeService = new ConnectionTypeClientService(rockContext, RequestContext.CurrentPerson); var filterOptions = new ConnectionTypeQueryOptions { IncludeInactive = true }; // If requesting to only have my connections returned then specify // the current person identifier. If they are not logged in then // use an invalid identifier value so that no matches will be returned. if (filterViewModel.OnlyMyConnections) { filterOptions.ConnectorPersonIds = new List <int> { RequestContext.CurrentPerson?.Id ?? 0 }; } // Get the connection types. var qry = connectionTypeService.GetConnectionTypesQuery(filterOptions); var types = connectionTypeService.GetViewAuthorizedConnectionTypes(qry, RequestContext.CurrentPerson); // Get the various counts to make available to the Lava template. // The conversion of the value to a dictionary is a temporary work-around // until we have a way to mark external types as lava safe. var connectionTypeIds = types.Select(t => t.Id).ToList(); var requestCounts = clientTypeService.GetConnectionTypeCounts(connectionTypeIds) .ToDictionary(k => k.Key, k => k.Value .GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .ToDictionary(prop => prop.Name, prop => prop.GetValue(k.Value, null))); // Process the connection opportunities with the template. var mergeFields = RequestContext.GetCommonMergeFields(); mergeFields.AddOrReplace("ConnectionTypes", types); mergeFields.AddOrReplace("DetailPage", DetailPageGuid); mergeFields.AddOrReplace("ConnectionRequestCounts", requestCounts); var content = TypeTemplate.ResolveMergeFields(mergeFields); // If we found a connection opportunity then process the header // template. mergeFields = RequestContext.GetCommonMergeFields(); var headerContent = HeaderTemplate.ResolveMergeFields(mergeFields); return(new GetContentViewModel { HeaderContent = headerContent, Content = content }); } }
/// <summary> /// Gets the connection types view model that can be sent to the client. /// </summary> private void GetConnectionTypes() { using (var rockContext = new RockContext()) { // Get the connection types. var connectionTypeService = new ConnectionTypeService(rockContext); var clientTypeService = new ConnectionTypeClientService(rockContext, CurrentPerson); var filterOptions = new ConnectionTypeQueryOptions { IncludeInactive = true }; var connectionTypesQuery = connectionTypeService.GetConnectionTypesQuery(); var connectionTypes = connectionTypeService.GetViewAuthorizedConnectionTypes(connectionTypesQuery, CurrentPerson); // Get the various counts to make available to the Lava template. // The conversion of the value to a dictionary is a temporary work-around // until we have a way to mark external types as lava safe. var connectionTypeIds = connectionTypes.Select(t => t.Id).ToList(); var requestCounts = clientTypeService.GetConnectionTypeCounts(connectionTypeIds); var connectionRequestCounts = new Dictionary <string, string>(); var sumTotalConnectionRequests = 0; foreach (var typeId in connectionTypeIds) { // For now, show TotalCount since there is no way to toggle between the two. // In the future, an options control should be added to allow seeing all vs "only my" //if ( CurrentPerson != null ) //{ // sumTotalConnectionRequests += requestCounts[typeId].AssignedToYouCount; // connectionRequestCounts.Add( typeId.ToString(), requestCounts[typeId].AssignedToYouCount.ToString() ); //} //else //{ sumTotalConnectionRequests += requestCounts[typeId].TotalCount; connectionRequestCounts.Add(typeId.ToString(), requestCounts[typeId].TotalCount.ToString()); //} } var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); mergeFields.AddOrReplace("ConnectionTypes", connectionTypes); mergeFields.AddOrReplace("DetailPage", DetailPageGuid.ToString()); mergeFields.AddOrReplace("ConnectionRequestCounts", connectionRequestCounts); mergeFields.AddOrReplace("SumTotalConnectionRequests", sumTotalConnectionRequests); var content = TypeTemplate .ResolveMergeFields(mergeFields) .ResolveClientIds(upConnectionSelectLava.ClientID); lContent.Text = content; } }