* Added context menu to download all linked files in one zip file

As long as there are 2 or more files linked to the entry, you can download them all as a zip using the link list context menu.
This commit is contained in:
Nathan Gray 2014-07-08 16:51:01 +00:00
parent 31960e45e2
commit b7a6e53163
3 changed files with 51 additions and 3 deletions

View File

@ -18,6 +18,10 @@
class etemplate_widget_link extends etemplate_widget
{
public $public_functions = array(
'download_zip' => true
);
protected $legacy_options = 'only_app';
/**
@ -237,6 +241,33 @@ class etemplate_widget_link extends etemplate_widget
$response->data(egw_link::unlink($value));
}
/**
* Download the files linked to the given entry as one ZIP.
*
* Because of the $public_functions entry, this is callable as a menuaction.
* This lets us just open the URL with app & id parametrs and get a ZIP. If
* the entry has no linked files, the ZIP will still be returned, but it will
* be empty.
*/
public function download_zip()
{
$app = $_GET['app'];
$id = $_GET['id'];
if(egw_link::file_access($app, $id))
{
$app_path = egw_link::vfs_path($app,$id,'',true);
// Pass the files linked, not the entry path
$files = egw_vfs::find($app_path);
if($files[0] == $app_path)
{
array_shift($files);
}
egw_vfs::download_zip($files, egw_link::title($app, $id));
common::egw_exit();
}
}
/**
* Validate input
*

View File

@ -1493,6 +1493,19 @@ var et2_link_list = et2_link_string.extend(
}
});
this.context.addItem("-", "-");
this.context.addItem("zip", this.egw().lang("Save as Zip"), this.egw().image('save_all'), function(menu_item) {
// Highlight files for nice UI indicating what will be in the zip.
// Files have negative IDs.
$j('[id^="link_-"]',this.list).effect('highlight',{},2000);
// Download ZIP
window.location = self.egw().link('/index.php',{
menuaction: 'etemplate.etemplate_widget_link.download_zip',
app: self.value.to_app,
id: self.value.to_id
});
});
this.context.addItem("-", "-");
this.context.addItem("delete", this.egw().lang("Delete link"), this.egw().image("delete"), function(menu_item) {
var link_id = isNaN(self.context.data.link_id) ? self.context.data : self.context.data.link_id;
var row = jQuery('#link_'+(self.context.data.dom_id ? self.context.data.dom_id : self.context.data.link_id), self.list);
@ -1626,6 +1639,8 @@ var et2_link_list = et2_link_string.extend(
row.bind("contextmenu", function(e) {
// File info only available for existing files
self.context.getItem("file_info").set_enabled(typeof _link_data.id != 'object' && _link_data.app == 'file');
// Zip download only offered if there are at least 2 files
self.context.getItem("zip").set_enabled($j('[id^="link_-"]',this.list).length >= 2);
self.context.data = _link_data;
self.context.showAt(e.pageX, e.pageY, true);

View File

@ -1323,11 +1323,12 @@ class egw_vfs extends vfs_stream_wrapper
/**
* Download the given file list as a ZIP
*
* @param array $files
* @param array $files List of files to include in the zip
* @param string $name optional Zip file name. If not provided, it will be determined automatically from the files
*
* @return undefined
*/
public static function download_zip(Array $files)
public static function download_zip(Array $files, $name = false)
{
error_log(__METHOD__ . ': '.implode(',',$files));
@ -1389,11 +1390,12 @@ class egw_vfs extends vfs_stream_wrapper
// A nice name for the user,
$filename = $GLOBALS['egw_info']['server']['site_title'] . '_' .
str_replace($replace,'_',(
$name ? $name : (
count($files) == 1 ?
// Just one file (hopefully a directory?) selected
self::basename($files[0]) :
// Use the lowest common directory (eg: Infolog, Open, nathan)
self::basename($base_dir)
self::basename($base_dir))
)) . '.zip';
// Make sure basename is a dir