예제 #1
0
        /// <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;
            }
        }