/** * Syncs the users of the list * * @return void */ public function batch() { // Check for a valid token. If invalid, send a 403 with the error message. JSession::checkToken('request') or $this->sendResponse(new Exception(JText::_('JINVALID_TOKEN'), 403)); // Put in a buffer to silence noise. ob_start(); // Remove the script time limit. @set_time_limit(0); $state = CmcSyncerState::getState(); $chimp = new CmcHelperChimp(); $members = $chimp->listMembers($state->lists[0]['mc_id'], 'subscribed', null, $state->offset, $state->batchSize); // Split the array of emails to 50 items - the max number possible for the listMemberInfo function $emails = array_chunk(array_map(function ($ar) { return $ar['email']; }, $members['data']), 50); $info = array(); // Let's fetch all the info about our members foreach ($emails as $chunks) { $memberInfo = $chimp->listMemberInfo($state->lists[0]['mc_id'], $chunks); if (!$memberInfo['errors']) { $info = array_merge($info, $memberInfo['data']); } } // Save the users in our database CmcHelperUsers::save($info, $state->lists[0]['id'], $state->lists[0]['mc_id']); $pages = $state->lists[0]['toSync'] / $state->batchSize; if ($state->offset < $pages) { $state->offset = $state->offset + 1; $state->header = JText::sprintf('COM_CMC_BATCH_SYNC_IN_LIST', $state->lists[0]['name']); $state->message = JText::sprintf('COM_CMC_BATCH_SYNC_PROGRESS', $state->offset * $state->batchSize, $state->batchSize); } if ($state->offset >= $pages) { // First list in the array was synced, lets remove it $oldList = array_shift($state->lists); // If we still have lists, then let us reset the offset if (count($state->lists)) { $state->header = JText::sprintf('COM_CMC_BATCH_SYNC_IN_OLD_LIST_COMPLETE', $oldList['name']); $state->message = JText::sprintf('COM_CMC_BATCH_SYNC_IN_OLD_LIST_COMPLETE_DESC', $oldList['toSync'], $oldList['name'], $state->lists[0]['name']); $state->offset = 0; } else { $state->header = JText::_('COM_CMC_SYNC_COMPLETE'); $state->message = '<div class="alert alert-info">' . JText::_('COM_CMC_SYNC_COMPLETE_DESC') . '</div>'; } } CmcSyncerState::setState($state); $this->sendResponse($state); }
<?php /** * @author Daniel Dimitrov <*****@*****.**> * @date 28.08.13 * * @copyright Copyright (C) 2008 - 2012 compojoom.com . All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE */ defined('_JEXEC') or die('Restricted access'); CompojoomHtmlBehavior::bootstrap31(true, true, true, false); jimport('joomla.filter.output'); JHTML::_('stylesheet', 'media/com_cmc/backend/css/cmc.css'); JHTML::_('script', 'media/com_cmc/backend/js/sync.js'); // Load bootstrap $chimp = new CmcHelperChimp(); $lists = $chimp->lists(); ?> <script type="text/javascript"> jQuery(document).ready(function () { new cmcSync(); }); </script> <div class="compojoom-bootstrap" style="clear: both"> <div class="box-info"> <h2 id="cmc-progress-header"><?php echo JText::_('COM_CMC_SYNCER_HEADER_INIT'); ?> </h2>
/** * Subscribe a user to mailchimp and if set create an entry for this user in our database * * @param string $listId - the list id * @param string $email - the email of the user * @param string $firstname - the first name of the user * @param string $lastname - the last name of the user * @param array $groupings - any groupings (merge fields + interest fields) * @param string $email_type - the type of email the user wants to receive * @param bool $update - are we updating an existing user * @param bool $updateLocal - shall we create an entry for this user in our DB? * * @return bool * * @throws Exception */ public static function subscribe($listId, $email, $firstname, $lastname, $groupings = array(), $email_type = "html", $update = false, $updateLocal = false) { $api = new CmcHelperChimp(); $merge_vars = array_merge(array('FNAME' => $firstname, 'LNAME' => $lastname), $groupings); // By default this sends a confirmation email - you will not see new members // until the link contained in it is clicked! $api->listSubscribe($listId, $email, $merge_vars, $email_type, false, $update); if ($api->errorCode) { JFactory::getApplication()->enqueueMessage(JTEXT::_("COM_CMC_SUBSCRIBE_FAILED") . " " . $api->errorCode . " / " . $api->errorMessage, 'error'); return false; } if ($updateLocal) { JLoader::discover('cmcModel', JPATH_ADMINISTRATOR . '/components/com_cmc/models/'); $subscription = CmcHelperUsers::getSubscription($email, $listId); $memberInfo = $api->listMemberInfo($listId, $email); if ($memberInfo['success']) { $memberInfo = $memberInfo['data'][0]; } $model = JModelLegacy::getInstance('User', 'CmcModel'); $user = CmcHelperUsers::getJoomlaUsers(array($email)); $saveData = CmcHelperUsers::bind($memberInfo, $user); if ($subscription) { $saveData['id'] = $subscription->id; } // Update in the local db $model->save($saveData); } return true; }