Skip to content

Commit b96ec5a

Browse files
committed
UI+Persistence for history cleanup per doc type
1 parent ae20c49 commit b96ec5a

File tree

17 files changed

+367
-313
lines changed

17 files changed

+367
-313
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace Umbraco.Core.Models.ContentEditing;
4+
5+
[DataContract(Name = "historyCleanup", Namespace = "")]
6+
public class HistoryCleanup
7+
{
8+
[DataMember(Name = "preventCleanup")]
9+
public bool PreventCleanup { get; set; }
10+
11+
[DataMember(Name = "keepAllVersionsNewerThanDays")]
12+
public int? KeepAllVersionsNewerThanDays { get;set; }
13+
14+
[DataMember(Name = "keepLatestVersionPerDayForDays")]
15+
public int? KeepLatestVersionPerDayForDays { get;set; }
16+
}

src/Umbraco.Core/Models/ContentType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using System.Runtime.Serialization;
6+
using Umbraco.Core.Models.ContentEditing;
67

78
namespace Umbraco.Core.Models
89
{
@@ -47,6 +48,8 @@ public ContentType(IContentType parent, string alias)
4748
/// <inheritdoc />
4849
public override bool SupportsPublishing => SupportsPublishingConst;
4950

51+
52+
5053
//Custom comparer for enumerable
5154
private static readonly DelegateEqualityComparer<IEnumerable<ITemplate>> TemplateComparer = new DelegateEqualityComparer<IEnumerable<ITemplate>>(
5255
(templates, enumerable) => templates.UnsortedSequenceEqual(enumerable),
@@ -93,6 +96,8 @@ public IEnumerable<ITemplate> AllowedTemplates
9396
}
9497
}
9598

99+
public HistoryCleanup HistoryCleanup { get; set; }
100+
96101
/// <summary>
97102
/// Determines if AllowedTemplates contains templateId
98103
/// </summary>

src/Umbraco.Core/Models/IContentType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using Umbraco.Core.Models.ContentEditing;
23

34
namespace Umbraco.Core.Models
45
{
@@ -17,6 +18,10 @@ public interface IContentType : IContentTypeComposition
1718
/// </summary>
1819
IEnumerable<ITemplate> AllowedTemplates { get; set; }
1920

21+
/// <summary>
22+
/// Gets or Sets the history cleanup configuration
23+
/// </summary>
24+
HistoryCleanup HistoryCleanup { get; set; }
2025
/// <summary>
2126
/// Determines if AllowedTemplates contains templateId
2227
/// </summary>

src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeCommonRepository.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Umbraco.Core.Cache;
66
using Umbraco.Core.Exceptions;
77
using Umbraco.Core.Models;
8+
using Umbraco.Core.Models.ContentEditing;
89
using Umbraco.Core.Persistence.Dtos;
910
using Umbraco.Core.Persistence.Factories;
1011
using Umbraco.Core.Scoping;
@@ -109,6 +110,7 @@ private IEnumerable<IContentTypeComposition> GetAllTypesInternal()
109110
MapTemplates(contentTypes);
110111
MapComposition(contentTypes);
111112
MapGroupsAndProperties(contentTypes);
113+
MapHistoryCleanup(contentTypes);
112114

113115
// finalize
114116
foreach (var contentType in contentTypes.Values)
@@ -119,6 +121,35 @@ private IEnumerable<IContentTypeComposition> GetAllTypesInternal()
119121
return contentTypes.Values;
120122
}
121123

124+
private void MapHistoryCleanup(Dictionary<int, IContentTypeComposition> contentTypes)
125+
{
126+
// get templates
127+
var sql1 = Sql()
128+
.Select<ContentVersionCleanupPolicyDto>()
129+
.From<ContentVersionCleanupPolicyDto>()
130+
.OrderBy<ContentVersionCleanupPolicyDto>(x => x.ContentTypeId);
131+
132+
var contentVersionCleanupPolicyDtos = Database.Fetch<ContentVersionCleanupPolicyDto>(sql1);
133+
134+
var contentVersionCleanupPolicyDictionary =
135+
contentVersionCleanupPolicyDtos.ToDictionary(x => x.ContentTypeId);
136+
foreach (var c in contentTypes.Values)
137+
{
138+
if (!(c is ContentType contentType)) continue;
139+
140+
var historyCleanup = new HistoryCleanup();
141+
142+
if (contentVersionCleanupPolicyDictionary.TryGetValue(contentType.Id, out var versionCleanup))
143+
{
144+
historyCleanup.PreventCleanup = versionCleanup.PreventCleanup;
145+
historyCleanup.KeepAllVersionsNewerThanDays = versionCleanup.KeepAllVersionsNewerThanDays;
146+
historyCleanup.KeepLatestVersionPerDayForDays = versionCleanup.KeepLatestVersionPerDayForDays;
147+
}
148+
149+
contentType.HistoryCleanup = historyCleanup;
150+
}
151+
}
152+
122153
private void MapTemplates(Dictionary<int, IContentTypeComposition> contentTypes)
123154
{
124155
// get templates
@@ -145,7 +176,7 @@ private void MapTemplates(Dictionary<int, IContentTypeComposition> contentTypes)
145176
templateDtoIx++;
146177
if (!templates.TryGetValue(allowedDto.TemplateNodeId, out var template)) continue;
147178
allowedTemplates.Add(template);
148-
179+
149180
if (allowedDto.IsDefault)
150181
defaultTemplateId = template.Id;
151182
}

src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepository.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,22 @@ protected override void PersistUpdatedItem(IContentType entity)
289289

290290
PersistUpdatedBaseContentType(entity);
291291
PersistTemplates(entity, true);
292+
PersistHistoryCleanup(entity);
292293

293294
entity.ResetDirtyProperties();
294295
}
296+
297+
private void PersistHistoryCleanup(IContentType entity)
298+
{
299+
ContentVersionCleanupPolicyDto dto = new ContentVersionCleanupPolicyDto()
300+
{
301+
ContentTypeId = entity.Id,
302+
Updated = DateTime.Now,
303+
PreventCleanup = entity.HistoryCleanup.PreventCleanup,
304+
KeepAllVersionsNewerThanDays = entity.HistoryCleanup.KeepAllVersionsNewerThanDays,
305+
KeepLatestVersionPerDayForDays = entity.HistoryCleanup.KeepLatestVersionPerDayForDays,
306+
};
307+
Database.InsertOrUpdate(dto);
308+
}
295309
}
296310
}

src/Umbraco.Core/Umbraco.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
<Compile Include="Models\ContentDataIntegrityReport.cs" />
164164
<Compile Include="Models\ContentDataIntegrityReportEntry.cs" />
165165
<Compile Include="Models\ContentDataIntegrityReportOptions.cs" />
166+
<Compile Include="Models\ContentEditing\HistoryCleanup.cs" />
166167
<Compile Include="Models\ContentVersionCleanupPolicySettings.cs" />
167168
<Compile Include="Models\Identity\ExternalLogin.cs" />
168169
<Compile Include="Models\Identity\IExternalLogin.cs" />

0 commit comments

Comments
 (0)