/** * Removes from the temporary tables those records that do not need processing * because they are identical. * * In *push* mode this will also remove any rows in the CiviCRM temp table * where there's an email match in the mailchimp table but the cid_guess is * different. This is to cover the case when two contacts in CiviCRM have the * same email and both are added to the membership group. Without this the * Push operation would attempt to craeate a 2nd Mailchimp member but with the * email address that's already on the list. This would mean the names kept * getting flipped around since it would be updating the same member twice - * very confusing. * * So for deleting the contacts from the CiviCRM table on *push* we avoid * this. However on *pull* we leave the contact in the table - they will then * get removed from the group, leaving just the single contact/member with * that particular email address. * * @param string $mode pull|push. * @return int */ public function removeInSync($mode) { // In push mode, delete duplicate CiviCRM contacts. $doubles = 0; if ($mode == 'push') { $doubles = CRM_Mailchimp_Sync::runSqlReturnAffectedRows('DELETE c FROM tmp_mailchimp_push_c c INNER JOIN tmp_mailchimp_push_m m ON c.email=m.email AND m.cid_guess != c.contact_id; '); if ($doubles) { CRM_Mailchimp_Utils::checkDebug("removeInSync removed {$doubles} contacts who are in the membership group but have the same email address as another contact that is also in the membership group."); } } // Delete records have the same hash - these do not need an update. // count for testing purposes. $dao = CRM_Core_DAO::executeQuery("SELECT COUNT(c.email) co FROM tmp_mailchimp_push_m m\n INNER JOIN tmp_mailchimp_push_c c ON m.cid_guess = c.contact_id AND m.hash = c.hash;"); $dao->fetch(); $count = $dao->co; if ($count > 0) { CRM_Core_DAO::executeQuery("DELETE m, c\n FROM tmp_mailchimp_push_m m\n INNER JOIN tmp_mailchimp_push_c c ON m.cid_guess = c.contact_id AND m.hash = c.hash;"); } CRM_Mailchimp_Utils::checkDebug("removeInSync removed {$count} in-sync contacts."); return $count + $doubles; }