/// <summary> /// Handles the Click event of the delete/archive button in the grid /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RowEventArgs" /> instance containing the event data.</param> protected void gGroups_DeleteOrArchive(object sender, RowEventArgs e) { var rockContext = new RockContext(); GroupService groupService = new GroupService(rockContext); GroupMemberService groupMemberService = new GroupMemberService(rockContext); AuthService authService = new AuthService(rockContext); Group group = null; GroupMember groupMember = null; if (GroupListGridMode == GridListGridMode.GroupsPersonMemberOf) { // the DataKey Id of the grid is GroupMemberId groupMember = groupMemberService.Get(e.RowKeyId); if (groupMember != null) { group = groupMember.Group; } } else { // the DataKey Id of the grid is GroupId group = groupService.Get(e.RowKeyId); } if (group != null) { bool isSecurityRoleGroup = group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()); if (GroupListGridMode == GridListGridMode.GroupsPersonMemberOf) { // Grid is in 'Groups that Person is member of' mode GroupMemberHistoricalService groupMemberHistoricalService = new GroupMemberHistoricalService(rockContext); bool archive = false; if (group.GroupType.EnableGroupHistory == true && groupMemberHistoricalService.Queryable().Any(a => a.GroupMemberId == groupMember.Id)) { // if the group has GroupHistory enabled, and this group member has group member history snapshots, they were prompted to Archive archive = true; } else { if (!(group.IsAuthorized(Authorization.EDIT, this.CurrentPerson) || group.IsAuthorized(Authorization.MANAGE_MEMBERS, this.CurrentPerson))) { mdGridWarning.Show("You are not authorized to delete members from this group", ModalAlertType.Information); return; } string errorMessage; if (!groupMemberService.CanDelete(groupMember, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } } int groupId = groupMember.GroupId; if (archive) { // NOTE: Delete will AutoArchive, but since we know that we need to archive, we can call .Archive directly groupMemberService.Archive(groupMember, this.CurrentPersonAliasId, true); } else { groupMemberService.Delete(groupMember, true); } rockContext.SaveChanges(); } else { // Grid is in 'Group List' mode bool archive = false; var groupMemberHistoricalService = new GroupHistoricalService(rockContext); if (group.GroupType.EnableGroupHistory == true && groupMemberHistoricalService.Queryable().Any(a => a.GroupId == group.Id)) { // if the group has GroupHistory enabled and has history snapshots, and they were prompted to Archive archive = true; } if (archive) { if (!group.IsAuthorized(Authorization.EDIT, this.CurrentPerson)) { mdGridWarning.Show("You are not authorized to archive this group", ModalAlertType.Information); return; } // NOTE: groupService.Delete will automatically Archive instead Delete if this Group has GroupHistory enabled, but since this block has UI logic for Archive vs Delete, we can do a direct Archive groupService.Archive(group, this.CurrentPersonAliasId, true); } else { if (!group.IsAuthorized(Authorization.EDIT, this.CurrentPerson)) { mdGridWarning.Show("You are not authorized to delete this group", ModalAlertType.Information); return; } string errorMessage; if (!groupService.CanDelete(group, out errorMessage)) { mdGridWarning.Show(errorMessage, ModalAlertType.Information); return; } groupService.Delete(group, true); } } rockContext.SaveChanges(); if (isSecurityRoleGroup) { Rock.Security.Authorization.Clear(); } } BindGrid(); }
/// <summary> /// Updates Group Historical for any groups that have data group history enabled /// </summary> /// <param name="context">The context.</param> public void UpdateGroupHistorical(IJobExecutionContext context) { var rockContext = new RockContext(); var groupHistoricalService = new GroupHistoricalService(rockContext); var groupService = new GroupService(rockContext); // Note that this query utilizes .AsNoFilter() to avoid having archived groups filtered out by the GroupConfiguration class. var groupsWithHistoryEnabledQuery = groupService.AsNoFilter() .Where(a => a.GroupType.EnableGroupHistory == true) .AsNoTracking(); var groupHistoricalsCurrentQuery = groupHistoricalService.Queryable().Where(a => a.CurrentRowIndicator == true).AsNoTracking(); // Mark GroupHistorical Rows as History ( CurrentRowIndicator = false, etc ) if any of the tracked field values change var groupHistoricalNoLongerCurrent = groupHistoricalsCurrentQuery.Join( groupsWithHistoryEnabledQuery, gh => gh.GroupId, g => g.Id, (gh, g) => new { Group = g, GroupHistorical = gh }) .Where(a => a.Group.Name != a.GroupHistorical.GroupName || a.Group.GroupType.Name != a.GroupHistorical.GroupTypeName || a.Group.CampusId != a.GroupHistorical.CampusId || a.Group.ParentGroupId != a.GroupHistorical.ParentGroupId || a.Group.ScheduleId != a.GroupHistorical.ScheduleId || (a.Group.ScheduleId.HasValue && (a.Group.Schedule.ModifiedDateTime != a.GroupHistorical.ScheduleModifiedDateTime)) || a.Group.Description != a.GroupHistorical.Description || a.Group.StatusValueId != a.GroupHistorical.StatusValueId || a.Group.IsArchived != a.GroupHistorical.IsArchived || a.Group.ArchivedDateTime != a.GroupHistorical.ArchivedDateTime || a.Group.ArchivedByPersonAliasId != a.GroupHistorical.ArchivedByPersonAliasId || a.Group.IsActive != a.GroupHistorical.IsActive || a.Group.InactiveDateTime != a.GroupHistorical.InactiveDateTime ).Select(a => a.GroupHistorical).AsNoTracking(); var effectiveExpireDateTime = RockDateTime.Now; int groupsLoggedToHistory = 0; int groupsSaveToHistoryCurrent = 0; if (groupHistoricalNoLongerCurrent.Any()) { groupsLoggedToHistory = rockContext.BulkUpdate(groupHistoricalNoLongerCurrent, gh => new GroupHistorical { CurrentRowIndicator = false, ExpireDateTime = effectiveExpireDateTime }); } // Insert Groups (that have GroupType.EnableGroupHistory) that don't have a CurrentRowIndicator row yet ( or don't have a CurrentRowIndicator because it was stamped with CurrentRowIndicator=false ) var groupsToAddToHistoricalCurrentsQuery = groupsWithHistoryEnabledQuery.Where(g => !groupHistoricalsCurrentQuery.Any(gh => gh.GroupId == g.Id)).AsNoTracking(); if (groupsToAddToHistoricalCurrentsQuery.Any()) { List <GroupHistorical> groupHistoricalCurrentsToInsert = groupsToAddToHistoricalCurrentsQuery .Include(a => a.GroupType) .Include(a => a.Schedule) .ToList() .Select(g => GroupHistorical.CreateCurrentRowFromGroup(g, effectiveExpireDateTime)).ToList(); groupsSaveToHistoryCurrent = groupHistoricalCurrentsToInsert.Count(); rockContext.BulkInsert(groupHistoricalCurrentsToInsert); } if (groupsLoggedToHistory > 0) { _jobStatusMessages.Add($"Logged {groupsLoggedToHistory} {"group history snapshot".PluralizeIf( groupsLoggedToHistory != 0 )}"); } if (groupsSaveToHistoryCurrent > 0) { int newGroupsAddedToHistory = groupsSaveToHistoryCurrent - groupsLoggedToHistory; if (newGroupsAddedToHistory > 0) { _jobStatusMessages.Add($"Added {newGroupsAddedToHistory} new {"group history snapshot".PluralizeIf( newGroupsAddedToHistory != 0 )}"); } } }