/// <summary>
        /// Builds the group types UI.
        /// </summary>
        private void BuildGroupTypesUI()
        {
            var rockContext = new RockContext();

            var groupTypeService      = new GroupTypeService(rockContext);
            var groupTypeTemplateGuid = this.GetAttributeValue("GroupTypeTemplate").AsGuidOrNull();

            nbGroupTypeWarning.Visible = !groupTypeTemplateGuid.HasValue;
            if (groupTypeTemplateGuid.HasValue)
            {
                var groupType  = groupTypeService.Get(groupTypeTemplateGuid.Value);
                var groupTypes = groupTypeService.GetChildGroupTypes(groupType.Id).OrderBy(a => a.Order).ThenBy(a => a.Name);
                rptGroupTypes.DataSource = groupTypes.ToList();
                rptGroupTypes.DataBind();
            }
        }
Пример #2
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        protected void BindGrid()
        {
            AddScheduleColumns();

            var rockContext = new RockContext();

            var groupLocationService = new GroupLocationService(rockContext);
            var groupTypeService     = new GroupTypeService(rockContext);
            var groupService         = new GroupService(rockContext);

            var groupPaths       = new List <GroupTypePath>();
            var groupLocationQry = groupLocationService.Queryable().Where(gl => gl.Group.IsActive);
            int groupTypeId;

            // if this page has a PageParam for groupTypeId use that to limit which groupTypeId to see. Otherwise, use the groupTypeId specified in the filter
            if (_groupTypeId.HasValue)
            {
                groupTypeId = _groupTypeId.Value;
            }
            else
            {
                groupTypeId = ddlGroupType.SelectedValueAsInt() ?? Rock.Constants.All.Id;
            }

            if (groupTypeId != Rock.Constants.All.Id)
            {
                var descendantGroupTypeIds = groupTypeService.GetAllAssociatedDescendents(groupTypeId).Select(a => a.Id);

                // filter to groups that either are of the GroupType or are of a GroupType that has the selected GroupType as a parent (ancestor)
                groupLocationQry = groupLocationQry.Where(a => a.Group.GroupType.Id == groupTypeId || descendantGroupTypeIds.Contains(a.Group.GroupTypeId));

                groupPaths = groupTypeService.GetAllAssociatedDescendentsPath(groupTypeId).ToList();
            }
            else
            {
                List <int> descendantGroupTypeIds = new List <int>();
                foreach (GroupType groupType in GetTopGroupTypes(rockContext))
                {
                    descendantGroupTypeIds.Add(groupType.Id);

                    groupPaths.AddRange(groupTypeService.GetAllAssociatedDescendentsPath(groupType.Id).ToList());
                    foreach (var childGroupType in groupTypeService.GetChildGroupTypes(groupType.Id))
                    {
                        descendantGroupTypeIds.Add(childGroupType.Id);
                        descendantGroupTypeIds.AddRange(groupTypeService.GetAllAssociatedDescendents(childGroupType.Id).Select(a => a.Id).ToList());
                    }
                }

                groupLocationQry = groupLocationQry.Where(a => descendantGroupTypeIds.Contains(a.Group.GroupTypeId));
            }

            if (gGroupLocationSchedule.SortProperty != null)
            {
                groupLocationQry = groupLocationQry.Sort(gGroupLocationSchedule.SortProperty);
            }
            else
            {
                groupLocationQry = groupLocationQry.OrderBy(a => a.Group.Name).ThenBy(a => a.Location.Name);
            }

            var qryList = groupLocationQry
                          .Where(a => a.Location != null)
                          .Select(a =>
                                  new
            {
                GroupLocationId = a.Id,
                a.Location,
                GroupId        = a.GroupId,
                GroupName      = a.Group.Name,
                ScheduleIdList = a.Schedules.Select(s => s.Id),
                GroupTypeId    = a.Group.GroupTypeId
            }).ToList();

            var locationService  = new LocationService(rockContext);
            int parentLocationId = pkrParentLocation.SelectedValueAsInt() ?? Rock.Constants.All.Id;

            if (parentLocationId != Rock.Constants.All.Id)
            {
                var currentAndDescendantLocationIds = new List <int>();
                currentAndDescendantLocationIds.Add(parentLocationId);
                currentAndDescendantLocationIds.AddRange(locationService.GetAllDescendents(parentLocationId).Select(a => a.Id));

                qryList = qryList.Where(a => currentAndDescendantLocationIds.Contains(a.Location.Id)).ToList();
            }

            // put stuff in a datatable so we can dynamically have columns for each Schedule
            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("GroupLocationId");
            dataTable.Columns.Add("GroupId");
            dataTable.Columns.Add("GroupName");
            dataTable.Columns.Add("GroupPath");
            dataTable.Columns.Add("LocationName");
            dataTable.Columns.Add("LocationPath");
            foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>())
            {
                dataTable.Columns.Add(field.DataField, typeof(bool));
            }

            var locationPaths = new Dictionary <int, string>();

            foreach (var row in qryList)
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow["GroupLocationId"] = row.GroupLocationId;
                dataRow["GroupName"]       = groupService.GroupAncestorPathName(row.GroupId);
                dataRow["GroupPath"]       = groupPaths.Where(gt => gt.GroupTypeId == row.GroupTypeId).Select(gt => gt.Path).FirstOrDefault();
                dataRow["LocationName"]    = row.Location.Name;

                if (row.Location.ParentLocationId.HasValue)
                {
                    int locationId = row.Location.ParentLocationId.Value;

                    if (!locationPaths.ContainsKey(locationId))
                    {
                        var locationNames  = new List <string>();
                        var parentLocation = locationService.Get(locationId);
                        while (parentLocation != null)
                        {
                            locationNames.Add(parentLocation.Name);
                            parentLocation = parentLocation.ParentLocation;
                        }
                        if (locationNames.Any())
                        {
                            locationNames.Reverse();
                            locationPaths.Add(locationId, locationNames.AsDelimited(" > "));
                        }
                        else
                        {
                            locationPaths.Add(locationId, string.Empty);
                        }
                    }

                    dataRow["LocationPath"] = locationPaths[locationId];
                }

                foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>())
                {
                    int scheduleId = int.Parse(field.DataField.Replace("scheduleField_", string.Empty));
                    dataRow[field.DataField] = row.ScheduleIdList.Any(a => a == scheduleId);
                }

                dataTable.Rows.Add(dataRow);
            }

            gGroupLocationSchedule.EntityTypeId = EntityTypeCache.Read <GroupLocation>().Id;
            gGroupLocationSchedule.DataSource   = dataTable;
            gGroupLocationSchedule.DataBind();
        }
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var entityTypeGroupGuid  = Rock.SystemGuid.EntityType.GROUP.AsGuid();
            var entityTypePersonGuid = Rock.SystemGuid.EntityType.PERSON.AsGuid();

            RockContext                rockContext                = new RockContext();
            PersonService              personService              = new PersonService(rockContext);
            GroupService               groupService               = new GroupService(rockContext);
            GroupTypeService           groupTypeService           = new GroupTypeService(rockContext);
            VolunteerMembershipService volunteerMembershipService = new VolunteerMembershipService(new VolunteerTrackingContext());

            var servingTeamGroupTypeGuid = GetAttributeValue("ServingGroupType").AsGuidOrNull();

            if (servingTeamGroupTypeGuid.HasValue)
            {
                var servingTeamChildGroupTypes = groupTypeService.GetChildGroupTypes(servingTeamGroupTypeGuid.Value);

                var servingGroupQry = groupService.Queryable().Where(g => servingTeamChildGroupTypes.Select(gt => gt.Id).Contains(g.GroupTypeId));

                var servingGroupIds = servingGroupQry.Select(g => g.Id).ToList();

                var servingGroups = new List <GroupSummary>();

                foreach (var servingGroup in servingGroupQry.ToList())
                {
                    servingGroup.LoadAttributes();
                    servingGroups.Add(new GroupSummary
                    {
                        GroupId         = servingGroup.Id,
                        ParentGroupId   = servingGroup.ParentGroupId,
                        GroupName       = servingGroup.Name,
                        ParentGroupName = servingGroup.ParentGroupId != null ? servingGroup.ParentGroup.Name : string.Empty,
                        Director        = servingGroup.GetAttributeValue(GetAttributeValue("DirectorAttributeKey")),
                        VolunteerGoal   = servingGroup.GetAttributeValue(GetAttributeValue("VolunteerGoalAttributeKey")).AsIntegerOrNull(),
                        LeaderGoal      = servingGroup.GetAttributeValue(GetAttributeValue("LeaderGoalAttributeKey")).AsIntegerOrNull()
                    });
                }

                var volunteerQry = volunteerMembershipService.Queryable().Where(vm => servingGroupIds.Contains(vm.GroupId));

                var volunteerSummary = volunteerQry.Select(vm => new
                {
                    GroupId   = vm.GroupId,
                    Volunteer = vm
                })
                                       .GroupBy(vm => vm.GroupId)
                                       .Select(vmg => new
                {
                    GroupId    = vmg.Key,
                    Volunteers = vmg.Select(v => v.Volunteer).ToList()
                });

                var servingGroupSummary = from g in servingGroups
                                          join vmg in volunteerSummary
                                          on g.GroupId equals vmg.GroupId into joinResult
                                          from x in joinResult.DefaultIfEmpty(new { GroupId = g.GroupId, Volunteers = new List <VolunteerMembership>() })
                                          select new
                {
                    Group      = g,
                    Volunteers = x.Volunteers
                };

                DateRange dateRange = new DateRange(dpStart.SelectedDate, dpEnd.SelectedDate);
                if (dpStart.SelectedDate == null)
                {
                    dateRange.Start = RockDateTime.Now;
                }

                if (dpStart.SelectedDate == null)
                {
                    dateRange.End = RockDateTime.Now;
                }

                IEnumerable <DateRange> dateRangeList = SplitDateRangeIntoWeeks(dateRange);

                var gListSource = servingGroupSummary.SelectMany(g => dateRangeList, (g, d) => new
                {
                    Ministry           = g.Group.ParentGroupId != null ? g.Group.ParentGroupName : g.Group.GroupName,
                    Director           = g.Group.Director,
                    DateRange          = d.Start.Value.ToShortDateString() + " - " + d.End.Value.ToShortDateString(),
                    StartingVolunteers = g.Volunteers.Where(vm => !vm.GroupRole.IsLeader &&
                                                            (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.Start.Value) &&
                                                            vm.JoinedGroupDateTime < d.Start.Value)
                                         .DistinctBy(vm => vm.PersonId)
                                         .Count(),
                    NewVolunteers = g.Volunteers.Where(vm => !vm.GroupRole.IsLeader &&
                                                       (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.End.Value) &&
                                                       (vm.JoinedGroupDateTime >= d.Start.Value && vm.JoinedGroupDateTime < d.End.Value))
                                    .DistinctBy(vm => vm.PersonId).Count(),
                    LostVolunteers = g.Volunteers.Where(vm => !vm.GroupRole.IsLeader)
                                     .GroupBy(vm => vm.PersonId)
                                     .Where(vmg => vmg.Where(v => v.LeftGroupDateTime >= d.Start.Value && v.LeftGroupDateTime < d.End.Value).Count() > vmg.Where(v => v.JoinedGroupDateTime >= d.Start.Value && v.JoinedGroupDateTime < d.End.Value).Count())
                                     .Count(),
                    TotalVolunteers = g.Volunteers.Where(vm => !vm.GroupRole.IsLeader &&
                                                         (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.End.Value) &&
                                                         vm.JoinedGroupDateTime < d.End.Value)
                                      .DistinctBy(vm => vm.PersonId)
                                      .Count(),
                    VolunteerGoal    = g.Group.VolunteerGoal ?? null,
                    VolunteerPercent = g.Group.VolunteerGoal != null ? g.Volunteers.Where(vm => !vm.GroupRole.IsLeader &&
                                                                                          (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.End.Value) &&
                                                                                          vm.JoinedGroupDateTime < d.End.Value)
                                       .DistinctBy(vm => vm.PersonId)
                                       .Count() / g.Group.VolunteerGoal : null,
                    StartingLeaders = g.Volunteers.Where(vm => vm.GroupRole.IsLeader &&
                                                         (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.Start.Value) &&
                                                         vm.JoinedGroupDateTime < d.Start.Value)
                                      .DistinctBy(vm => vm.PersonId)
                                      .Count(),
                    NewLeaders = g.Volunteers.Where(vm => vm.GroupRole.IsLeader &&
                                                    (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.End.Value) &&
                                                    (vm.JoinedGroupDateTime >= d.Start.Value && vm.JoinedGroupDateTime < d.End.Value))
                                 .DistinctBy(vm => vm.PersonId).Count(),
                    LostLeaders = g.Volunteers.Where(vm => vm.GroupRole.IsLeader)
                                  .GroupBy(vm => vm.PersonId)
                                  .Where(vmg => vmg.Where(v => v.LeftGroupDateTime >= d.Start.Value && v.LeftGroupDateTime < d.End.Value).Count() > vmg.Where(v => v.JoinedGroupDateTime >= d.Start.Value && v.JoinedGroupDateTime < d.End.Value).Count())
                                  .Count(),
                    TotalLeaders = g.Volunteers.Where(vm => vm.GroupRole.IsLeader &&
                                                      (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.End.Value) &&
                                                      vm.JoinedGroupDateTime < d.End.Value)
                                   .DistinctBy(vm => vm.PersonId)
                                   .Count(),
                    LeaderGoal    = g.Group.LeaderGoal ?? null,
                    LeaderPercent = g.Group.LeaderGoal != null ? g.Volunteers.Where(vm => vm.GroupRole.IsLeader &&
                                                                                    (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > d.End.Value) &&
                                                                                    vm.JoinedGroupDateTime < d.End.Value)
                                    .DistinctBy(vm => vm.PersonId)
                                    .Count() / g.Group.LeaderGoal : null
                }).ToList();

                gList.DataSource = gListSource.ToList();
                gList.DataBind();


                var gVolunteersSource = new[] { new {
                                                    StartingVolunteers = volunteerQry.Where(vm => !vm.GroupRole.IsLeader &&
                                                                                            (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.Start.Value) &&
                                                                                            vm.JoinedGroupDateTime < dateRange.Start.Value)
                                                                         .DistinctBy(vm => vm.PersonId)
                                                                         .Count(),
                                                    NewVolunteers = volunteerQry.Where(vm => !vm.GroupRole.IsLeader &&
                                                                                       (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                       (vm.JoinedGroupDateTime >= dateRange.Start.Value && vm.JoinedGroupDateTime < dateRange.End.Value))
                                                                    .DistinctBy(vm => vm.PersonId).Count(),
                                                    LostVolunteers = volunteerQry.Where(vm => !vm.GroupRole.IsLeader)
                                                                     .GroupBy(vm => vm.PersonId)
                                                                     .Where(vmg => vmg.Where(v => v.LeftGroupDateTime >= dateRange.Start.Value && v.LeftGroupDateTime < dateRange.End.Value).Count() > vmg.Where(v => v.JoinedGroupDateTime >= dateRange.Start.Value && v.JoinedGroupDateTime < dateRange.End.Value).Count())
                                                                     .Count(),
                                                    TotalVolunteers = volunteerQry.Where(vm => !vm.GroupRole.IsLeader &&
                                                                                         (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                         vm.JoinedGroupDateTime < dateRange.End.Value)
                                                                      .DistinctBy(vm => vm.PersonId)
                                                                      .Count(),
                                                    VolunteerGoal    = servingGroups.Sum(g => g.VolunteerGoal) ?? null,
                                                    VolunteerPercent = servingGroups.Sum(g => g.VolunteerGoal) > 0 ? volunteerQry.Where(vm => !vm.GroupRole.IsLeader &&
                                                                                                                                        (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                                                                        vm.JoinedGroupDateTime < dateRange.End.Value)
                                                                       .DistinctBy(vm => vm.PersonId)
                                                                       .Count() / servingGroups.Sum(g => g.VolunteerGoal) : null,
                                                } };

                gVolunteers.DataSource = gVolunteersSource.ToList();
                gVolunteers.DataBind();
                gVolunteers.ShowFooter = false;

                var gLeadersSource = new[] { new {
                                                 StartingLeaders = volunteerQry.Where(vm => vm.GroupRole.IsLeader &&
                                                                                      (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.Start.Value) &&
                                                                                      vm.JoinedGroupDateTime < dateRange.Start.Value)
                                                                   .DistinctBy(vm => vm.PersonId)
                                                                   .Count(),
                                                 NewLeaders = volunteerQry.Where(vm => vm.GroupRole.IsLeader &&
                                                                                 (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                 (vm.JoinedGroupDateTime >= dateRange.Start.Value && vm.JoinedGroupDateTime < dateRange.End.Value))
                                                              .DistinctBy(vm => vm.PersonId).Count(),
                                                 LostLeaders = volunteerQry.Where(vm => vm.GroupRole.IsLeader)
                                                               .GroupBy(vm => vm.PersonId)
                                                               .Where(vmg => vmg.Where(v => v.LeftGroupDateTime >= dateRange.Start.Value && v.LeftGroupDateTime < dateRange.End.Value).Count() > vmg.Where(v => v.JoinedGroupDateTime >= dateRange.Start.Value && v.JoinedGroupDateTime < dateRange.End.Value).Count())
                                                               .Count(),
                                                 TotalLeaders = volunteerQry.Where(vm => vm.GroupRole.IsLeader &&
                                                                                   (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                   vm.JoinedGroupDateTime < dateRange.End.Value)
                                                                .DistinctBy(vm => vm.PersonId)
                                                                .Count(),
                                                 LeaderGoal    = servingGroups.Sum(g => g.LeaderGoal) ?? null,
                                                 LeaderPercent = servingGroups.Sum(g => g.LeaderGoal) > 0 ? volunteerQry.Where(vm => vm.GroupRole.IsLeader &&
                                                                                                                               (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                                                               vm.JoinedGroupDateTime < dateRange.End.Value)
                                                                 .DistinctBy(vm => vm.PersonId)
                                                                 .Count() / servingGroups.Sum(g => g.LeaderGoal) : null,
                                             } };

                gLeaders.DataSource = gLeadersSource.ToList();
                gLeaders.DataBind();

                var gUniquesSource = new[] { new {
                                                 UniqueVolunteers = volunteerQry.Where(vm => !vm.GroupRole.IsLeader &&
                                                                                       (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                       vm.JoinedGroupDateTime < dateRange.End.Value)
                                                                    .DistinctBy(vm => vm.PersonId)
                                                                    .Count(),
                                                 UniqueLeaders = volunteerQry.Where(vm => vm.GroupRole.IsLeader &&
                                                                                    (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                    vm.JoinedGroupDateTime < dateRange.End.Value)
                                                                 .DistinctBy(vm => vm.PersonId)
                                                                 .Count(),
                                                 UniqueTotal = volunteerQry.Where(vm =>
                                                                                  (!vm.LeftGroupDateTime.HasValue || vm.LeftGroupDateTime > dateRange.End.Value) &&
                                                                                  vm.JoinedGroupDateTime < dateRange.End.Value)
                                                               .DistinctBy(vm => vm.PersonId)
                                                               .Count()
                                             } };

                gUniques.DataSource = gUniquesSource.ToList();
                gUniques.DataBind();
            }
        }
        /// <summary>
        /// Binds the grid.
        /// </summary>
        protected void BindGrid()
        {
            AddScheduleColumns();

            var rockContext = new RockContext();

            var groupLocationService = new GroupLocationService(rockContext);
            var groupTypeService     = new GroupTypeService(rockContext);
            var groupService         = new GroupService(rockContext);

            IEnumerable <GroupTypePath> groupPaths = new List <GroupTypePath>();
            var groupLocationQry = groupLocationService.Queryable();

            List <int> currentAndDescendantGroupTypeIds = new List <int>();
            var        currentGroupTypeIds = this.CurrentGroupTypeIds.ToList();

            currentAndDescendantGroupTypeIds.AddRange(currentGroupTypeIds);
            foreach (var templateGroupType in groupTypeService.Queryable().Where(a => currentGroupTypeIds.Contains(a.Id)))
            {
                foreach (var childGroupType in groupTypeService.GetChildGroupTypes(templateGroupType.Id))
                {
                    currentAndDescendantGroupTypeIds.Add(childGroupType.Id);
                    currentAndDescendantGroupTypeIds.AddRange(groupTypeService.GetAllAssociatedDescendents(childGroupType.Id).Select(a => a.Id).ToList());
                }
            }

            groupLocationQry = groupLocationQry.Where(a => currentAndDescendantGroupTypeIds.Contains(a.Group.GroupTypeId));

            groupLocationQry = groupLocationQry.OrderBy(a => a.Group.Name).ThenBy(a => a.Location.Name);

            List <int> currentDeviceLocationIdList = this.GetGroupTypesLocations(rockContext).Select(a => a.Id).Distinct().ToList();

            var qryList = groupLocationQry
                          .Where(a => currentDeviceLocationIdList.Contains(a.LocationId))
                          .Select(a =>
                                  new
            {
                GroupLocationId = a.Id,
                a.Location,
                GroupId        = a.GroupId,
                GroupName      = a.Group.Name,
                ScheduleIdList = a.Schedules.Select(s => s.Id),
                GroupTypeId    = a.Group.GroupTypeId
            }).ToList();

            var locationService = new LocationService(rockContext);

            // put stuff in a datatable so we can dynamically have columns for each Schedule
            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("GroupLocationId");
            dataTable.Columns.Add("GroupId");
            dataTable.Columns.Add("GroupName");
            dataTable.Columns.Add("GroupPath");
            dataTable.Columns.Add("LocationName");
            dataTable.Columns.Add("LocationPath");
            foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>())
            {
                dataTable.Columns.Add(field.DataField, typeof(bool));
            }

            var locationPaths = new Dictionary <int, string>();

            foreach (var row in qryList)
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow["GroupLocationId"] = row.GroupLocationId;
                dataRow["GroupName"]       = groupService.GroupAncestorPathName(row.GroupId);
                dataRow["GroupPath"]       = groupPaths.Where(gt => gt.GroupTypeId == row.GroupTypeId).Select(gt => gt.Path).FirstOrDefault();
                dataRow["LocationName"]    = row.Location.Name;

                if (row.Location.ParentLocationId.HasValue)
                {
                    int locationId = row.Location.ParentLocationId.Value;

                    if (!locationPaths.ContainsKey(locationId))
                    {
                        var locationNames  = new List <string>();
                        var parentLocation = locationService.Get(locationId);
                        while (parentLocation != null)
                        {
                            locationNames.Add(parentLocation.Name);
                            parentLocation = parentLocation.ParentLocation;
                        }

                        if (locationNames.Any())
                        {
                            locationNames.Reverse();
                            locationPaths.Add(locationId, locationNames.AsDelimited(" > "));
                        }
                        else
                        {
                            locationPaths.Add(locationId, string.Empty);
                        }
                    }

                    dataRow["LocationPath"] = locationPaths[locationId];
                }

                foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>())
                {
                    int scheduleId = int.Parse(field.DataField.Replace("scheduleField_", string.Empty));
                    dataRow[field.DataField] = row.ScheduleIdList.Any(a => a == scheduleId);
                }

                dataTable.Rows.Add(dataRow);
            }

            gGroupLocationSchedule.EntityTypeId = EntityTypeCache.Read <GroupLocation>().Id;
            gGroupLocationSchedule.DataSource   = dataTable;
            gGroupLocationSchedule.DataBind();
        }
Пример #5
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        protected void BindGrid()
        {
            AddScheduleColumns();

            var rockContext = new RockContext();

            var groupLocationService = new GroupLocationService(rockContext);
            var groupTypeService     = new GroupTypeService(rockContext);
            IEnumerable <GroupTypePath> groupPaths = new List <GroupTypePath>();
            var groupLocationQry = groupLocationService.Queryable();
            int groupTypeId;

            // if this page has a PageParam for groupTypeId use that to limit which groupTypeId to see. Otherwise, use the groupTypeId specified in the filter
            int?groupTypeIdPageParam = this.PageParameter("groupTypeId").AsIntegerOrNull();

            if (groupTypeIdPageParam.HasValue)
            {
                groupTypeId = groupTypeIdPageParam ?? Rock.Constants.All.Id;
            }
            else
            {
                groupTypeId = ddlGroupType.SelectedValueAsInt() ?? Rock.Constants.All.Id;
            }

            if (groupTypeId != Rock.Constants.All.Id)
            {
                var descendantGroupTypeIds = groupTypeService.GetAllAssociatedDescendents(groupTypeId).Select(a => a.Id);

                // filter to groups that either are of the GroupType or are of a GroupType that has the selected GroupType as a parent (ancestor)
                groupLocationQry = groupLocationQry.Where(a => a.Group.GroupType.Id == groupTypeId || descendantGroupTypeIds.Contains(a.Group.GroupTypeId));

                groupPaths = groupTypeService.GetAllAssociatedDescendentsPath(groupTypeId);
            }
            else
            {
                // if no specific GroupType is specified, show all GroupTypes with GroupTypePurpose of Checkin Template and their descendents (since this blocktype is specifically for Checkin)
                int        groupTypePurposeCheckInTemplateId = DefinedValueCache.Read(new Guid(Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE)).Id;
                List <int> descendantGroupTypeIds            = new List <int>();
                foreach (var templateGroupType in groupTypeService.Queryable().Where(a => a.GroupTypePurposeValueId == groupTypePurposeCheckInTemplateId))
                {
                    foreach (var childGroupType in groupTypeService.GetChildGroupTypes(templateGroupType.Id))
                    {
                        descendantGroupTypeIds.Add(childGroupType.Id);
                        descendantGroupTypeIds.AddRange(groupTypeService.GetAllAssociatedDescendents(childGroupType.Id).Select(a => a.Id).ToList());
                    }
                }

                groupLocationQry = groupLocationQry.Where(a => descendantGroupTypeIds.Contains(a.Group.GroupTypeId));
            }

            if (gGroupLocationSchedule.SortProperty != null)
            {
                groupLocationQry = groupLocationQry.Sort(gGroupLocationSchedule.SortProperty);
            }
            else
            {
                groupLocationQry = groupLocationQry.OrderBy(a => a.Group.Name).ThenBy(a => a.Location.Name);
            }

            var qryList = groupLocationQry.Select(a =>
                                                  new
            {
                GroupLocationId = a.Id,
                GroupName       = a.Group.Name,
                LocationName    = a.Location.Name,
                ScheduleIdList  = a.Schedules.Select(s => s.Id),
                a.LocationId,
                GroupTypeId = a.Group.GroupTypeId
            }).ToList();

            int parentLocationId = pkrParentLocation.SelectedValueAsInt() ?? Rock.Constants.All.Id;

            if (parentLocationId != Rock.Constants.All.Id)
            {
                var descendantLocationIds = new LocationService(rockContext).GetAllDescendents(parentLocationId).Select(a => a.Id);
                qryList = qryList.Where(a => descendantLocationIds.Contains(a.LocationId)).ToList();
            }

            // put stuff in a datatable so we can dynamically have columns for each Schedule
            DataTable dataTable = new DataTable();

            dataTable.Columns.Add("GroupLocationId");
            dataTable.Columns.Add("GroupName");
            dataTable.Columns.Add("LocationName");
            dataTable.Columns.Add("Path");
            foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>())
            {
                dataTable.Columns.Add(field.DataField, typeof(bool));
            }

            foreach (var row in qryList)
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow["GroupLocationId"] = row.GroupLocationId;
                dataRow["GroupName"]       = row.GroupName;
                dataRow["LocationName"]    = row.LocationName;
                dataRow["Path"]            = groupPaths.Where(gt => gt.GroupTypeId == row.GroupTypeId).Select(gt => gt.Path).FirstOrDefault();
                foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>())
                {
                    int scheduleId = int.Parse(field.DataField.Replace("scheduleField_", string.Empty));
                    dataRow[field.DataField] = row.ScheduleIdList.Any(a => a == scheduleId);
                }

                dataTable.Rows.Add(dataRow);
            }

            gGroupLocationSchedule.DataSource = dataTable;
            gGroupLocationSchedule.DataBind();
        }