Skip to content

Commit

Permalink
Add, and use, option to detect duplicate on import using any email fi…
Browse files Browse the repository at this point in the history
…eld.

Bug: 14119 Used when automatically adding recipients to default
addressbook.
  • Loading branch information
mrubinsk committed Oct 1, 2015
1 parent 8db98a6 commit f8caeaa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 9 additions & 1 deletion imp/lib/Compose.php
Expand Up @@ -1551,7 +1551,15 @@ public function _saveRecipients(Horde_Mail_Rfc822_List $recipients)
: $recipient->personal;

try {
$registry->call('contacts/import', array(array('name' => $name, 'email' => $recipient->bare_address), 'array', $abook));
$registry->call(
'contacts/import',
array(
array('name' => $name, 'email' => $recipient->bare_address),
'array',
$abook,
array('match_on_email' => true)
)
);
$notification->push(sprintf(_("Entry \"%s\" was successfully added to the address book"), $name), 'horde.success');
} catch (Turba_Exception_ObjectExists $e) {
} catch (Horde_Exception $e) {
Expand Down
24 changes: 22 additions & 2 deletions turba/lib/Api.php
Expand Up @@ -656,13 +656,18 @@ public function getHighestModSeq($id = null)
* text/x-vcard, and activesync.
* @param string $source The source into which the contact will be
* imported.
* @param array $options Additional options:
* - match_on_email: (boolean) If true, will detect entry as duplicate
* if ANY email field matches. Useful for
* automatically adding contacts from an
* email application, such as IMP.
*
* @return string The new UID.
*
* @throws Turba_Exception
* @throws Turba_Exception_ObjectExists
*/
public function import($content, $contentType = 'array', $source = null)
public function import($content, $contentType = 'array', $source = null, array $options = array())
{
global $injector;

Expand Down Expand Up @@ -728,8 +733,23 @@ public function import($content, $contentType = 'array', $source = null)
}
}

if (!empty($options['match_on_email'])) {
$content_copy = array();
foreach (Turba::getAvailableEmailFields() as $field) {
if (!empty($content[$field])) {
$rfc = new Horde_Mail_Rfc822();
$email = $rfc->parseAddressList($content[$field]);
$content_copy[$field] = (string)$email;
}
}
} else {
$content_copy = $content;
}

// Check if the entry already exists in the data source.
$result = $driver->search($content);
$result = $driver->search(
$content_copy, null, !empty($options['match_on_email']) ? 'OR' : 'AND');

if (count($result)) {
throw new Turba_Exception_ObjectExists(_("Already Exists"));
}
Expand Down

0 comments on commit f8caeaa

Please sign in to comment.