2018-08-29 01:15:46 +10:00
|
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
2020-10-12 15:22:11 +02:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2017-12-28 09:18:09 +01:00
|
|
|
|
using Umbraco.Core.Services.Implement;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Extensions;
|
2015-01-04 20:46:00 +00:00
|
|
|
|
|
2019-02-14 09:15:47 +01:00
|
|
|
|
namespace Umbraco.Core.Compose
|
2015-01-04 20:46:00 +00:00
|
|
|
|
{
|
2019-01-03 21:00:28 +01:00
|
|
|
|
public sealed class RelateOnTrashComponent : IComponent
|
2015-01-04 20:46:00 +00:00
|
|
|
|
{
|
2019-12-05 14:03:09 +01:00
|
|
|
|
private readonly IRelationService _relationService;
|
|
|
|
|
|
private readonly IEntityService _entityService;
|
|
|
|
|
|
private readonly ILocalizedTextService _textService;
|
2019-12-18 11:16:43 +01:00
|
|
|
|
private readonly IAuditService _auditService;
|
2020-10-12 15:22:11 +02:00
|
|
|
|
private readonly IScopeProvider _scopeProvider;
|
|
|
|
|
|
|
|
|
|
|
|
public RelateOnTrashComponent(
|
|
|
|
|
|
IRelationService relationService,
|
|
|
|
|
|
IEntityService entityService,
|
|
|
|
|
|
ILocalizedTextService textService,
|
|
|
|
|
|
IAuditService auditService,
|
|
|
|
|
|
IScopeProvider scopeProvider)
|
2019-12-05 14:03:09 +01:00
|
|
|
|
{
|
|
|
|
|
|
_relationService = relationService;
|
|
|
|
|
|
_entityService = entityService;
|
|
|
|
|
|
_textService = textService;
|
2019-12-18 11:16:43 +01:00
|
|
|
|
_auditService = auditService;
|
2020-10-12 15:22:11 +02:00
|
|
|
|
_scopeProvider = scopeProvider;
|
2019-12-05 14:03:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-07 09:30:47 +01:00
|
|
|
|
public void Initialize()
|
2015-01-04 20:46:00 +00:00
|
|
|
|
{
|
2020-09-04 00:28:38 +10:00
|
|
|
|
ContentService.Moved += ContentService_Moved;
|
|
|
|
|
|
ContentService.Trashed += ContentService_Trashed;
|
|
|
|
|
|
MediaService.Moved += MediaService_Moved;
|
|
|
|
|
|
MediaService.Trashed += MediaService_Trashed;
|
2015-01-04 20:46:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-07 09:30:47 +01:00
|
|
|
|
public void Terminate()
|
2020-09-04 00:28:38 +10:00
|
|
|
|
{
|
|
|
|
|
|
ContentService.Moved -= ContentService_Moved;
|
|
|
|
|
|
ContentService.Trashed -= ContentService_Trashed;
|
|
|
|
|
|
MediaService.Moved -= MediaService_Moved;
|
|
|
|
|
|
MediaService.Trashed -= MediaService_Trashed;
|
|
|
|
|
|
}
|
2019-01-07 09:30:47 +01:00
|
|
|
|
|
2020-09-04 00:28:38 +10:00
|
|
|
|
private void ContentService_Moved(IContentService sender, MoveEventArgs<IContent> e)
|
2015-01-04 20:46:00 +00:00
|
|
|
|
{
|
2021-02-18 11:06:02 +01:00
|
|
|
|
foreach (var item in e.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Cms.Core.Constants.System.RecycleBinContentString)))
|
2015-01-04 20:46:00 +00:00
|
|
|
|
{
|
2019-12-05 14:03:09 +01:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
const string relationTypeAlias = Cms.Core.Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
|
2020-09-04 00:28:38 +10:00
|
|
|
|
var relations = _relationService.GetByChildId(item.Entity.Id);
|
2015-01-04 20:46:00 +00:00
|
|
|
|
|
|
|
|
|
|
foreach (var relation in relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias)))
|
|
|
|
|
|
{
|
2020-09-04 00:28:38 +10:00
|
|
|
|
_relationService.Delete(relation);
|
2015-01-04 20:46:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-04 00:28:38 +10:00
|
|
|
|
private void MediaService_Moved(IMediaService sender, MoveEventArgs<IMedia> e)
|
2018-07-27 14:56:18 +01:00
|
|
|
|
{
|
2021-02-18 11:06:02 +01:00
|
|
|
|
foreach (var item in e.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Cms.Core.Constants.System.RecycleBinMediaString)))
|
2018-07-27 14:56:18 +01:00
|
|
|
|
{
|
2021-02-18 11:06:02 +01:00
|
|
|
|
const string relationTypeAlias = Cms.Core.Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
|
2020-09-04 00:28:38 +10:00
|
|
|
|
var relations = _relationService.GetByChildId(item.Entity.Id);
|
2018-07-27 14:56:18 +01:00
|
|
|
|
foreach (var relation in relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias)))
|
|
|
|
|
|
{
|
2020-09-04 00:28:38 +10:00
|
|
|
|
_relationService.Delete(relation);
|
2018-07-27 14:56:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-04 00:28:38 +10:00
|
|
|
|
private void ContentService_Trashed(IContentService sender, MoveEventArgs<IContent> e)
|
2015-01-04 20:46:00 +00:00
|
|
|
|
{
|
2020-10-12 15:22:11 +02:00
|
|
|
|
using (var scope = _scopeProvider.CreateScope())
|
2015-01-04 20:46:00 +00:00
|
|
|
|
{
|
2021-02-18 11:06:02 +01:00
|
|
|
|
const string relationTypeAlias = Cms.Core.Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
|
2020-10-12 15:22:11 +02:00
|
|
|
|
var relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias);
|
2015-01-04 20:46:00 +00:00
|
|
|
|
|
2020-10-12 15:22:11 +02:00
|
|
|
|
// check that the relation-type exists, if not, then recreate it
|
|
|
|
|
|
if (relationType == null)
|
|
|
|
|
|
{
|
2021-02-18 11:06:02 +01:00
|
|
|
|
var documentObjectType = Cms.Core.Constants.ObjectTypes.Document;
|
2020-10-12 15:22:11 +02:00
|
|
|
|
const string relationTypeName =
|
2021-02-18 11:06:02 +01:00
|
|
|
|
Cms.Core.Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName;
|
2015-01-04 20:46:00 +00:00
|
|
|
|
|
2020-10-12 15:22:11 +02:00
|
|
|
|
relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType,
|
|
|
|
|
|
documentObjectType);
|
|
|
|
|
|
_relationService.Save(relationType);
|
|
|
|
|
|
}
|
2015-01-04 20:46:00 +00:00
|
|
|
|
|
2020-10-12 15:22:11 +02:00
|
|
|
|
foreach (var item in e.MoveInfoCollection)
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
2020-10-12 15:22:11 +02:00
|
|
|
|
var originalPath = item.OriginalPath.ToDelimitedList();
|
|
|
|
|
|
var originalParentId = originalPath.Count > 2
|
|
|
|
|
|
? int.Parse(originalPath[originalPath.Count - 2])
|
2021-02-18 11:06:02 +01:00
|
|
|
|
: Cms.Core.Constants.System.Root;
|
2020-10-12 15:22:11 +02:00
|
|
|
|
|
|
|
|
|
|
//before we can create this relation, we need to ensure that the original parent still exists which
|
|
|
|
|
|
//may not be the case if the encompassing transaction also deleted it when this item was moved to the bin
|
|
|
|
|
|
|
|
|
|
|
|
if (_entityService.Exists(originalParentId))
|
|
|
|
|
|
{
|
|
|
|
|
|
// Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later
|
2020-10-13 15:47:02 +11:00
|
|
|
|
var relation = _relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ??
|
2020-10-12 15:22:11 +02:00
|
|
|
|
new Relation(originalParentId, item.Entity.Id, relationType);
|
|
|
|
|
|
_relationService.Save(relation);
|
|
|
|
|
|
|
|
|
|
|
|
_auditService.Add(AuditType.Delete,
|
|
|
|
|
|
item.Entity.WriterId,
|
|
|
|
|
|
item.Entity.Id,
|
|
|
|
|
|
ObjectTypes.GetName(UmbracoObjectTypes.Document),
|
|
|
|
|
|
string.Format(_textService.Localize(
|
|
|
|
|
|
"recycleBin/contentTrashed"),
|
|
|
|
|
|
item.Entity.Id, originalParentId));
|
|
|
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
|
}
|
2020-10-12 15:22:11 +02:00
|
|
|
|
|
|
|
|
|
|
scope.Complete();
|
2015-01-04 20:46:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-07-27 14:56:18 +01:00
|
|
|
|
|
2020-10-12 15:22:11 +02:00
|
|
|
|
public void MediaService_Trashed(IMediaService sender, MoveEventArgs<IMedia> e)
|
2018-07-27 14:56:18 +01:00
|
|
|
|
{
|
2020-10-12 15:26:18 +02:00
|
|
|
|
using (var scope = _scopeProvider.CreateScope())
|
2018-07-27 14:56:18 +01:00
|
|
|
|
{
|
2020-10-12 15:26:18 +02:00
|
|
|
|
const string relationTypeAlias =
|
2021-02-18 11:06:02 +01:00
|
|
|
|
Cms.Core.Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
|
2020-10-12 15:26:18 +02:00
|
|
|
|
var relationType = _relationService.GetRelationTypeByAlias(relationTypeAlias);
|
|
|
|
|
|
// check that the relation-type exists, if not, then recreate it
|
|
|
|
|
|
if (relationType == null)
|
|
|
|
|
|
{
|
2021-02-18 11:06:02 +01:00
|
|
|
|
var documentObjectType = Cms.Core.Constants.ObjectTypes.Document;
|
2020-10-12 15:26:18 +02:00
|
|
|
|
const string relationTypeName =
|
2021-02-18 11:06:02 +01:00
|
|
|
|
Cms.Core.Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteName;
|
2020-10-12 15:26:18 +02:00
|
|
|
|
relationType = new RelationType(relationTypeName, relationTypeAlias, false, documentObjectType,
|
|
|
|
|
|
documentObjectType);
|
|
|
|
|
|
_relationService.Save(relationType);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in e.MoveInfoCollection)
|
2018-07-27 14:56:18 +01:00
|
|
|
|
{
|
2020-10-12 15:26:18 +02:00
|
|
|
|
var originalPath = item.OriginalPath.ToDelimitedList();
|
|
|
|
|
|
var originalParentId = originalPath.Count > 2
|
|
|
|
|
|
? int.Parse(originalPath[originalPath.Count - 2])
|
2021-02-18 11:06:02 +01:00
|
|
|
|
: Cms.Core.Constants.System.Root;
|
2020-10-12 15:26:18 +02:00
|
|
|
|
//before we can create this relation, we need to ensure that the original parent still exists which
|
|
|
|
|
|
//may not be the case if the encompassing transaction also deleted it when this item was moved to the bin
|
|
|
|
|
|
if (_entityService.Exists(originalParentId))
|
|
|
|
|
|
{
|
|
|
|
|
|
// Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later
|
|
|
|
|
|
var relation =
|
2020-10-13 15:05:03 +02:00
|
|
|
|
_relationService.GetByParentAndChildId(originalParentId, item.Entity.Id, relationType) ??
|
2020-10-12 15:26:18 +02:00
|
|
|
|
new Relation(originalParentId, item.Entity.Id, relationType);
|
|
|
|
|
|
_relationService.Save(relation);
|
|
|
|
|
|
_auditService.Add(AuditType.Delete,
|
|
|
|
|
|
item.Entity.CreatorId,
|
|
|
|
|
|
item.Entity.Id,
|
|
|
|
|
|
ObjectTypes.GetName(UmbracoObjectTypes.Media),
|
|
|
|
|
|
string.Format(_textService.Localize(
|
|
|
|
|
|
"recycleBin/mediaTrashed"),
|
|
|
|
|
|
item.Entity.Id, originalParentId));
|
|
|
|
|
|
}
|
2018-07-27 14:56:18 +01:00
|
|
|
|
}
|
2020-10-12 15:26:18 +02:00
|
|
|
|
|
|
|
|
|
|
scope.Complete();
|
2018-07-27 14:56:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-01-04 20:46:00 +00:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|