summaryrefslogtreecommitdiff
path: root/plugins/kolab_folders
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@test90-7.test90.kolabsys.com>2011-10-27 16:24:04 (GMT)
committerAleksander Machniak <machniak@test90-7.test90.kolabsys.com>2011-10-27 16:24:04 (GMT)
commit000212dd9ad7e753aac0d9e6244bf7f565252d4f (patch)
tree0c8ada69c219321bf9d7db881cac947f874a3795 /plugins/kolab_folders
parent4a498714dd4114a46311799889f2542f4a255b9d (diff)
downloadroundcubemail-plugins-kolab-000212dd9ad7e753aac0d9e6244bf7f565252d4f.tar.gz
Added options to create default folders
Diffstat (limited to 'plugins/kolab_folders')
-rw-r--r--plugins/kolab_folders/README5
-rw-r--r--plugins/kolab_folders/config.inc.php.dist34
-rw-r--r--plugins/kolab_folders/kolab_folders.php178
3 files changed, 172 insertions, 45 deletions
diff --git a/plugins/kolab_folders/README b/plugins/kolab_folders/README
index ac4a56d..eddd5aa 100644
--- a/plugins/kolab_folders/README
+++ b/plugins/kolab_folders/README
@@ -11,14 +11,15 @@ With this plugin enabled it is possible to:
- set/get/change folder's type
- filter folders list by folder type.
- style folders list rows (in folder manager)
+ - create default folders with specified type
http://www.kolab.org/doc/kolabformat-2.0-html
2. Examples.
------------
-To get list of all folders of type 'events' use code:
+To get list of all folders of type 'event' use code:
-$RCMAIL->imap->list_mailboxes('', '*', 'events');
+$RCMAIL->imap->list_mailboxes('', '*', 'event');
Plugin provides also set_folder_type/get_folder_type methods.
diff --git a/plugins/kolab_folders/config.inc.php.dist b/plugins/kolab_folders/config.inc.php.dist
new file mode 100644
index 0000000..d907a98
--- /dev/null
+++ b/plugins/kolab_folders/config.inc.php.dist
@@ -0,0 +1,34 @@
+<?php
+
+// Default kolab-specific folders. Set values to non-empty
+// strings to create default folders of apropriate type.
+// If there is no default folder with specified type in user mailbox,
+// it will be created.
+// Note: Mail folders will be also subscribed.
+
+// Default Configuration folder
+$rcmail_config['kolab_folders_configuration_default'] = '';
+// Default Calendar folder
+$rcmail_config['kolab_folders_event_default'] = '';
+// Default Contacts (Addressbook) folder
+$rcmail_config['kolab_folders_contact_default'] = '';
+// Default Tasks folder
+$rcmail_config['kolab_folders_task_default'] = '';
+// Default Notes folder
+$rcmail_config['kolab_folders_note_default'] = '';
+// Default Journal folder
+$rcmail_config['kolab_folders_journal_default'] = '';
+
+// INBOX folder
+$rcmail_config['kolab_folders_mail_inbox'] = '';
+// Drafts folder
+$rcmail_config['kolab_folders_mail_drafts'] = '';
+// Sent folder
+$rcmail_config['kolab_folders_mail_sentitems'] = '';
+// Trash folder
+$rcmail_config['kolab_folders_mail_junkemail'] = '';
+// Others folders
+$rcmail_config['kolab_folders_mail_outbox'] = '';
+$rcmail_config['kolab_folders_mail_wastebasket'] = '';
+
+?>
diff --git a/plugins/kolab_folders/kolab_folders.php b/plugins/kolab_folders/kolab_folders.php
index 16f89b0..7622d65 100644
--- a/plugins/kolab_folders/kolab_folders.php
+++ b/plugins/kolab_folders/kolab_folders.php
@@ -27,7 +27,7 @@ class kolab_folders extends rcube_plugin
public $task = '?(?!login).*';
public $types = array('mail', 'event', 'journal', 'task', 'note', 'contact', 'configuration');
- public $mail_types = array('drafts', 'sentitems', 'outbox', 'wastebasket', 'junkemail');
+ public $mail_types = array('inbox', 'drafts', 'sentitems', 'outbox', 'wastebasket', 'junkemail');
private $rc;
const CTYPE_KEY = '/shared/vendor/kolab/folder-type';
@@ -69,7 +69,7 @@ class kolab_folders extends rcube_plugin
}
// get folders types
- $folderdata = $this->get_folder_type_list($args['root'].$args['name']);
+ $folderdata = $this->get_folder_type_list($args['root'].$args['name'], true);
if (!is_array($folderdata)) {
$args['folders'] = false;
@@ -82,9 +82,9 @@ class kolab_folders extends rcube_plugin
if ($args['mode'] == 'LIST' && $filter != 'mail'
&& $args['root'] == '' && $args['name'] == '*'
) {
- foreach ($folderdata as $folder => $data) {
- if (!preg_match($regexp, $data[kolab_folders::CTYPE_KEY])) {
- unset ($folderdata[$folder]);
+ foreach ($folderdata as $folder => $type) {
+ if (!preg_match($regexp, $type)) {
+ unset($folderdata[$folder]);
}
}
$args['folders'] = array_keys($folderdata);
@@ -107,12 +107,11 @@ class kolab_folders extends rcube_plugin
// Filter folders list
foreach ($args['folders'] as $idx => $folder) {
- $data = $folderdata[$folder];
- // Empty data => mail
- if ($filter == 'mail' && empty($data)) {
+ $type = $folderdata[$folder];
+ if ($filter == 'mail' && empty($type)) {
continue;
}
- if (empty($data) || !preg_match($regexp, $data[kolab_folders::CTYPE_KEY])) {
+ if (empty($type) || !preg_match($regexp, $type)) {
unset($args['folders'][$idx]);
}
}
@@ -143,10 +142,7 @@ class kolab_folders extends rcube_plugin
for ($i=1, $cnt=$table->size(); $i<=$cnt; $i++) {
$attrib = $table->get_row_attribs($i);
$folder = $attrib['foldername']; // UTF7-IMAP
- $data = $folderdata[$folder];
-
- if (!empty($data))
- $type = $data[kolab_folders::CTYPE_KEY];
+ $type = $folderdata[$folder];
if (!$type)
$type = 'mail';
@@ -442,30 +438,43 @@ class kolab_folders extends rcube_plugin
/**
* Returns list of folder(s) type(s)
*
- * @param string $mbox Folder name or pattern
+ * @param string $mbox Folder name or pattern
+ * @param bool $defaults Enables creation of configured default folders
*
* @return array List of folders data, indexed by folder name
*/
- function get_folder_type_list($mbox)
+ function get_folder_type_list($mbox, $create_defaults = false)
{
// Use mailboxes. prefix so the cache will be cleared by core
// together with other mailboxes-related cache data
- $cache_key = 'mailboxes.types.'.$mbox;
+ $cache_key = 'mailboxes.folder-type.'.$mbox;
// get cached metadata
$metadata = $this->rc->imap->get_cache($cache_key);
- if (is_array($metadata)) {
- return $metadata;
- }
- $metadata = $this->rc->imap->get_metadata($mbox, kolab_folders::CTYPE_KEY);
+ if (!is_array($metadata)) {
+ $metadata = $this->rc->imap->get_metadata($mbox, kolab_folders::CTYPE_KEY);
+ $need_update = true;
+ }
if (!is_array($metadata)) {
return false;
}
+ // make the result more flat
+ if ($need_update) {
+ $metadata = array_map('implode', $metadata);
+ }
+
+ // create default folders if needed
+ if ($create_defaults) {
+ $this->create_default_folders($metadata, $cache_key);
+ }
+
// write mailboxlist to cache
- $this->rc->imap->update_cache($cache_key, $metadata);
+ if ($need_update) {
+ $this->rc->imap->update_cache($cache_key, $metadata);
+ }
return $metadata;
}
@@ -487,30 +496,19 @@ class kolab_folders extends rcube_plugin
$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;
+ // get all folders of specified type
+ $folderdata = array_intersect($folderdata, array($type));
+ unset($folders[0]);
- 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;
+ foreach ($folderdata as $folder => $data) {
+ // check if folder is in personal namespace
+ foreach (array('shared', 'other') as $nskey) {
+ if (!empty($namespace[$nskey])) {
+ foreach ($namespace[$nskey] as $ns) {
+ if ($ns[0] && substr($folder, 0, strlen($ns[0])) == $ns[0]) {
+ continue 3;
+ }
}
}
}
@@ -548,4 +546,98 @@ class kolab_folders extends rcube_plugin
{
unset($_SESSION['horde_session_objects']['kolab_folderlist']);
}
+
+ /**
+ * Creates default folders if they doesn't exist
+ */
+ private function create_default_folders(&$folderdata, $cache_key = null)
+ {
+ $namespace = $this->rc->imap->get_namespace();
+ $defaults = array();
+ $need_update = false;
+
+ // Find personal namespace prefix
+ if (is_array($namespace['personal']) && count($namespace['personal']) == 1) {
+ $prefix = $namespace['personal'][0][0];
+ }
+ else {
+ $prefix = '';
+ }
+
+ $this->load_config();
+
+ // get configured defaults
+ foreach ($this->types as $type) {
+ $subtypes = $type == 'mail' ? $this->mail_types : array('default');
+ foreach ($subtypes as $subtype) {
+ $opt_name = 'kolab_folders_' . $type . '_' . $subtype;
+ if ($folder = $this->rc->config->get($opt_name)) {
+ // convert configuration value to UTF7-IMAP charset
+ $folder = rcube_charset_convert($folder, RCMAIL_CHARSET, 'UTF7-IMAP');
+ // and namespace prefix if needed
+ if ($prefix && strpos($folder, $prefix) === false && $folder != 'INBOX') {
+ $folder = $prefix . $folder;
+ }
+ $defaults[$type . '.' . $subtype] = $folder;
+ }
+ }
+ }
+
+ // find default folders
+ foreach ($defaults as $type => $foldername) {
+ // folder exists, do nothing
+ if (!empty($folderdata[$foldername])) {
+ continue;
+ }
+
+ // special case, need to set type only
+ if ($foldername == 'INBOX' || $type == 'mail.inbox') {
+ $this->set_folder_type($foldername, 'mail.inbox');
+ continue;
+ }
+
+ // get all folders of specified type
+ $folders = array_intersect($folderdata, array($type));
+ unset($folders[0]);
+
+ // find folders in personal namespace
+ foreach ($folders as $folder) {
+ if ($folder) {
+ foreach (array('shared', 'other') as $nskey) {
+ if (!empty($namespace[$nskey])) {
+ foreach ($namespace[$nskey] as $ns) {
+ if ($ns[0] && substr($folder, 0, strlen($ns[0])) == $ns[0]) {
+ continue 3;
+ }
+ }
+ }
+ }
+ }
+
+ // got folder in personal namespace
+ continue 2;
+ }
+
+ list($type1, $type2) = explode('.', $type);
+
+ // create folder
+ if ($type1 != 'mail' || !$this->rc->imap->mailbox_exists($foldername)) {
+ $this->rc->imap->create_mailbox($foldername, $type1 == 'mail');
+ }
+
+ // set type
+ $result = $this->set_folder_type($foldername, $type);
+
+ // add new folder to the result
+ if ($result) {
+ $folderdata[$foldername] = $type;
+ $need_update = true;
+ }
+ }
+
+ // update cache
+ if ($need_update && $cache_key) {
+ $this->rc->imap->update_cache($cache_key, $folderdata);
+ }
+ }
}