Пример #1
0
 /**
  * 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);
 }