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)

---
 CHANGELOG                                  |    1 
 program/steps/addressbook/groups.inc       |   31 ++++++-
 program/steps/addressbook/import.inc       |    7 +
 program/steps/settings/delete_identity.inc |   13 +-
 program/steps/addressbook/save.inc         |   38 +++++----
 program/steps/settings/save_identity.inc   |   35 ++++----
 program/steps/addressbook/delete.inc       |   13 +-
 program/steps/addressbook/copy.inc         |   32 ++++---
 program/steps/mail/addcontact.inc          |   13 +-
 9 files changed, 112 insertions(+), 71 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 9a9bb5a..405f963 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -12,6 +12,7 @@
 - Add option do bind for an individual LDAP address book (#1486997)
 - Change reply prefix to display email address only if sender name doesn't exist (#1486550)
 - Fix charset replacement in HTML message bodies (#1487021)
+- Plugin API: improved 'abort' flag handling, added 'result' item in some hooks (#1486914) 
 
 RELEASE 0.4.1
 -------------
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'];
     }
   }
 
diff --git a/program/steps/addressbook/delete.inc b/program/steps/addressbook/delete.inc
index 08ae36d..bb0457b 100644
--- a/program/steps/addressbook/delete.inc
+++ b/program/steps/addressbook/delete.inc
@@ -23,14 +23,15 @@
     ($cid = get_input_value('_cid', RCUBE_INPUT_POST)) &&
     preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid)
 ) {
-  $plugin = $RCMAIL->plugins->exec_hook('contact_delete', array('id' => $cid, 'source' => get_input_value('_source', RCUBE_INPUT_GPC)));
+  $plugin = $RCMAIL->plugins->exec_hook('contact_delete', array(
+    'id' => $cid, 'source' => get_input_value('_source', RCUBE_INPUT_GPC)));
 
-  $deleted = !$plugin['abort'] ? $CONTACTS->delete($cid) : false;
-  if (!$deleted)
-    {
+  $deleted = !$plugin['abort'] ? $CONTACTS->delete($cid) : $plugin['result'];
+
+  if (!$deleted) {
     // send error message
     exit;
-    }
+  }
 
   // count contacts for this user
   $result = $CONTACTS->count();
@@ -46,7 +47,7 @@
 
   // send response
   $OUTPUT->send();
-  }
+}
 
 exit;
 
diff --git a/program/steps/addressbook/groups.inc b/program/steps/addressbook/groups.inc
index 542628e..66619fa 100644
--- a/program/steps/addressbook/groups.inc
+++ b/program/steps/addressbook/groups.inc
@@ -33,9 +33,18 @@
     $CONTACTS->set_group($gid);
     $num2add = count(explode(',', $plugin['ids']));
     
-    if (!$plugin['abort'] && ($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + $num2add > $maxnum))
-      $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
-    else if (!$plugin['abort'] && $CONTACTS->add_to_group($gid, $plugin['ids']))
+    if (!$plugin['abort']) {
+      if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($CONTACTS->count()->count + $num2add > $maxnum)) {
+        $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
+        $OUTPUT->send();
+      }
+      $result = $CONTACTS->add_to_group($gid, $plugin['ids']);
+    }
+    else {
+      $result = $plugin['result'];
+    }
+
+    if ($result) 
       $OUTPUT->show_message('contactaddedtogroup');
     else if ($plugin['message'])
       $OUTPUT->show_message($plugin['message'], 'warning');
@@ -46,7 +55,12 @@
   if (($gid = get_input_value('_gid', RCUBE_INPUT_POST)) && ($ids = get_input_value('_cid', RCUBE_INPUT_POST))) {
     $plugin = $RCMAIL->plugins->exec_hook('group_delmembers', array('group_id' => $gid, 'ids' => $ids, 'source' => $source));
     
-    if (!$plugin['abort'] && $CONTACTS->remove_from_group($gid, $plugin['ids']))
+    if (!$plugin['abort'])
+      $result = $CONTACTS->remove_from_group($gid, $plugin['ids']);
+    else
+      $result = $plugin['result'];
+
+    if ($result) 
       $OUTPUT->show_message('contactremovedfromgroup');
     else if ($plugin['message'])
       $OUTPUT->show_message($plugin['message'], 'warning');
@@ -56,8 +70,11 @@
 else if ($RCMAIL->action == 'group-create') {
   if ($name = trim(get_input_value('_name', RCUBE_INPUT_POST))) {
     $plugin = $RCMAIL->plugins->exec_hook('group_create', array('name' => $name, 'source' => $source));
+
     if (!$plugin['abort'])
       $created = $CONTACTS->create_group($plugin['name']);
+    else
+      $created = $plugin['result'];
   }
   
   if ($created && $OUTPUT->ajax_call) {
@@ -72,8 +89,11 @@
 else if ($RCMAIL->action == 'group-rename') {
   if (($gid = get_input_value('_gid', RCUBE_INPUT_POST)) && ($name = trim(get_input_value('_name', RCUBE_INPUT_POST)))) {
     $plugin = $RCMAIL->plugins->exec_hook('group_rename', array('group_id' => $gid, 'name' => $name, 'source' => $source));
+
     if (!$plugin['abort'])
       $newname = $CONTACTS->rename_group($gid, $plugin['name']);
+    else
+      $newname = $plugin['result'];
   }
 
   if ($newname && $OUTPUT->ajax_call)
@@ -85,8 +105,11 @@
 else if ($RCMAIL->action == 'group-delete') {
   if ($gid = get_input_value('_gid', RCUBE_INPUT_POST)) {
     $plugin = $RCMAIL->plugins->exec_hook('group_delete', array('group_id' => $gid, 'source' => $source));
+
     if (!$plugin['abort'])
       $deleted = $CONTACTS->delete_group($gid);
+    else
+      $deleted = $plugin['result'];
   }
 
   if ($deleted)
diff --git a/program/steps/addressbook/import.inc b/program/steps/addressbook/import.inc
index ac6dc92..2390e98 100644
--- a/program/steps/addressbook/import.inc
+++ b/program/steps/addressbook/import.inc
@@ -159,7 +159,12 @@
       $a_record = $plugin['record'];
 
       // insert record and send response
-      if (!$plugin['abort'] && ($success = $CONTACTS->insert($a_record))) {
+      if (!$plugin['abort'])
+        $success = $CONTACTS->insert($a_record);
+      else
+        $success = $plugin['result'];
+
+      if ($success) {
         $IMPORT_STATS->inserted++;
         $IMPORT_STATS->names[] = $vcard->displayname;
       } else {
diff --git a/program/steps/addressbook/save.inc b/program/steps/addressbook/save.inc
index b4b9ae4..f0244b4 100644
--- a/program/steps/addressbook/save.inc
+++ b/program/steps/addressbook/save.inc
@@ -58,8 +58,12 @@
     array('id' => $cid, 'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC)));
   $a_record = $plugin['record'];
   
-  if (!$plugin['abort'] && ($result = $CONTACTS->update($cid, $a_record)))
-  {
+  if (!$plugin['abort'])
+    $result = $CONTACTS->update($cid, $a_record);
+  else
+    $result = $plugin['result'];
+
+  if ($result) {
     // LDAP DN change
     if (is_string($result) && strlen($result)>1) {
       $newcid = $result;
@@ -81,34 +85,37 @@
     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
     rcmail_overwrite_action('show');
   }
-  else
-  {
+  else {
     // show error message
-    $OUTPUT->show_message('errorsaving', 'error', null, false);
+    $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
     rcmail_overwrite_action('show');
   }
 }
 
 // insert a new contact
-else
-{
+else {
   // check for existing contacts
   $existing = $CONTACTS->search('email', $a_record['email'], true, false);
 
   // show warning message
-  if ($existing->count)
-  {
+  if ($existing->count) {
     $OUTPUT->show_message('contactexists', 'warning', null, false);
     rcmail_overwrite_action('add');
     return;
   }
 
-  $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC)));
+  $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
+    'record' => $a_record, 'source' => get_input_value('_source', RCUBE_INPUT_GPC)));
   $a_record = $plugin['record'];
 
   // insert record and send response
-  if (!$plugin['abort'] && ($insert_id = $CONTACTS->insert($a_record)))
-  {
+  if (!$plugin['abort'])
+    $insert_id = $CONTACTS->insert($a_record);
+  else
+    $insert_id = $plugin['result'];
+
+
+  if ($insert_id) {
     // add contact row or jump to the page where it should appear
     $CONTACTS->reset();
     $result = $CONTACTS->search($CONTACTS->primary_key, $insert_id);
@@ -124,12 +131,9 @@
     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
     $OUTPUT->send('iframe');
   }
-  else
-  {
+  else {
     // show error message
-    $OUTPUT->show_message('errorsaving', 'error', null, false);
+    $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
     rcmail_overwrite_action('add');
   }
 }
-
-
diff --git a/program/steps/mail/addcontact.inc b/program/steps/mail/addcontact.inc
index 7a2b69e..d46db8e 100644
--- a/program/steps/mail/addcontact.inc
+++ b/program/steps/mail/addcontact.inc
@@ -30,8 +30,7 @@
 {
   $contact_arr = $IMAP->decode_address_list(get_input_value('_address', RCUBE_INPUT_POST, true), 1, false);
   
-  if (!empty($contact_arr[1]['mailto']))
-  {
+  if (!empty($contact_arr[1]['mailto'])) {
     $contact = array(
       'email' => $contact_arr[1]['mailto'],
       'name' => $contact_arr[1]['name']
@@ -45,21 +44,23 @@
 
     // check for existing contacts
     $existing = $CONTACTS->search('email', $contact['email'], true, false);
+
     if ($done = $existing->count)
       $OUTPUT->show_message('contactexists', 'warning');
-    else
-    {
+    else {
       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
       $contact = $plugin['record'];
 
-      if (!$plugin['abort'] && ($done = $CONTACTS->insert($contact)))
+      $done = !$plugin['abort'] ? $CONTACTS->insert($contact) : $plugin['result'];
+
+      if ($done)
         $OUTPUT->show_message('addedsuccessfully', 'confirmation');
     }
   }
 }
 
 if (!$done)
-  $OUTPUT->show_message('errorsavingcontact', 'warning');
+  $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsavingcontact', 'warning');
 
 $OUTPUT->send();
 
diff --git a/program/steps/settings/delete_identity.inc b/program/steps/settings/delete_identity.inc
index 4667fd3..81609d6 100644
--- a/program/steps/settings/delete_identity.inc
+++ b/program/steps/settings/delete_identity.inc
@@ -32,12 +32,13 @@
 {
   $plugin = $RCMAIL->plugins->exec_hook('identity_delete', array('id' => $iid));
   
-  if (!$plugin['abort'] && $USER->delete_identity($iid)) {
+  $deleted = !$plugin['abort'] ? $USER->delete_identity($iid) : $plugin['result'];
+
+  if ($deleted)
     $OUTPUT->show_message('deletedsuccessfully', 'confirmation', null, false);
-  }
-  else {
-    $OUTPUT->show_message('nodeletelastidentity', 'error', null, false);
-  }
+  else
+    $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'nodeletelastidentity', 'error', null, false);
+
   // send response
   if ($OUTPUT->ajax_call)
     $OUTPUT->send();
@@ -48,5 +49,3 @@
 
 // go to identities page
 rcmail_overwrite_action('identities');
-
-
diff --git a/program/steps/settings/save_identity.inc b/program/steps/settings/save_identity.inc
index 313e9df..30cc124 100644
--- a/program/steps/settings/save_identity.inc
+++ b/program/steps/settings/save_identity.inc
@@ -83,23 +83,25 @@
   if ($save_data['reply-to'])
     $save_data['reply-to'] = idn_to_ascii($save_data['reply-to']);
 
-  if (!$plugin['abort'] && ($updated = $USER->update_identity($iid, $save_data)))
-  {
+  if (!$plugin['abort'])
+    $updated = $USER->update_identity($iid, $save_data);
+  else
+    $updated = $plugin['result'];
+
+  if ($updated) {
     $OUTPUT->show_message('successfullysaved', 'confirmation');
-    
+
     if (!empty($_POST['_standard']))
       $default_id = get_input_value('_iid', RCUBE_INPUT_POST);
-    
-    if ($_POST['_framed'])
-    {
+
+    if ($_POST['_framed']) {
       // update the changed col in list
       // ...
     }
   }
-  else if ($plugin['abort'] || $DB->is_error())
-  {
+  else {
     // show error message
-    $OUTPUT->show_message('errorsaving', 'error', null, false);
+    $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
     rcmail_overwrite_action('edit-identity');
     return;
   }
@@ -118,8 +120,12 @@
   $save_data['bcc']      = idn_to_ascii($save_data['bcc']);
   $save_data['reply-to'] = idn_to_ascii($save_data['reply-to']);
 
-  if (!$plugin['abort'] && $save_data['email'] && ($insert_id = $USER->insert_identity($save_data)))
-  {
+  if (!$plugin['abort'])
+    $insert_id = $save_data['email'] ? $USER->insert_identity($save_data) : null;
+  else
+    $insert_id = $plugin['result'];
+
+  if ($insert_id) {
     $OUTPUT->show_message('successfullysaved', 'confirmation', null, false);
     
     $_GET['_iid'] = $insert_id;
@@ -127,10 +133,9 @@
     if (!empty($_POST['_standard']))
       $default_id = $insert_id;
   }
-  else
-  {
+  else {
     // show error message
-    $OUTPUT->show_message('errorsaving', 'error', null, false);
+    $OUTPUT->show_message($plugin['message'] ? $plugin['message'] : 'errorsaving', 'error', null, false);
     rcmail_overwrite_action('edit-identity');
     return;
   }
@@ -145,5 +150,3 @@
 
 // go to next step
 rcmail_overwrite_action('identities');
-
-

--
Gitblit v1.9.1