4 +-----------------------------------------------------------------------+
5 | program/steps/settings/manage_folders.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2008, 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 1159 2008-02-29 11:49:09Z thomasb $
22 // init IMAP connection
23 rcmail_imap_init(TRUE);
25 $OUTPUT->include_script('list.js');
28 // subscribe to one or more mailboxes
29 if ($_action=='subscribe')
31 if ($mboxes = get_input_value('_mboxes', RCUBE_INPUT_POST))
32 $IMAP->subscribe($mboxes);
34 if ($OUTPUT->ajax_call)
35 $OUTPUT->remote_response('// subscribed');
38 // unsubscribe one or more mailboxes
39 else if ($_action=='unsubscribe')
41 if ($mboxes = get_input_value('_mboxes', RCUBE_INPUT_POST))
42 $IMAP->unsubscribe($mboxes);
44 if ($OUTPUT->ajax_call)
45 $OUTPUT->remote_response('// unsubscribed');
48 // create a new mailbox
49 else if ($_action=='create-folder')
51 if (!empty($_POST['_name']))
52 $create = $IMAP->create_mailbox(trim(get_input_value('_name', RCUBE_INPUT_POST, FALSE, 'UTF-7')), TRUE);
54 if ($create && $OUTPUT->ajax_call)
56 $delimiter = $IMAP->get_hierarchy_delimiter();
57 $foldersplit = explode($delimiter, $create);
58 $display_create = str_repeat(' ', substr_count($create, $delimiter)) . rcube_charset_convert($foldersplit[count($foldersplit)-1], 'UTF-7');
59 $OUTPUT->command('add_folder_row', $create, $display_create);
62 else if (!$create && $OUTPUT->ajax_call)
64 $OUTPUT->show_message('errorsaving', 'error');
65 $OUTPUT->remote_response();
68 $OUTPUT->show_message('errorsaving', 'error');
72 else if ($_action=='rename-folder')
74 $a_mboxes = array_unique(array_merge($IMAP->list_mailboxes(), $IMAP->list_unsubscribed()));
75 $delimiter = $IMAP->get_hierarchy_delimiter();
77 if (!empty($_POST['_folder_oldname']) && !empty($_POST['_folder_newname']))
78 $rename = $IMAP->rename_mailbox(($oldname = get_input_value('_folder_oldname', RCUBE_INPUT_POST)), trim(get_input_value('_folder_newname', RCUBE_INPUT_POST, FALSE, 'UTF-7')));
80 if ($rename && $OUTPUT->ajax_call)
82 $delimiter = $IMAP->get_hierarchy_delimiter();
83 $foldersplit = explode($delimiter, $rename);
84 $level = count($foldersplit) - 1;
85 $display_rename = str_repeat(' ', $level) . rcube_charset_convert($foldersplit[$level], 'UTF-7');
86 $OUTPUT->command('replace_folder_row', $oldname, $rename, $display_rename);
87 foreach ($a_mboxes as $mbox)
88 if (preg_match('/^'.preg_quote($oldname . $delimiter, '/').'/', $mbox))
90 $c_rename = preg_replace('/^'.preg_quote($oldname, '/').'/', $rename, $mbox);
91 $foldersplit = explode($delimiter, $c_rename);
92 $level = count($foldersplit) - 1;
93 $display_rename = str_repeat(' ', $level) . rcube_charset_convert($foldersplit[$level], 'UTF-7');
94 $OUTPUT->command('replace_folder_row', $mbox, $c_rename, $display_rename);
96 $OUTPUT->command('reset_folder_rename');
99 else if (!$rename && $OUTPUT->ajax_call)
101 $OUTPUT->command('reset_folder_rename');
102 $OUTPUT->show_message('errorsaving', 'error');
106 $OUTPUT->show_message('errorsaving', 'error');
109 // delete an existing IMAP mailbox
110 else if ($_action=='delete-folder')
112 $a_mboxes = array_merge($IMAP->list_mailboxes(), $IMAP->list_unsubscribed());
113 $delimiter = $IMAP->get_hierarchy_delimiter();
115 if ($mboxes = get_input_value('_mboxes', RCUBE_INPUT_POST))
116 $deleted = $IMAP->delete_mailbox(array($mboxes));
118 if ($OUTPUT->ajax_call && $deleted)
120 $OUTPUT->command('remove_folder_row', get_input_value('_mboxes', RCUBE_INPUT_POST));
121 foreach ($a_mboxes as $mbox)
123 $regex = get_input_value('_mboxes', RCUBE_INPUT_POST) . $delimiter;
124 $regex = preg_quote($regex, '/');
125 if (preg_match('/^'. $regex .'/', $mbox))
127 $OUTPUT->command('remove_folder_row', $mbox);
130 $OUTPUT->show_message('folderdeleted', 'confirmation');
133 else if ($OUTPUT->ajax_call)
135 $OUTPUT->show_message('errorsaving', 'error');
142 // build table with all folders listed by server
143 function rcube_subscription_form($attrib)
145 global $IMAP, $CONFIG, $OUTPUT;
147 list($form_start, $form_end) = get_form_tags($attrib, 'folders');
148 unset($attrib['form']);
152 $attrib['id'] = 'rcmSubscriptionlist';
154 // allow the following attributes to be added to the <table> tag
155 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
157 $out = "$form_start\n<table" . $attrib_str . ">\n";
161 $out .= "<thead><tr>\n";
162 $out .= sprintf('<td class="name">%s</td><td class="msgcount">%s</td><td class="subscribed">%s</td>'.
163 '<td class="rename"> </td><td class="delete"> </td>',
164 rcube_label('foldername'), rcube_label('messagecount'), rcube_label('subscribed'));
166 $out .= "\n</tr></thead>\n<tbody>\n";
169 // get folders from server
170 $IMAP->clear_cache('mailboxes');
172 $a_unsubscribed = $IMAP->list_unsubscribed();
173 $a_subscribed = $IMAP->list_mailboxes();
174 $delimiter = $IMAP->get_hierarchy_delimiter();
175 $a_js_folders = array();
177 $checkbox_subscribe = new checkbox(array('name' => '_subscribed[]', 'onclick' => JS_OBJECT_NAME.".command(this.checked?'subscribe':'unsubscribe',this.value)"));
179 if (!empty($attrib['deleteicon']))
180 $del_button = sprintf('<img src="%s%s" alt="%s" border="0" />', $CONFIG['skin_path'], $attrib['deleteicon'], rcube_label('delete'));
182 $del_button = rcube_label('delete');
184 if (!empty($attrib['renameicon']))
185 $edit_button = sprintf('<img src="%s%s" alt="%s" border="0" />', $CONFIG['skin_path'], $attrib['renameicon'], rcube_label('rename'));
187 $del_button = rcube_label('rename');
189 // create list of available folders
190 foreach ($a_unsubscribed as $i => $folder)
192 $subscribed = in_array($folder, $a_subscribed);
193 $protected = ($CONFIG['protect_default_folders'] == TRUE && in_array($folder,$CONFIG['default_imap_folders']));
194 $zebra_class = $i%2 ? 'even' : 'odd';
195 $folder_js = JQ($folder);
196 $foldersplit = explode($delimiter, $folder);
197 $level = count($foldersplit) - 1;
198 $display_folder = str_repeat(' ', $level) . rcube_charset_convert($foldersplit[$level], 'UTF-7');
199 $folder_html = $CONFIG['protect_default_folders'] && in_array($folder, $CONFIG['default_imap_folders']) ? rcmail_localize_foldername($folder) : $display_folder;
202 $a_js_folders['rcmrow'.($i+1)] = array($folder, rcube_charset_convert($folder, 'UTF-7'));
204 $out .= sprintf('<tr id="rcmrow%d" class="%s"><td class="name">%s</td><td class="msgcount">%d</td>',
208 $IMAP->messagecount($folder));
211 $out .= '<td class="subscribed"> '.($subscribed ? '•' : '-').'</td>';
213 $out .= '<td class="subscribed">'.$checkbox_subscribe->show($subscribed?$folder:'', array('value' => $folder)).'</td>';
215 // add rename and delete buttons
217 $out .= sprintf('<td class="rename"><a href="#rename" title="%s">%s</a>'.
218 '<td class="delete"><a href="#delete" title="%s">%s</a></td>',
219 rcube_label('renamefolder'),
221 rcube_label('deletefolder'),
224 $out .= '<td></td><td></td>';
229 $out .= "</tbody>\n</table>";
230 $out .= "\n$form_end";
232 $OUTPUT->add_gui_object('subscriptionlist', $attrib['id']);
233 $OUTPUT->set_env('subscriptionrows', $a_js_folders);
234 $OUTPUT->set_env('defaultfolders', $CONFIG['default_imap_folders']);
235 $OUTPUT->set_env('delimiter', $delimiter);
241 function rcube_create_folder_form($attrib)
245 list($form_start, $form_end) = get_form_tags($attrib, 'create-folder');
246 unset($attrib['form']);
248 if ($attrib['hintbox'])
249 $OUTPUT->add_gui_object('createfolderhint', $attrib['hintbox']);
251 // return the complete edit form as table
252 $out = "$form_start\n";
254 $input = new textfield(array('name' => '_folder_name'));
255 $out .= $input->show();
257 if (get_boolean($attrib['button']))
259 $button = new input_field(array('type' => 'button',
260 'value' => rcube_label('create'),
261 'onclick' => JS_OBJECT_NAME.".command('create-folder',this.form)"));
262 $out .= $button->show();
265 $out .= "\n$form_end";
270 function rcube_rename_folder_form($attrib)
272 global $CONFIG, $IMAP;
274 list($form_start, $form_end) = get_form_tags($attrib, 'rename-folder');
275 unset($attrib['form']);
277 // return the complete edit form as table
278 $out = "$form_start\n";
280 $a_unsubscribed = $IMAP->list_unsubscribed();
281 $select_folder = new select(array('name' => '_folder_oldname', 'id' => 'rcmfd_oldfolder'));
283 foreach ($a_unsubscribed as $i => $folder)
285 if ($CONFIG['protect_default_folders'] == TRUE && in_array($folder,$CONFIG['default_imap_folders']))
288 $select_folder->add($folder);
291 $out .= $select_folder->show();
294 $inputtwo = new textfield(array('name' => '_folder_newname'));
295 $out .= $inputtwo->show();
297 if (get_boolean($attrib['button']))
299 $button = new input_field(array('type' => 'button',
300 'value' => rcube_label('rename'),
301 'onclick' => JS_OBJECT_NAME.".command('rename-folder',this.form)"));
302 $out .= $button->show();
305 $out .= "\n$form_end";
311 // register UI objects
312 $OUTPUT->add_handlers(array(
313 'foldersubscription' => 'rcube_subscription_form',
314 'createfolder' => 'rcube_create_folder_form',
315 'renamefolder' => 'rcube_rename_folder_form'
318 // add some labels to client
319 rcube_add_label('deletefolderconfirm','addsubfolderhint');
321 $OUTPUT->send('managefolders');