Added custom event handler to create the relation for trashed nodes.

Removed the additional (AJAX) resource call from the Angular controller, as the relation removal is handled in server-side code during the move.
This commit is contained in:
leekelleher
2015-01-04 20:46:00 +00:00
parent 573192c33f
commit 8a32f02781
4 changed files with 78 additions and 12 deletions

View File

@@ -1131,9 +1131,6 @@ namespace Umbraco.Core.Services
return;
}
// Add a relation for the item being deleted, so that we can know the original parent for if we need to restore later
ApplicationContext.Current.Services.RelationService.Relate(content.Parent(), content, Constants.Conventions.Relations.RelateParentDocumentOnDelete);
var moveInfo = new List<MoveEventInfo<IContent>>
{
new MoveEventInfo<IContent>(content, originalPath, Constants.System.RecycleBinContent)

View File

@@ -7,13 +7,18 @@ angular.module("umbraco").controller("Umbraco.Editors.Content.RestoreController"
relationResource.getByChildId(node.id, "relateParentDocumentOnDelete").then(function (data) {
$scope.relation = data[0];
contentResource.getById($scope.relation.ParentId).then(function (data) {
$scope.target = data;
if ($scope.relation.ParentId == -1) {
$scope.target = { id: -1, name: "Root" };
}, function (err) {
$scope.success = false;
$scope.error = err;
});
} else {
contentResource.getById($scope.relation.ParentId).then(function (data) {
$scope.target = data;
}, function (err) {
$scope.success = false;
$scope.error = err;
});
}
}, function (err) {
$scope.success = false;
@@ -21,6 +26,7 @@ angular.module("umbraco").controller("Umbraco.Editors.Content.RestoreController"
});
$scope.restore = function () {
// this code was copied from `content.move.controller.js`
contentResource.move({ parentId: $scope.target.id, id: node.id })
.then(function (path) {
$scope.error = false;
@@ -43,9 +49,6 @@ angular.module("umbraco").controller("Umbraco.Editors.Content.RestoreController"
}
});
// remove the relation, as the item has been restored
relationResource.deleteById($scope.relation.Id);
}, function (err) {
$scope.success = false;
$scope.error = err;

View File

@@ -0,0 +1,65 @@
using System;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Auditing;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace Umbraco.Web.Strategies
{
public sealed class RelateOnTrashHandler : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Moved += ContentService_Moved;
ContentService.Trashed += ContentService_Trashed;
}
private void ContentService_Moved(IContentService sender, MoveEventArgs<IContent> e)
{
foreach (var item in e.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Constants.System.RecycleBinContent.ToInvariantString())))
{
var relationService = ApplicationContext.Current.Services.RelationService;
var relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
var relations = relationService.GetByChildId(item.Entity.Id);
foreach (var relation in relations.Where(x => x.RelationType.Alias.InvariantEquals(relationTypeAlias)))
{
relationService.Delete(relation);
}
}
}
private void ContentService_Trashed(IContentService sender, MoveEventArgs<IContent> e)
{
var relationService = ApplicationContext.Current.Services.RelationService;
var relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
var relationType = relationService.GetRelationTypeByAlias(relationTypeAlias);
// check that the relation-type exists, if not, then recreate it
if (relationType == null)
{
var documentObjectType = new Guid(Constants.ObjectTypes.Document);
var relationTypeName = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName;
relationType = new RelationType(documentObjectType, documentObjectType, relationTypeAlias, relationTypeName);
relationService.Save(relationType);
}
foreach (var item in e.MoveInfoCollection)
{
var originalPath = item.OriginalPath.ToDelimitedList();
var originalParentId = originalPath.Count > 2
? int.Parse(originalPath[originalPath.Count - 2])
: Constants.System.Root;
// 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 = new Relation(originalParentId, item.Entity.Id, relationType);
relationService.Save(relation);
Audit.Add(AuditTypes.Delete, string.Format("Trashed content with Id: '{0}' related to original parent content with Id: '{1}'", item.Entity.Id, originalParentId), item.Entity.WriterId, item.Entity.Id);
}
}
}
}

View File

@@ -494,6 +494,7 @@
<Compile Include="Mvc\UmbracoVirtualNodeRouteHandler.cs" />
<Compile Include="Routing\CustomRouteUrlProvider.cs" />
<Compile Include="Routing\UrlProviderExtensions.cs" />
<Compile Include="Strategies\RelateOnTrashHandler.cs" />
<Compile Include="Strategies\Migrations\ClearCsrfCookiesAfterUpgrade.cs" />
<Compile Include="Strategies\Migrations\ClearMediaXmlCacheForDeletedItemsAfterUpgrade.cs" />
<Compile Include="Strategies\Migrations\EnsureListViewDataTypeIsCreated.cs" />