public BlockActionResult DeleteScheduledUnavailability(Guid attendanceGuid) { var rockContext = new RockContext(); // The schedule exclusion service. var scheduleExclusionService = new PersonScheduleExclusionService(rockContext); // Get the specific scheduled exclusion from the Guid. var scheduleExclusion = scheduleExclusionService.GetNoTracking(attendanceGuid); if (scheduleExclusion == null) { return(ActionNotFound()); } // Get the person schedule exclusion. var personScheduleExclusion = scheduleExclusionService.Get(scheduleExclusion.Id); if (personScheduleExclusion == null) { return(ActionNotFound()); } // Find all of the children. var scheduleExclusionChildren = scheduleExclusionService.Queryable().Where(x => x.ParentPersonScheduleExclusionId == personScheduleExclusion.Id); foreach (var scheduleExclusionChild in scheduleExclusionChildren) { scheduleExclusionChild.ParentPersonScheduleExclusionId = null; } // Delete the exclusion. scheduleExclusionService.Delete(personScheduleExclusion); rockContext.SaveChanges(); return(ActionOk()); }
/// <summary> /// Gets the content view model for ScheduleUnavailability mobile block. /// </summary> /// <returns>A <see cref="ContentBag"/></returns> private ContentBag GetScheduleContent() { var rockContext = new RockContext(); // The dictionary of merge fields. var mergeFields = RequestContext.GetCommonMergeFields(); // Get the current user's family members. var familyMemberAliasIds = new PersonService(rockContext) .GetFamilyMembers(this.CurrentPersonId, true) .SelectMany(m => m.Person.Aliases) .Select(a => a.Id).ToList(); var currentDateTime = RockDateTime.Now.Date; var personScheduleExclusionService = new PersonScheduleExclusionService(rockContext); // Get the schedule exclusions. var personScheduleExclusions = personScheduleExclusionService .Queryable("PersonAlias.Person") .AsNoTracking() .Where(e => familyMemberAliasIds.Contains(e.PersonAliasId.Value)) .Where(e => e.StartDate >= currentDateTime || e.EndDate >= currentDateTime) .OrderBy(e => e.StartDate) .ThenBy(e => e.EndDate) .Select(e => new GroupScheduleRowInfo { Title = e.Title, Guid = e.Guid, Id = e.Id, OccurrenceStartDate = DbFunctions.TruncateTime(e.StartDate).Value, OccurrenceEndDate = DbFunctions.TruncateTime(e.EndDate).Value, Group = e.Group, PersonAlias = e.PersonAlias, GroupScheduleType = GroupScheduleType.Unavailable }); var groupService = new GroupService(rockContext); // get groups that the selected person is an active member of and have SchedulingEnabled and have at least one location with a schedule. var groups = groupService .Queryable() .AsNoTracking() .Where(x => x.Members.Any(m => m.PersonId == this.CurrentPersonId && m.IsArchived == false && m.GroupMemberStatus == GroupMemberStatus.Active)) .Where(x => x.IsActive == true && x.IsArchived == false && x.GroupType.IsSchedulingEnabled == true && x.DisableScheduling == false && x.DisableScheduleToolboxAccess == false) .Where(x => x.GroupLocations.Any(gl => gl.Schedules.Any())) .OrderBy(x => new { x.Order, x.Name }) .AsNoTracking() .ToList(); // A list of the group names and guids. var groupsList = new List <ListItemViewModel>(); // Every result from our query. foreach (var group in groups) { // Add it to the groups list with the necessary information. var groupInformation = new ListItemViewModel { Text = group.Name, Value = group.Guid.ToStringSafe() }; groupsList.Add(groupInformation); } // Getting the family members. var personService = new PersonService(rockContext); var familyMembersQuery = personService.GetFamilyMembers(this.CurrentPersonId, false) .AsNoTracking() .ToList(); // A list of family member names and guids. var familyMembersList = new List <ListItemViewModel>(); foreach (var familyMember in familyMembersQuery) { // Add it to the family members list with the necessary information. var familyMemberInformation = new ListItemViewModel { Text = familyMember.Person.FullName, Value = familyMember.Guid.ToStringSafe() }; familyMembersList.Add(familyMemberInformation); } // Add in our list of schedule exclusions. mergeFields.AddOrReplace("ScheduleExclusionsList", personScheduleExclusions); // Pass those in as content. var content = TypeTemplate.ResolveMergeFields(mergeFields); // Return all of the necessary information. return(new ContentBag { Content = content, GroupInformation = groupsList, FamilyMemberInformation = familyMembersList, IsDescriptionRequired = DescriptionRequired }); }