Example #1
0
        /// <summary>
        /// Gets the subscription options for the current person.
        /// </summary>
        /// <returns>A collection of <see cref="Subscription"/> objects.</returns>
        protected virtual IEnumerable <Subscription> GetSubscriptions()
        {
            int communicationListGroupTypeId            = GroupTypeCache.Get(Rock.SystemGuid.GroupType.GROUPTYPE_COMMUNICATIONLIST.AsGuid()).Id;
            int?communicationListGroupTypeDefaultRoleId = GroupTypeCache.Get(communicationListGroupTypeId).DefaultGroupRoleId;

            if (RequestContext.CurrentPerson == null)
            {
                return(new List <Subscription>());
            }

            var rockContext = new RockContext();

            //
            // Get all the lists this person is already a member of.
            //
            var memberOfList = new GroupMemberService(rockContext)
                               .GetByPersonId(RequestContext.CurrentPerson.Id)
                               .AsNoTracking()
                               .Where(a => a.Group.GroupTypeId == communicationListGroupTypeId)
                               .Select(a => a.GroupId)
                               .ToList();

            //
            // Get a list of syncs for the communication list groups
            // where the default role is sync'd AND the current person
            // is NOT a member of.
            // This is used to filter out the list of communication lists.
            //
            var commGroupSyncsForDefaultRole = new GroupSyncService(rockContext)
                                               .Queryable()
                                               .Where(a => a.Group.GroupTypeId == communicationListGroupTypeId)
                                               .Where(a => a.GroupTypeRoleId == communicationListGroupTypeDefaultRoleId)
                                               .Where(a => !memberOfList.Contains(a.GroupId))
                                               .Select(a => a.GroupId)
                                               .ToList();

            //
            // Get all the communication lists, excluding those from the above
            // query about synced groups.
            //
            var communicationLists = new GroupService(rockContext)
                                     .Queryable()
                                     .Where(a => a.GroupTypeId == communicationListGroupTypeId)
                                     .Where(a => !commGroupSyncsForDefaultRole.Contains(a.Id))
                                     .ToList();

            var categoryGuids = CommunicationListCategories;

            var viewableCommunicationLists = communicationLists
                                             .Where(a =>
            {
                a.LoadAttributes(rockContext);

                if (!categoryGuids.Any())
                {
                    //
                    // If no categories where specified, only show
                    // lists that the person has VIEW auth to.
                    //
                    if (a.IsAuthorized(Rock.Security.Authorization.VIEW, RequestContext.CurrentPerson))
                    {
                        return(true);
                    }
                }
                else
                {
                    //
                    // If categories were specified, ensure that this
                    // communication list has a category and is one of
                    // the specified categories.
                    //
                    Guid?categoryGuid = a.GetAttributeValue("Category").AsGuidOrNull();
                    if (categoryGuid.HasValue && categoryGuids.Contains(categoryGuid.Value))
                    {
                        return(true);
                    }
                }

                return(false);
            })
                                             .ToList();

            var groupIds = viewableCommunicationLists.Select(a => a.Id).ToList();
            var personId = RequestContext.CurrentPerson.Id;

            var communicationListsMember = new GroupMemberService(rockContext)
                                           .Queryable()
                                           .AsNoTracking()
                                           .Where(a => groupIds.Contains(a.GroupId) && a.PersonId == personId)
                                           .GroupBy(a => a.GroupId)
                                           .ToList()
                                           .ToDictionary(k => k.Key, v => v.FirstOrDefault());

            return(viewableCommunicationLists
                   .Select(a =>
            {
                var publicName = a.GetAttributeValue("PublicName");

                return new Subscription
                {
                    DisplayName = publicName.IsNotNullOrWhiteSpace() ? publicName : a.Name,
                    CommunicationList = a,
                    Member = communicationListsMember.GetValueOrDefault(a.Id, null)
                };
            })
                   .OrderBy(a => a.DisplayName)
                   .ToList());
        }