4 +-----------------------------------------------------------------------+
5 | program/steps/settings/manage_folders.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Provide functionality to create/delete/rename folders |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: manage_folders.inc 2505 2009-05-20 09:55:15Z alec $
22 // WARNING: folder names in UI are encoded with RCMAIL_CHARSET
24 // init IMAP connection
25 $RCMAIL->imap_init(true);
27 // subscribe to one or more mailboxes
28 if ($RCMAIL->action=='subscribe')
30 if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF7-IMAP'))
31 $IMAP->subscribe(array($mbox));
34 // unsubscribe one or more mailboxes
35 else if ($RCMAIL->action=='unsubscribe')
37 if ($mbox = get_input_value('_mbox', RCUBE_INPUT_POST, false, 'UTF7-IMAP'))
38 $IMAP->unsubscribe(array($mbox));
41 // create a new mailbox
42 else if ($RCMAIL->action=='create-folder')
44 if (!empty($_POST['_name']))
46 $name = trim(get_input_value('_name', RCUBE_INPUT_POST, FALSE, 'UTF7-IMAP'));
47 $create = $IMAP->create_mailbox($name, TRUE);
50 if ($create && $OUTPUT->ajax_call)
52 $delimiter = $IMAP->get_hierarchy_delimiter();
53 $folderlist = $IMAP->list_unsubscribed();
54 $index = array_search($create, $folderlist);
55 $before = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF7-IMAP') : false;
57 $create = rcube_charset_convert($create, 'UTF7-IMAP');
58 $foldersplit = explode($delimiter, $create);
59 $display_create = str_repeat(' ', substr_count($create, $delimiter)) . $foldersplit[count($foldersplit)-1];
61 $OUTPUT->command('add_folder_row', $create, $display_create, false, $before);
65 $OUTPUT->show_message('errorsaving', 'error');
70 else if ($RCMAIL->action=='rename-folder')
72 if (!empty($_POST['_folder_oldname']) && !empty($_POST['_folder_newname']))
74 $name_utf8 = trim(get_input_value('_folder_newname', RCUBE_INPUT_POST));
75 $oldname_utf8 = get_input_value('_folder_oldname', RCUBE_INPUT_POST);
76 $name = rcube_charset_convert($name_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
77 $oldname = rcube_charset_convert($oldname_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
79 $rename = $IMAP->rename_mailbox($oldname, $name);
82 if ($rename && $OUTPUT->ajax_call)
84 $folderlist = $IMAP->list_unsubscribed();
85 $delimiter = $IMAP->get_hierarchy_delimiter();
87 $regexp = '/^' . preg_quote($rename . $delimiter, '/') . '/';
90 for ($x=sizeof($folderlist)-1; $x>=0; $x--)
92 if (preg_match($regexp, $folderlist[$x]))
94 $oldfolder = $oldname . $delimiter . preg_replace($regexp, '', $folderlist[$x]);
95 $foldersplit = explode($delimiter, $folderlist[$x]);
96 $level = count($foldersplit) - 1;
97 $display_rename = str_repeat(' ', $level)
98 . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
100 $before = isset($folderlist[$x+1]) ? rcube_charset_convert($folderlist[$x+1], 'UTF7-IMAP') : false;
102 $OUTPUT->command('replace_folder_row', rcube_charset_convert($oldfolder, 'UTF7-IMAP'),
103 rcube_charset_convert($folderlist[$x], 'UTF7-IMAP'), $display_rename, $before);
107 $foldersplit = explode($delimiter, $rename);
108 $level = count($foldersplit) - 1;
109 $display_rename = str_repeat(' ', $level) . rcube_charset_convert($foldersplit[$level], 'UTF7-IMAP');
110 $index = array_search($rename, $folderlist);
111 $before = $index !== false && isset($folderlist[$index+1]) ? rcube_charset_convert($folderlist[$index+1], 'UTF7-IMAP') : false;
113 $OUTPUT->command('replace_folder_row', $oldname_utf8, rcube_charset_convert($rename, 'UTF7-IMAP'), $display_rename, $before);
114 $OUTPUT->command('reset_folder_rename');
116 else if (!$rename && $OUTPUT->ajax_call)
118 $OUTPUT->command('reset_folder_rename');
119 $OUTPUT->show_message('errorsaving', 'error');
122 $OUTPUT->show_message('errorsaving', 'error');
125 // delete an existing IMAP mailbox
126 else if ($RCMAIL->action=='delete-folder')
128 $a_mboxes = $IMAP->list_unsubscribed();
129 $delimiter = $IMAP->get_hierarchy_delimiter();
131 $mboxes_utf8 = get_input_value('_mboxes', RCUBE_INPUT_POST);
132 $mboxes = rcube_charset_convert($mboxes_utf8, RCMAIL_CHARSET, 'UTF7-IMAP');
135 $deleted = $IMAP->delete_mailbox(array($mboxes));
137 if ($OUTPUT->ajax_call && $deleted)
139 $OUTPUT->command('remove_folder_row', $mboxes_utf8);
140 foreach ($a_mboxes as $mbox)
142 if (preg_match('/^'. preg_quote($mboxes.$delimiter, '/') .'/', $mbox))
144 $OUTPUT->command('remove_folder_row', rcube_charset_convert($mbox, 'UTF7-IMAP'));
147 $OUTPUT->show_message('folderdeleted', 'confirmation');
151 $OUTPUT->show_message('errorsaving', 'error');
155 if ($OUTPUT->ajax_call)
159 // build table with all folders listed by server
160 function rcube_subscription_form($attrib)
162 global $IMAP, $CONFIG, $OUTPUT;
164 list($form_start, $form_end) = get_form_tags($attrib, 'folders');
165 unset($attrib['form']);
168 $attrib['id'] = 'rcmSubscriptionlist';
170 $table = new html_table();
173 $table->add_header('name', rcube_label('foldername'));
174 $table->add_header('msgcount', rcube_label('messagecount'));
175 $table->add_header('subscribed', rcube_label('subscribed'));
176 $table->add_header('rename', ' ');
177 $table->add_header('delete', ' ');
180 // get folders from server
181 $IMAP->clear_cache('mailboxes');
183 $a_unsubscribed = $IMAP->list_unsubscribed();
184 $a_subscribed = $IMAP->list_mailboxes();
185 $delimiter = $IMAP->get_hierarchy_delimiter();
186 $a_js_folders = $seen_folders = $list_folders = array();
188 // pre-process folders list
189 foreach ($a_unsubscribed as $i => $folder) {
190 $foldersplit = explode($delimiter, $folder);
191 $name = rcube_charset_convert(array_pop($foldersplit), 'UTF7-IMAP');
192 $parent_folder = join($delimiter, $foldersplit);
193 $level = count($foldersplit);
195 // add any necessary "virtual" parent folders
196 if ($parent_folder && !$seen[$parent_folder]) {
197 for ($i=1; $i<=$level; $i++) {
198 $ancestor_folder = join($delimiter, array_slice($foldersplit, 0, $i));
199 if ($ancestor_folder && !$seen[$ancestor_folder]++) {
200 $ancestor_name = rcube_charset_convert($foldersplit[$i-1], 'UTF7-IMAP');
201 $list_folders[] = array('id' => $ancestor_folder, 'name' => $ancestor_name, 'level' => $i-1, 'virtual' => true);
206 $list_folders[] = array('id' => $folder, 'name' => $name, 'level' => $level);
210 $checkbox_subscribe = new html_checkbox(array(
211 'name' => '_subscribed[]',
212 'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)",
215 if (!empty($attrib['deleteicon']))
216 $del_button = html::img(array('src' => $CONFIG['skin_path'] . $attrib['deleteicon'], 'alt' => rcube_label('delete')));
218 $del_button = rcube_label('delete');
220 if (!empty($attrib['renameicon']))
221 $edit_button = html::img(array('src' => $CONFIG['skin_path'] . $attrib['renameicon'], 'alt' => rcube_label('rename')));
223 $edit_button = rcube_label('rename');
225 // create list of available folders
226 foreach ($list_folders as $i => $folder) {
228 $subscribed = in_array($folder['id'], $a_subscribed);
229 $protected = ($CONFIG['protect_default_folders'] == true && in_array($folder['id'], $CONFIG['default_imap_folders']));
230 $classes = array($i%2 ? 'even' : 'odd');
231 $folder_js = JQ($folder['id']);
232 $display_folder = str_repeat(' ', $folder['level']) . ($protected ? rcmail_localize_foldername($folder['id']) : $folder['name']);
233 $folder_utf8 = rcube_charset_convert($folder['id'], 'UTF7-IMAP');
235 if ($folder['virtual'])
236 $classes[] = 'virtual';
238 $table->add_row(array('id' => 'rcmrow'.$idx, 'class' => join(' ', $classes)));
240 $table->add('name', Q($display_folder));
241 $table->add('msgcount', ($folder['virtual'] ? '' : $IMAP->messagecount($folder['id'])));
242 $table->add('subscribed', ($protected || $folder['virtual']) ? ($subscribed ? ' •' : ' ') :
243 $checkbox_subscribe->show(($subscribed ? $folder_utf8 : ''), array('value' => $folder_utf8)));
245 // add rename and delete buttons
246 if (!$protected && !$folder['virtual']) {
247 $table->add('rename', html::a(array('href' => "#rename", 'title' => rcube_label('renamefolder')), $edit_button));
248 $table->add('delete', html::a(array('href' => "#delete", 'title' => rcube_label('deletefolder')), $del_button));
251 $table->add('rename', ' ');
252 $table->add('delete', ' ');
255 $a_js_folders['rcmrow'.$idx] = array($folder_utf8, $display_folder, $protected || $folder['virtual']);
258 rcmail::get_instance()->plugins->exec_hook('manage_folders', array('table'=>$table));
260 $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
261 $OUTPUT->set_env('subscriptionrows', $a_js_folders);
262 $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
263 $OUTPUT->set_env('delimiter', $delimiter);
265 return $form_start . $table->show($attrib) . $form_end;
269 function rcube_create_folder_form($attrib)
273 list($form_start, $form_end) = get_form_tags($attrib, 'create-folder');
274 unset($attrib['form']);
276 if ($attrib['hintbox'])
277 $OUTPUT->add_gui_object('createfolderhint', $attrib['hintbox']);
279 // return the complete edit form as table
280 $out = "$form_start\n";
282 $input = new html_inputfield(array('name' => '_folder_name'));
283 $out .= $input->show();
285 if (get_boolean($attrib['button']))
287 $button = new html_inputfield(array('type' => 'button',
288 'value' => rcube_label('create'),
289 'onclick' => JS_OBJECT_NAME.".command('create-folder',this.form)"));
290 $out .= $button->show();
293 $out .= "\n$form_end";
298 function rcube_rename_folder_form($attrib)
300 global $CONFIG, $IMAP;
302 list($form_start, $form_end) = get_form_tags($attrib, 'rename-folder');
303 unset($attrib['form']);
305 // return the complete edit form as table
306 $out = "$form_start\n";
308 $a_unsubscribed = $IMAP->list_unsubscribed();
309 $select_folder = new html_select(array('name' => '_folder_oldname', 'id' => 'rcmfd_oldfolder'));
311 foreach ($a_unsubscribed as $i => $folder)
313 if ($CONFIG['protect_default_folders'] == TRUE && in_array($folder,$CONFIG['default_imap_folders']))
316 $select_folder->add($folder);
319 $out .= $select_folder->show();
322 $inputtwo = new html_inputfield(array('name' => '_folder_newname'));
323 $out .= $inputtwo->show();
325 if (get_boolean($attrib['button']))
327 $button = new html_inputfield(array('type' => 'button',
328 'value' => rcube_label('rename'),
329 'onclick' => JS_OBJECT_NAME.".command('rename-folder',this.form)"));
330 $out .= $button->show();
333 $out .= "\n$form_end";
338 $OUTPUT->set_pagetitle(rcube_label('folders'));
339 $OUTPUT->include_script('list.js');
341 // register UI objects
342 $OUTPUT->add_handlers(array(
343 'foldersubscription' => 'rcube_subscription_form',
344 'createfolder' => 'rcube_create_folder_form',
345 'renamefolder' => 'rcube_rename_folder_form'
348 // add some labels to client
349 $OUTPUT->add_label('deletefolderconfirm','addsubfolderhint','forbiddencharacter','folderdeleting','folderrenaming','foldercreating','foldermoving');
351 $OUTPUT->send('managefolders');