thomascube
2008-09-18 7dfb1fba5001299300736e6b5d95d9400575e3e7
commit | author | age
ed132e 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/import.inc                                  |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2008, 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(', ', $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);
cf1d86 119     $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
ed132e 120     
T 121     if ($replace)
122       $CONTACTS->delete_all();
123     
124     foreach ($vcards as $vcard) {
125       $email = $vcard->email[0];
126       
127       if (!$replace) {
128         // compare e-mail address
129         $existing = $CONTACTS->search('email', $email, false, false);
130         if (!$existing->count) {  // compare display name
131           $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
132         }
133         if ($existing->count) {
134           $IMPORT_STATS->skipped++;
135           continue;
136         }
137       }
cf1d86 138       // skip entries without an e-mail address
T 139       if (empty($email)) {
140         $IMPORT_STATS->nomail++;
141         continue;
142       }
ed132e 143       
T 144       $success = $CONTACTS->insert(array(
145         'name' => $vcard->displayname,
146         'firstname' => $vcard->firstname,
147         'surname' => $vcard->surname,
148         'email' => $email,
149         'vcard' => $vcard->export(),
150       ));
151       
152       if ($success) {
153         $IMPORT_STATS->inserted++;
154         $IMPORT_STATS->names[] = $vcard->displayname;
155       }
156       else {
157         $IMPORT_STATS->errors++;
158       }
159     }
160
161     $importstep = 'rcmail_import_confirm';
162   }
163 }
164 else if ($err = $_FILES['_file']['error']) {
165   if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
166     $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
167   }
168   else {
169     $OUTPUT->show_message('fileuploaderror', 'error');
170   }
171 }
172
173
174 $OUTPUT->set_pagetitle(rcube_label('importcontacts'));
175
176 $OUTPUT->add_handlers(array(
177   'importstep' => $importstep,
178   'importnav' => 'rcmail_import_buttons',
179 ));
180
181 // render page
182 $OUTPUT->send('importcontacts');
183
184 ?>