Esempio 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());
        }
        // GET:
        public ActionResult Index(int?memberId, int?ccgId,
                                  int?dateRangeFilter = (int)DateRangeFilter.TwoWeeks,
                                  int?sortOption      = (int)ContactsSort.DateDescending,
                                  int?page            = 1, int?itemsPerPage = 10, bool getAll = false, string query = null)
        {
            // Cast int passed by route to an enum
            var dateFilter   = dateRangeFilter != null ? (DateRangeFilter)dateRangeFilter : DateRangeFilter.TwoWeeks;
            var contactsSort = sortOption == null ? ContactsSort.DateDescending : (ContactsSort)sortOption;

            #region Set params to pass to view
            var prayerRequestsList = new PrayerRequestListViewModel
            {
                Params = new ActionMethodParams
                {
                    MemberId        = memberId,
                    CCGId           = ccgId,
                    ItemsPerPage    = itemsPerPage,
                    Page            = page,
                    GetAll          = getAll,
                    DateRangeFilter = dateFilter,
                    Query           = query,
                    ContactsSort    = contactsSort
                }
            };
            #endregion

            // Get principal user obj
            var user = unitOfWork.AppUserRepository.FindUserByEmail(User.Identity.Name);

            if (memberId != null)
            {
                var member = unitOfWork.MemberRepository.FindMemberById(memberId);
                ViewBag.MemberName = $"{member.FirstName} {member.LastName}";
            }

            DateTime dateTimeOffset;
            _service.GetOffsetDate(dateFilter, out dateTimeOffset);

            List <ContactRecord> prayerRequests;


            // determine if user is leadership, pastor or admin
            AppUserRole[] roles = new AppUserRole[] { AppUserRole.Admin,
                                                      AppUserRole.DeaconLeadership,
                                                      AppUserRole.Pastor };
            // get all if user is admin, leadership, or pastor
            getAll = AuthHelper.IsInRole(User.Identity.Name, roles);

            // not leadership so only get prayer requests within ccg
            if (!getAll && ccgId == null)
            {
                ccgId = user.CcgId;
            }

            prayerRequests = _service.GetPrayerRequests(memberId,
                                                        dateTimeOffset, ccgId, getAll, user, query).ToList();

            // Sort prayer requests
            prayerRequests = _service.SortContactRecords(contactsSort, prayerRequests).ToList();

            // Used to check permission to edit in the view
            prayerRequestsList.Params.UserId = user.Id;

            var prayerRequestsVM = Mapper.Map <IList <PrayerRequestViewModel> >(prayerRequests);

            prayerRequestsList.PrayerRequests = prayerRequestsVM.ToPagedList(page ?? 1, itemsPerPage ?? 10);


            // Truncates the subject and comments
            // Keeps comments private if marked as such
            _service.TruncateTextAndKeepPrivate(prayerRequestsList.PrayerRequests, user.Id);

            // Get CCGs for filter menu
            var ccgs = unitOfWork.CCGRepository.FindAll().ToList();

            // Add 'All CCGs' option to menu
            _ccgService.SetCCGViewName(ccgs);
            ccgs.Insert(0, new CCG {
                Id = -1, CCGName = "All CCGs"
            });
            prayerRequestsList.CCGs = new SelectList(ccgs, "Id", "CcgName");

            if (ccgId != null && ccgId != -1)
            {
                var ccg = unitOfWork.CCGRepository.Find(g => g.Id == ccgId);
                prayerRequestsList.CcgName = ccg.CCGName;
            }

            return(View(prayerRequestsList));
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the prayer requests to be displayed.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <returns>A list of <see cref="PrayerRequest"/> entities.</returns>
        private List <PrayerRequest> GetPrayerRequests(RockContext rockContext)
        {
            var prayerRequestService = new PrayerRequestService(rockContext);

            // Determine the category to filter to.
            var categoryGuid        = GetAttributeValue(AttributeKey.Category).AsGuidOrNull();
            var categoryIdQryString = PageParameter(PageParameterKey.CategoryId).AsIntegerOrNull();

            if (categoryGuid == null && categoryIdQryString.HasValue)
            {
                categoryGuid = CategoryCache.Get(categoryIdQryString.Value)?.Guid;
            }

            // Determine the campus to filter to.
            List <Guid> campusGuids = null;
            var         qryCampusId = PageParameter(PageParameterKey.CampusId).AsIntegerOrNull();

            if (qryCampusId.HasValue)
            {
                var campusCache = CampusCache.Get(qryCampusId.Value);

                if (campusCache != null)
                {
                    campusGuids = new List <Guid> {
                        campusCache.Guid
                    };
                }
            }
            else if (cpCampus.Visible)
            {
                if (cpCampus.SelectedCampusId.HasValue)
                {
                    var campusCache = CampusCache.Get(cpCampus.SelectedCampusId.Value);

                    if (campusCache != null)
                    {
                        campusGuids = new List <Guid> {
                            campusCache.Guid
                        };
                    }
                }
                else
                {
                    campusGuids = cpCampus.Items
                                  .Cast <ListItem>()
                                  .Select(i => i.Value)
                                  .AsIntegerList()
                                  .Select(i => CampusCache.Get(i))
                                  .Where(c => c != null)
                                  .Select(c => c.Guid)
                                  .ToList();
                }
            }

            IEnumerable <PrayerRequest> qryPrayerRequests = prayerRequestService.GetPrayerRequests(new PrayerRequestQueryOptions
            {
                IncludeEmptyCampus = true,
                IncludeNonPublic   = !GetAttributeValue(AttributeKey.PublicOnly).AsBoolean(),
                Campuses           = campusGuids,
                Categories         = categoryGuid.HasValue ? new List <Guid> {
                    categoryGuid.Value
                } : null
            });

            // Order by how the block has been configured.
            var sortBy = GetAttributeValue(AttributeKey.Order).ConvertToEnum <PrayerRequestOrder>(PrayerRequestOrder.LeastPrayedFor);

            qryPrayerRequests = qryPrayerRequests.OrderBy(sortBy);

            // Limit the maximum number of prayer requests.
            int?maxResults = GetAttributeValue(AttributeKey.MaxResults).AsIntegerOrNull();

            if (maxResults.HasValue && maxResults > 0)
            {
                qryPrayerRequests = qryPrayerRequests.Take(maxResults.Value);
            }

            return(qryPrayerRequests.ToList());
        }