You are here

function _mailchimp_lists_subscription_has_changed in Mailchimp 7.3

Same name and namespace in other branches
  1. 8 modules/mailchimp_lists/mailchimp_lists.module \_mailchimp_lists_subscription_has_changed()
  2. 7.5 modules/mailchimp_lists/mailchimp_lists.module \_mailchimp_lists_subscription_has_changed()
  3. 7.4 modules/mailchimp_lists/mailchimp_lists.module \_mailchimp_lists_subscription_has_changed()
  4. 2.x modules/mailchimp_lists/mailchimp_lists.module \_mailchimp_lists_subscription_has_changed()

Helper function to avoid sending superfluous updates to MailChimp.

This is necessary due to the nature of the field implementation of subscriptions. If we don't do this, we send an update to MailChimp every time an entity is updated.

returns bool

1 call to _mailchimp_lists_subscription_has_changed()
mailchimp_lists_process_subscribe_form_choices in modules/mailchimp_lists/mailchimp_lists.module
Processor for various list form submissions.

File

modules/mailchimp_lists/mailchimp_lists.module, line 299

Code

function _mailchimp_lists_subscription_has_changed($instance, $field, $entity, $email, $choices) {
  if (isset($entity->original)) {
    $new_entity_wrapper = entity_metadata_wrapper($instance['entity_type'], $entity);
    $old_entity_wrapper = entity_metadata_wrapper($instance['entity_type'], $entity->original);

    // First compare Interest Group settings:
    if ($instance['settings']['show_interest_groups']) {
      $old_settings = $old_entity_wrapper->{$field['field_name']}
        ->value();
      $new_settings = $new_entity_wrapper->{$field['field_name']}
        ->value();
      foreach ($new_settings['interest_groups'] as $id => $new_interests) {
        if (!isset($old_settings['interest_groups'][$id])) {
          return TRUE;
        }
        $old_interests = $old_settings['interest_groups'][$id];
        if (!is_array($new_interests)) {
          $new_interests = array(
            $new_interests => $new_interests,
          );
        }
        foreach ($new_interests as $index => $new_int) {
          if ($old_interests[$index] !== $new_int) {
            return TRUE;
          }
        }
      }
    }

    // Expand tokens for both the new entity and the old one, then compare the
    // values to see if anything has changed.
    $mergevars_new = mailchimp_mergevars_populate($instance['settings']['mergefields'], $entity, $instance['entity_type']);
    $mergevars_old = mailchimp_mergevars_populate($instance['settings']['mergefields'], $entity->original, $instance['entity_type']);
    if ($mergevars_new != $mergevars_old) {
      return TRUE;
    }
  }
  else {
    $member_info = mailchimp_get_memberinfo($field['settings']['mc_list_id'], $email);
    if (isset($member_info['merges']['GROUPINGS'])) {
      foreach ($member_info['merges']['GROUPINGS'] as $grouping) {
        if (!isset($choices['interest_groups'][$grouping['id']])) {
          return TRUE;
        }
        else {
          if (!is_array($choices['interest_groups'][$grouping['id']])) {

            // Standardize formatting of choices:
            $choices['interest_groups'][$grouping['id']] = array(
              $choices['interest_groups'][$grouping['id']] => $choices['interest_groups'][$grouping['id']],
            );
          }
          foreach ($grouping['groups'] as $group) {
            $selected = isset($choices['interest_groups'][$grouping['id']][$group['name']]) && $choices['interest_groups'][$grouping['id']][$group['name']];
            if ($group['interested'] && !$selected || !$group['interested'] && $selected) {
              return TRUE;
            }
          }
        }
      }
    }
  }

  // No changes detected:
  return FALSE;
}