/*
* Copyright 2025 Anyware Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Actions of the trash.
* @private
*/
Ext.define('Ametys.plugins.cms.trash.TrashActions', {
singleton: true,
/**
* Permanently delete all the element in the trash
* @param {Ametys.ribbon.element.ui.ButtonController} controller the controller
*/
empty: function(controller)
{
this._confirmBeforeAct(
"{{i18n UITOOL_TRASH_BUTTON_EMPTY_TITLE}}",
"{{i18n UITOOL_TRASH_BUTTON_EMPTY_CONFIRM_MSG}}",
"empty", [],
this._emptyCb, {}
);
},
/**
* @private
*/
_emptyCb: function(response)
{
Ametys.notify({
type: 'info',
iconGlyph: 'ametysicon-desktop-trash',
title: "{{i18n UITOOL_TRASH_BUTTON_EMPTY_TITLE}}",
description: "{{i18n UITOOL_TRASH_BUTTON_EMPTY_NOTIFY_MSG}}"
});
Ext.create("Ametys.message.Message", {
type: Ametys.message.Message.DELETED,
targets: [{id: Ametys.message.MessageTarget.TRASH}]
});
},
/**
* Restore the currently selected element
* @param {Ametys.ribbon.element.ui.ButtonController} controller the controller
*/
restoreObject: function(controller)
{
var target = controller.getMatchingTargets()[0];
if (target != null)
{
var targetParams = target.getParameters();
this._confirmBeforeAct(
"{{i18n UITOOL_TRASH_BUTTON_OBJECT_RESTORE_TITLE}}",
Ext.String.format("{{i18n UITOOL_TRASH_BUTTON_OBJECT_RESTORE_CONFIRM_MSG}}", Ext.String.escapeHtml(targetParams.title), Ext.String.escapeHtml(targetParams.type)),
"restore", [targetParams.id],
this._restoreObjectCb, { target: target }
);
}
},
/**
* @private
*/
_restoreObjectCb: function(response, args)
{
if (response.success)
{
let action = this._getRestorationNotificationAction(response);
let description = response.restorationDescription;
Ametys.notify({
type: 'info',
iconGlyph: 'ametysicon-desktop-trash',
title: "{{i18n UITOOL_TRASH_BUTTON_OBJECT_RESTORE_TITLE}}",
description: description,
action: action
});
Ext.create("Ametys.message.Message", {
type: Ametys.message.Message.DELETED,
targets: [args.target]
});
let restoredTargets = response.restoredLinkedObjects.map(obj => ({id: obj.type, parameters: {ids: [obj.id]}}));
restoredTargets.push({id: response.restoredObject.type, parameters: {ids: [response.restoredObject.id]}});
Ext.create("Ametys.message.Message", {
type: Ametys.message.Message.CREATED,
parameters: {restored: true},
targets: restoredTargets
});
}
else if (response.reason == "unknown-parent")
{
Ametys.notify({
type: 'error',
iconGlyph: 'ametysicon-desktop-trash',
title: "{{i18n UITOOL_TRASH_BUTTON_OBJECT_RESTORE_TITLE}}",
description: Ext.String.format("{{i18n UITOOL_TRASH_BUTTON_OBJECT_RESTORE_INVALID_PARENT_ERROR}}", Ext.String.escapeHtml(args.target.getParameters().title))
});
}
else
{
Ametys.notify({
type: 'error',
iconGlyph: 'ametysicon-desktop-trash',
title: "{{i18n UITOOL_TRASH_BUTTON_OBJECT_RESTORE_TITLE}}",
description: Ext.String.format("{{i18n UITOOL_TRASH_BUTTON_OBJECT_RESTORE_ERROR}}", Ext.String.escapeHtml(args.target.getParameters().title ))
});
}
},
/**
* @private
*/
_getRestorationNotificationAction: function(response)
{
if (response && response.notificationOpenToolAction
&& response.notificationOpenToolAction.toolId)
{
return Ext.bind(Ametys.tool.ToolsManager.openTool, Ametys.tool.ToolsManager, [response.notificationOpenToolAction.toolId, response.notificationOpenToolAction.toolParams], false);
}
return null;
},
/**
* Permanently delete the selected element
* @param {Ametys.ribbon.element.ui.ButtonController} controller the controller
*/
deleteObject: function(controller)
{
var target = controller.getMatchingTargets()[0];
if (target != null)
{
var targetParams = target.getParameters();
this._confirmBeforeAct(
"{{i18n UITOOL_TRASH_BUTTON_OBJECT_DELETE_TITLE}}",
Ext.String.format("{{i18n UITOOL_TRASH_BUTTON_OBJECT_DELETE_CONFIRM_MSG}}", Ext.String.escapeHtml(targetParams.title), Ext.String.escapeHtml(targetParams.type)),
"delete", [targetParams.id],
this._deleteObjectCb, { target: target }
);
}
},
/**
* @private
*/
_deleteObjectCb: function(response, args)
{
var targetParams = args.target.getParameters();
Ametys.notify({
type: 'info',
iconGlyph: 'ametysicon-desktop-trash',
title: "{{i18n UITOOL_TRASH_BUTTON_OBJECT_DELETE_TITLE}}",
description: Ext.String.format("{{i18n UITOOL_TRASH_BUTTON_OBJECT_DELETE_NOTIFY_MSG}}", Ext.String.escapeHtml(targetParams.title), Ext.String.escapeHtml(targetParams.type))
});
Ext.create("Ametys.message.Message", {
type: Ametys.message.Message.DELETED,
targets: [args.target]
});
},
/**
* @private
*/
_confirmBeforeAct: function(confirmTitle, confirmMsg, methodName, methodParams, methodCallback, methodCallbackArgs)
{
Ametys.Msg.confirm(
confirmTitle,
confirmMsg,
function (answer)
{
if (answer == 'yes')
{
Ametys.data.ServerComm.callMethod({
role: "org.ametys.cms.trash.TrashManager",
methodName: methodName,
parameters: methodParams,
callback: {
handler: methodCallback,
scope: this,
arguments: methodCallbackArgs
},
waitMessage: true,
errorMessage: true
});
}
},
this
);
}
});