Exemplo n.º 1
0
        /// <summary>
        /// Gets the prayer requests that match our configuration.
        /// </summary>
        /// <param name="campusGuid">The campus unique identifier.</param>
        /// <param name="rockContext">The rock context to operate in.</param>
        /// <returns>A collection of <see cref="PrayerRequest"/> objects.</returns>
        private List <PrayerRequest> GetPrayerRequests(Guid?campusGuid, RockContext rockContext)
        {
            var prayerRequestService = new PrayerRequestService(rockContext);

            var queryOptions = new PrayerRequestQueryOptions
            {
                IncludeNonPublic     = !PublicOnly,
                IncludeEmptyCampus   = true,
                IncludeGroupRequests = IncludeGroupRequests,
                Categories           = new List <Guid> {
                    CategoryGuid ?? Guid.Empty
                }
            };

            // If we have been requested to show only prayer requests attached
            // to a specific group, then add that identifier to the options.
            if (GroupGuid.HasValue)
            {
                queryOptions.GroupGuids = new List <Guid> {
                    GroupGuid.Value
                };
            }

            // If we have shown the campus picker and been provided with a campus
            // then add its identifier to the options.
            if (!AlwaysHideCampus && campusGuid.HasValue)
            {
                queryOptions.Campuses = new List <Guid> {
                    campusGuid.Value
                };
            }

            // Get the prayer requests filtered to our block settings.
            IEnumerable <PrayerRequest> prayerRequests = prayerRequestService.GetPrayerRequests(queryOptions);

            // Order by how the block has been configured.
            prayerRequests = prayerRequests.OrderBy(PrayerOrder);

            // Limit the maximum number of prayer requests.
            if (MaxRequests.HasValue)
            {
                prayerRequests = prayerRequests.Take(MaxRequests.Value);
            }

            return(prayerRequests.ToList());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a collection of prayer requests by applying a standard filtering
        /// algorithm as specified in the options.
        /// </summary>
        /// <param name="options">The options that specifies the filters.</param>
        /// <returns>A collection of <see cref="PrayerRequest"/> objects.</returns>
        public IQueryable <PrayerRequest> GetPrayerRequests(PrayerRequestQueryOptions options)
        {
            var qryPrayerRequests = Queryable();

            // If not including inactive requests then filter them out.
            if (!options.IncludeInactive)
            {
                qryPrayerRequests = qryPrayerRequests.Where(r => r.IsActive ?? true);
            }

            // If not including expired requests then filter them out.
            if (!options.IncludeExpired)
            {
                qryPrayerRequests = qryPrayerRequests.Where(r => !r.ExpirationDate.HasValue ||
                                                            r.ExpirationDate >= RockDateTime.Now);
            }

            // If not including unapproved requests then filter them out.
            if (!options.IncludeUnapproved)
            {
                qryPrayerRequests = qryPrayerRequests.Where(r => r.IsApproved == true);
            }

            // If not including non-public requests then filter them out.
            if (!options.IncludeNonPublic)
            {
                qryPrayerRequests = qryPrayerRequests.Where(r => r.IsPublic == true);
            }

            // Filter by category if we have been given any.
            if (options.Categories != null && options.Categories.Any())
            {
                var categoryService = new CategoryService(( RockContext )Context);
                var categories      = new List <Guid>(options.Categories);

                // If filtered by category, only show prayer requests in that
                // category or any of its descendant categories.
                foreach (var categoryGuid in options.Categories)
                {
                    categoryService.GetAllDescendents(categoryGuid)
                    .Select(a => a.Guid)
                    .ToList()
                    .ForEach(c => categories.Add(c));
                }

                categories = categories.Distinct().ToList();

                qryPrayerRequests = qryPrayerRequests
                                    .Include(r => r.Category)
                                    .Where(r => r.CategoryId.HasValue && categories.Contains(r.Category.Guid));
            }

            // Filter by campus if we have been given any.
            if (options.Campuses != null && options.Campuses.Any())
            {
                if (options.IncludeEmptyCampus)
                {
                    qryPrayerRequests = qryPrayerRequests
                                        .Include(r => r.Campus)
                                        .Where(r => !r.CampusId.HasValue || (r.CampusId.HasValue && options.Campuses.Contains(r.Campus.Guid)));
                }
                else
                {
                    qryPrayerRequests = qryPrayerRequests
                                        .Include(r => r.Campus)
                                        .Where(r => r.CampusId.HasValue && options.Campuses.Contains(r.Campus.Guid));
                }
            }

            // Filter by group if it has been specified.
            if (options.GroupGuids?.Any() ?? false)
            {
                qryPrayerRequests = qryPrayerRequests
                                    .Where(a => options.GroupGuids.Contains(a.Group.Guid));
            }

            // If we are not filtering by group, then exclude any group requests
            // unless the block setting including them is enabled.
            if (!(options.GroupGuids?.Any() ?? false) && !options.IncludeGroupRequests)
            {
                qryPrayerRequests = qryPrayerRequests.Where(a => !a.GroupId.HasValue);
            }

            return(qryPrayerRequests);
        }