From 905df56bb91612508a89b9048f02c360b7a0667f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Fri, 30 Oct 2015 10:22:11 +0100 Subject: [PATCH] "Add from cloud" for event attachments (T809) --- plugins/kolab_files/kolab_files.js | 99 +++++++++++------ .../kolab_files/lib/kolab_files_engine.php | 104 ++++++++++++------ .../skins/larry/templates/message_plugin.html | 2 +- plugins/kolab_files/skins/larry/ui.js | 2 +- 4 files changed, 135 insertions(+), 72 deletions(-) diff --git a/plugins/kolab_files/kolab_files.js b/plugins/kolab_files/kolab_files.js index 1759ee47..93989fdb 100644 --- a/plugins/kolab_files/kolab_files.js +++ b/plugins/kolab_files/kolab_files.js @@ -29,28 +29,7 @@ window.rcmail && window.files_api && rcmail.addEventListener('init', function() if (rcmail.task == 'mail') { // mail compose if (rcmail.env.action == 'compose') { - var elem = $('#compose-attachments > div'), - input = $('') - .attr('tabindex', $('input', elem).attr('tabindex') || 0) - .val(rcmail.gettext('kolab_files.fromcloud')) - .click(function() { kolab_files_selector_dialog(); }) - .appendTo(elem); - - if (rcmail.gui_objects.filelist) { - rcmail.file_list = new rcube_list_widget(rcmail.gui_objects.filelist, { - multiselect: true, - keyboard: true, - column_movable: false, - dblclick_time: rcmail.dblclick_time - }); - rcmail.file_list.addEventListener('select', function(o) { kolab_files_list_select(o); }) - .addEventListener('listupdate', function(e) { rcmail.triggerEvent('listupdate', e); }); - - rcmail.enable_command('files-sort', 'files-search', 'files-search-reset', true); - - rcmail.file_list.init(); - kolab_files_list_coltypes(); - } + kolab_files_from_cloud_widget($('#compose-attachments > div')); // register some commands to skip warning message on compose page $.merge(rcmail.env.compose_commands, ['files-list', 'files-sort', 'files-search', 'files-search-reset']); @@ -74,6 +53,20 @@ window.rcmail && window.files_api && rcmail.addEventListener('init', function() rcmail.enable_command('folder-create', true); } + if (!rcmail.env.action || rcmail.env.action == 'show' || rcmail.env.action == 'preview') { + // add "attach from cloud" button for event/task dialog in mail + rcmail.addEventListener('plugin.mail2event_dialog', function() { + if (!$('#calendar-attachment-form input.fromcloud').length) + kolab_files_from_cloud_widget($('#calendar-attachment-form > div.formbuttons')); + }); + } + + kolab_files_init(); + } + else if (rcmail.task == 'calendar') { + // add "attach from cloud" button for event/task dialog + if (!rcmail.env.action) + kolab_files_from_cloud_widget($('#calendar-attachment-form > div.formbuttons')); kolab_files_init(); } else if (rcmail.task == 'files') { @@ -156,6 +149,31 @@ function kolab_files_token() return window.parent && parent.rcmail && parent.rcmail.env.files_token ? parent.rcmail.env.files_token : rcmail.env.files_token; }; +function kolab_files_from_cloud_widget(elem) +{ + var input = $('') + .attr('tabindex', $('input', elem).attr('tabindex') || 0) + .val(rcmail.gettext('kolab_files.fromcloud')) + .click(function() { kolab_files_selector_dialog(); }) + .appendTo(elem); + + if (rcmail.gui_objects.filelist) { + rcmail.file_list = new rcube_list_widget(rcmail.gui_objects.filelist, { + multiselect: true, + keyboard: true, + column_movable: false, + dblclick_time: rcmail.dblclick_time + }); + rcmail.file_list.addEventListener('select', function(o) { kolab_files_list_select(o); }) + .addEventListener('listupdate', function(e) { rcmail.triggerEvent('listupdate', e); }); + + rcmail.enable_command('files-sort', 'files-search', 'files-search-reset', true); + + rcmail.file_list.init(); + kolab_files_list_coltypes(); + } +} + // folder selection dialog function kolab_directory_selector_dialog(id) { @@ -223,6 +241,8 @@ function kolab_directory_selector_dialog(id) if (!rcmail.env.folders_loaded) { fn = function() { + rcmail.env.folder_list_selector = '#files-dialog #files-folder-list'; + rcmail.env.folder_search_selector = '#files-dialog #foldersearch'; file_api.folder_list(); rcmail.env.folders_loaded = true; }; @@ -240,7 +260,7 @@ function kolab_directory_selector_dialog(id) }, fn); // "enable" folder creation when dialog is displayed in parent window - if (rcmail.is_framed() && !parent.rcmail.folder_create) { + if (rcmail.is_framed()) { parent.rcmail.enable_command('folder-create', true); parent.rcmail.folder_create = function() { win.kolab_files_folder_create_dialog(); @@ -294,6 +314,8 @@ function kolab_files_selector_dialog() }, function() { rcmail.file_list.resize(); }); if (!rcmail.env.files_loaded) { + rcmail.env.folder_list_selector = '#files-compose-dialog #files-folder-list'; + rcmail.env.folder_search_selector = '#files-compose-dialog #foldersearch'; file_api.folder_list(); rcmail.env.files_loaded = true; } @@ -638,7 +660,7 @@ kolab_files_list_coltypes = function() rcmail.env.subject_col = null; - if ((n = $.inArray('name', rcmail.env.coltypes)) >= 0) { + if ((n = $.inArray('name', rcmail.env.file_coltypes)) >= 0) { rcmail.env.subject_col = n; list.subject_col = n; } @@ -648,7 +670,7 @@ kolab_files_list_coltypes = function() kolab_files_set_list_options = function(cols, sort_col, sort_order) { - var update = 0, i, idx, name, newcols = [], oldcols = rcmail.env.coltypes; + var update = 0, i, idx, name, newcols = [], oldcols = rcmail.env.file_coltypes; if (sort_col === undefined) sort_col = rcmail.env.sort_col; @@ -695,19 +717,19 @@ kolab_files_set_coltypes = function(list) { var i, found, name, cols = list.list.tHead.rows[0].cells; - rcmail.env.coltypes = []; + rcmail.env.file_coltypes = []; for (i=0; i= 0) +// if ((found = $.inArray('name', rcmail.env.file_coltypes)) >= 0) // rcmail.env.subject_col = found; rcmail.env.subject_col = list.subject_col; - rcmail.http_post('files/prefs', {kolab_files_list_cols: rcmail.env.coltypes}); + rcmail.http_post('files/prefs', {kolab_files_list_cols: rcmail.env.file_coltypes}); }; kolab_files_list_dblclick = function(list) @@ -1176,19 +1198,24 @@ function kolab_files_ui() return; var first, body, rows = [], - elem = $('#files-folder-list'), - searchbox = $('#foldersearch'), + list_selector = rcmail.env.folder_list_selector || '#files-folder-list', + search_selector = rcmail.env.folder_search_selector || '#foldersearch', + elem = $(list_selector), + searchbox = $(search_selector), list = $(''), - collections = !rcmail.env.action.match(/^(preview|show)$/) ? ['audio', 'video', 'image', 'document'] : []; + collections = ['audio', 'video', 'image', 'document']; // try parent window if the list element does not exist // i.e. called from dialog in parent window if (!elem.length && window.parent && parent.rcmail) { body = window.parent.document.body; - elem = $('#files-folder-list', body); - searchbox = $('#foldersearch', body); + elem = $(list_selector, body); + searchbox = $(search_selector, body); } + if (elem.data('no-collections') == true) + collections = []; + elem.html(''); this.env.folders = this.folder_list_parse(response.result && response.result.list ? response.result.list : response.result); @@ -1956,8 +1983,8 @@ function kolab_files_ui() { var c, col, row = ''; - for (c in rcmail.env.coltypes) { - c = rcmail.env.coltypes[c]; + for (c in rcmail.env.file_coltypes) { + c = rcmail.env.file_coltypes[c]; if (c == 'name') col = '' + '' + escapeHTML(data.name) + ''; diff --git a/plugins/kolab_files/lib/kolab_files_engine.php b/plugins/kolab_files/lib/kolab_files_engine.php index 15e3a999..ac064e0f 100644 --- a/plugins/kolab_files/lib/kolab_files_engine.php +++ b/plugins/kolab_files/lib/kolab_files_engine.php @@ -50,13 +50,15 @@ class kolab_files_engine { $this->plugin->add_texts('localization/'); + $templates = array(); + // set templates of Files UI and widgets if ($this->rc->task == 'mail') { - if ($this->rc->action == 'compose') { - $template = 'compose_plugin'; + if (in_array($this->rc->action, array('', 'show', 'compose'))) { + $templates[] = 'compose_plugin'; } - else if (in_array($this->rc->action, array('show', 'preview', 'get'))) { - $template = 'message_plugin'; + if (in_array($this->rc->action, array('show', 'preview', 'get'))) { + $templates[] = 'message_plugin'; if ($this->rc->action == 'get') { // add "Save as" button into attachment toolbar @@ -87,6 +89,20 @@ class kolab_files_engine } } + $list_widget = true; + } + else if ($this->rc->task == 'calendar' && !$this->rc->action) { + $list_widget = true; + $templates[] = 'compose_plugin'; + } + else if ($this->rc->task == 'files') { + $templates[] = 'files'; + + // get list of external sources + $this->get_external_storage_drivers(); + } + + if ($list_widget) { $this->folder_list_env(); $this->plugin->add_label('save', 'cancel', 'saveto', @@ -95,12 +111,6 @@ class kolab_files_engine 'folderauthtitle', 'authenticating' ); } - else if ($this->rc->task == 'files') { - $template = 'files'; - - // get list of external sources - $this->get_external_storage_drivers(); - } // add taskbar button if (empty($_REQUEST['framed'])) { @@ -115,7 +125,7 @@ class kolab_files_engine $this->plugin->include_stylesheet($this->plugin->local_skin_path().'/style.css'); - if (!empty($template)) { + if (!empty($templates)) { $collapsed_folders = (string) $this->rc->config->get('kolab_files_collapsed_folders'); $this->plugin->include_script($this->url . '/js/files_api.js'); @@ -138,9 +148,11 @@ class kolab_files_engine )); if ($this->rc->task != 'files') { - // add dialog content at the end of page body - $this->rc->output->add_footer( - $this->rc->output->parse('kolab_files.' . $template, false, false)); + // add dialog(s) content at the end of page body + foreach ($templates as $template) { + $this->rc->output->add_footer( + $this->rc->output->parse('kolab_files.' . $template, false, false)); + } } } } @@ -422,7 +434,7 @@ class kolab_files_engine $this->rc->output->add_gui_object('filelist', $attrib['id']); $this->rc->output->set_env('sort_col', $_SESSION['kolab_files_sort_col']); $this->rc->output->set_env('sort_order', $_SESSION['kolab_files_sort_order']); - $this->rc->output->set_env('coltypes', $a_show_cols); + $this->rc->output->set_env('file_coltypes', $a_show_cols); $this->rc->output->set_env('search_threads', $this->rc->config->get('kolab_files_search_threads')); $this->rc->output->include_script('list.js'); @@ -545,7 +557,7 @@ class kolab_files_engine $head = html::tag('tr', null, $head); - $this->rc->output->set_env('coltypes', $a_show_cols); + $this->rc->output->set_env('file_coltypes', $a_show_cols); $this->rc->output->command('files_list_update', $head); } @@ -980,23 +992,26 @@ class kolab_files_engine */ protected function action_attach_file() { - $files = rcube_utils::get_input_value('files', rcube_utils::INPUT_POST); - $uploadid = rcube_utils::get_input_value('uploadid', rcube_utils::INPUT_POST); - $COMPOSE_ID = rcube_utils::get_input_value('id', rcube_utils::INPUT_POST); - $COMPOSE = null; - $errors = array(); + $files = rcube_utils::get_input_value('files', rcube_utils::INPUT_POST); + $uploadid = rcube_utils::get_input_value('uploadid', rcube_utils::INPUT_POST); + $COMPOSE_ID = rcube_utils::get_input_value('id', rcube_utils::INPUT_POST); + $COMPOSE = null; + $errors = array(); + $attachments = array(); - if ($COMPOSE_ID && $_SESSION['compose_data_'.$COMPOSE_ID]) { - $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID]; - } + if ($this->rc->task == 'mail') { + if ($COMPOSE_ID && $_SESSION['compose_data_'.$COMPOSE_ID]) { + $COMPOSE =& $_SESSION['compose_data_'.$COMPOSE_ID]; + } - if (!$COMPOSE) { - die("Invalid session var!"); - } + if (!$COMPOSE) { + die("Invalid session var!"); + } - // attachment upload action - if (!is_array($COMPOSE['attachments'])) { - $COMPOSE['attachments'] = array(); + // attachment upload action + if (!is_array($COMPOSE['attachments'])) { + $COMPOSE['attachments'] = array(); + } } // clear all stored output properties (like scripts and env vars) @@ -1069,13 +1084,18 @@ class kolab_files_engine } $attachment = array( - 'path' => $path, - 'size' => $file_params['size'], - 'name' => $file_params['name'], + 'path' => $path, + 'size' => $file_params['size'], + 'name' => $file_params['name'], 'mimetype' => $file_params['type'], - 'group' => $COMPOSE_ID, + 'group' => $COMPOSE_ID, ); + if ($this->rc->task != 'mail') { + $attachments[] = $attachment; + continue; + } + $attachment = $this->rc->plugins->exec_hook('attachment_save', $attachment); if ($attachment['status'] && !$attachment['abort']) { @@ -1123,6 +1143,22 @@ class kolab_files_engine $this->rc->output->command('display_message', $this->plugin->gettext('attacherror'), 'error'); $this->rc->output->command('remove_from_attachment_list', $uploadid); } + else if ($this->rc->task == 'calendar') { + // for uploads in events/tasks we'll use its standard upload handler, + // for this we have to fake $_FILES and some other POST args + foreach ($attachments as $attach) { + $_FILES['_attachments']['tmp_name'][] = $attachment['path']; + $_FILES['_attachments']['name'][] = $attachment['name']; + $_FILES['_attachments']['size'][] = $attachment['size']; + $_FILES['_attachments']['type'][] = $attachment['mimetype']; + $_FILES['_attachments']['error'][] = null; + } + + $_GET['_uploadid'] = $uploadid; + $_GET['_id'] = $COMPOSE_ID; + + libcalendaring::get_instance()->attachment_upload(calendar::SESSION_KEY, 'cal-'); + } // send html page with JS calls as response $this->rc->output->command('auto_save_start', false); diff --git a/plugins/kolab_files/skins/larry/templates/message_plugin.html b/plugins/kolab_files/skins/larry/templates/message_plugin.html index 1192a309..df8452b0 100644 --- a/plugins/kolab_files/skins/larry/templates/message_plugin.html +++ b/plugins/kolab_files/skins/larry/templates/message_plugin.html @@ -11,7 +11,7 @@