From ce92ba767a9557daf7f18be94882dd7e6f4591fb Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Thu, 30 Sep 2010 09:24:33 -0400
Subject: [PATCH] - Plugin API: improved 'abort' flag handling, added 'result' item in some hooks: group_*, contact_*, identity_* (#1486914)

---
 program/steps/addressbook/copy.inc |   32 ++++++++++++++++++--------------
 1 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/program/steps/addressbook/copy.inc b/program/steps/addressbook/copy.inc
index a37e932..8a83790 100644
--- a/program/steps/addressbook/copy.inc
+++ b/program/steps/addressbook/copy.inc
@@ -37,28 +37,30 @@
     $ids = array();
 
     foreach ($arr_cids as $cid) {
-      $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
-        'record' => $CONTACTS->get_record($cid, true),
-        'source' => $target,
-        'group' => $target_group,
-      ));
-      $a_record = $plugin['record'];
+      $a_record = $CONTACTS->get_record($cid, true);
 
-      if (!$plugin['abort']) {
-        // check if contact exists, if so, we'll need it's ID
-        $result = $TARGET->search('email', $a_record['email'], true, true);
+      // check if contact exists, if so, we'll need it's ID
+      $result = $TARGET->search('email', $a_record['email'], true, true);
 
-        // insert contact record
-        if (!$result->count) {
+      // insert contact record
+      if (!$result->count) {
+        $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
+          'record' => $a_record, 'source' => $target, 'group' => $target_group));
+
+        if (!$plugin['abort']) {
           if ($insert_id = $TARGET->insert($a_record, false)) {
             $ids[] = $insert_id;
             $success++;
           }
         }
-        else {
-          $record = $result->first();
-          $ids[] = $record['ID'];
+        else if ($plugin['result']) {
+          $ids = array_merge($ids, $plugin['result']);
+          $success++;
         }
+      }
+      else {
+        $record = $result->first();
+        $ids[] = $record['ID'];
       }
     }
 
@@ -79,6 +81,8 @@
         if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
           $success = $cnt;
       }
+      else if ($plugin['result'])
+        $success = $plugin['result'];
     }
   }
 

--
Gitblit v1.9.1