From 4619d55045bfee2a3af841488604fa00bb2e4b39 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Wed, 18 Dec 2019 16:31:54 +0100 Subject: [PATCH] * File selector: implement Overwrite and Rename possibilities for saving an existing email message or attachment --- api/js/etemplate/et2_widget_vfs.js | 110 ++++++++++++++++++----------- mail/inc/class.mail_ui.inc.php | 52 ++++++++------ 2 files changed, 102 insertions(+), 60 deletions(-) diff --git a/api/js/etemplate/et2_widget_vfs.js b/api/js/etemplate/et2_widget_vfs.js index 510154937c..c02d4f7db9 100644 --- a/api/js/etemplate/et2_widget_vfs.js +++ b/api/js/etemplate/et2_widget_vfs.js @@ -1236,55 +1236,85 @@ var et2_vfsSelect = (function(){ "use strict"; return et2_inputWidget.extend( // define a mini app object for vfs select UI 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 = []; - switch(_data.content.mode) - { - case 'open-multiple': - if (_value.dir && _value.dir.selected) + case 'open-multiple': + if (submit_value.dir && submit_value.dir.selected) + { + for(var key in Object.keys(submit_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(_value.path+'/'+_value.dir.selected[key]); - } + files.push(submit_value.path+'/'+submit_value.dir.selected[key]); } } - break; - case 'select-dir': - files = _value.path; - break; - default: - if (self.options.method === 'download') _value.path = _data.content.download_baseUrl; - files = _value.path+'/'+_value.name; - break; - } - self._setRecentPaths(_value.path); - self.value = files; - if (self.options.method && self.options.method !== 'download') - { - egw(window).json( - self.options.method, - [self.options.method_id, files, _button_id], - function(){ - jQuery(self.node).change(); + } + break; + case 'select-dir': + files = submit_value.path; + break; + default: + if (self.options.method === 'download') submit_value.path = _data.content.download_baseUrl; + files = submit_value.path+'/'+submit_value.name; + if (self.options.method !== 'download' && !savemode) + { + for(var p in _data.content.dir) + { + if (_data.content.dir[p]['name'] == submit_value.name) + { + var saveModeDialogButtons = [ + {text: self.egw().lang("Yes"), id: "overwrite", class: "ui-priority-primary", "default": true, image: 'check'}, + {text: self.egw().lang("Rename"), id:"rename", image: 'edit'}, + {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); - } - else - { - jQuery(self.node).change(); - } - delete app.vfsSelectUI; - return true; + } + break; } - }, + 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, buttons: buttons, minWidth: 500, diff --git a/mail/inc/class.mail_ui.inc.php b/mail/inc/class.mail_ui.inc.php index fd0d05351a..90a8d0ceba 100644 --- a/mail/inc/class.mail_ui.inc.php +++ b/mail/inc/class.mail_ui.inc.php @@ -2813,18 +2813,22 @@ $filter['before']= date("d-M-Y", $cutoffdate2); * action => string * ) * @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(); switch ($params['action']) { case 'message': - $result = $this->vfsSaveMessages($params['ids'], $path); + $result = $this->vfsSaveMessages($params['ids'], $path, $savemode); break; case 'attachment': - $result = $this->vfsSaveAttachments($params['ids'], $path); + $result = $this->vfsSaveAttachments($params['ids'], $path, $savemode); break; } $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 $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 * array ( @@ -2842,7 +2847,7 @@ $filter['before']= date("d-M-Y", $cutoffdate2); * 'success' => BOOLEAN * ) */ - function vfsSaveMessages($ids,$path) + function vfsSaveMessages($ids,$path, $savemode) { // add mail translation 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)); } - // Check if file already exists, then try to assign a none existance filename - $counter = 1; - $tmp_file = $file; - while (Vfs::file_exists($tmp_file)) + if ($savemode != 'overwrite') { + // Check if file already exists, then try to assign a none existance filename + $counter = 1; $tmp_file = $file; - $pathinfo = pathinfo(Vfs::basename($tmp_file)); - $tmp_file = $dir . '/' . $pathinfo['filename'] . '(' . $counter . ')' . '.' . $pathinfo['extension']; - $counter++; + while (Vfs::file_exists($tmp_file)) + { + $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)) { @@ -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 $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 * array ( @@ -2934,7 +2943,7 @@ $filter['before']= date("d-M-Y", $cutoffdate2); * 'success' => BOOLEAN * ) */ - function vfsSaveAttachments($ids,$path) + function vfsSaveAttachments($ids,$path, $savemode) { $res = array ( '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'])); - $counter = 1; - $tmp_file = $file; - while (Vfs::file_exists($tmp_file)) + if ($savemode != 'overwrite') { + $counter = 1; $tmp_file = $file; - $pathinfo = pathinfo(Vfs::basename($tmp_file)); - $tmp_file = $dir . '/' . $pathinfo['filename'] . '(' . $counter . ')' . '.' . $pathinfo['extension']; - $counter++; + while (Vfs::file_exists($tmp_file)) + { + $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,$attachment['attachment']))