From 67cb017b0cdda3c2dcc7d9434fdbaca5f4ead938 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Sun, 20 Jan 2013 16:28:02 +0100 Subject: Extend archive plugin with user-configurable options to move messages into subfolders according to their date, sender or originating folder --- plugins/archive/archive.js | 23 +++++- plugins/archive/archive.php | 136 +++++++++++++++++++++++++++++++-- plugins/archive/localization/en_US.inc | 11 ++- plugins/archive/localization/nb_NB.inc | 21 ----- plugins/archive/package.xml | 42 +++++++++- 5 files changed, 199 insertions(+), 34 deletions(-) delete mode 100644 plugins/archive/localization/nb_NB.inc diff --git a/plugins/archive/archive.js b/plugins/archive/archive.js index af2b0d26d..eee41d336 100644 --- a/plugins/archive/archive.js +++ b/plugins/archive/archive.js @@ -1,6 +1,6 @@ /* * Archive plugin script - * @version @package_version@ + * @version 2.0 */ function rcmail_archive(prop) @@ -8,8 +8,19 @@ function rcmail_archive(prop) if (!rcmail.env.uid && (!rcmail.message_list || !rcmail.message_list.get_selection().length)) return; - if (rcmail.env.mailbox != rcmail.env.archive_folder) - rcmail.command('moveto', rcmail.env.archive_folder); + if (rcmail.env.mailbox.indexOf(rcmail.env.archive_folder) != 0) { + if (!rcmail.env.archive_type) { + // simply move to archive folder (if no partition type is set) + rcmail.command('moveto', rcmail.env.archive_folder); + } + else { + // let the server sort the messages to the according subfolders + rcmail.http_post( + 'plugin.move2archive', + { _uid: rcmail.message_list.get_selection().join(','), _mbox: rcmail.env.mailbox } + ); + } + } } // callback for app-onload event @@ -29,6 +40,12 @@ if (window.rcmail) { var li; if (rcmail.env.archive_folder && (li = rcmail.get_folder_li(rcmail.env.archive_folder, '', true))) $(li).addClass('archive'); + + // callback for server response + rcmail.addEventListener('plugin.move2archive_response', function(result) { + if (result.update) + rcmail.command('checkmail'); // refresh list + }); }) } diff --git a/plugins/archive/archive.php b/plugins/archive/archive.php index 9b03cb186..38b4f9fcb 100644 --- a/plugins/archive/archive.php +++ b/plugins/archive/archive.php @@ -6,7 +6,7 @@ * Plugin that adds a new button to the mailbox toolbar * to move messages to a (user selectable) archive folder. * - * @version @package_version@ + * @version 2.0 * @license GNU GPLv3+ * @author Andre Rodier, Thomas Bruederli */ @@ -45,14 +45,19 @@ class archive extends rcube_plugin // register hook to localize the archive folder $this->add_hook('render_mailboxlist', array($this, 'render_mailboxlist')); - // set env variable for client + // set env variables for client $rcmail->output->set_env('archive_folder', $archive_folder); + $rcmail->output->set_env('archive_type', $rcmail->config->get('archive_type','')); // add archive folder to the list of default mailboxes if (($default_folders = $rcmail->config->get('default_folders')) && !in_array($archive_folder, $default_folders)) { $default_folders[] = $archive_folder; $rcmail->config->set('default_folders', $default_folders); - } + } + } + else if ($rcmail->task == 'mail') { + // handler for ajax request + $this->register_action('plugin.move2archive', array($this, 'move_messages')); } else if ($rcmail->task == 'settings') { $dont_override = $rcmail->config->get('dont_override', array()); @@ -62,7 +67,10 @@ class archive extends rcube_plugin } } } - + + /** + * Hook to give the archive folder a localized name in the mailbox list + */ function render_mailboxlist($p) { $rcmail = rcmail::get_instance(); @@ -80,7 +88,10 @@ class archive extends rcube_plugin return $p; } - function _mod_folder_name(&$list, $folder, $new_name) + /** + * Helper method to find the archive folder in the mailbox tree + */ + private function _mod_folder_name(&$list, $folder, $new_name) { foreach ($list as $idx => $item) { if ($item['id'] == $folder) { @@ -93,6 +104,100 @@ class archive extends rcube_plugin return false; } + /** + * Plugin action to move the submitted list of messages to the archive subfolders + * according to the user settings and their headers. + */ + function move_messages() + { + $rcmail = rcmail::get_instance(); + $this->add_texts('localization'); + + $storage = $rcmail->get_storage(); + $storage->set_folder(($current_mbox = rcube_utils::get_input_value('_mbox', RCUBE_INPUT_POST))); + + $delimiter = $storage->get_hierarchy_delimiter(); + $archive_folder = $rcmail->config->get('archive_mbox'); + $archive_type = $rcmail->config->get('archive_type', ''); + + $result = array('reload' => false, 'update' => false, 'errors' => array()); + + $uids = explode(',', rcube_utils::get_input_value('_uid', RCUBE_INPUT_POST)); + foreach ($uids as $uid) { + if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) { + continue; + } + + $subfolder = null; + switch ($archive_type) { + case 'year': + $subfolder = $rcmail->format_date($message->timestamp, 'Y'); + break; + + case 'month': + $subfolder = $rcmail->format_date($message->timestamp, 'Y') . $delimiter . $rcmail->format_date($message->timestamp, 'm'); + break; + + case 'folder': + $subfolder = $current_mbox; + break; + + case 'sender': + $from = $message->get('from'); + if (preg_match('/[\b<](.+@.+)[\b>]/i', $from, $m)) { + $subfolder = $m[1]; + } + else { + $subfolder = $this->gettext('unkownsender'); + } + + // replace reserved characters in folder name + $repl = $delimiter == '-' ? '_' : '-'; + $replacements[$delimiter] = $repl; + $replacements['.'] = $repl; // some IMAP server do not allow . characters + $subfolder = strtr($subfolder, $replacements); + break; + + default: + $subfolder = ''; + break; + } + + // compose full folder path + $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : ''); + + // create archive subfolder if it doesn't yet exist + if (!$storage->folder_exists($folder, false)) { + if ($storage->create_folder($folder, true)) + $result['reload'] = true; + } + + // move message to target folder + if ($storage->move_message(array($uid), $folder)) { + $result['update'] = true; + } + else { + $result['errors'][] = $uid; + } + } // end for + + // send response + if ($result['errors']) { + $rcmail->output->show_message($this->gettext('archiveerror'), 'warning'); + } + if ($result['reload']) { + $rcmail->output->show_message($this->gettext('archivedreload'), 'confirmation'); + } + else if ($result['update']) { + $rcmail->output->show_message($this->gettext('archived'), 'confirmation'); + } + + $rcmail->output->command('plugin.move2archive_response', $result); + } + + /** + * Hook to inject plugin-specific user settings + */ function prefs_table($args) { global $CURR_SECTION; @@ -113,15 +218,36 @@ class archive extends rcube_plugin 'title' => $this->gettext('archivefolder'), 'content' => $select->show($rcmail->config->get('archive_mbox'), array('name' => "_archive_mbox")) ); + + // add option for structuring the archive folder + $archive_type = new html_select(array('name' => '_archive_type', 'id' => 'ff_archive_type')); + $archive_type->add($this->gettext('none'), ''); + $archive_type->add($this->gettext('archivetypeyear'), 'year'); + $archive_type->add($this->gettext('archivetypemonth'), 'month'); + $archive_type->add($this->gettext('archivetypesender'), 'sender'); + $archive_type->add($this->gettext('archivetypefolder'), 'folder'); + + $args['blocks']['archive'] = array( + 'name' => Q(rcube_label('settingstitle', 'archive')), + 'options' => array('archive_type' => array( + 'title' => $this->gettext('archivetype'), + 'content' => $archive_type->show($rcmail->config->get('archive_type')) + ) + ) + ); } return $args; } + /** + * Hook to save plugin-specific user settings + */ function save_prefs($args) { if ($args['section'] == 'folders') { $args['prefs']['archive_mbox'] = rcube_utils::get_input_value('_archive_mbox', rcube_utils::INPUT_POST); + $args['prefs']['archive_type'] = rcube_utils::get_input_value('_archive_type', rcube_utils::INPUT_POST); return $args; } } diff --git a/plugins/archive/localization/en_US.inc b/plugins/archive/localization/en_US.inc index fade70852..d3714c118 100644 --- a/plugins/archive/localization/en_US.inc +++ b/plugins/archive/localization/en_US.inc @@ -5,7 +5,7 @@ | plugins/archive/localization/.inc | | | | Localization file of the Roundcube Webmail Archive plugin | - | Copyright (C) 2012, The Roundcube Dev Team | + | Copyright (C) 2013, The Roundcube Dev Team | | | | Licensed under the GNU General Public License version 3 or | | any later version with exceptions for skins & plugins. | @@ -20,6 +20,15 @@ $labels = array(); $labels['buttontext'] = 'Archive'; $labels['buttontitle'] = 'Archive this message'; $labels['archived'] = 'Successfully archived'; +$labels['archivedreload'] = 'Successfully archived. Reload the page to see the new archive folders.'; +$labels['archiveerror'] = 'Some messages could not be archived'; $labels['archivefolder'] = 'Archive'; +$labels['settingstitle'] = 'Archive'; +$labels['archivetype'] = 'Divide archive by'; +$labels['archivetypeyear'] = 'Year (e.g. Archive/2012)'; +$labels['archivetypemonth'] = 'Month (e.g. Archive/2012/06)'; +$labels['archivetypefolder'] = 'Original folder'; +$labels['archivetypesender'] = 'Sender email'; +$labels['unkownsender'] = 'unknown'; ?> diff --git a/plugins/archive/localization/nb_NB.inc b/plugins/archive/localization/nb_NB.inc deleted file mode 100644 index 46e49aba0..000000000 --- a/plugins/archive/localization/nb_NB.inc +++ /dev/null @@ -1,21 +0,0 @@ - | - +-----------------------------------------------------------------------+ -*/ - -$labels = array(); -$labels['buttontext'] = 'Arkiv'; -$labels['archivefolder'] = 'Arkiv'; -$labels['buttontitle'] = 'Arkiver meldingen'; -$labels['archived'] = 'Arkivert'; - diff --git a/plugins/archive/package.xml b/plugins/archive/package.xml index 1aeffaf41..62a009a99 100644 --- a/plugins/archive/package.xml +++ b/plugins/archive/package.xml @@ -6,17 +6,17 @@ archive pear.roundcube.net Archive feature for Roundcube - This adds a button to move the selected messages to an archive folder. The folder can be selected in the settings panel. + This adds a button to move the selected messages to an archive folder. The folder (and the optional structure of subfolders) can be selected in the settings panel. Thomas Bruederli thomasb roundcube@gmail.com yes - 2011-11-23 + 2013-01-20 - 1.6 - 1.6 + 2.0 + 2.0 stable @@ -34,21 +34,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3