/*
 *  Copyright 2013 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.
 */
/**
 * Singleton class to handle actions on folders for the resources explorer.
 * @private
 */
Ext.define('Ametys.cms.attach.AttachmentsActions', {
	singleton: true,
	
	/**
	 * Open the search tool on the current attachments' folder
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling the function
	 */
	openSearch: function(controller)
	{
		var holderTarget = controller.getMatchingTargets()[0];
		var target = Ametys.message.MessageTargetHelper.findTarget(holderTarget, /^(explorer-collection|resource)$/);
		if (!target)
		{
			return;
		}
		
		var toolId = controller.getInitialConfig('search-tool-id') || 'uitool-content-attachments-search';
		
		var targetParams = target.getParameters();
		Ametys.tool.ToolsManager.openTool(toolId, {
			id: targetParams.id,
			applicationId: targetParams.applicationId,
			path: targetParams.path,
			rootId: targetParams.rootId,
			holderId: holderTarget.getParameters().id,
			holderTitle: holderTarget.getParameters().title
		});
	},
	
	//------------------------------------------------------//
	//						ARCHIVE							//
	//------------------------------------------------------//
	/**
	 * Function called to export a folder to a ZIP archive
	 * @param {Ametys.ribbon.element.ui.ButtonController} controller The controller calling the function
	 */
	archive: function (controller)
	{
		var rootTarget = controller.getMatchingTargets()[0];
		if (rootTarget)
		{
			var target = Ametys.message.MessageTargetHelper.findTarget(controller.getMatchingTargets(), Ametys.message.MessageTarget.EXPLORER_COLLECTION);
			if (target)
			{
				var name;
				if (target.getParameters().id == target.getParameters().rootId)
				{
					name = rootTarget.getParameters().title + " - {{i18n PLUGINS_CMS_HELPER_CHOOSEATTACHMENTSFILE_ROOT_NODE}}";
				}
				Ametys.explorer.resources.actions.Folder.archive(target.getParameters().id, name);
			}
		}
	}
});