From 4a05e8a7e8a39aee331a1d5bc45fbc1710ac6a15 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Fri, 08 Nov 2013 04:57:00 -0500
Subject: [PATCH] Finish advanced prefs feature (#1488829)

---
 program/lib/Roundcube/rcube_addressbook.php |   29 +++++++++++++++++++++++++----
 1 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/program/lib/Roundcube/rcube_addressbook.php b/program/lib/Roundcube/rcube_addressbook.php
index 9301211..886f65c 100644
--- a/program/lib/Roundcube/rcube_addressbook.php
+++ b/program/lib/Roundcube/rcube_addressbook.php
@@ -134,7 +134,7 @@
     abstract function get_record($id, $assoc=false);
 
     /**
-     * Returns the last error occured (e.g. when updating/inserting failed)
+     * Returns the last error occurred (e.g. when updating/inserting failed)
      *
      * @return array Hash array with the following fields: type, message
      */
@@ -209,6 +209,7 @@
     public function validate(&$save_data, $autofix = false)
     {
         $rcube = rcube::get_instance();
+        $valid = true;
 
         // check validity of email addresses
         foreach ($this->get_col_values('email', $save_data, true) as $email) {
@@ -216,12 +217,28 @@
                 if (!rcube_utils::check_email(rcube_utils::idn_to_ascii($email))) {
                     $error = $rcube->gettext(array('name' => 'emailformaterror', 'vars' => array('email' => $email)));
                     $this->set_error(self::ERROR_VALIDATE, $error);
-                    return false;
+                    $valid = false;
+                    break;
                 }
             }
         }
 
-        return true;
+        // allow plugins to do contact validation and auto-fixing
+        $plugin = $rcube->plugins->exec_hook('contact_validate', array(
+            'record'  => $save_data,
+            'autofix' => $autofix,
+            'valid'   => $valid,
+        ));
+
+        if ($valid && !$plugin['valid']) {
+            $this->set_error(self::ERROR_VALIDATE, $plugin['error']);
+        }
+
+        if (is_array($plugin['record'])) {
+            $save_data = $plugin['record'];
+        }
+
+        return $plugin['valid'];
     }
 
     /**
@@ -515,8 +532,12 @@
             $fn = join(' ', array($contact['surname'], $contact['firstname'], $contact['middlename']));
         else if ($compose_mode == 1)
             $fn = join(' ', array($contact['firstname'], $contact['middlename'], $contact['surname']));
-        else
+        else if ($compose_mode == 0)
             $fn = !empty($contact['name']) ? $contact['name'] : join(' ', array($contact['prefix'], $contact['firstname'], $contact['middlename'], $contact['surname'], $contact['suffix']));
+        else {
+            $plugin = rcube::get_instance()->plugins->exec_hook('contact_listname', array('contact' => $contact));
+            $fn     = $plugin['fn'];
+        }
 
         $fn = trim($fn, ', ');
 

--
Gitblit v1.9.1