]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/import.inc
Imported Upstream version 0.2.1
[roundcube.git] / program / steps / addressbook / import.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/import.inc                                  |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Import contacts from a vCard or CSV file                            |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: $
19
20 */
21
22 /**
23  * Handler function to display the import/upload form
24  */
25 function rcmail_import_form($attrib)
26 {
27   global $RCMAIL, $OUTPUT;
28   
29   $attrib += array('id' => "rcmImportForm");
30   
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());
33   
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')));
37   
38   $OUTPUT->add_label('selectimportfile','importwait');
39   $OUTPUT->add_gui_object('importform', $attrib['id']);
40   
41   $out = html::p(null, Q(rcube_label('importtext'), 'show'));
42   
43   $out .= $OUTPUT->form_tag(array(
44       'action' => $RCMAIL->url('import'),
45       'method' => 'post',
46       'enctype' => 'multipart/form-data') + $attrib,
47     $form);
48   
49   return $out;
50 }
51
52
53 /**
54  * Render the confirmation page for the import process
55  */
56 function rcmail_import_confirm($attrib)
57 {
58   global $IMPORT_STATS;
59   
60   $vars = get_object_vars($IMPORT_STATS);
61   $vars['names'] = join(', ', array_map('Q', $IMPORT_STATS->names));
62   
63   return html::p($attrib, Q(rcube_label(array(
64     'name' => 'importconfirm',
65     'nr' => $IMORT_STATS->inserted,
66     'vars' => $vars,
67   )), 'show'));
68 }
69
70
71 /**
72  * Create navigation buttons for the current import step
73  */
74 function rcmail_import_buttons($attrib)
75 {
76   global $IMPORT_STATS, $OUTPUT;
77   
78   $attrib += array('type' => "input");
79   unset($attrib['name']);
80   
81   if (is_object($IMPORT_STATS)) {
82     $attrib['class'] = trim($attrib['class'] . ' mainaction');
83     $out = $OUTPUT->button(array('command' => "list", 'label' => "done") + $attrib);
84   }
85   else {
86     $out = $OUTPUT->button(array('command' => "list", 'label' => "cancel") + $attrib);
87     $out .= '&nbsp;';
88     $attrib['class'] = trim($attrib['class'] . ' mainaction');
89     $out .= $OUTPUT->button(array('command' => "import", 'label' => "import") + $attrib);
90   }
91   
92   return $out;
93 }
94
95
96 /** The import process **/
97
98 $importstep = 'rcmail_import_form';
99
100 if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
101
102   $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
103   $CONTACTS = $RCMAIL->get_address_book(null, true);
104
105   // let rcube_vcard do the hard work :-)
106   $vcards = rcube_vcard::import(file_get_contents($_FILES['_file']['tmp_name']));
107
108   // no vcards detected
109   if (!count($vcards)) {
110     $OUTPUT->show_message('importerror', 'error');
111   }
112   else if ($CONTACTS->readonly) {
113     $OUTPUT->show_message('addresswriterror', 'error');
114   }
115   else {
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;
120     
121     if ($replace)
122       $CONTACTS->delete_all();
123     
124     foreach ($vcards as $vcard) {
125       $email = $vcard->email[0];
126       
127       // skip entries without an e-mail address
128       if (empty($email)) {
129         $IMPORT_STATS->nomail++;
130         continue;
131       }
132       
133       if (!$replace) {
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);
138         }
139         if ($existing->count) {
140           $IMPORT_STATS->skipped++;
141           continue;
142         }
143       }
144       
145       $success = $CONTACTS->insert(array(
146         'name' => $vcard->displayname,
147         'firstname' => $vcard->firstname,
148         'surname' => $vcard->surname,
149         'email' => $email,
150         'vcard' => $vcard->export(),
151       ));
152       
153       if ($success) {
154         $IMPORT_STATS->inserted++;
155         $IMPORT_STATS->names[] = $vcard->displayname;
156       }
157       else {
158         $IMPORT_STATS->errors++;
159       }
160     }
161
162     $importstep = 'rcmail_import_confirm';
163   }
164 }
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')))));
168   }
169   else {
170     $OUTPUT->show_message('fileuploaderror', 'error');
171   }
172 }
173
174
175 $OUTPUT->set_pagetitle(rcube_label('importcontacts'));
176
177 $OUTPUT->add_handlers(array(
178   'importstep' => $importstep,
179   'importnav' => 'rcmail_import_buttons',
180 ));
181
182 // render page
183 $OUTPUT->send('importcontacts');
184
185 ?>