/// <summary> /// Gets the connection requests view model that can be sent to the client. /// </summary> /// <param name="connectionOpportunityGuid">The connection opportunity unique identifier.</param> /// <param name="filterViewModel">The filter.</param> /// <param name="pageNumber">The page number.</param> /// <returns>The <see cref="GetRequestsViewModel"/> that contains the information about the response.</returns> private GetRequestsViewModel GetConnectionRequests(Guid connectionOpportunityGuid, GetConnectionRequestsFilterViewModel filterViewModel, int pageNumber) { using (var rockContext = new RockContext()) { var connectionRequestService = new ConnectionRequestService(rockContext); var connectionOpportunity = new ConnectionOpportunityService(rockContext).GetNoTracking(connectionOpportunityGuid); bool hasMore; List <ConnectionRequest> requests; if (filterViewModel.OnlyMyConnections && RequestContext.CurrentPerson == null) { hasMore = false; requests = new List <ConnectionRequest>(); } else { var filterOptions = new ConnectionRequestQueryOptions { ConnectionOpportunityGuids = new List <Guid> { connectionOpportunityGuid }, ConnectionStates = filterViewModel.ConnectionStates }; if (filterViewModel.OnlyMyConnections) { filterOptions.ConnectorPersonIds = new List <int> { RequestContext.CurrentPerson.Id }; } var qry = connectionRequestService.GetConnectionRequestsQuery(filterOptions); // We currently don't support showing connected connection requests // since that could end up being a massive list for mobile. qry = qry.Where(r => r.ConnectionState != ConnectionState.Connected); // Put all the requests in memory so we can check security and // then get the current set of requests, plus one. The extra is // so that we can tell if there are more to load. requests = qry.ToList() .Where(r => r.IsAuthorized(Authorization.VIEW, RequestContext.CurrentPerson)) .Skip((pageNumber * MaxRequestsToShow)) .Take(MaxRequestsToShow + 1) .ToList(); // Determine if we have more requests to show and then properly // limit the requests to the correct amount. hasMore = requests.Count > MaxRequestsToShow; requests = requests.Take(MaxRequestsToShow).ToList(); } // Process the connection requests with the template. var mergeFields = RequestContext.GetCommonMergeFields(); mergeFields.AddOrReplace("ConnectionRequests", requests); mergeFields.AddOrReplace("DetailPage", DetailPageGuid); var content = RequestTemplate.ResolveMergeFields(mergeFields); string headerContent = string.Empty; // If we found a connection opportunity then process the header // template. if (connectionOpportunity != null) { mergeFields = RequestContext.GetCommonMergeFields(); mergeFields.Add("ConnectionOpportunity", connectionOpportunity); headerContent = HeaderTemplate.ResolveMergeFields(mergeFields); } return(new GetRequestsViewModel { HasMore = hasMore, PageNumber = pageNumber, HeaderContent = headerContent, Content = content }); } }
/// <summary> /// Gets the connection requests view model that can be sent to the client. /// </summary> private void GetConnectionRequests(bool previous = false) { if (ViewState[ViewStateKeys.GetRequestsViewModel] != null) { _currentRequestsViewModel = ViewState[ViewStateKeys.GetRequestsViewModel] as GetRequestsViewModel; } int pageNumber = 0; if (!previous) { pageNumber = _currentRequestsViewModel != null ? _currentRequestsViewModel.PageNumber + 1 : 0; } else { pageNumber = _currentRequestsViewModel != null ? _currentRequestsViewModel.PageNumber - 1 : 0; } using (var rockContext = new RockContext()) { var connectionRequestService = new ConnectionRequestService(rockContext); var connectionOpportunity = new ConnectionOpportunityService(rockContext).GetNoTracking(_connectionOpportunityGuid); if (connectionOpportunity == null) { return; } bool hasMore; List <ConnectionRequest> requests; // Determine if we should update the page title with the connection opportunity name var updatePageTitle = GetAttributeValue(AttributeKey.UpdatePageTitle).AsBoolean(); if (updatePageTitle) { RockPage.PageTitle = connectionOpportunity.Name; var pageBreadCrumb = RockPage.PageReference.BreadCrumbs.FirstOrDefault(); if (pageBreadCrumb != null) { pageBreadCrumb.Name = RockPage.PageTitle; } } if (_onlyShowMyConnections && CurrentPerson == null) { hasMore = false; requests = new List <ConnectionRequest>(); } else { var filterOptions = new ConnectionRequestQueryOptions { ConnectionOpportunityGuids = new List <Guid> { _connectionOpportunityGuid }, }; if (_connectionStates.Count > 0) { filterOptions.ConnectionStates = _connectionStates; } else { filterOptions.ConnectionStates = null; } if (_onlyShowMyConnections) { filterOptions.ConnectorPersonIds = new List <int> { CurrentPerson.Id }; } var qry = connectionRequestService.GetConnectionRequestsQuery(filterOptions) .Include(r => r.PersonAlias.Person) .Include(r => r.ConnectionRequestActivities); // We currently don't support showing connected connection requests // since that could end up being a massive list for mobile. qry = qry.Where(r => r.ConnectionState != ConnectionState.Connected); if (connectionOpportunity.ConnectionType.EnableRequestSecurity) { // Put all the requests in memory so we can check security and // then get the current set of requests, plus one. The extra is // so that we can tell if there are more to load. requests = qry .OrderByDescending(r => r.CreatedDateKey).ThenByDescending(r => r.Id) .ToList() .Where(r => r.IsAuthorized(Authorization.VIEW, CurrentPerson)) .Skip((pageNumber * MaxRequestsToShow)) .Take(MaxRequestsToShow + 1) .ToList(); } else { requests = qry .OrderByDescending(r => r.CreatedDateKey).ThenByDescending(r => r.Id) .Skip((pageNumber * MaxRequestsToShow)) .Take(MaxRequestsToShow + 1) .ToList(); } // Determine if we have more requests to show and then properly // limit the requests to the correct amount. hasMore = requests.Count > MaxRequestsToShow; requests = requests.Take(MaxRequestsToShow).ToList(); } // Process the connection requests with the template. var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); mergeFields.AddOrReplace("ConnectionRequests", requests); mergeFields.AddOrReplace("DetailPage", DetailPageGuid); var content = RequestTemplate .ResolveMergeFields(mergeFields) .ResolveClientIds(upConnectionSelectLava.ClientID); if (connectionOpportunity != null) { mergeFields.Add("ConnectionOpportunity", connectionOpportunity); } lContent.Text = content; _currentRequestsViewModel = new GetRequestsViewModel { HasMore = hasMore, PageNumber = pageNumber, }; lbLoadPrevious.Visible = pageNumber != 0; lbLoadMore.Visible = _currentRequestsViewModel.HasMore; //Store current page information in view state so we can load next data pages ViewState[ViewStateKeys.GetRequestsViewModel] = _currentRequestsViewModel; } }