From fc52af24f1418d6590a2d37a0d8cc31b123e38f6 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Tue, 19 Aug 2014 12:08:35 -0400
Subject: [PATCH] Fix merge error that disabled contact drag'n'drop

---
 program/steps/addressbook/import.inc |   58 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/program/steps/addressbook/import.inc b/program/steps/addressbook/import.inc
index 654a336..915aac8 100644
--- a/program/steps/addressbook/import.inc
+++ b/program/steps/addressbook/import.inc
@@ -30,7 +30,7 @@
 
   $attrib += array('id' => "rcmImportForm");
 
-  $writable_books = $RCMAIL->get_address_sources(true);
+  $writable_books = $RCMAIL->get_address_sources(true, true);
 
   $upload = new html_inputfield(array(
     'type' => 'file',
@@ -43,7 +43,7 @@
 
   // addressbook selector
   if (count($writable_books) > 1) {
-    $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget'));
+    $select = new html_select(array('name' => '_target', 'id' => 'rcmimporttarget', 'is_escaped' => true));
 
     foreach ($writable_books as $book)
         $select->add($book['name'], $book['id']);
@@ -64,7 +64,7 @@
   $OUTPUT->add_label('selectimportfile','importwait');
   $OUTPUT->add_gui_object('importform', $attrib['id']);
 
-  $out = html::p(null, Q(rcube_label('importtext'), 'show'));
+  $out = html::p(null, Q(rcube_label('importdesc'), 'show'));
 
   $out .= $OUTPUT->form_tag(array(
       'action' => $RCMAIL->url('import'),
@@ -88,7 +88,7 @@
 
   $content = html::p(null, rcube_label(array(
       'name' => 'importconfirm',
-      'nr' => $IMORT_STATS->inserted,
+      'nr' => $IMPORT_STATS->inserted,
       'vars' => $vars,
     )) . ($IMPORT_STATS->names ? ':' : '.'));
 
@@ -98,7 +98,7 @@
   if ($IMPORT_STATS->skipped) {
       $content .= html::p(null, rcube_label(array(
           'name' => 'importconfirmskipped',
-          'nr' => $IMORT_STATS->skipped,
+          'nr' => $IMPORT_STATS->skipped,
           'vars' => $vars,
         )) . ':');
       $content .= html::p('em', join(', ', array_map('Q', $IMPORT_STATS->skipped_names)));
@@ -159,11 +159,22 @@
                 $upload_error = $err;
             }
             else {
+                $file_content = file_get_contents($filepath);
+
                 // let rcube_vcard do the hard work :-)
                 $vcard_o = new rcube_vcard();
                 $vcard_o->extend_fieldmap($CONTACTS->vcard_map);
+                $v_list = $vcard_o->import($file_content);
 
-                $v_list = $vcard_o->import(file_get_contents($filepath));
+                if (!empty($v_list)) {
+                    $vcards = array_merge($vcards, $v_list);
+                    continue;
+                }
+
+                // no vCards found, try CSV
+                $csv = new rcube_csv2vcard($_SESSION['language']);
+                $csv->import($file_content);
+                $v_list = $csv->export();
 
                 if (!empty($v_list)) {
                     $vcards = array_merge($vcards, $v_list);
@@ -181,7 +192,7 @@
             $OUTPUT->show_message('fileuploaderror', 'error');
         }
         else {
-            $OUTPUT->show_message('importerror', 'error');
+            $OUTPUT->show_message('importformaterror', 'error');
         }
     }
     else {
@@ -189,32 +200,45 @@
         $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;
+        $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->invalid = $IMPORT_STATS->errors = 0;
 
         if ($replace) {
             $CONTACTS->delete_all();
         }
 
         foreach ($vcards as $vcard) {
-            $email    = $vcard->email[0];
             $a_record = $vcard->get_assoc();
 
-            // skip entries without an e-mail address or invalid
-            if (empty($email) || !$CONTACTS->validate($a_record, true)) {
-                $IMPORT_STATS->nomail++;
+            // Generate contact's display name (must be before validation), the same we do in save.inc
+            if (empty($a_record['name'])) {
+                $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true);
+                // Reset it if equals to email address (from compose_display_name())
+                if ($a_record['name'] == $a_record['email'][0]) {
+                    $a_record['name'] = '';
+                }
+            }
+
+            // skip invalid (incomplete) entries
+            if (!$CONTACTS->validate($a_record, true)) {
+                $IMPORT_STATS->invalid++;
                 continue;
             }
 
             // We're using UTF8 internally
+            $email = $vcard->email[0];
             $email = rcube_idn_to_utf8($email);
 
-            if (!$replace && $email) {
+            if (!$replace) {
+                $existing = null;
                 // compare e-mail address
-                $existing = $CONTACTS->search('email', $email, 1, false);
-                if (!$existing->count && $vcard->displayname) {  // compare display name
+                if ($email) {
+                    $existing = $CONTACTS->search('email', $email, 1, false);
+                }
+                // compare display name if email not found
+                if ((!$existing || !$existing->count) && $vcard->displayname) {
                     $existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
                 }
-                if ($existing->count) {
+                if ($existing && $existing->count) {
                     $IMPORT_STATS->skipped++;
                     $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email;
                     continue;
@@ -235,7 +259,7 @@
 
             if ($success) {
                 $IMPORT_STATS->inserted++;
-                $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email;
+                $IMPORT_STATS->names[] = $a_record['name'] ? $a_record['name'] : $email;
             }
             else {
                 $IMPORT_STATS->errors++;

--
Gitblit v1.9.1