From 052bef84ce5dc17138ed9ed1bdc50a08b27f6402 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Fri, 13 Oct 2017 13:22:45 +0200 Subject: [PATCH] W.I.P vfsSelect Widget: - Fix upload feature not working --- api/js/etemplate/et2_widget_vfs.js | 4 ++ api/js/etemplate/vfsSelectUI.js | 55 +++++++++----------- api/src/Etemplate/Widget/Vfs.php | 73 +++++++++++++++++++++++++++ api/templates/default/vfsSelectUI.xet | 2 +- 4 files changed, 101 insertions(+), 33 deletions(-) diff --git a/api/js/etemplate/et2_widget_vfs.js b/api/js/etemplate/et2_widget_vfs.js index c482afc097..d34748c5d4 100644 --- a/api/js/etemplate/et2_widget_vfs.js +++ b/api/js/etemplate/et2_widget_vfs.js @@ -1055,6 +1055,10 @@ var et2_vfsSelect = (function(){ "use strict"; return et2_inputWidget.extend( resizable: false }, et2_dialog._create_parent('api')); this.dialog.template.uniqueId = 'api.vfsSelectUI'; + // we need an etemplate_exec_id for better handling serverside parts of + // widgets and since we can not have a etemplate_exec_id specifically + // for dialog template our best shot is to inherit its parent etemplate_exec_id. + this.dialog.template.etemplate_exec_id = this.getInstanceManager().etemplate_exec_id; app.vfsSelectUI.et2 = this.dialog.template.widgetContainer; app.vfsSelectUI.vfsSelectWidget = this; this.dialog.div.on('load', function(e) { diff --git a/api/js/etemplate/vfsSelectUI.js b/api/js/etemplate/vfsSelectUI.js index 0297cf2862..2668d9765c 100644 --- a/api/js/etemplate/vfsSelectUI.js +++ b/api/js/etemplate/vfsSelectUI.js @@ -89,38 +89,35 @@ app.classes.vfsSelectUI = (function(){ "use strict"; return AppJS.extend( }, /** - * Send names of uploaded files (again) to server, to process them: either copy to vfs or ask overwrite/rename + * Send names of uploaded files (again) to server, + * to process them: either copy to vfs or ask overwrite/rename * * @param {event} _event - * @param {number} _file_count - * @param {string=} _path where the file is uploaded to, default current directory */ - upload: function(_event, _file_count, _path) + storeFile: function(_event) { - if(typeof _path == 'undefined') - { - _path = this.get_path(); - } - if (_file_count && !jQuery.isEmptyObject(_event.data.getValue())) + var path = this.get_path(); + + if (!jQuery.isEmptyObject(_event.data.getValue())) { var widget = _event.data; - egw(window).json('filemanager_ui::ajax_action', ['upload', widget.getValue(), _path], - this._upload_callback, this, true, this + egw(window).json('EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_vfsSelect_storeFile', [widget.getValue(), path], + this._storeFile_callback, this, true, this ).sendRequest(true); widget.set_value(''); } }, /** - * Callback for server response to upload request: + * Callback for server response to storeFile request: * - display message and refresh list * - ask use to confirm overwritting existing files or rename upload * * @param {object} _data values for attributes msg, files, ... */ - _upload_callback: function(_data) + _storeFile_callback: function(_data) { - if (_data.msg || _data.uploaded) window.egw_refresh(_data.msg, this.appname); + if (_data.msg || _data.uploaded) egw(window).message(_data.msg); var that = this; for(var file in _data.uploaded) @@ -146,8 +143,8 @@ app.classes.vfsSelectUI = (function(){ "use strict"; return AppJS.extend( uploaded[this.my_data.file].name = _value; delete uploaded[this.my_data.file].confirm; // send overwrite-confirmation and/or rename request to server - egw.json('filemanager_ui::ajax_action', [this.my_data.action, uploaded, this.my_data.path, this.my_data.props], - that._upload_callback, that, true, that + egw.json('EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_vfsSelect_storeFile', [uploaded, this.my_data.path], + that._storeFile_callback, that, true, that ).sendRequest(); return; case "cancel": @@ -164,13 +161,15 @@ app.classes.vfsSelectUI = (function(){ "use strict"; return AppJS.extend( _data.uploaded[file].name, buttons, file); // setting required data for callback in as my_data dialog.my_data = { - action: _data.action, file: file, path: _data.path, data: _data.uploaded[file], - props: _data.props }; } + else + { + this.submit(); + } } }, @@ -295,20 +294,12 @@ app.classes.vfsSelectUI = (function(){ "use strict"; return AppJS.extend( submit: function(_field, _val, _callback) { var arrMgrs = this.et2.getArrayMgrs(); - arrMgrs.content.data[_field] = _val; - jQuery.extend(arrMgrs.content.data, arrMgrs.modifications.data); - this.et2.setArrayMgrs(arrMgrs); + if (_field && _val) + { + arrMgrs.content.data[_field] = _val; + jQuery.extend(arrMgrs.content.data, arrMgrs.modifications.data); + this.et2.setArrayMgrs(arrMgrs); + } this.vfsSelectWidget._content(arrMgrs.content.data, _callback); - }, - - /** - * - * @param {type} _widget - * @returns {undefined} - * @todo: implementation of upload file - */ - uploaded: function (_widget) - { - } });}).call(this); diff --git a/api/src/Etemplate/Widget/Vfs.php b/api/src/Etemplate/Widget/Vfs.php index 4e46d67f9c..ad822249e7 100644 --- a/api/src/Etemplate/Widget/Vfs.php +++ b/api/src/Etemplate/Widget/Vfs.php @@ -562,6 +562,79 @@ class Vfs extends File $response->data($msg); } + /** + * Store uploaded temp file into VFS + * + * @param array $files temp files + * @param string $dir vfs path to store the file + */ + static function ajax_vfsSelect_storeFile ($files, $dir) + { + $response = Api\Json\Response::get(); + $result = array ( + 'errs' => 0, + 'msg' => '', + 'files' => 0, + ); + $script_error = 0; + foreach($files as $tmp_name => &$data) + { + $path = \EGroupware\Api\Vfs::concat($dir, \EGroupware\Api\Vfs::encodePathComponent($data['name'])); + + if(\EGroupware\Api\Vfs::deny_script($path)) + { + if (!isset($script_error)) + { + $result['msg'] .= ($result['msg'] ? "\n" : '').lang('You are NOT allowed to upload a script!'); + } + ++$script_error; + ++$result['errs']; + unset($files[$tmp_name]); + } + elseif (\EGroupware\Api\Vfs::is_dir($path)) + { + $data['confirm'] = 'is_dir'; + } + elseif (!$data['confirmed'] && \EGroupware\Api\Vfs::stat($path)) + { + $data['confirm'] = true; + } + else + { + if (is_dir($GLOBALS['egw_info']['server']['temp_dir']) && is_writable($GLOBALS['egw_info']['server']['temp_dir'])) + { + $tmp_path = $GLOBALS['egw_info']['server']['temp_dir'] . '/' . basename($tmp_name); + } + else + { + $tmp_path = ini_get('upload_tmp_dir').'/'.basename($tmp_name); + } + + if (\EGroupware\Api\Vfs::copy_uploaded($tmp_path, $path, null, false)) + { + ++$result['files']; + $uploaded[] = $data['name']; + } + else + { + ++$result['errs']; + } + } + } + if ($result['errs'] > $script_error) + { + $result['msg'] .= ($result['msg'] ? "\n" : '').lang('Error uploading file!'); + } + if ($result['files']) + { + $result['msg'] .= ($result['msg'] ? "\n" : '').lang('%1 successful uploaded.', implode(', ', $uploaded)); + } + $result['uploaded'] = $files; + $result['path'] = $dir; + + $response->data($result); + } + /** * Get a list off all apps having an application directory in VFS * diff --git a/api/templates/default/vfsSelectUI.xet b/api/templates/default/vfsSelectUI.xet index 1e95d3a93b..c1abbdea05 100644 --- a/api/templates/default/vfsSelectUI.xet +++ b/api/templates/default/vfsSelectUI.xet @@ -17,7 +17,7 @@