You are here

function mailchimp_lists_update_member_batch in Mailchimp 7.5

Same name and namespace in other branches
  1. 7.4 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_update_member_batch()

Batch processor for member mergevar updates.

1 string reference to 'mailchimp_lists_update_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 438

Code

function mailchimp_lists_update_member_batch($entity_type, $bundle_name, $field, $mergefields, $list_id, &$context) {
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['entity_info'] = entity_get_info($entity_type);
    $context['sandbox']['progress'] = 0;
    $context['results']['errors'] = 0;
    $context['results']['updates'] = 0;
    $context['results']['no_entity'] = 0;
  }
  $batch_size = variable_get('mailchimp_batch_limit', 100);

  // Query MailChimp for list/audience subscribers in batches to limit resource usage.
  $options = array(
    'offset' => $context['sandbox']['progress'],
    'count' => $batch_size,
  );

  // Get the email addresses of all subscribers of the MailChimp list/audience.
  $subscriber_emails = array();
  $subscribers = array();
  $update_queue = array();
  $matches = mailchimp_get_members($field['settings']['mc_list_id'], 'subscribed', $options);
  if (!empty($matches) && $matches->total_items > 0) {
    if (!isset($context['sandbox']['max'])) {
      $context['sandbox']['max'] = $matches->total_items;
    }
    foreach ($matches->members as $result) {
      $subscribers[strtolower($result->email_address)] = $result;
      $subscriber_emails[] = strtolower($result->email_address);
    }
  }

  // Split up mergefield token to determine referenced entity field or property.
  $entity_email_field_parts = explode(':', str_replace(array(
    ']',
    '[',
  ), '', $mergefields['EMAIL']));

  // Determine the Drupal field name from the mergefield token.
  $entity_email_field_name = str_replace('-', '_', $entity_email_field_parts[1]);
  $entity_email_field_info = field_info_field($entity_email_field_name);
  if (!empty($entity_email_field_info)) {

    // Set the default field value column in the field database table.
    $entity_email_field_column = 'value';
  }
  else {

    // If not a field, then the mergefield token refers to a property.
    $entity_email_property_column = $entity_email_field_parts[1];
  }

  // Unable to match email mergefield token with a field/property of the entity.
  if (empty($entity_email_field_column) && empty($entity_email_property_column)) {
    watchdog('mailchimp_lists', 'Could not update merge variables. Unable to match token @email with a field or property of the @entity entity.', array(
      '@email' => $mergefields['EMAIL'],
      '@entity' => $entity_type,
    ), WATCHDOG_ERROR);
    drupal_set_message(t('Unable to update merge variables. Please see your error log for details.'), 'error');

    // Not possible to send merge variables. End batch process.
    $context['finished'] = 1;
    return;
  }

  // For each list/audience subscriber email, load the associated entity.
  foreach ($subscriber_emails as $subscriber_email) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', $entity_type);
    if ($entity_type !== 'user') {
      $query
        ->entityCondition('bundle', $bundle_name);
    }
    if (!empty($entity_email_field_column)) {
      $query
        ->fieldCondition($entity_email_field_name, $entity_email_field_column, $subscriber_email, '=');
    }
    elseif (!empty($entity_email_property_column)) {
      $query
        ->propertyCondition($entity_email_property_column, $subscriber_email, '=');
    }
    $query_results = $query
      ->execute();
    if (isset($query_results[$entity_type])) {
      $entity_id = array_keys($query_results[$entity_type])[0];
      $entity = entity_load_single($entity_type, $entity_id);
    }

    // If an entity exists, populate the mergevars for MailChimp.
    if (!empty($entity)) {
      $merge_vars = _mailchimp_lists_mergevars_populate($mergefields, $entity, $entity_type, $field['settings']['mc_list_id']);
      if ($merge_vars['EMAIL'] && isset($subscribers[strtolower($merge_vars['EMAIL'])])) {
        $update_queue[] = array(
          'email' => $merge_vars['EMAIL'],
          // Preserve subscriber's email type selection.
          'email_type' => $subscribers[strtolower($merge_vars['EMAIL'])]->email_type,
          'merge_vars' => $merge_vars,
        );
      }
    }
    else {
      $context['results']['no_entity']++;
    }
  }

  // Send updated mergevars to MailChimp.
  $batch_result = mailchimp_lists_execute_mergevar_batch_update($list_id, $update_queue);

  // Log any errors returned by MailChimp.
  $context['results']['errors'] += $batch_result->errored_operations;

  // Update batch operation progress.
  $context['sandbox']['progress'] += count($subscriber_emails);
  $context['results']['updates'] += count($subscriber_emails);
  $context['message'] = t('Updating items %count - %next of %total.', array(
    '%count' => $context['sandbox']['progress'],
    '%next' => $context['sandbox']['progress'] + $batch_size,
    '%total' => $context['sandbox']['max'],
  ));
  $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}