/// <summary>
        /// Gets the type request counts (TotalCount and AssignedToYouCount) for the
        /// given connection types. The Person the service was initialized with is used to calculate
        /// <see cref="ConnectionRequestCountsViewModel.AssignedToYouCount"/>.
        /// </summary>
        /// <remarks>This method does not check security, it is assumed you have already done so.</remarks>
        /// <param name="connectionTypeIds">The connection type identifiers.</param>
        /// <returns>A dictionary of connection request count objects.</returns>
        public Dictionary <int, ConnectionRequestCountsViewModel> GetConnectionTypeCounts(IEnumerable <int> connectionTypeIds)
        {
            var opportunityClientService = new ConnectionOpportunityClientService(RockContext, Person);

            var opportunities = new ConnectionOpportunityService(RockContext)
                                .Queryable()
                                .Where(o => connectionTypeIds.Contains(o.ConnectionTypeId))
                                .Select(o => new
            {
                o.Id,
                o.ConnectionTypeId
            })
                                .ToList();
            var opportunityIds = opportunities.Select(o => o.Id).ToList();

            var requestCounts = opportunityClientService.GetOpportunityRequestCounts(opportunityIds)
                                .Select(c => new
            {
                TypeId = opportunities.Single(o => o.Id == c.Key).ConnectionTypeId,
                Counts = c.Value
            })
                                .GroupBy(c => c.TypeId)
                                .ToDictionary(g => g.Key, g => new ConnectionRequestCountsViewModel
            {
                AssignedToYouCount = g.Sum(c => c.Counts.AssignedToYouCount),
                TotalCount         = g.Sum(c => c.Counts.TotalCount)
            });

            // Fill in any missing types with empty counts.
            foreach (var typeId in connectionTypeIds)
            {
                if (!requestCounts.ContainsKey(typeId))
                {
                    requestCounts.Add(typeId, new ConnectionRequestCountsViewModel());
                }
            }

            return(requestCounts);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the connection opportunities view model that can be sent to the client.
        /// </summary>
        /// <param name="connectionTypeGuid">The connection type unique identifier.</param>
        /// <param name="filterViewModel">The filter.</param>
        /// <returns>The <see cref="GetContentViewModel"/> that contains the information about the response.</returns>
        private GetContentViewModel GetConnectionOpportunities(Guid connectionTypeGuid, GetConnectionOpportunitiesFilterViewModel filterViewModel)
        {
            using (var rockContext = new RockContext())
            {
                var opportunityService       = new ConnectionOpportunityService(rockContext);
                var opportunityClientService = new ConnectionOpportunityClientService(rockContext, RequestContext.CurrentPerson);
                var connectionType           = new ConnectionTypeService(rockContext).GetNoTracking(connectionTypeGuid);

                var filterOptions = new ConnectionOpportunityQueryOptions
                {
                    ConnectionTypeGuids = new List <Guid> {
                        connectionTypeGuid
                    },
                    IncludeInactive = true
                };

                if (filterViewModel.OnlyMyConnections)
                {
                    filterOptions.ConnectorPersonIds = new List <int> {
                        RequestContext.CurrentPerson?.Id ?? 0
                    };
                }

                // Put all the opportunities in memory so we can check security.
                var qry           = opportunityService.GetConnectionOpportunitiesQuery(filterOptions);
                var opportunities = qry.ToList()
                                    .Where(o => o.IsAuthorized(Authorization.VIEW, 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 opportunityIds = opportunities.Select(o => o.Id).ToList();
                var requestCounts  = opportunityClientService.GetOpportunityRequestCounts(opportunityIds)
                                     .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("ConnectionOpportunities", opportunities);
                mergeFields.AddOrReplace("DetailPage", DetailPageGuid);
                mergeFields.AddOrReplace("ConnectionRequestCounts", requestCounts);

                var content = OpportunityTemplate.ResolveMergeFields(mergeFields);

                // Process the header template for.
                mergeFields = RequestContext.GetCommonMergeFields();
                if (connectionType != null)
                {
                    mergeFields.Add("ConnectionType", connectionType);
                }

                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 GetConnectionOpportunities()
        {
            using (var rockContext = new RockContext())
            {
                var opportunityService = new ConnectionOpportunityService(rockContext);

                var opportunityClientService = new ConnectionOpportunityClientService(rockContext, CurrentPerson);
                var connectionType           = new ConnectionTypeService(rockContext).GetNoTracking(_connectionTypeGuid);

                if (connectionType == null)
                {
                    return;
                }

                // Determine if we should update the page title with the connection opportunity name
                var updatePageTitle = GetAttributeValue(AttributeKey.UpdatePageTitle).AsBoolean();
                if (updatePageTitle)
                {
                    RockPage.PageTitle = connectionType.Name;

                    var pageBreadCrumb = RockPage.PageReference.BreadCrumbs.FirstOrDefault();
                    if (pageBreadCrumb != null)
                    {
                        pageBreadCrumb.Name = RockPage.PageTitle;
                    }
                }

                var filterOptions = new ConnectionOpportunityQueryOptions
                {
                    ConnectionTypeGuids = new List <Guid> {
                        _connectionTypeGuid
                    },
                    IncludeInactive = true
                };

                if (_onlyShowOpportunitiesWithRequestsForUser && CurrentPerson != null)
                {
                    filterOptions.ConnectorPersonIds = new List <int> {
                        CurrentPerson.Id
                    };
                }
                else
                {
                    filterOptions.ConnectorPersonIds = null;
                }

                // Put all the opportunities in memory so we can check security.
                var connectionOpportunityQuery = opportunityService.GetConnectionOpportunitiesQuery(filterOptions);
                var opportunities = connectionOpportunityQuery.ToList()
                                    .Where(o => o.IsAuthorized(Authorization.VIEW, 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 opportunityIds             = opportunities.Select(o => o.Id).ToList();
                var connectionRequestCounts    = new Dictionary <string, string>();
                var sumTotalConnectionRequests = 0;

                // We only need to perform this query under this condition, otherwise we will use the
                // requestCountsPerOpportunity fetched earlier.
                if (_onlyShowOpportunitiesWithRequestsForUser && CurrentPerson != null)
                {
                    var requestCounts = opportunityClientService.GetOpportunityRequestCounts(opportunityIds);
                    foreach (var opportunityId in opportunityIds)
                    {
                        // Note use AssignedToYouCount here:
                        sumTotalConnectionRequests += requestCounts[opportunityId].AssignedToYouCount;
                        connectionRequestCounts.Add(opportunityId.ToString(), requestCounts[opportunityId].AssignedToYouCount.ToString());
                    }
                }
                else
                {
                    var requestCounts = opportunityClientService.GetOpportunityRequestCounts(opportunityIds);
                    foreach (var opportunityId in opportunityIds)
                    {
                        // Note use TotalCount here:
                        sumTotalConnectionRequests += requestCounts[opportunityId].TotalCount;
                        connectionRequestCounts.Add(opportunityId.ToString(), requestCounts[opportunityId].TotalCount.ToString());
                    }
                }
                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);

                mergeFields.AddOrReplace("ConnectionOpportunities", opportunities);
                mergeFields.AddOrReplace("DetailPage", DetailPageGuid);
                mergeFields.AddOrReplace("ConnectionRequestCounts", connectionRequestCounts);
                mergeFields.AddOrReplace("SumTotalConnectionRequests", sumTotalConnectionRequests);

                var content = OpportunityTemplate
                              .ResolveMergeFields(mergeFields)
                              .ResolveClientIds(upConnectionSelectLava.ClientID);

                lContent.Text = content;
            }
        }