]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/steps/addressbook/import.inc
Imported Upstream version 0.7
[roundcube.git] / program / steps / addressbook / import.inc
index 59490973c4caa88928b4ee8052e7df6e4d293afb..63a6dae30928f48be02c41c7467af8a19e20953c 100644 (file)
@@ -4,8 +4,8 @@
  +-----------------------------------------------------------------------+
  | program/steps/addressbook/import.inc                                  |
  |                                                                       |
- | This file is part of the RoundCube Webmail client                     |
- | Copyright (C) 2008, RoundCube Dev. - Switzerland                      |
+ | This file is part of the Roundcube Webmail client                     |
+ | Copyright (C) 2008-2009, The Roundcube Dev Team                       |
  | Licensed under the GNU GPL                                            |
  |                                                                       |
  | PURPOSE:                                                              |
  |                                                                       |
  +-----------------------------------------------------------------------+
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
+ | Author: Aleksander Machniak <machniak@kolabsys.com>                   |
  +-----------------------------------------------------------------------+
 
- $Id$
+ $Id$
 
 */
 
 function rcmail_import_form($attrib)
 {
   global $RCMAIL, $OUTPUT;
-  
+  $target = get_input_value('_target', RCUBE_INPUT_GPC);
+
   $attrib += array('id' => "rcmImportForm");
-  
+
+  $writable_books = $RCMAIL->get_address_sources(true);
+
   $upload = new html_inputfield(array('type' => 'file', 'name' => '_file', 'id' => 'rcmimportfile', 'size' => 40));
-  $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . html::br() . $upload->show());
-  
+  $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . $upload->show());
+
+  // addressbook selector
+  if (count($writable_books) > 1) {
+    $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget'));
+
+    foreach ($writable_books as $book)
+        $select->add($book['name'], $book['id']);
+
+    $form .= html::p(null, html::label('rcmimporttarget', rcube_label('importtarget'))
+        . $select->show($target));
+  }
+  else {
+    $abook = new html_hiddenfield(array('name' => '_target', 'value' => key($writable_books)));
+    $form .= $abook->show();
+  }
+
   $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
   $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
     html::label('rcmimportreplace', rcube_label('importreplace')));
-  
+
+  $OUTPUT->set_env('writable_source', !empty($writable_books));
   $OUTPUT->add_label('selectimportfile','importwait');
   $OUTPUT->add_gui_object('importform', $attrib['id']);
-  
+
   $out = html::p(null, Q(rcube_label('importtext'), 'show'));
-  
+
   $out .= $OUTPUT->form_tag(array(
       'action' => $RCMAIL->url('import'),
       'method' => 'post',
       'enctype' => 'multipart/form-data') + $attrib,
     $form);
-  
+
   return $out;
 }
 
@@ -56,15 +76,29 @@ function rcmail_import_form($attrib)
 function rcmail_import_confirm($attrib)
 {
   global $IMPORT_STATS;
-  
+
   $vars = get_object_vars($IMPORT_STATS);
-  $vars['names'] = join(', ', array_map('Q', $IMPORT_STATS->names));
-  
-  return html::p($attrib, Q(rcube_label(array(
-    'name' => 'importconfirm',
-    'nr' => $IMORT_STATS->inserted,
-    'vars' => $vars,
-  )), 'show'));
+  $vars['names'] = $vars['skipped_names'] = '';
+
+  $content = html::p(null, rcube_label(array(
+      'name' => 'importconfirm',
+      'nr' => $IMORT_STATS->inserted,
+      'vars' => $vars,
+    )) . ($IMPORT_STATS->names ? ':' : '.'));
+
+  if ($IMPORT_STATS->names)
+    $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->names)));
+
+  if ($IMPORT_STATS->skipped) {
+      $content .= html::p(null, rcube_label(array(
+          'name' => 'importconfirmskipped',
+          'nr' => $IMORT_STATS->skipped,
+          'vars' => $vars,
+        )) . ':');
+      $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->skipped_names)));
+  }
+
+  return html::div($attrib, $content);
 }
 
 
@@ -74,21 +108,22 @@ function rcmail_import_confirm($attrib)
 function rcmail_import_buttons($attrib)
 {
   global $IMPORT_STATS, $OUTPUT;
-  
-  $attrib += array('type' => "input");
+  $target = get_input_value('_target', RCUBE_INPUT_GPC);
+
+  $attrib += array('type' => 'input');
   unset($attrib['name']);
-  
+
   if (is_object($IMPORT_STATS)) {
     $attrib['class'] = trim($attrib['class'] . ' mainaction');
-    $out = $OUTPUT->button(array('command' => "list", 'label' => "done") + $attrib);
+    $out = $OUTPUT->button(array('command' => 'list', 'prop' => $target, 'label' => 'done') + $attrib);
   }
   else {
-    $out = $OUTPUT->button(array('command' => "list", 'label' => "cancel") + $attrib);
+    $out = $OUTPUT->button(array('command' => 'list', 'label' => 'cancel') + $attrib);
     $out .= '&nbsp;';
     $attrib['class'] = trim($attrib['class'] . ' mainaction');
-    $out .= $OUTPUT->button(array('command' => "import", 'label' => "import") + $attrib);
+    $out .= $OUTPUT->button(array('command' => 'import', 'label' => 'import') + $attrib);
   }
-  
+
   return $out;
 }
 
@@ -98,12 +133,15 @@ function rcmail_import_buttons($attrib)
 $importstep = 'rcmail_import_form';
 
 if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) {
-
   $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
-  $CONTACTS = $RCMAIL->get_address_book(null, true);
+  $target = get_input_value('_target', RCUBE_INPUT_GPC);
+  $CONTACTS = $RCMAIL->get_address_book($target, true);
 
   // let rcube_vcard do the hard work :-)
-  $vcards = rcube_vcard::import(file_get_contents($_FILES['_file']['tmp_name']));
+  $vcard_o = new rcube_vcard();
+  $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
+
+  $vcards = $vcard_o->import(file_get_contents($_FILES['_file']['tmp_name']));
 
   // no vcards detected
   if (!count($vcards)) {
@@ -115,46 +153,54 @@ if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'
   else {
     $IMPORT_STATS = new stdClass;
     $IMPORT_STATS->names = array();
+    $IMPORT_STATS->skipped_names = array();
     $IMPORT_STATS->count = count($vcards);
     $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0;
-    
+
     if ($replace)
       $CONTACTS->delete_all();
-    
+
     foreach ($vcards as $vcard) {
       $email = $vcard->email[0];
-      
+
       // skip entries without an e-mail address
       if (empty($email)) {
         $IMPORT_STATS->nomail++;
         continue;
       }
-      
-      if (!$replace) {
+
+      // We're using UTF8 internally
+      $email = rcube_idn_to_utf8($email);
+
+      if (!$replace && $email) {
         // compare e-mail address
-        $existing = $CONTACTS->search('email', $email, false, false);
-        if (!$existing->count) {  // compare display name
-          $existing = $CONTACTS->search('name', $vcard->displayname, false, false);
+        $existing = $CONTACTS->search('email', $email, 1, false);
+        if (!$existing->count && $vcard->displayname) {  // compare display name
+          $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
         }
         if ($existing->count) {
           $IMPORT_STATS->skipped++;
+          $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email;
           continue;
         }
       }
-      
-      $success = $CONTACTS->insert(array(
-        'name' => $vcard->displayname,
-        'firstname' => $vcard->firstname,
-        'surname' => $vcard->surname,
-        'email' => $email,
-        'vcard' => $vcard->export(),
-      ));
-      
+
+      $a_record = $vcard->get_assoc();
+      $a_record['vcard'] = $vcard->export();
+
+      $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));
+      $a_record = $plugin['record'];
+
+      // insert record and send response
+      if (!$plugin['abort'])
+        $success = $CONTACTS->insert($a_record);
+      else
+        $success = $plugin['result'];
+
       if ($success) {
         $IMPORT_STATS->inserted++;
-        $IMPORT_STATS->names[] = $vcard->displayname;
-      }
-      else {
+        $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email;
+      } else {
         $IMPORT_STATS->errors++;
       }
     }
@@ -165,8 +211,7 @@ if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'
 else if ($err = $_FILES['_file']['error']) {
   if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
     $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize')))));
-  }
-  else {
+  } else {
     $OUTPUT->show_message('fileuploaderror', 'error');
   }
 }
@@ -181,5 +226,3 @@ $OUTPUT->add_handlers(array(
 
 // render page
 $OUTPUT->send('importcontacts');
-
-?>
\ No newline at end of file