Skip to content

Commit

Permalink
Merge pull request #71 from Kajakaran/master
Browse files Browse the repository at this point in the history
Fixes for stats, accuracy, speed
  • Loading branch information
deepak-srivastava committed Jul 14, 2014
2 parents 0728673 + 57b95ad commit fb32b26
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 40 deletions.
109 changes: 75 additions & 34 deletions CRM/Mailchimp/Form/Pull.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -65,17 +79,37 @@ 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;
}

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];

Expand All @@ -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}"
);

Expand All @@ -100,58 +135,64 @@ 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'],
'FNAME' => $contact['First Name'],
'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');
}
Expand Down
19 changes: 16 additions & 3 deletions CRM/Mailchimp/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions api/v3/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
);
Expand Down
4 changes: 2 additions & 2 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
Expand Down
16 changes: 15 additions & 1 deletion templates/CRM/Mailchimp/Form/Pull.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
<div class="crm-block crm-form-block crm-campaignmonitor-sync-form-block">
<div class="crm-block crm-form-block crm-campaignmonitor-sync-form-block">

{if $smarty.get.state eq 'done'}
<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 colspan=2><td>{ts}Total{/ts}:</td><td>{$stats.Total}</td></tr>
</table>
</div>
{/if}

<div class="crm-block crm-form-block crm-campaignmonitor-sync-form-block">
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl"}
</div>
</div>

</div>

0 comments on commit fb32b26

Please sign in to comment.