4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/import.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2009, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Import contacts from a vCard or CSV file |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 | Author: Aleksander Machniak <machniak@kolabsys.com> |
17 +-----------------------------------------------------------------------+
24 * Handler function to display the import/upload form
26 function rcmail_import_form($attrib)
28 global $RCMAIL, $OUTPUT;
29 $target = get_input_value('_target', RCUBE_INPUT_GPC);
31 $attrib += array('id' => "rcmImportForm");
33 $writable_books = $RCMAIL->get_address_sources(true);
35 $upload = new html_inputfield(array('type' => 'file', 'name' => '_file', 'id' => 'rcmimportfile', 'size' => 40));
36 $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . $upload->show());
38 // addressbook selector
39 if (count($writable_books) > 1) {
40 $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget'));
42 foreach ($writable_books as $book)
43 $select->add($book['name'], $book['id']);
45 $form .= html::p(null, html::label('rcmimporttarget', rcube_label('importtarget'))
46 . $select->show($target));
49 $abook = new html_hiddenfield(array('name' => '_target', 'value' => key($writable_books)));
50 $form .= $abook->show();
53 $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
54 $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
55 html::label('rcmimportreplace', rcube_label('importreplace')));
57 $OUTPUT->set_env('writable_source', !empty($writable_books));
58 $OUTPUT->add_label('selectimportfile','importwait');
59 $OUTPUT->add_gui_object('importform', $attrib['id']);
61 $out = html::p(null, Q(rcube_label('importtext'), 'show'));
63 $out .= $OUTPUT->form_tag(array(
64 'action' => $RCMAIL->url('import'),
66 'enctype' => 'multipart/form-data') + $attrib,
74 * Render the confirmation page for the import process
76 function rcmail_import_confirm($attrib)
80 $vars = get_object_vars($IMPORT_STATS);
81 $vars['names'] = $vars['skipped_names'] = '';
83 $content = html::p(null, rcube_label(array(
84 'name' => 'importconfirm',
85 'nr' => $IMORT_STATS->inserted,
87 )) . ($IMPORT_STATS->names ? ':' : '.'));
89 if ($IMPORT_STATS->names)
90 $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->names)));
92 if ($IMPORT_STATS->skipped) {
93 $content .= html::p(null, rcube_label(array(
94 'name' => 'importconfirmskipped',
95 'nr' => $IMORT_STATS->skipped,
98 $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->skipped_names)));
101 return html::div($attrib, $content);
106 * Create navigation buttons for the current import step
108 function rcmail_import_buttons($attrib)
110 global $IMPORT_STATS, $OUTPUT;
111 $target = get_input_value('_target', RCUBE_INPUT_GPC);
113 $attrib += array('type' => 'input');
114 unset($attrib['name']);
116 if (is_object($IMPORT_STATS)) {
117 $attrib['class'] = trim($attrib['class'] . ' mainaction');
118 $out = $OUTPUT->button(array('command' => 'list', 'prop' => $target, 'label' => 'done') + $attrib);
121 $out = $OUTPUT->button(array('command' => 'list', 'label' => 'cancel') + $attrib);
123 $attrib['class'] = trim($attrib['class'] . ' mainaction');
124 $out .= $OUTPUT->button(array('command' => 'import', 'label' => 'import') + $attrib);
131 /** The import process **/
133 $importstep = 'rcmail_import_form';
135 if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
136 $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
137 $target = get_input_value('_target', RCUBE_INPUT_GPC);
138 $CONTACTS = $RCMAIL->get_address_book($target, true);
140 // let rcube_vcard do the hard work :-)
141 $vcard_o = new rcube_vcard();
142 $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
144 $vcards = $vcard_o->import(file_get_contents($_FILES['_file']['tmp_name']));
146 // no vcards detected
147 if (!count($vcards)) {
148 $OUTPUT->show_message('importerror', 'error');
150 else if ($CONTACTS->readonly) {
151 $OUTPUT->show_message('addresswriterror', 'error');
154 $IMPORT_STATS = new stdClass;
155 $IMPORT_STATS->names = array();
156 $IMPORT_STATS->skipped_names = array();
157 $IMPORT_STATS->count = count($vcards);
158 $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
161 $CONTACTS->delete_all();
163 foreach ($vcards as $vcard) {
164 $email = $vcard->email[0];
166 // skip entries without an e-mail address
168 $IMPORT_STATS->nomail++;
172 // We're using UTF8 internally
173 $email = rcube_idn_to_utf8($email);
175 if (!$replace && $email) {
176 // compare e-mail address
177 $existing = $CONTACTS->search('email', $email, false, false);
178 if (!$existing->count && $vcard->displayname) { // compare display name
179 $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
181 if ($existing->count) {
182 $IMPORT_STATS->skipped++;
183 $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email;
188 $a_record = $vcard->get_assoc();
189 $a_record['vcard'] = $vcard->export();
191 $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));
192 $a_record = $plugin['record'];
194 // insert record and send response
195 if (!$plugin['abort'])
196 $success = $CONTACTS->insert($a_record);
198 $success = $plugin['result'];
201 $IMPORT_STATS->inserted++;
202 $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email;
204 $IMPORT_STATS->errors++;
208 $importstep = 'rcmail_import_confirm';
211 else if ($err = $_FILES['_file']['error']) {
212 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
213 $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
215 $OUTPUT->show_message('fileuploaderror', 'error');
220 $OUTPUT->set_pagetitle(rcube_label('importcontacts'));
222 $OUTPUT->add_handlers(array(
223 'importstep' => $importstep,
224 'importnav' => 'rcmail_import_buttons',
228 $OUTPUT->send('importcontacts');