* File selector: implement Overwrite and Rename possibilities for saving an existing email message or attachment

This commit is contained in:
Hadi Nategh 2019-12-18 16:31:54 +01:00
parent 07272a9174
commit 4619d55045
2 changed files with 102 additions and 60 deletions

View File

@ -1236,55 +1236,85 @@ var et2_vfsSelect = (function(){ "use strict"; return et2_inputWidget.extend(
// define a mini app object for vfs select UI // define a mini app object for vfs select UI
app.vfsSelectUI = new app.classes.vfsSelectUI; app.vfsSelectUI = new app.classes.vfsSelectUI;
this.dialog = et2_createWidget("dialog", // callback for dialog
this.submit_callback = function(submit_button_id, submit_value, savemode)
{ {
callback: function(_button_id, _value) if ((submit_button_id == 'submit' || (extra_buttons_action && extra_buttons_action[submit_button_id])) && submit_value)
{ {
if ((_button_id == 'submit' || (extra_buttons_action && extra_buttons_action[_button_id])) && _value) var files = [];
switch(_data.content.mode)
{ {
var files = []; case 'open-multiple':
switch(_data.content.mode) if (submit_value.dir && submit_value.dir.selected)
{ {
case 'open-multiple': for(var key in Object.keys(submit_value.dir.selected))
if (_value.dir && _value.dir.selected)
{ {
for(var key in Object.keys(_value.dir.selected)) if (submit_value.dir.selected[key] != "")
{ {
if (_value.dir.selected[key] != "") files.push(submit_value.path+'/'+submit_value.dir.selected[key]);
{
files.push(_value.path+'/'+_value.dir.selected[key]);
}
} }
} }
break; }
case 'select-dir': break;
files = _value.path; case 'select-dir':
break; files = submit_value.path;
default: break;
if (self.options.method === 'download') _value.path = _data.content.download_baseUrl; default:
files = _value.path+'/'+_value.name; if (self.options.method === 'download') submit_value.path = _data.content.download_baseUrl;
break; files = submit_value.path+'/'+submit_value.name;
} if (self.options.method !== 'download' && !savemode)
self._setRecentPaths(_value.path); {
self.value = files; for(var p in _data.content.dir)
if (self.options.method && self.options.method !== 'download') {
{ if (_data.content.dir[p]['name'] == submit_value.name)
egw(window).json( {
self.options.method, var saveModeDialogButtons = [
[self.options.method_id, files, _button_id], {text: self.egw().lang("Yes"), id: "overwrite", class: "ui-priority-primary", "default": true, image: 'check'},
function(){ {text: self.egw().lang("Rename"), id:"rename", image: 'edit'},
jQuery(self.node).change(); {text: self.egw().lang("Cancel"), id:"cancel"}
];
return et2_dialog.show_prompt(function(_button_id, _value) {
switch (_button_id)
{
case "overwrite":
return self.submit_callback(submit_button_id, submit_value, 'overwrite');
case "rename":
submit_value.name = _value;
return self.submit_callback(submit_button_id, submit_value, 'rename');
}
},
self.egw().lang('Do you want to overwrite existing file %1 in directory %2?', submit_value.name, submit_value.path),
self.egw().lang('File %1 already exists', submit_value.name),
submit_value.name, saveModeDialogButtons);
}
} }
).sendRequest(true); }
} break;
else
{
jQuery(self.node).change();
}
delete app.vfsSelectUI;
return true;
} }
}, self._setRecentPaths(submit_value.path);
self.value = files;
if (self.options.method && self.options.method !== 'download')
{
egw(window).json(
self.options.method,
[self.options.method_id, files, submit_button_id, savemode],
function(){
jQuery(self.node).change();
}
).sendRequest(true);
}
else
{
jQuery(self.node).change();
}
delete app.vfsSelectUI;
return true;
}
};
this.dialog = et2_createWidget("dialog",
{
callback: this.submit_callback,
title: this.options.dialog_title, title: this.options.dialog_title,
buttons: buttons, buttons: buttons,
minWidth: 500, minWidth: 500,

View File

@ -2813,18 +2813,22 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
* action => string * action => string
* ) * )
* @param string $path path to save the emails * @param string $path path to save the emails
* @param string $submit_button_id dialog button id of triggered submit
* @param string $savemode save mode: 'overwrite' or 'rename'
*/ */
function ajax_vfsSave ($params,$path) function ajax_vfsSave ($params, $path, $submit_button_id, $savemode)
{ {
unset($submit_button_id); // not used here
$response = Api\Json\Response::get(); $response = Api\Json\Response::get();
switch ($params['action']) switch ($params['action'])
{ {
case 'message': case 'message':
$result = $this->vfsSaveMessages($params['ids'], $path); $result = $this->vfsSaveMessages($params['ids'], $path, $savemode);
break; break;
case 'attachment': case 'attachment':
$result = $this->vfsSaveAttachments($params['ids'], $path); $result = $this->vfsSaveAttachments($params['ids'], $path, $savemode);
break; break;
} }
$response->call('app.mail.vfsSaveCallback', $result); $response->call('app.mail.vfsSaveCallback', $result);
@ -2835,6 +2839,7 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
* *
* @param string|array $ids use splitRowID, to separate values * @param string|array $ids use splitRowID, to separate values
* @param string $path path in vfs (no Vfs::PREFIX!), only directory for multiple id's ($ids is an array) * @param string $path path in vfs (no Vfs::PREFIX!), only directory for multiple id's ($ids is an array)
* @param string $savemode save mode: 'overwrite' or 'rename'
* *
* @return array returns an array including message and success result * @return array returns an array including message and success result
* array ( * array (
@ -2842,7 +2847,7 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
* 'success' => BOOLEAN * 'success' => BOOLEAN
* ) * )
*/ */
function vfsSaveMessages($ids,$path) function vfsSaveMessages($ids,$path, $savemode)
{ {
// add mail translation // add mail translation
Api\Translation::add_app('mail'); Api\Translation::add_app('mail');
@ -2884,17 +2889,20 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
$file = $dir . '/' . mail_bo::clean_subject_for_filename(str_replace($dir.'/', '', $path)); $file = $dir . '/' . mail_bo::clean_subject_for_filename(str_replace($dir.'/', '', $path));
} }
// Check if file already exists, then try to assign a none existance filename if ($savemode != 'overwrite')
$counter = 1;
$tmp_file = $file;
while (Vfs::file_exists($tmp_file))
{ {
// Check if file already exists, then try to assign a none existance filename
$counter = 1;
$tmp_file = $file; $tmp_file = $file;
$pathinfo = pathinfo(Vfs::basename($tmp_file)); while (Vfs::file_exists($tmp_file))
$tmp_file = $dir . '/' . $pathinfo['filename'] . '(' . $counter . ')' . '.' . $pathinfo['extension']; {
$counter++; $tmp_file = $file;
$pathinfo = pathinfo(Vfs::basename($tmp_file));
$tmp_file = $dir . '/' . $pathinfo['filename'] . '(' . $counter . ')' . '.' . $pathinfo['extension'];
$counter++;
}
$file = $tmp_file;
} }
$file = $tmp_file;
if (!($fp = Vfs::fopen($file,'wb')) || !fwrite($fp,$message)) if (!($fp = Vfs::fopen($file,'wb')) || !fwrite($fp,$message))
{ {
@ -2927,6 +2935,7 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
* *
* @param string|array $ids '::' delimited mailbox::uid::part-id::is_winmail::name (::name for multiple id's) * @param string|array $ids '::' delimited mailbox::uid::part-id::is_winmail::name (::name for multiple id's)
* @param string $path path in vfs (no Vfs::PREFIX!), only directory for multiple id's ($ids is an array) * @param string $path path in vfs (no Vfs::PREFIX!), only directory for multiple id's ($ids is an array)
* @param string $savemode save mode: 'overwrite' or 'rename'
* *
* @return array returns an array including message and success result * @return array returns an array including message and success result
* array ( * array (
@ -2934,7 +2943,7 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
* 'success' => BOOLEAN * 'success' => BOOLEAN
* ) * )
*/ */
function vfsSaveAttachments($ids,$path) function vfsSaveAttachments($ids,$path, $savemode)
{ {
$res = array ( $res = array (
'msg' => lang('Attachment has been saved successfully.'), 'msg' => lang('Attachment has been saved successfully.'),
@ -3032,16 +3041,19 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
$file = $dir. '/' . ($filename ? $filename : mail_bo::clean_subject_for_filename($attachment['filename'])); $file = $dir. '/' . ($filename ? $filename : mail_bo::clean_subject_for_filename($attachment['filename']));
$counter = 1; if ($savemode != 'overwrite')
$tmp_file = $file;
while (Vfs::file_exists($tmp_file))
{ {
$counter = 1;
$tmp_file = $file; $tmp_file = $file;
$pathinfo = pathinfo(Vfs::basename($tmp_file)); while (Vfs::file_exists($tmp_file))
$tmp_file = $dir . '/' . $pathinfo['filename'] . '(' . $counter . ')' . '.' . $pathinfo['extension']; {
$counter++; $tmp_file = $file;
$pathinfo = pathinfo(Vfs::basename($tmp_file));
$tmp_file = $dir . '/' . $pathinfo['filename'] . '(' . $counter . ')' . '.' . $pathinfo['extension'];
$counter++;
}
$file = $tmp_file;
} }
$file = $tmp_file;
if (!($fp = Vfs::fopen($file,'wb')) || if (!($fp = Vfs::fopen($file,'wb')) ||
!fwrite($fp,$attachment['attachment'])) !fwrite($fp,$attachment['attachment']))