diff --git a/CRM/Mailchimp/Form/Pull.php b/CRM/Mailchimp/Form/Pull.php index 0f7db23..686b6a9 100644 --- a/CRM/Mailchimp/Form/Pull.php +++ b/CRM/Mailchimp/Form/Pull.php @@ -6,7 +6,21 @@ class CRM_Mailchimp_Form_Pull extends CRM_Core_Form { const END_URL = 'civicrm/mailchimp/pull'; const END_PARAMS = 'state=done'; const BATCH_COUNT = 10; - + + function preProcess() { + $state = CRM_Utils_Request::retrieve('state', 'String', CRM_Core_DAO::$_nullObject, FALSE, 'tmp', 'GET'); + if ($state == 'done') { + $stats = array(); + $listmembercount = civicrm_api('Mailchimp' , 'getmembercount' , array('version' => 3)); + $total= array_sum($listmembercount['values']); + $stats = CRM_Core_BAO_Setting::getItem(CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'pull_stats'); + $ignored = $total - array_sum($stats); + $stats['Total'] = $total; + $stats['Ignored'] = $ignored; + $this->assign('stats', $stats); + } + } + public function buildQuickForm() { // Create the Submit Button. $buttons = array( @@ -65,6 +79,8 @@ static function getRunner() { 'errorMode'=> CRM_Queue_Runner::ERROR_ABORT, 'onEndUrl' => CRM_Utils_System::url(self::END_URL, self::END_PARAMS, TRUE, NULL, FALSE), )); + $query = "UPDATE civicrm_setting SET value = NULL WHERE name = 'pull_stats'"; + CRM_Core_DAO::executeQuery($query); return $runner; } return FALSE; @@ -72,10 +88,28 @@ static function getRunner() { static function syncLists(CRM_Queue_TaskContext $ctx, $listid) { + $apikey = CRM_Core_BAO_Setting::getItem(CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'api_key'); + $contactColumns = array(); + $contacts = array(); + $dc = 'us'.substr($apikey, -1); + $url = 'http://'.$dc.'.api.mailchimp.com/export/1.0/list?apikey='.$apikey.'&id='.$listid; + $json = file_get_contents($url); + $temp = explode("\n", $json); + array_pop($temp); + foreach($temp as $key => $value){ + if($key == 0){ + $contactColumns = json_decode($value,TRUE); + continue; + } + $data = json_decode($value,TRUE); + $contacts[$listid][] = array_combine($contactColumns, $data); + } + $lists = array(); $listmembercount = array(); $listmembercount = civicrm_api('Mailchimp' , 'getmembercount' , array('version' => 3)); $lists = civicrm_api('Mailchimp' , 'getlists' , array('version' => 3)); + $mcGroups = civicrm_api('Mailchimp' , 'getgroupid' , array('version' => 3,'id' => $listid)); // get member count $count = $listmembercount['values'][$listid]; @@ -86,10 +120,11 @@ static function syncLists(CRM_Queue_TaskContext $ctx, $listid) { $i = 0; while ($i < $rounds) { $start = $i * self::BATCH_COUNT; + $contactsarray = array_slice($contacts[$listid], $start, self::BATCH_COUNT, TRUE); $counter = ($rounds > 1) ? ($start + self::BATCH_COUNT) : $count; $task = new CRM_Queue_Task( array('CRM_Mailchimp_Form_Pull', 'syncContacts'), - array($listid, $start), + array($listid, array($contactsarray), array($mcGroups)), "Pulling '{$lists['values'][$listid]}' - Contacts {$counter} of {$count}" ); @@ -100,27 +135,12 @@ static function syncLists(CRM_Queue_TaskContext $ctx, $listid) { return CRM_Queue_Task::TASK_SUCCESS; } - static function syncContacts(CRM_Queue_TaskContext $ctx, $listid, $start) { + static function syncContacts(CRM_Queue_TaskContext $ctx, $listid, $contactsarray, $mcGroups) { - $apikey = CRM_Core_BAO_Setting::getItem(CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'api_key'); - $defaultgroup = CRM_Core_BAO_Setting::getItem(CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'default_group'); - $contactColumns = array(); - $contacts = array(); $groupContact = array(); - $dc = 'us'.substr($apikey, -1); - $url = 'http://'.$dc.'.api.mailchimp.com/export/1.0/list?apikey='.$apikey.'&id='.$listid; - $json = file_get_contents($url); - $temp = explode("\n", $json); - foreach($temp as $key => $value){ - if($key == 0){ - $contactColumns = json_decode($value,TRUE); - continue; - } - $data = json_decode($value,TRUE); - $contacts[$listid][] = array_combine($contactColumns, $data); - } - $contactsarray = array_slice($contacts[$listid], $start, self::BATCH_COUNT, TRUE); - $mcGroups = civicrm_api('Mailchimp' , 'getgroupid' , array('version' => 3,'id' => $listid)); + $defaultgroup = CRM_Core_BAO_Setting::getItem(CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'default_group'); + $mcGroups = array_shift($mcGroups); + $contactsarray = array_shift($contactsarray); foreach($contactsarray as $key => $contact){ $updateParams = array( 'EMAIL' => $contact['Email Address'], @@ -128,30 +148,51 @@ static function syncContacts(CRM_Queue_TaskContext $ctx, $listid, $start) { 'LNAME' => $contact['Last Name'], ); $contactID = CRM_Mailchimp_Utils::updateContactDetails($updateParams); + if(!empty($updateParams)) { + if($updateParams['status']['Added'] == 1) { + $setting = CRM_Core_BAO_Setting::getItem(CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'pull_stats'); + CRM_Core_BAO_Setting::setItem(array('Added' => (1 + $setting['Added']), 'Updated' => $setting['Updated']), + CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'pull_stats' + ); + } + if($updateParams['status']['Updated'] == 1) { + $setting = CRM_Core_BAO_Setting::getItem(CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'pull_stats'); + CRM_Core_BAO_Setting::setItem(array('Updated' => (1 + $setting['Updated']), 'Added' => $setting['Added']), + CRM_Mailchimp_Form_Setting::MC_SETTING_GROUP, 'pull_stats'); + } + } + if(!empty($contactID)) { - foreach ($contact as $parms => $value){ if(!empty($mcGroups)){ - foreach ($mcGroups['values'] as $mcGroupDetails) { - //check whether a contact belongs to more than one group under one grouping - $valuearray = explode(',', $value); - foreach($valuearray as $val){ - if (in_array(trim($val), $mcGroupDetails)) { - $civiGroupID = CRM_Mailchimp_Utils::getGroupIdForMailchimp($listid, $mcGroupDetails['groupingid'] , $mcGroupDetails['groupid']); - if(!empty($civiGroupID)) { - $groupContact[$civiGroupID][] = $contactID; - } else { + foreach ($contact as $parms => $value){ + foreach ($mcGroups['values'] as $mcGroupDetails) { + //check whether a contact belongs to more than one group under one grouping + $valuearray = explode(',', $value); + if(empty($valuearray[0])) { + if(in_array($parms, $mcGroupDetails )) { + //Group Details are present but the contact is not assigned to any group in mailchimp $groupContact[$defaultgroup][] = $contactID; + break; } } - } + foreach($valuearray as $val){ + if (in_array(trim($val), $mcGroupDetails)) { + $civiGroupID = CRM_Mailchimp_Utils::getGroupIdForMailchimp($listid, $mcGroupDetails['groupingid'] , $mcGroupDetails['groupid']); + if(!empty($civiGroupID)) { + $groupContact[$civiGroupID][] = $contactID; + } else { + $groupContact[$defaultgroup][] = $contactID; + } + } + } + } } - } else { + }else { // if a list doesn't have groups,assign the contact to default group $groupContact[$defaultgroup][] = $contactID; } } } - } foreach($groupContact as $groupID => $contactIDs ){ CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID, 'Admin', 'Added'); } diff --git a/CRM/Mailchimp/Utils.php b/CRM/Mailchimp/Utils.php index 3328959..2b095a0 100644 --- a/CRM/Mailchimp/Utils.php +++ b/CRM/Mailchimp/Utils.php @@ -148,11 +148,11 @@ static function getGroupIdForMailchimp($listID, $groupingID, $groupID) { /* * Create/Update contact details in CiviCRM, based on the data from Mailchimp webhook */ - static function updateContactDetails($params, $delay = FALSE) { + static function updateContactDetails(&$params, $delay = FALSE) { if (empty($params)) { return NULL; } - + $params['status'] = array('Added' => 0, 'Updated' => 0); $contactParams = array( 'version' => 3, @@ -167,7 +167,10 @@ static function updateContactDetails($params, $delay = FALSE) { sleep(20); } $contactids = array(); - $query = "SELECT `contact_id` FROM `civicrm_email` WHERE `email` = %1"; + $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; @@ -178,6 +181,16 @@ static function updateContactDetails($params, $delay = FALSE) { } if(count($contactids) == 1) { $contactParams['id'] = $contactids[0]; + $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; } // Create/Update Contact details $contactResult = civicrm_api('Contact' , 'create' , $contactParams); diff --git a/api/v3/Mailchimp.php b/api/v3/Mailchimp.php index c427b8d..d7b28a9 100644 --- a/api/v3/Mailchimp.php +++ b/api/v3/Mailchimp.php @@ -102,6 +102,7 @@ function civicrm_api3_mailchimp_getgroupid($params) { foreach($result['groups'] as $group) { $groups[] = array( 'groupingid' => $result['id'], + 'groupingname' => $result['name'], 'groupname' => $group['name'], 'groupid' => $group['id'], ); diff --git a/mailchimp.php b/mailchimp.php index fb4c8fa..d22a218 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -42,12 +42,12 @@ function mailchimp_civicrm_install() { 'url' => 'civicrm/mailchimp/settings&reset=1', ), array( - 'label' => ts('Sync Contacts To Mailchimp'), + 'label' => ts('Sync Civi Contacts To Mailchimp'), 'name' => 'Mailchimp_Sync', 'url' => 'civicrm/mailchimp/sync&reset=1', ), array( - 'label' => ts('Bulk Import Mailchimp Contacts'), + 'label' => ts('Sync Mailchimp Contacts To Civi'), 'name' => 'Mailchimp_Pull', 'url' => 'civicrm/mailchimp/pull&reset=1', ), diff --git a/templates/CRM/Mailchimp/Form/Pull.tpl b/templates/CRM/Mailchimp/Form/Pull.tpl index 7c864eb..9d12e55 100644 --- a/templates/CRM/Mailchimp/Form/Pull.tpl +++ b/templates/CRM/Mailchimp/Form/Pull.tpl @@ -1,7 +1,21 @@
{ts}Added{/ts}: | {$stats.Added} |
{ts}Updated{/ts}: | {$stats.Updated} |
{ts}Ignored{/ts}: | {$stats.Ignored} |
{ts}Total{/ts}: | {$stats.Total} |