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;
29 $attrib += array('id' => "rcmImportForm");
31 $upload = new html_inputfield(array('type' => 'file', 'name' => '_file', 'id' => 'rcmimportfile', 'size' => 40));
32 $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . html::br() . $upload->show());
34 $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
35 $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
36 html::label('rcmimportreplace', rcube_label('importreplace')));
38 $OUTPUT->add_label('selectimportfile','importwait');
39 $OUTPUT->add_gui_object('importform', $attrib['id']);
41 $out = html::p(null, Q(rcube_label('importtext'), 'show'));
43 $out .= $OUTPUT->form_tag(array(
44 'action' => $RCMAIL->url('import'),
46 'enctype' => 'multipart/form-data') + $attrib,
54 * Render the confirmation page for the import process
56 function rcmail_import_confirm($attrib)
60 $vars = get_object_vars($IMPORT_STATS);
61 $vars['names'] = join(', ', array_map('Q', $IMPORT_STATS->names));
63 return html::p($attrib, Q(rcube_label(array(
64 'name' => 'importconfirm',
65 'nr' => $IMORT_STATS->inserted,
72 * Create navigation buttons for the current import step
74 function rcmail_import_buttons($attrib)
76 global $IMPORT_STATS, $OUTPUT;
78 $attrib += array('type' => "input");
79 unset($attrib['name']);
81 if (is_object($IMPORT_STATS)) {
82 $attrib['class'] = trim($attrib['class'] . ' mainaction');
83 $out = $OUTPUT->button(array('command' => "list", 'label' => "done") + $attrib);
86 $out = $OUTPUT->button(array('command' => "list", 'label' => "cancel") + $attrib);
88 $attrib['class'] = trim($attrib['class'] . ' mainaction');
89 $out .= $OUTPUT->button(array('command' => "import", 'label' => "import") + $attrib);
96 /** The import process **/
98 $importstep = 'rcmail_import_form';
100 if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
102 $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
103 $CONTACTS = $RCMAIL->get_address_book(null, true);
105 // let rcube_vcard do the hard work :-)
106 $vcards = rcube_vcard::import(file_get_contents($_FILES['_file']['tmp_name']));
108 // no vcards detected
109 if (!count($vcards)) {
110 $OUTPUT->show_message('importerror', 'error');
112 else if ($CONTACTS->readonly) {
113 $OUTPUT->show_message('addresswriterror', 'error');
116 $IMPORT_STATS = new stdClass;
117 $IMPORT_STATS->names = array();
118 $IMPORT_STATS->count = count($vcards);
119 $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
122 $CONTACTS->delete_all();
124 foreach ($vcards as $vcard) {
125 $email = $vcard->email[0];
127 // skip entries without an e-mail address
129 $IMPORT_STATS->nomail++;
134 // compare e-mail address
135 $existing = $CONTACTS->search('email', $email, false, false);
136 if (!$existing->count) { // compare display name
137 $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
139 if ($existing->count) {
140 $IMPORT_STATS->skipped++;
145 $success = $CONTACTS->insert(array(
146 'name' => $vcard->displayname,
147 'firstname' => $vcard->firstname,
148 'surname' => $vcard->surname,
150 'vcard' => $vcard->export(),
154 $IMPORT_STATS->inserted++;
155 $IMPORT_STATS->names[] = $vcard->displayname;
158 $IMPORT_STATS->errors++;
162 $importstep = 'rcmail_import_confirm';
165 else if ($err = $_FILES['_file']['error']) {
166 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
167 $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
170 $OUTPUT->show_message('fileuploaderror', 'error');
175 $OUTPUT->set_pagetitle(rcube_label('importcontacts'));
177 $OUTPUT->add_handlers(array(
178 'importstep' => $importstep,
179 'importnav' => 'rcmail_import_buttons',
183 $OUTPUT->send('importcontacts');