4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/import.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2009, Roundcube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Import contacts from a vCard or CSV file |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
23 * Handler function to display the import/upload form
25 function rcmail_import_form($attrib)
27 global $RCMAIL, $OUTPUT;
28 $target = get_input_value('_target', RCUBE_INPUT_GPC);
30 $attrib += array('id' => "rcmImportForm");
32 $abook = new html_hiddenfield(array('name' => '_target', 'value' => $target));
33 $form = $abook->show();
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')) . html::br() . $upload->show());
38 $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
39 $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
40 html::label('rcmimportreplace', rcube_label('importreplace')));
42 $OUTPUT->add_label('selectimportfile','importwait');
43 $OUTPUT->add_gui_object('importform', $attrib['id']);
45 $out = html::p(null, Q(rcube_label('importtext'), 'show'));
47 $out .= $OUTPUT->form_tag(array(
48 'action' => $RCMAIL->url('import'),
50 'enctype' => 'multipart/form-data') + $attrib,
58 * Render the confirmation page for the import process
60 function rcmail_import_confirm($attrib)
64 $vars = get_object_vars($IMPORT_STATS);
65 $vars['names'] = join(', ', array_map('Q', $IMPORT_STATS->names));
67 return html::p($attrib, Q(rcube_label(array(
68 'name' => 'importconfirm',
69 'nr' => $IMORT_STATS->inserted,
76 * Create navigation buttons for the current import step
78 function rcmail_import_buttons($attrib)
80 global $IMPORT_STATS, $OUTPUT;
81 $target = get_input_value('_target', RCUBE_INPUT_GPC);
83 $attrib += array('type' => 'input');
84 unset($attrib['name']);
86 if (is_object($IMPORT_STATS)) {
87 $attrib['class'] = trim($attrib['class'] . ' mainaction');
88 $out = $OUTPUT->button(array('command' => 'list', 'prop' => $target, 'label' => 'done') + $attrib);
91 $out = $OUTPUT->button(array('command' => 'list', 'label' => 'cancel') + $attrib);
93 $attrib['class'] = trim($attrib['class'] . ' mainaction');
94 $out .= $OUTPUT->button(array('command' => 'import', 'label' => 'import') + $attrib);
101 /** The import process **/
103 $importstep = 'rcmail_import_form';
105 if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
106 $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
107 $target = get_input_value('_target', RCUBE_INPUT_GPC);
108 $CONTACTS = $RCMAIL->get_address_book($target, true);
110 // let rcube_vcard do the hard work :-)
111 $vcards = rcube_vcard::import(file_get_contents($_FILES['_file']['tmp_name']));
113 // no vcards detected
114 if (!count($vcards)) {
115 $OUTPUT->show_message('importerror', 'error');
117 else if ($CONTACTS->readonly) {
118 $OUTPUT->show_message('addresswriterror', 'error');
121 $IMPORT_STATS = new stdClass;
122 $IMPORT_STATS->names = array();
123 $IMPORT_STATS->count = count($vcards);
124 $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
127 $CONTACTS->delete_all();
129 foreach ($vcards as $vcard) {
130 $email = $vcard->email[0];
132 // skip entries without an e-mail address
134 $IMPORT_STATS->nomail++;
138 // We're using UTF8 internally
139 $email = rcube_idn_to_utf8($email);
142 // compare e-mail address
143 $existing = $CONTACTS->search('email', $email, false, false);
144 if (!$existing->count) { // compare display name
145 $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
147 if ($existing->count) {
148 $IMPORT_STATS->skipped++;
154 'name' => $vcard->displayname,
155 'firstname' => $vcard->firstname,
156 'surname' => $vcard->surname,
158 'vcard' => $vcard->export(),
161 $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));
162 $a_record = $plugin['record'];
164 // insert record and send response
165 if (!$plugin['abort'])
166 $success = $CONTACTS->insert($a_record);
168 $success = $plugin['result'];
171 $IMPORT_STATS->inserted++;
172 $IMPORT_STATS->names[] = $vcard->displayname;
174 $IMPORT_STATS->errors++;
178 $importstep = 'rcmail_import_confirm';
181 else if ($err = $_FILES['_file']['error']) {
182 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
183 $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
185 $OUTPUT->show_message('fileuploaderror', 'error');
190 $OUTPUT->set_pagetitle(rcube_label('importcontacts'));
192 $OUTPUT->add_handlers(array(
193 'importstep' => $importstep,
194 'importnav' => 'rcmail_import_buttons',
198 $OUTPUT->send('importcontacts');