U4-10681 Restore option in media recycle bin (#2342)

This commit is contained in:
Richard Thompson
2018-07-27 14:56:18 +01:00
committed by Sebastiaan Janssen
parent c0cdf727ca
commit 60910efc53
15 changed files with 278 additions and 27 deletions

View File

@@ -341,6 +341,16 @@ namespace Umbraco.Core
/// ContentType alias for default relation type "Relate Parent Document On Delete".
/// </summary>
public const string RelateParentDocumentOnDeleteAlias = "relateParentDocumentOnDelete";
/// <summary>
/// ContentType name for default relation type "Relate Parent Media Folder On Delete".
/// </summary>
public const string RelateParentMediaFolderOnDeleteName = "Relate Parent Media Folder On Delete";
/// <summary>
/// ContentType alias for default relation type "Relate Parent Media Folder On Delete".
/// </summary>
public const string RelateParentMediaFolderOnDeleteAlias = "relateParentMediaFolderOnDelete";
}
}
}

View File

@@ -9,7 +9,7 @@ namespace Umbraco.Core.Models.Rdbms
[ExplicitColumns]
internal class RelationTypeDto
{
public const int NodeIdSeed = 3;
public const int NodeIdSeed = 4;
[Column("id")]
[PrimaryKeyColumn(IdentitySeed = NodeIdSeed)]

View File

@@ -310,6 +310,9 @@ namespace Umbraco.Core.Persistence.Migrations.Initial
relationType = new RelationTypeDto { Id = 2, Alias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias, ChildObjectType = new Guid(Constants.ObjectTypes.Document), ParentObjectType = new Guid(Constants.ObjectTypes.Document), Dual = false, Name = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteName };
relationType.UniqueId = (relationType.Alias + "____" + relationType.Name).ToGuid();
_database.Insert("umbracoRelationType", "id", false, relationType);
relationType = new RelationTypeDto { Id = 3, Alias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias, ChildObjectType = new Guid(Constants.ObjectTypes.Media), ParentObjectType = new Guid(Constants.ObjectTypes.Media), Dual = false, Name = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteName };
relationType.UniqueId = (relationType.Alias + "____" + relationType.Name).ToGuid();
_database.Insert("umbracoRelationType", "id", false, relationType);
}
private void CreateCmsTaskTypeData()

View File

@@ -0,0 +1,38 @@
using System;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Persistence.DatabaseAnnotations;
using Umbraco.Core.Persistence.SqlSyntax;
namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenTwelveZero
{
[Migration("7.12.0", 0, Constants.System.UmbracoMigrationName)]
public class AddRelationTypeForMediaFolderOnDelete : MigrationBase
{
public AddRelationTypeForMediaFolderOnDelete(ISqlSyntaxProvider sqlSyntax, ILogger logger)
: base(sqlSyntax, logger)
{
}
public override void Up()
{
var exists = Context.Database.FirstOrDefault<RelationTypeDto>("WHERE alias=@alias", new { alias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias });
if (exists == null)
{
var relationTypeDto = new RelationTypeDto
{
Alias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias,
Name = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteName,
ChildObjectType = Guid.Parse(Constants.ObjectTypes.MediaType),
ParentObjectType = Guid.Parse(Constants.ObjectTypes.MediaType),
Dual = false
};
Context.Database.Insert(relationTypeDto);
}
}
public override void Down()
{ }
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Linq;
using Umbraco.Core.Auditing;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
@@ -13,6 +12,9 @@ namespace Umbraco.Core.Strategies
{
ContentService.Moved += ContentService_Moved;
ContentService.Trashed += ContentService_Trashed;
MediaService.Moved += MediaService_Moved;
MediaService.Trashed += MediaService_Trashed;
}
private void ContentService_Moved(IContentService sender, MoveEventArgs<IContent> e)
@@ -30,10 +32,26 @@ namespace Umbraco.Core.Strategies
}
}
private void MediaService_Moved(IMediaService sender, MoveEventArgs<IMedia> e)
{
foreach (var item in e.MoveInfoCollection.Where(x => x.OriginalPath.Contains(Constants.System.RecycleBinMedia.ToInvariantString())))
{
var relationService = ApplicationContext.Current.Services.RelationService;
var relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
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 entityService = ApplicationContext.Current.Services.EntityService;
var textService = ApplicationContext.Current.Services.TextService;
var relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias;
var relationType = relationService.GetRelationTypeByAlias(relationTypeAlias);
@@ -64,7 +82,9 @@ namespace Umbraco.Core.Strategies
relationService.Save(relation);
ApplicationContext.Current.Services.AuditService.Add(AuditType.Delete,
string.Format("Trashed content with Id: '{0}' related to original parent content with Id: '{1}'", item.Entity.Id, originalParentId),
string.Format(textService.Localize(
"recycleBin/contentTrashed"),
item.Entity.Id, originalParentId),
item.Entity.WriterId,
item.Entity.Id);
}
@@ -72,5 +92,49 @@ namespace Umbraco.Core.Strategies
}
}
private void MediaService_Trashed(IMediaService sender, MoveEventArgs<IMedia> e)
{
var relationService = ApplicationContext.Current.Services.RelationService;
var entityService = ApplicationContext.Current.Services.EntityService;
var textService = ApplicationContext.Current.Services.TextService;
var relationTypeAlias = Constants.Conventions.RelationTypes.RelateParentMediaFolderOnDeleteAlias;
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.RelateParentMediaFolderOnDeleteName;
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;
//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 = new Relation(originalParentId, item.Entity.Id, relationType);
relationService.Save(relation);
ApplicationContext.Current.Services.AuditService.Add(AuditType.Delete,
string.Format(textService.Localize(
"recycleBin/mediaTrashed"),
item.Entity.Id, originalParentId),
item.Entity.CreatorId,
item.Entity.Id);
}
}
}
}
}

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -568,12 +568,13 @@
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenEightZero\AddInstructionCountColumn.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenEightZero\AddCmsMediaTable.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenEightZero\AddUserLoginTable.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\RenameTrueFalseField.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\SetDefaultTagsStorageType.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\RenameTrueFalseField.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\SetDefaultTagsStorageType.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenNineZero\AddUmbracoAuditTable.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenNineZero\AddUmbracoConsentTable.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenNineZero\AddIsSensitiveMemberTypeColumn.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenNineZero\CreateSensitiveDataUserGroup.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\AddRelationTypeForMediaFolderOnDelete.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenSevenZero\AddIndexToDictionaryKeyColumn.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenSevenZero\EnsureContentTemplatePermissions.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenSevenZero\ReduceDictionaryKeyColumnsSize.cs" />
@@ -638,7 +639,7 @@
<Compile Include="Media\Exif\TIFFStrip.cs" />
<Compile Include="Media\Exif\Utility.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenThreeOne\UpdateUserLanguagesToIsoCode.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\IncreaseLanguageIsoCodeColumnLength.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\IncreaseLanguageIsoCodeColumnLength.cs" />
<Compile Include="Persistence\Migrations\Upgrades\TargetVersionSevenTwelveZero\UpdateUmbracoConsent.cs" />
<Compile Include="Persistence\PocoDataDataReader.cs" />
<Compile Include="Persistence\Querying\CachedExpression.cs" />
@@ -1688,4 +1689,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

View File

@@ -139,7 +139,7 @@ namespace Umbraco.Tests.Persistence.Repositories
Assert.That(relationTypes, Is.Not.Null);
Assert.That(relationTypes.Any(), Is.True);
Assert.That(relationTypes.Any(x => x == null), Is.False);
Assert.That(relationTypes.Count(), Is.EqualTo(4));
Assert.That(relationTypes.Count(), Is.EqualTo(5));
}
}
@@ -174,7 +174,7 @@ namespace Umbraco.Tests.Persistence.Repositories
// Act
var exists = repository.Exists(3);
var doesntExist = repository.Exists(5);
var doesntExist = repository.Exists(6);
// Assert
Assert.That(exists, Is.True);
@@ -196,7 +196,7 @@ namespace Umbraco.Tests.Persistence.Repositories
int count = repository.Count(query);
// Assert
Assert.That(count, Is.EqualTo(4));
Assert.That(count, Is.EqualTo(5));
}
}

View File

@@ -1,5 +1,5 @@
angular.module("umbraco").controller("Umbraco.Editors.Content.RestoreController",
function ($scope, relationResource, contentResource, navigationService, appState, treeService) {
function ($scope, relationResource, contentResource, navigationService, appState, treeService, localizationService) {
var dialogOptions = $scope.dialogOptions;
var node = dialogOptions.currentNode;
@@ -12,9 +12,9 @@ angular.module("umbraco").controller("Umbraco.Editors.Content.RestoreController"
if (data.length == 0) {
$scope.success = false;
$scope.error = {
errorMsg: "Cannot automatically restore this item",
errorMsg: localizationService.localize('recycleBin_itemCannotBeRestored'),
data: {
Message: "There is no 'restore' relation found for this node. Use the Move menu item to move it manually."
Message: localizationService.localize('recycleBin_noRestoreRelation')
}
}
return;
@@ -32,9 +32,11 @@ angular.module("umbraco").controller("Umbraco.Editors.Content.RestoreController"
// make sure the target item isn't in the recycle bin
if($scope.target.path.indexOf("-20") !== -1) {
$scope.error = {
errorMsg: "Cannot automatically restore this item",
errorMsg: localizationService.localize('recycleBin_itemCannotBeRestored'),
data: {
Message: "The item you want to restore it under (" + $scope.target.name + ") is in the recycle bin. Use the Move menu item to move the item manually."
Message: localizationService.localize('recycleBin_restoreUnderRecycled').then(function (value) {
value.replace('%0%', $scope.target.name);
})
}
};
$scope.success = false;
@@ -80,4 +82,4 @@ angular.module("umbraco").controller("Umbraco.Editors.Content.RestoreController"
$scope.error = err;
});
};
});
});

View File

@@ -2,9 +2,9 @@
<div class="umb-dialog-body">
<umb-pane>
<p class="abstract" ng-hide="error != null || success == true">
Restore <strong>{{currentNode.name}}</strong> under <strong>{{target.name}}</strong>?
</p>
<p class="abstract" ng-hide="error != null || success == true">
<localize key="actions_restore">Restore</localize> <strong>{{currentNode.name}}</strong> <localize key="general_under">under</localize> <strong>{{target.name}}</strong>?
</p>
<div class="alert alert-error" ng-show="error != null">
<div><strong>{{error.errorMsg}}</strong></div>
@@ -12,15 +12,15 @@
</div>
<div class="alert alert-success" ng-show="success == true">
<p><strong>{{currentNode.name}}</strong> was moved underneath <strong>{{target.name}}</strong></p>
<button class="btn btn-primary" ng-click="nav.hideDialog()">OK</button>
<p><strong>{{currentNode.name}}</strong> <localize key="editdatatype_wasMoved">was moved underneath</localize> <strong>{{target.name}}</strong></p>
<button class="btn btn-primary" ng-click="nav.hideDialog()"><localize key="general_ok">OK</localize></button>
</div>
</umb-pane>
</div>
<div class="umb-dialog-footer btn-toolbar umb-btn-toolbar" ng-hide="success == true">
<a class="btn btn-link" ng-click="nav.hideDialog()">Cancel</a>
<button class="btn btn-primary" ng-click="restore()" ng-show="error == null">Restore</button>
<a class="btn btn-link" ng-click="nav.hideDialog()"><localize key="general_cancel">Cancel</localize></a>
<button class="btn btn-primary" ng-click="restore()" ng-show="error == null"><localize key="actions_restore">Restore</localize></button>
</div>
</div>
</div>

View File

@@ -0,0 +1,85 @@
angular.module("umbraco").controller("Umbraco.Editors.Media.RestoreController",
function ($scope, relationResource, mediaResource, navigationService, appState, treeService, localizationService) {
var dialogOptions = $scope.dialogOptions;
var node = dialogOptions.currentNode;
$scope.error = null;
$scope.success = false;
relationResource.getByChildId(node.id, "relateParentDocumentOnDelete").then(function (data) {
if (data.length == 0) {
$scope.success = false;
$scope.error = {
errorMsg: localizationService.localize('recycleBin_itemCannotBeRestored'),
data: {
Message: localizationService.localize('recycleBin_noRestoreRelation')
}
}
return;
}
$scope.relation = data[0];
if ($scope.relation.parentId == -1) {
$scope.target = { id: -1, name: "Root" };
} else {
mediaResource.getById($scope.relation.parentId).then(function (data) {
$scope.target = data;
// make sure the target item isn't in the recycle bin
if ($scope.target.path.indexOf("-20") !== -1) {
$scope.error = {
errorMsg: localizationService.localize('recycleBin_itemCannotBeRestored'),
data: {
Message: localizationService.localize('recycleBin_restoreUnderRecycled').then(function (value) {
value.replace('%0%', $scope.target.name);
})
}
};
$scope.success = false;
}
}, function (err) {
$scope.success = false;
$scope.error = err;
});
}
}, function (err) {
$scope.success = false;
$scope.error = err;
});
$scope.restore = function () {
// this code was copied from `content.move.controller.js`
mediaResource.move({ parentId: $scope.target.id, id: node.id })
.then(function (path) {
$scope.success = true;
//first we need to remove the node that launched the dialog
treeService.removeNode($scope.currentNode);
//get the currently edited node (if any)
var activeNode = appState.getTreeState("selectedNode");
//we need to do a double sync here: first sync to the moved media item - but don't activate the node,
//then sync to the currenlty edited media item (note: this might not be the media item that was moved!!)
navigationService.syncTree({ tree: "media", path: path, forceReload: true, activate: false }).then(function (args) {
if (activeNode) {
var activeNodePath = treeService.getPath(activeNode).join();
//sync to this node now - depending on what was copied this might already be synced but might not be
navigationService.syncTree({ tree: "media", path: activeNodePath, forceReload: false, activate: true });
}
});
}, function (err) {
$scope.success = false;
$scope.error = err;
});
};
});

View File

@@ -0,0 +1,26 @@
<div ng-controller="Umbraco.Editors.Media.RestoreController">
<div class="umb-dialog-body">
<umb-pane>
<p class="abstract" ng-hide="error != null || success == true">
<localize key="actions_restore">Restore</localize> <strong>{{currentNode.name}}</strong> <localize key="general_under">under</localize> <strong>{{target.name}}</strong>?
</p>
<div class="alert alert-error" ng-show="error != null">
<div><strong>{{error.errorMsg}}</strong></div>
<div>{{error.data.Message}}</div>
</div>
<div class="alert alert-success" ng-show="success == true">
<p><strong>{{currentNode.name}}</strong> <localize key="editdatatype_wasMoved">was moved underneath</localize> <strong>{{target.name}}</strong></p>
<button class="btn btn-primary" ng-click="nav.hideDialog()"><localize key="general_ok">OK</localize></button>
</div>
</umb-pane>
</div>
<div class="umb-dialog-footer btn-toolbar umb-btn-toolbar" ng-hide="success == true">
<a class="btn btn-link" ng-click="nav.hideDialog()"><localize key="general_cancel">Cancel</localize></a>
<button class="btn btn-primary" ng-click="restore()" ng-show="error == null"><localize key="actions_restore">Restore</localize></button>
</div>
</div>

View File

@@ -656,6 +656,7 @@
<key alias="submit">Submit</key>
<key alias="type">Type</key>
<key alias="typeToSearch">Type to search...</key>
<key alias="under">under</key>
<key alias="up">Up</key>
<key alias="update">Update</key>
<key alias="upgrade">Upgrade</key>
@@ -2201,4 +2202,11 @@ To manage your website, simply open the Umbraco back office and start adding con
<area alias="textbox">
<key alias="characters_left">characters left</key>
</area>
<area alias="recycleBin">
<key alias="contentTrashed">Trashed content with Id: {0} related to original parent content with Id: {1}</key>
<key alias="mediaTrashed">Trashed media with Id: {0} related to original parent media item with Id: {1}</key>
<key alias="itemCannotBeRestored">Cannot automatically restore this item</key>
<key alias="noRestoreRelation">There is no 'restore' relation found for this node. Use the Move menu item to move it manually.</key>
<key alias="restoreUnderRecycled">The item you want to restore it under ('%0%') is in the recycle bin. Use the Move menu item to move the item manually.</key>
</area>
</language>

View File

@@ -656,6 +656,7 @@
<key alias="submit">Submit</key>
<key alias="type">Type</key>
<key alias="typeToSearch">Type to search...</key>
<key alias="under">under</key>
<key alias="up">Up</key>
<key alias="update">Update</key>
<key alias="upgrade">Upgrade</key>
@@ -2193,4 +2194,11 @@ To manage your website, simply open the Umbraco back office and start adding con
<area alias="textbox">
<key alias="characters_left">characters left</key>
</area>
<area alias="recycleBin">
<key alias="contentTrashed">Trashed content with Id: {0} related to original parent content with Id: {1}</key>
<key alias="mediaTrashed">Trashed media with Id: {0} related to original parent media item with Id: {1}</key>
<key alias="itemCannotBeRestored">Cannot automatically restore this item</key>
<key alias="noRestoreRelation">There is no 'restore' relation found for this node. Use the Move menu item to move it manually.</key>
<key alias="restoreUnderRecycled">The item you want to restore it under ('%0%') is in the recycle bin. Use the Move menu item to move the item manually.</key>
</area>
</language>

View File

@@ -140,7 +140,13 @@ namespace Umbraco.Web.Trees
//if the media item is in the recycle bin, don't have a default menu, just show the regular menu
if (item.Path.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Contains(RecycleBinId.ToInvariantString()))
{
menu.DefaultMenuAlias = null;
menu.DefaultMenuAlias = null;
menu.Items.Insert(2, new MenuItem(ActionRestore.Instance, ui.Text("actions", ActionRestore.Instance.Alias)));
}
else
{
//set the default to create
menu.DefaultMenuAlias = ActionNew.Instance.Alias;
}
return menu;

View File

@@ -5,7 +5,7 @@ using umbraco.BasePages;
namespace umbraco.BusinessLogic.Actions
{
/// <summary>
/// This action is invoked when the content item is to be restored from the recycle bin
/// This action is invoked when the content/media item is to be restored from the recycle bin
/// </summary>
public class ActionRestore : IAction
{