You are here

function mailchimp_get_mergevars in Mailchimp 7.3

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_get_mergevars()
  2. 7.5 mailchimp.module \mailchimp_get_mergevars()
  3. 7.4 mailchimp.module \mailchimp_get_mergevars()
  4. 2.x mailchimp.module \mailchimp_get_mergevars()

Wrapper around MCAPI->Lists->mergeVars.

Parameters

array $list_ids: Array of MailChimp list IDs.

bool $reset: Set to TRUE if mergevars should not be loaded from cache.

Return value

array Struct describing mergevars for the specified lists.

4 calls to mailchimp_get_mergevars()
MailchimpListsTestCase::testGetMergevars in modules/mailchimp_lists/tests/mailchimp_lists.test
Tests retrieval of mergevars for a set of lists.
mailchimp_campaign_campaign_form in modules/mailchimp_campaign/includes/mailchimp_campaign.admin.inc
Returns a form for creating a campaign.
mailchimp_signup_form in modules/mailchimp_signup/includes/mailchimp_signup.admin.inc
Return a form for adding/editing a MailChimp signup form.
_mailchimp_lists_field_instance_settings_form_process in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Element processor. Expand the mergefields mapping form.

File

./mailchimp.module, line 247
Mailchimp module.

Code

function mailchimp_get_mergevars($list_ids, $reset = FALSE) {
  $mergevars = array();
  if (!$reset) {
    foreach ($list_ids as $key => $list_id) {
      $cache = cache_get($list_id . '-mergevars', 'cache_mailchimp');

      // Get cached data and unset from our remaining lists to query.
      if ($cache) {
        $mergevars[$list_id] = $cache->data;
        unset($list_ids[$key]);
      }
    }
  }

  // Get the uncached merge vars from MailChimp.
  if (count($list_ids)) {
    $mcapi = mailchimp_get_api_object();
    $last_list_id = NULL;
    try {
      if (!$mcapi) {
        throw new MailchimpException('Cannot get merge vars without MailChimp API. Check API key has been entered.');
      }
      $result = $mcapi->lists
        ->mergeVars($list_ids);
      if ($result['success_count']) {
        foreach ($result['data'] as $list_mergevars) {
          $last_list_id = $list_mergevars['id'];
          $mergevars[$last_list_id] = $list_mergevars;
          cache_set($last_list_id . '-mergevars', $list_mergevars, 'cache_mailchimp', CACHE_TEMPORARY);
        }
      }
    } catch (Exception $e) {
      watchdog('mailchimp', 'An error occurred requesting mergevars for list @list. "%message"', array(
        '@list' => $last_list_id,
        '%message' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
    }
  }
  return $mergevars;
}