From 9336ba21cfdee9aca3898361a62b42a6027242df Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Thu, 12 Apr 2012 07:52:09 -0400
Subject: [PATCH] - Fix importing to LDAP addressbook when mail attribute is required by   validating input data with autofix

---
 program/steps/addressbook/import.inc |    8 ++++----
 program/include/rcube_ldap.php       |   17 +++++++++++++++--
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/program/include/rcube_ldap.php b/program/include/rcube_ldap.php
index 130eada..08b7cd9 100644
--- a/program/include/rcube_ldap.php
+++ b/program/include/rcube_ldap.php
@@ -993,10 +993,13 @@
         if ($missing) {
             // try to complete record automatically
             if ($autofix) {
-                $reverse_map = array_flip($this->fieldmap);
-                $name_parts  = preg_split('/[\s,.]+/', $save_data['name']);
                 $sn_field    = $this->fieldmap['surname'];
                 $fn_field    = $this->fieldmap['firstname'];
+                $mail_field  = $this->fieldmap['email'];
+
+                // try to extract surname and firstname from displayname
+                $reverse_map = array_flip($this->fieldmap);
+                $name_parts  = preg_split('/[\s,.]+/', $save_data['name']);
 
                 if ($sn_field && $missing[$sn_field]) {
                     $save_data['surname'] = array_pop($name_parts);
@@ -1007,6 +1010,16 @@
                     $save_data['firstname'] = array_shift($name_parts);
                     unset($missing[$fn_field]);
                 }
+
+                // try to fix missing e-mail, very often on import
+                // from vCard we have email:other only defined
+                if ($mail_field && $missing[$mail_field]) {
+                    $emails = $this->get_col_values('email', $save_data, true);
+                    if (!empty($emails) && ($email = array_shift($emails))) {
+                        $save_data['email'] = $email;
+                        unset($missing[$mail_field]);
+                    }
+                }
             }
 
             // TODO: generate message saying which fields are missing
diff --git a/program/steps/addressbook/import.inc b/program/steps/addressbook/import.inc
index 7b52bdc..018c980 100644
--- a/program/steps/addressbook/import.inc
+++ b/program/steps/addressbook/import.inc
@@ -164,10 +164,11 @@
       $CONTACTS->delete_all();
 
     foreach ($vcards as $vcard) {
-      $email = $vcard->email[0];
+      $email    = $vcard->email[0];
+      $a_record = $vcard->get_assoc();
 
-      // skip entries without an e-mail address
-      if (empty($email)) {
+      // skip entries without an e-mail address or invalid
+      if (empty($email) || !$CONTACTS->validate($a_record, true)) {
         $IMPORT_STATS->nomail++;
         continue;
       }
@@ -188,7 +189,6 @@
         }
       }
 
-      $a_record = $vcard->get_assoc();
       $a_record['vcard'] = $vcard->export();
 
       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null));

--
Gitblit v1.9.1