Files
Umbraco-CMS/src/Umbraco.Core/Components/RelateOnCopyComponent.cs

45 lines
1.6 KiB
C#
Raw Normal View History

using Umbraco.Core.Composing;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
2017-12-28 09:18:09 +01:00
using Umbraco.Core.Services.Implement;
namespace Umbraco.Core.Components
{
//TODO: This should just exist in the content service/repo!
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
2016-09-08 18:43:58 +02:00
public sealed class RelateOnCopyComponent : UmbracoComponentBase, IUmbracoCoreComponent
{
2016-09-08 18:43:58 +02:00
public void Initialize()
{
ContentService.Copied += ContentServiceCopied;
}
private static void ContentServiceCopied(IContentService sender, Events.CopyEventArgs<IContent> e)
{
if (e.RelateToOriginal == false) return;
var relationService = Current.Services.RelationService;
var relationType = relationService.GetRelationTypeByAlias(Constants.Conventions.RelationTypes.RelateDocumentOnCopyAlias);
if (relationType == null)
{
2017-09-19 18:19:05 +02:00
relationType = new RelationType(Constants.ObjectTypes.Document,
Constants.ObjectTypes.Document,
Constants.Conventions.RelationTypes.RelateDocumentOnCopyAlias,
Constants.Conventions.RelationTypes.RelateDocumentOnCopyName) { IsBidirectional = true };
relationService.Save(relationType);
}
var relation = new Relation(e.Original.Id, e.Copy.Id, relationType);
relationService.Save(relation);
Current.Services.AuditService.Add(
AuditType.Copy,
$"Copied content with Id: '{e.Copy.Id}' related to original content with Id: '{e.Original.Id}'",
e.Copy.WriterId, e.Copy.Id);
}
}
}