From fcbcab7f3676fd448b078825f7fa4395f3478c9d Mon Sep 17 00:00:00 2001 From: "Aleksander Machniak (Kolab Systems)" Date: Thu, 7 Jul 2011 15:50:50 +0200 Subject: [PATCH] There can be only one default folder of specified type --- plugins/kolab_folders/kolab_folders.php | 78 ++++++++++++++++++-- plugins/kolab_folders/localization/en_US.inc | 2 + plugins/kolab_folders/localization/pl_PL.inc | 1 + 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/plugins/kolab_folders/kolab_folders.php b/plugins/kolab_folders/kolab_folders.php index ae98b2f7..059c5dd0 100644 --- a/plugins/kolab_folders/kolab_folders.php +++ b/plugins/kolab_folders/kolab_folders.php @@ -25,8 +25,6 @@ class kolab_folders extends rcube_plugin { - public $task = '?(?!login).*'; - public $types = array('mail', 'event', 'journal', 'task', 'note', 'contact'); public $mail_types = array('inbox', 'drafts', 'sentitems', 'outbox', 'wastebasket', 'junkemail'); private $rc; @@ -182,6 +180,8 @@ class kolab_folders extends rcube_plugin // Get type of the folder or the parent if (strlen($mbox)) { list($ctype, $subtype) = $this->get_folder_type($mbox); + if (strlen($args['parent_name']) && $subtype == 'default') + $subtype = ''; // there can be only one } if (!$ctype) { @@ -234,8 +234,23 @@ class kolab_folders extends rcube_plugin return $args; } - // @TODO: There can be only one default folder of specified type. Add check for this. + // load translations + $this->add_texts('localization/', false); + + // Skip folder creation/rename in core + // @TODO: Maybe we should provide folder_create_after and folder_update_after hooks? + // Using create_mailbox/rename_mailbox here looks bad + $args['abort'] = true; + + // There can be only one default folder of specified type if ($subtype == 'default') { + $default = $this->get_default_folder($ctype); + + if ($default !== null && $old_mbox != $default) { + $args['result'] = false; + $args['message'] = $this->gettext('defaultfolderexists'); + return $args; + } } // Subtype sanity-checks else if ($subtype && ($ctype != 'mail' || !in_array($subtype, $this->mail_types))) { @@ -277,11 +292,6 @@ class kolab_folders extends rcube_plugin $args['record']['class'] = self::folder_class_name($ctype); $args['record']['subscribe'] = $subscribe; - - // Skip folder creation/rename in core - // @TODO: Maybe we should provide folder_create_after and folder_update_after hooks? - // Using create_mailbox/rename_mailbox here looks bad - $args['abort'] = true; $args['result'] = $result; return $args; @@ -396,6 +406,58 @@ class kolab_folders extends rcube_plugin return $metadata; } + /** + * Returns the name of default folder + * + * @param string $type Folder type + * + * @return string Folder name + */ + function get_default_folder($type) + { + $folderdata = $this->get_folder_type_list('*'); + + if (!is_array($folderdata)) { + return null; + } + + $type .= '.default'; + $namespace = $this->rc->imap->get_namespace(); + $delimiter = $this->rc->imap->get_hierarchy_delimiter(); + + foreach ($folderdata as $folder => $data) { + if ($data[kolab_folders::CTYPE_KEY] != $type) { + unset ($folderdata[$folder]); + continue; + } + + // folder found, check if it is in personal namespace + $fname = $folder . $delimiter; + + if (!empty($namespace['other'])) { + foreach ($namespace['other'] as $item) { + if ($item[0] === $fname) { + unset ($folderdata[$folder]); + continue 2; + } + } + } + if (!empty($namespace['shared'])) { + foreach ($namespace['shared'] as $item) { + if ($item[0] === $fname) { + unset ($folderdata[$folder]); + continue 2; + } + } + } + + // There can be only one default folder of specified type + return $folder; + } + + return null; + } + /** * Returns CSS class name for specified folder type * diff --git a/plugins/kolab_folders/localization/en_US.inc b/plugins/kolab_folders/localization/en_US.inc index 79e2e805..86040eb8 100644 --- a/plugins/kolab_folders/localization/en_US.inc +++ b/plugins/kolab_folders/localization/en_US.inc @@ -18,4 +18,6 @@ $labels['outbox'] = 'Outbox'; $labels['wastebasket'] = 'Trash'; $labels['junkemail'] = 'Junk'; +$messages['defaultfolderexists'] = 'There is already default folder of specified type'; + ?> diff --git a/plugins/kolab_folders/localization/pl_PL.inc b/plugins/kolab_folders/localization/pl_PL.inc index 6dd4772a..8bd00e65 100644 --- a/plugins/kolab_folders/localization/pl_PL.inc +++ b/plugins/kolab_folders/localization/pl_PL.inc @@ -15,5 +15,6 @@ $labels['sentitems'] = 'Wysłane'; $labels['outbox'] = 'Wychodzące'; $labels['wastebasket'] = 'Kosz'; $labels['junkemail'] = 'Spam'; +$messages['defaultfolderexists'] = 'Folder domyślny dla podanego typu już istnieje'; ?>