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 b3018cc commit 021a906
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
10 changes: 9 additions & 1 deletion imp/lib/Compose.php
Expand Up @@ -1357,7 +1357,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
25 changes: 23 additions & 2 deletions turba/lib/Api.php
Expand Up @@ -631,13 +631,19 @@ 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.
* @since 4.2.9
*
* @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 $cfgSources, $injector, $prefs;

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

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
1 change: 1 addition & 0 deletions turba/lib/Turba.php
Expand Up @@ -713,6 +713,7 @@ static public function addBrowseJs()
* Return an array of all available attributes of type 'email'.
*
* @return array An array of email fields.
* @since 4.2.9
*/
public static function getAvailableEmailFields()
{
Expand Down

0 comments on commit 021a906

Please sign in to comment.