Skip to content

Commit

Permalink
Merge pull request #72 from Kajakaran/master
Browse files Browse the repository at this point in the history
Fixes for contacts with no primary email address
  • Loading branch information
deepak-srivastava committed Jul 16, 2014
2 parents fb32b26 + b3a0311 commit ac7366d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
77 changes: 58 additions & 19 deletions CRM/Mailchimp/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,43 +161,82 @@ static function updateContactDetails(&$params, $delay = FALSE) {
'last_name' => $params['LNAME'],
'email' => $params['EMAIL'],
);

if($delay){
//To avoid a new duplicate contact to be created as both profile and upemail events are happening at the same time
sleep(20);
}
$contactids = array();
$query = "
SELECT `contact_id` FROM civicrm_email ce
INNER JOIN civicrm_contact cc ON ce.`contact_id` = cc.id
WHERE ce.email = %1 AND ce.is_primary = 1 AND cc.is_deleted = 0 ";
$dao = CRM_Core_DAO::executeQuery($query, array( '1' => array("{$params['EMAIL']}", 'String')));
while ($dao->fetch()) {
$contactids[] = $dao->contact_id;
}
$contactids = CRM_Mailchimp_Utils::getContactFromEmail($params['EMAIL']);

if(count($contactids) > 1) {
CRM_Core_Error::debug_log_message( 'Mailchimp Pull/Webhook: Multiple contacts found for the email address '. print_r($params['EMAIL'], true), $out = false );
return NULL;
}
if(count($contactids) == 1) {
$contactParams['id'] = $contactids[0];
$contactParams = CRM_Mailchimp_Utils::updateParamsExactMatch($contactids, $params);
$params['status']['Updated'] = 1;
unset($contactParams['contact_type']);
// Don't update firstname/lastname if it was empty
if(empty($params['FNAME']))
unset($contactParams['first_name']);
if(empty($params['LNAME']))
unset ($contactParams['last_name']);
}
if(empty($contactids)) {
$params['status']['Added'] = 1;
//check for contacts with no primary email address
$id = CRM_Mailchimp_Utils::getContactFromEmail($params['EMAIL'], FALSE);

if(count($id) > 1) {
CRM_Core_Error::debug_log_message( 'Mailchimp Pull/Webhook: Multiple contacts found for the email address which is not primary '. print_r($params['EMAIL'], true), $out = false );
return NULL;
}
if(count($id) == 1) {
$contactParams = CRM_Mailchimp_Utils::updateParamsExactMatch($id, $params);
$params['status']['Updated'] = 1;
}
// Else create new contact
if(empty($id)) {
$params['status']['Added'] = 1;
}

}
// Create/Update Contact details
$contactResult = civicrm_api('Contact' , 'create' , $contactParams);

return $contactResult['id'];
}

static function getContactFromEmail($email, $primary = TRUE) {
$primaryEmail = 1;
if(!$primary) {
$primaryEmail = 0;
}
$contactids = array();
$query = "
SELECT `contact_id` FROM civicrm_email ce
INNER JOIN civicrm_contact cc ON ce.`contact_id` = cc.id
WHERE ce.email = %1 AND ce.is_primary = {$primaryEmail} AND cc.is_deleted = 0 ";
$dao = CRM_Core_DAO::executeQuery($query, array( '1' => array($email, 'String')));
while($dao->fetch()) {
$contactids[] = $dao->contact_id;
}
return $contactids;
}

static function updateParamsExactMatch($contactids = array(), $params) {
$contactParams =
array(
'version' => 3,
'contact_type' => 'Individual',
'first_name' => $params['FNAME'],
'last_name' => $params['LNAME'],
'email' => $params['EMAIL'],
);
if(count($contactids) == 1) {
$contactParams['id'] = $contactids[0];
unset($contactParams['contact_type']);
// Don't update firstname/lastname if it was empty
if(empty($params['FNAME']))
unset($contactParams['first_name']);
if(empty($params['LNAME']))
unset ($contactParams['last_name']);
}
return $contactParams;
}
/*
* Function to get the associated CiviCRM Groups IDs for the Grouping array sent from Mialchimp Webhook
*/
Expand Down
6 changes: 3 additions & 3 deletions templates/CRM/Mailchimp/Form/Pull.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<div class="help">
{ts}Import completed with result counts as:{/ts}<br/>
<table class="form-layout-compressed bold">
<tr><td>{ts}Added{/ts}:</td><td>{$stats.Added}</td></tr>
<tr><td>{ts}Updated{/ts}:</td><td>{$stats.Updated}</td></tr>
<tr><td>{ts}Ignored{/ts}:</td><td>{$stats.Ignored}</td></tr>
<tr><td>{ts}Contacts Added{/ts}:</td><td>{$stats.Added}</td></tr>
<tr><td>{ts}Contacts Updated{/ts}:</td><td>{$stats.Updated}</td></tr>
<tr><td>{ts}Contacts Ignored{/ts}:</td><td>{$stats.Ignored}&nbsp; (Multiple Contacts Matches)</td></tr>
<tr colspan=2><td>{ts}Total{/ts}:</td><td>{$stats.Total}</td></tr>
</table>
</div>
Expand Down

0 comments on commit ac7366d

Please sign in to comment.