Example #1
0
        /// <summary>
        /// Gets the grid data source list (ordered)
        /// </summary>
        /// <returns>List&lt;PersonalLinkSectionViewModel&gt;.</returns>
        private List <PersonalLinkSectionViewModel> GetGridDataSourceList(RockContext rockContext)
        {
            var limitToSharedSections = GetAttributeValue(AttributeKey.SharedSections).AsBoolean();
            List <PersonalLinkSection> personalLinkSectionList;
            Dictionary <int, PersonalLinkSectionOrder> currentPersonSectionOrderLookupBySectionId = null;

            if (limitToSharedSections)
            {
                // only show shared sections in this mode
                var sharedPersonalLinkSectionsQuery = new PersonalLinkSectionService(rockContext).Queryable().Where(a => a.IsShared);
                personalLinkSectionList = sharedPersonalLinkSectionsQuery.Include(a => a.PersonalLinks).OrderBy(a => a.Name).AsNoTracking().ToList();
            }
            else
            {
                // show both shared and non-shared, but don't let shared sections get deleted (even if authorized)
                var personalLinkService = new PersonalLinkService(rockContext);
                if (personalLinkService.AddMissingPersonalLinkSectionOrders(this.CurrentPerson))
                {
                    rockContext.SaveChanges();
                }

                var orderedPersonalLinkSectionsQuery = new PersonalLinkService(rockContext).GetOrderedPersonalLinkSectionsQuery(this.CurrentPerson);

                personalLinkSectionList = orderedPersonalLinkSectionsQuery
                                          .Include(a => a.PersonalLinks)
                                          .AsNoTracking()
                                          .ToList()
                                          .Where(a => a.IsAuthorized(Rock.Security.Authorization.VIEW, this.CurrentPerson))
                                          .ToList();

                // NOTE: We might be making changes when resorting this, so don't use AsNoTracking()
                var sectionOrderQuery = personalLinkService.GetSectionOrderQuery(this.CurrentPerson);
                currentPersonSectionOrderLookupBySectionId = sectionOrderQuery.ToDictionary(k => k.SectionId, v => v);
            }

            gSectionList.EntityTypeId = EntityTypeCache.GetId <PersonalLinkSection>();

            var viewModelList = personalLinkSectionList.Select(a =>
            {
                var personalLinkSectionViewModel = new PersonalLinkSectionViewModel
                {
                    Id        = a.Id,
                    Name      = a.Name,
                    LinkCount = a.PersonalLinks.Where(x => x.IsAuthorized(Rock.Security.Authorization.VIEW, this.CurrentPerson)).Count(),
                    IsShared  = a.IsShared,
                    PersonalLinkSectionOrder = currentPersonSectionOrderLookupBySectionId?.GetValueOrNull(a.Id)
                };

                if (limitToSharedSections)
                {
                    // if we are only showing shared sections, let them edit it if authorized edit
                    personalLinkSectionViewModel.CanEdit = a.IsAuthorized(Authorization.EDIT, CurrentPerson);
                }
                else
                {
                    // Don't allow shared sections to be deleted/edited if we showing both shared and non-shared sections
                    personalLinkSectionViewModel.CanEdit = !a.IsShared;
                }

                personalLinkSectionViewModel.CanDelete = personalLinkSectionViewModel.CanEdit;

                return(personalLinkSectionViewModel);
            }).ToList();

            return(viewModelList.OrderBy(a => a.PersonalLinkSectionOrder?.Order ?? 0).ThenBy(a => a.Name).ToList());
        }