You are here

function mailchimp_lists_populate_member_batch in Mailchimp 7.3

Same name and namespace in other branches
  1. 8 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_populate_member_batch()
  2. 2.x modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_populate_member_batch()

Batch processor for member mergevar updates to built the mergevar arrays.

1 string reference to 'mailchimp_lists_populate_member_batch'
mailchimp_lists_update_member_merge_values in modules/mailchimp_lists/mailchimp_lists.module
Triggers an update of all merge field values for appropriate entities.

File

modules/mailchimp_lists/mailchimp_lists.module, line 431

Code

function mailchimp_lists_populate_member_batch($entity_type, $bundle_name, $field, $mergefields, &$context) {
  if (!isset($context['sandbox']['progress'])) {

    // Load up all our eligible entities.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $entity_type);
    if ($entity_type !== 'user') {
      $query
        ->entityCondition('bundle', $bundle_name);
    }
    $query_results = $query
      ->execute();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = isset($query_results[$entity_type]) ? count($query_results[$entity_type]) : 0;
    if ($context['sandbox']['max']) {
      $context['sandbox']['entity_ids'] = array_keys($query_results[$entity_type]);
      $context['results']['update_queue'] = array();
    }
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $batch_size = 50;
    $item_ids = array_slice($context['sandbox']['entity_ids'], $context['sandbox']['progress'], $batch_size);
    $entities = entity_load($entity_type, $item_ids);
    foreach ($entities as $entity) {
      $merge_vars = _mailchimp_lists_mergevars_populate($mergefields, $entity, $entity_type);
      if ($merge_vars['EMAIL'] && isset($context['results']['subscribers'][strtolower($merge_vars['EMAIL'])])) {
        $context['results']['update_queue'][] = array(
          'email' => array(
            'email' => $merge_vars['EMAIL'],
          ),
          // Preserve subscribers's email type selection:
          'email_type' => $context['results']['subscribers'][strtolower($merge_vars['EMAIL'])]['email_type'],
          'merge_vars' => $merge_vars,
        );
      }
      $context['sandbox']['progress']++;
    }
    $context['message'] = t('Checking for changes on items %count - %next.', array(
      '%count' => $context['sandbox']['progress'],
      '%next' => $context['sandbox']['progress'] + $batch_size,
    ));
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}