You are here

function mailchimp_user_update in Mailchimp 7

Implements hook_user_update().

File

./mailchimp.module, line 122
Mailchimp module.

Code

function mailchimp_user_update(&$edit, $account, $category) {
  if ($q = _mailchimp_get_api_object()) {
    foreach ((array) _mailchimp_get_required_lists() as $list) {

      // update a users newsletter subscription immediately
      if (!variable_get('mailchimp_cron', FALSE)) {

        // determine if a user is allowed in a given list
        $is_allowed = FALSE;
        $roles = $account->roles;
        foreach ($list->roles as $key => $value) {
          if (array_key_exists($key, $roles)) {
            $is_allowed = TRUE;
            break;
          }
        }

        // they are allowed, update or subscribe
        if ($is_allowed) {
          $userinfo = _mailchimp_load_user_list_mergevars($account, $list->id);
          if (isset($edit['mail'])) {
            $userinfo['EMAIL'] = $edit['mail'];
          }
          _mailchimp_subscribe_user($list, $account->mail, $userinfo, FALSE, $q);
        }
        else {
          _mailchimp_unsubscribe_user($list, $account->mail, FALSE, $q);
        }
      }
      else {
        db_update('mailchimp_user')
          ->fields(array(
          'status' => MAILCHIMP_USERSTATUS_PENDING,
        ))
          ->condition('uid', $account->uid)
          ->execute();
      }
    }
  }
}