View source
<?php
require_once "MCAPI.class.php";
define('SUBSCRIPTION_SUCCESS', t('Thank you, you have been successfully subscribed.'));
define('SUBSCRIPTION_FAILURE', t('We were unable to subscribe you at this time. Please try again later.'));
define('UNSUBSCRIPTION_SUCCESS', t('Thank you, you have been successfully unsubscribed.'));
define('UNSUBSCRIPTION_FAILURE', t('We were unable to unsubscribe you at this time. Please try again later.'));
function mailchimp_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case "delete":
if ($q = _mailchimp_get_api_object()) {
foreach ((array) $q
->lists() as $list) {
$ret = $ret || _mailchimp_unsubscribe_user($list, $account->mail, true, $q);
if (!$ret) {
watchdog('mailchimp', $q->errorMessage, 'error');
}
}
if (!$ret) {
watchdog('mailchimp', $q->errorMessage, 'error');
}
}
break;
case "form":
if (variable_get('mailchimp_user_edit', true) && $category == 'account' && ($q = _mailchimp_get_api_object())) {
$form = array();
$list_form = array();
$lists = _mailchimp_get_available_lists($account, $q);
foreach ($lists as $list) {
if (variable_get('mailchimp_list_' . $list['id'] . '_listtype', '') !== 'required') {
$list_form['chimpmail_list_' . $list['id']] = array(
'#type' => 'checkbox',
'#title' => $list["name"],
'#default_value' => _mailchimp_is_subscribed($list['id'], $account->mail, $q),
'#description' => variable_get('mailchimp_list_' . $list['id'] . '_description', ''),
);
}
}
if ($list_form) {
$form['chimpmail_lists'] = array(
'#type' => 'fieldset',
'#title' => t('Newsletter subscriptions'),
'#weight' => 5,
'#collapsible' => TRUE,
);
$form['chimpmail_lists'] = array_merge($form['chimpmail_lists'], $list_form);
}
return $form;
}
break;
case "insert":
case "update":
if ($q = _mailchimp_get_api_object()) {
$lists = _mailchimp_get_available_lists($account, $q);
foreach ($lists as $list) {
if (variable_get('mailchimp_list_' . $list['id'] . '_listtype', '') === 'required') {
if (!variable_get('mailchimp_cron', false)) {
if ($account->mail !== $edit['mail']) {
$ret = $q
->listUnsubscribe($list['id'], $account->mail, false);
}
$userinfo = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
->listMergeVars($list['id']));
$userinfo['EMAIL'] = $edit['mail'];
$ret = _mailchimp_subscribe_user($list, $userinfo, false, $q);
}
}
else {
$is_subscribed = _mailchimp_is_subscribed($list['id'], $account->mail, $q);
$ret = true;
if (variable_get('mailchimp_user_edit', true) && $op == "update" && $category == 'account' || variable_get('mailchimp_user_register', true) && $op == "insert") {
if (!$is_subscribed && @$edit['chimpmail_list_' . $list['id']]) {
$merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
->listMergeVars($list['id']));
$merge_vars['EMAIL'] = $edit['mail'];
$ret = _mailchimp_subscribe_user($list, $merge_vars, true, $q);
}
else {
if ($is_subscribed && !@$edit['chimpmail_list_' . $list['id']]) {
$ret = _mailchimp_unsubscribe_user($list, $account->mail, true, $q);
}
else {
if ($is_subscribed && $account->mail !== $edit['mail']) {
$merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
->listMergeVars($list['id']));
$merge_vars['EMAIL'] = $edit['mail'];
$ret = _mailchimp_update_user($list, $account->mail, $merge_vars, $q);
}
}
}
}
else {
if ($is_subscribed) {
$merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list['id'], $q
->listMergeVars($list['id']));
$ret = _mailchimp_update_user($list, $account->mail, $merge_vars, $q);
}
}
if (!$ret) {
watchdog('mailchimp', $q->errorMessage, 'error');
}
}
}
}
break;
}
}
function mailchimp_form_alter($form_id, &$form) {
if ($form_id == 'user_register' && variable_get('mailchimp_user_register', true)) {
$account = new stdClass();
$account->roles = array(
2 => "authenticated user",
);
$lists = _mailchimp_get_available_lists($account, $q);
if (!empty($lists)) {
foreach ($lists as $list) {
if (variable_get('mailchimp_list_' . $list['id'] . '_listtype', '') !== 'required') {
$list_form['chimpmail_list_' . $list['id']] = array(
'#type' => 'checkbox',
'#title' => $list["name"],
'#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_listtype', '') == 'optout' ? true : false,
'#description' => variable_get('mailchimp_list_' . $list['id'] . '_description', ''),
);
}
}
if ($list_form) {
$form['chimpmail_lists'] = array(
'#type' => 'fieldset',
'#title' => t('Newsletter subscriptions'),
'#weight' => 5,
'#collapsible' => TRUE,
);
$form['chimpmail_lists'] = array_merge($form['chimpmail_lists'], $list_form);
}
}
}
}
function mailchimp_cron() {
if (variable_get('mailchimp_cron', false) && ($q = _mailchimp_get_api_object())) {
$result = db_query('SELECT uid FROM {users} where status = %d', 1);
if ($result) {
$lists = _mailchimp_get_required_lists($q
->lists());
foreach ($lists as $key => $list) {
$lists[$key]['batch'] = array();
$lists[$key]['mergevars'] = $q
->listMergeVars($list['id']);
}
while ($row = db_fetch_object($result)) {
$account = user_load(array(
'uid' => $row->uid,
));
foreach ((array) $lists as $key => $list) {
foreach ((array) $account->roles as $rid => $info) {
if (variable_get('mailchimp_list_' . $list['id'] . '_role_' . $rid, 0)) {
$lists[$key]['batch'][] = _mailchimp_load_user_list_mergevars($row->uid, $list['id'], $lists[$key]['mergevars']);
break;
}
}
}
}
$count = 0;
foreach ($lists as $key => $list) {
if (count($lists[$key]['batch'])) {
$ret = $q
->listBatchSubscribe($list['id'], $lists[$key]['batch'], false, true);
if ($ret['error_count'] > 0) {
foreach ((array) $ret['errors'] as $error) {
watchdog('mailchimp', $error['email_address'] . ': ' . $error['message'], WATCHDOG_ERROR);
}
}
}
$count += $ret['success_count'];
}
watchdog('mailchimp', t('Updated !count records in MailChimp', array(
'!count' => $count,
)), WATCHDOG_NOTICE);
}
}
}
function mailchimp_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'mailchimp/subscribe',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'mailchimp_subscribe_form',
),
'title' => t('Newsletter Subscription'),
'type' => MENU_CALLBACK,
'access' => true,
);
$items[] = array(
'path' => 'mailchimp/reset',
'callback' => 'mailchimp_reset',
'type' => MENU_CALLBACK,
'access' => user_access('administer site configuration'),
);
$items[] = array(
'path' => 'mailchimp/subscribe/message',
'callback' => 'mailchimp_subscribe_message',
'title' => t('MailChump Subscription Message'),
'type' => MENU_CALLBACK,
'access' => true,
);
$items[] = array(
'path' => 'mailchimp/unsubscribe',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'mailchimp_unsubscribe_form',
),
'title' => t('Newsletter Unsubscribe'),
'type' => MENU_CALLBACK,
'access' => true,
);
$items[] = array(
'path' => 'mailchimp/unsubscribe/success',
'callback' => 'mailchimp_unsubscribe_success',
'title' => t('Unsubscription Confirmed'),
'type' => MENU_CALLBACK,
'access' => true,
);
$items[] = array(
'path' => 'mailchimp/unsubscribe/failure',
'callback' => 'mailchimp_unsubscribe_failure',
'title' => t('Unsubscription Failed'),
'type' => MENU_CALLBACK,
'access' => true,
);
$items[] = array(
'path' => 'admin/settings/mailchimp',
'title' => t('MailChimp'),
'description' => t('Manage MailChimp Settings.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'mailchimp_admin_settings',
),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
function mailchimp_reset() {
variable_del('mailchimp_username');
variable_del('mailchimp_password');
drupal_goto('admin/settings/mailchimp');
}
function mailchimp_admin_settings() {
$form['mailchimp_account_info'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => 'MailChimp Account Information',
);
$username = variable_get('mailchimp_username', false);
$password = variable_get('mailchimp_password', false);
if ($username && $password) {
$form['mailchimp_account_info']['mailchimp_setup_account'] = array(
'#value' => t('Current configured account is for: @username.', array(
'@username' => $username,
)),
);
$form['mailchimp_account_info']['reset'] = array(
'#value' => l('Click here to login as a different user.', 'mailchimp/reset'),
);
$q = new MCAPI($username, $password);
if (!$q->errorCode) {
$lists = $q
->lists();
if (!empty($lists)) {
$form['mailchimp_lists'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#title' => 'MailChimp Subscription Lists',
);
foreach ($lists as $list) {
$form['mailchimp_lists']['mailchimp_list_' . $list['id']] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => $list['name'],
);
$form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_roles'] = array(
'#type' => 'fieldset',
'#title' => t('Roles'),
'#description' => t('Choose which roles may subscribe to this list. Select the anonymous user role to have the list appear on the !subscribe page. Select the authenticated user role to have the list appear on the !signup page. For all other roles, the list will appear on the settings page for users with that role.', array(
'!subscribe' => l(t('Newsletter Subscription'), 'mailchimp/subscribe'),
'!signup' => l(t('User Registration'), 'user/register'),
)),
'#tree' => false,
);
foreach (user_roles() as $rid => $name) {
$form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_roles']['mailchimp_list_' . $list['id'] . '_role_' . $rid] = array(
'#type' => 'checkbox',
'#title' => $name,
'#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_role_' . $rid, 0),
);
}
$form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_description'] = array(
'#type' => 'textarea',
'#title' => t('List Description'),
'#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_description', ''),
'#description' => t('This description will be shown to the user on the list signup and user account settings pages'),
);
$form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_listtype'] = array(
'#type' => 'select',
'#title' => t('Subscription Method'),
'#options' => array(
'optin' => "Opt-in",
'optin' => "Opt-in",
'optout' => 'Opt-out',
'required' => 'Required',
),
'#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_listtype', 'optin'),
'#description' => t('<strong>Opt-in:</strong> Users must sign up to recieve messages.<br/><strong>Opt-out: </strong> Users are automatically signed up but may unsubscribe.<br/><strong>Required: </strong> Users will remain on the list as long as they have an account and cannot unsubscribe.'),
);
$form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_doublein'] = array(
'#type' => 'checkbox',
'#title' => t('Require subscribers to Double Opt-in'),
'#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_doublein', 0),
'#description' => t('New subscribers will be sent a link with an email they must follow to confirm their subscription.'),
);
$mergevars = $q
->listMergeVars($list['id']);
if ($mergevars) {
$form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_mergevars'] = array(
'#type' => 'fieldset',
'#title' => t('Merge Variables'),
'#description' => t('Select Drupal user variables to send to Mailchimp as Merge Variables. Available Drupal variables are any Profile, Bio module or Token variables for the given user. For more information on Merge Variables, see the !doc', array(
'!doc' => l(t('Mailchimp Documentation'), 'http://server.iad.liveperson.net/hc/s-31286565/cmd/kbresource/kb-8214439208090042855/view_question!PAGETYPE?sq=merge%2bvariables&sf=101113&sg=0&st=188569&documentid=143258&action=view'),
)),
'#tree' => false,
);
$mergeoptions = mailchimp_get_merge_keys();
foreach ($mergevars as $mergevar) {
if ($mergevar['tag'] !== 'EMAIL') {
$form['mailchimp_lists']['mailchimp_list_' . $list['id']]['mailchimp_list_' . $list['id'] . '_mergevars']['mailchimp_list_' . $list['id'] . '_merge_' . $mergevar['tag']] = array(
'#type' => 'select',
'#title' => $mergevar['name'],
'#options' => $mergeoptions,
'#default_value' => variable_get('mailchimp_list_' . $list['id'] . '_merge_' . $mergevar['tag'], ''),
);
}
}
}
}
$form['mailchimp_messages'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => 'Messaging',
);
$form['mailchimp_messages']['mailchimp_subscription_success_message'] = array(
'#type' => 'textarea',
'#title' => t('Subscription Success Message'),
'#default_value' => variable_get('mailchimp_subscription_success_message', SUBSCRIPTION_SUCCESS),
);
$form['mailchimp_messages']['mailchimp_subscription_failure_message'] = array(
'#type' => 'textarea',
'#title' => t('Subscription Failure Message'),
'#default_value' => variable_get('mailchimp_subscription_failure_message', SUBSCRIPTION_FAILURE),
);
$form['mailchimp_messages']['mailchimp_unsubscription_success_message'] = array(
'#type' => 'textarea',
'#title' => t('Unsubscription Success Message'),
'#default_value' => variable_get('mailchimp_unsubscription_success_message', UNSUBSCRIPTION_SUCCESS),
);
$form['mailchimp_messages']['mailchimp_unsubscription_failure_message'] = array(
'#type' => 'textarea',
'#title' => t('Unsubscription Failure Message'),
'#default_value' => variable_get('mailchimp_unsubscription_failure_message', UNSUBSCRIPTION_FAILURE),
);
$form['mailchimp_user_register'] = array(
'#type' => 'checkbox',
'#title' => t('Show Subscription Options on User Registration Page'),
'#default_value' => variable_get('mailchimp_registration', true),
);
$form['mailchimp_user_edit'] = array(
'#type' => 'checkbox',
'#title' => t('Show Subscription Options on User Edit Screen'),
'#default_value' => variable_get('mailchimp_user_edit', true),
);
$form['mailchimp_cron'] = array(
'#type' => 'checkbox',
'#title' => t('Sync Required Lists During Cron'),
'#default_value' => variable_get('mailchimp_cron', false),
'#description' => t('If this is set, users will be subscribed to the required list during cron runs. Otherwise subscription will take place when a user is added/edited.'),
);
}
else {
drupal_set_message(t('You do not have any valid MailChimp mailing lists.'));
}
}
else {
if (false && $q->errorCode === 'INVALID_LOGIN') {
drupal_set_message(t('Could not login to mailchimp. Please check your username and password.'), "error");
}
else {
if ($q->errorMessage) {
drupal_set_message(t('Could not retrieve info for mailchimp. The following error was returned: %error.', array(
'%error' => $q->errorMessage,
)), "error");
}
else {
drupal_set_message(t('Could not retrieve info for mailchimp for an unknown reason. Please try again later'), "error");
}
}
}
}
else {
$form['mailchimp_account_info']['mailchimp_username'] = array(
'#type' => 'textfield',
'#title' => t('Mailchimp username'),
'#required' => TRUE,
'#default_value' => variable_get('mailchimp_username', ''),
'#description' => t('The username used to login to your MailChimp account.'),
);
$form['mailchimp_account_info']['mailchimp_password'] = array(
'#type' => 'password',
'#title' => t('Mailchimp password'),
'#required' => TRUE,
'#default_value' => variable_get('mailchimp_password', ''),
'#description' => t('The password used to login to your MailChimp account.'),
);
}
return system_settings_form($form);
}
function mailchimp_subscribe_form() {
global $user;
if ($user->uid) {
drupal_set_message(t('You can manage your newsletter subscriptions from your user account page.'));
drupal_goto('user/' . $user->uid);
}
if ($q = _mailchimp_get_api_object()) {
$lists = _mailchimp_get_available_lists($user, $q);
if (count($lists) > 0) {
foreach ($lists as $list) {
$form['list_' . $list['id']] = array(
'#type' => 'fieldset',
'#title' => $list['name'],
'#collapsible' => true,
'#tree' => true,
);
$checked = variable_get('mailchimp_list_' . $list['id'] . '_listtype', 'optin') == "optout" ? 1 : 0;
$form['list_' . $list['id']][$list['id']] = array(
'#type' => 'checkbox',
'#title' => t('Subscribe to the @newsletter newsletter', array(
'@newsletter' => $list['name'],
)),
'#default_value' => $checked,
'#description' => variable_get('mailchimp_list_' . $list['id'] . '_description', ''),
);
foreach ((array) $q
->listMergeVars($list['id']) as $mergevar) {
$form['list_' . $list['id']][$mergevar['tag']] = array(
'#type' => 'textfield',
'#title' => $mergevar['name'],
);
}
if ($intgroup = $q
->listInterestGroups($list['id'])) {
$form['list_' . $list['id']][$intgroup['name']] = array(
'#type' => 'fieldset',
'#title' => $intgroup['name'],
);
foreach ((array) $intgroup['groups'] as $group) {
$form['list_' . $list['id']][$intgroup['name']][$group] = array(
'#type' => $intgroup['form_field'],
'#title' => $group,
);
}
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sign Up!'),
);
}
}
return $form;
}
function mailchimp_subscribe_form_submit($form_id, $form_values) {
global $user;
if ($q = _mailchimp_get_api_object()) {
$lists = _mailchimp_get_available_lists($user, $q);
$success = true;
foreach ($lists as $list) {
$lid = $list['id'];
$sub_info = $q
->listMemberInfo($lid, $form_values['list_' . $lid]['EMAIL']);
$is_subscribed = @$sub_info['status'] == 'subscribed';
if ($form_values['list_' . $list['id']][$lid] && !$is_subscribed) {
$success = $success && _mailchimp_subscribe_user($list, $form_values['list_' . $list['id']], true, $q);
}
}
drupal_goto('mailchimp/subscribe/message', 'success=' . $success);
}
}
function mailchimp_subscribe_message() {
if ($_GET['success']) {
return variable_get('mailchimp_subscription_success_message', SUBSCRIPTION_SUCCESS);
}
else {
return variable_get('mailchimp_subscription_failure_message', SUBSCRIPTION_SUCCESS);
}
}
function mailchimp_unsubscribe_form($form_values = array()) {
$form = array();
$form['intro'] = array(
'#type' => 'markup',
'#value' => t('Use this form to unsubscribe from all of our newsletters.'),
);
$form['EMAIL'] = array(
'#type' => 'textfield',
'#title' => t('Email Address'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Unsubscribe'),
);
return $form;
}
function mailchimp_unsubscribe_form_submit($form_values = array()) {
if ($q || ($q = _mailchimp_get_api_object())) {
$success = true;
foreach ((array) $q
->lists() as $list) {
$success = $success && _mailchimp_unsubscribe_user($list, $form['EMAIL'], true, $q);
if ($success) {
drupal_set_message(variable_get('mailchimp_unsubscription_success_message', SUBSCRIPTION_SUCCESS));
}
else {
drupal_set_message(variable_get('mailchimp_unsubscription_failure_message', SUBSCRIPTION_SUCCESS));
}
}
}
}
function mailchimp_unsubscribe_form_validate($form_id, $form_values) {
if (!$form_values['EMAIL']) {
form_set_error('EMAIL', t('Please enter your email address.'));
}
else {
if ($q || ($q = _mailchimp_get_api_object())) {
$subscribed = false;
foreach ((array) $q
->lists() as $list) {
$subscribed = $subscribed || _mailchimp_is_subscribed($list['id'], $form_values['EMAIL'], $q);
}
if (!$subscribed) {
form_set_error('', t('That email address could not be found.'));
}
}
else {
form_set_error('', t('There was an error processing your request. Please try again later.'));
}
}
}
function mailchimp_mailchimp_merge_keys() {
$out = array(
0 => '<none>',
);
$user_fields = array(
'name' => t('Username'),
'mail' => t('Email Address'),
'uid' => t('User ID'),
'signature' => t("User's Signature"),
);
foreach ($user_fields as $key => $field) {
$out[$key] = t('User: !field', array(
'!field' => $field,
));
}
if (function_exists('_profile_get_fields')) {
$categories = profile_categories();
if (!empty($categories)) {
foreach ($categories as $category) {
$result = _profile_get_fields($category['name'], $register);
while ($field = db_fetch_object($result)) {
$out[$field->name] = t('Profile: !cat - !field', array(
'!cat' => $field->category,
'!field' => $field->title,
));
}
}
}
}
if (function_exists('token_get_list')) {
$tokens = token_get_list('user');
foreach ($tokens['user'] as $token => $name) {
$out['token_' . $token] = t('Token: !field', array(
'!field' => $name,
));
}
}
return $out;
}
function mailchimp_mailchimp_merge_values($user) {
$out = array();
$out = (array) $user;
$out = array_merge($out, _mailchimp_get_user_tokens($user));
return $out;
}
function _mailchimp_get_user_tokens($user) {
$out = array();
if (function_exists('token_get_values')) {
$vars = token_get_values('user', $user);
foreach ($vars->tokens as $key => $value) {
$out['token_' . $value] = $vars->values[$key];
}
}
return $out;
}
function mailchimp_get_merge_keys() {
return module_invoke_all('mailchimp_merge_keys');
}
function mailchimp_get_merge_values($uid) {
if ($user = user_load(array(
'uid' => $uid,
))) {
return module_invoke_all('mailchimp_merge_values', $user);
}
return array();
}
function _mailchimp_load_user_list_mergevars($uid, $list_id, $mergevars) {
$account = mailchimp_get_merge_values($uid);
$uservars = array(
'EMAIL' => $account['mail'],
);
foreach ($mergevars as $mergevar) {
if ($key = variable_get('mailchimp_list_' . $list_id . '_merge_' . $mergevar['tag'], '')) {
$uservars[$mergevar['tag']] = $account[$key];
}
}
return $uservars;
}
function _mailchimp_is_subscribed($listid, $mail, $q = NULL) {
$is_subscribed = false;
if ($q || ($q = _mailchimp_get_api_object())) {
$sub_info = $q
->listMemberInfo($listid, $mail);
$is_subscribed = @$sub_info['status'] == 'subscribed';
}
return $is_subscribed;
}
function _mailchimp_subscribe_user($list, $merge_vars, $message = true, $q = NULL) {
if ($q || ($q = _mailchimp_get_api_object())) {
$double_optin = variable_get('mailchimp_list_' . $list['id'] . '_doublein', 0);
if (_mailchimp_is_subscribed($list['id'], $merge_vars['EMAIL'], $q)) {
$success = $q
->listUpdateMember($list['id'], $merge_vars['EMAIL'], $merge_vars);
}
else {
$success = $q
->listSubscribe($list['id'], $merge_vars['EMAIL'], $merge_vars, 'html', $double_optin);
if ($message && $success && $double_optin) {
drupal_set_message(t('You have chosen to subscribe to %list. An email will be sent to your address. Click the link in the email to confirm the subscription.', array(
'%list' => $list['name'],
)));
}
else {
if ($message && $success) {
drupal_set_message(t('You have subscribed to %list.', array(
'%list' => $list['name'],
)));
}
}
}
}
return $success;
}
function _mailchimp_update_user($list, $mail, $merge_vars, $q = NULL) {
if ($q || ($q = _mailchimp_get_api_object())) {
$q
->listUpdateMember($list['id'], $mail, $merge_vars);
}
return $success;
}
function _mailchimp_unsubscribe_user($list, $mail, $message = true, $q = NULL) {
if ($q || ($q = _mailchimp_get_api_object())) {
if (_mailchimp_is_subscribed($list['id'], $mail, $q)) {
$success = $q
->listUnsubscribe($list['id'], $mail, false);
if ($message && $success) {
drupal_set_message(t('You have unsubscribed from %list.', array(
'%list' => $list['name'],
)));
}
}
}
return true;
}
function _mailchimp_get_available_lists($user, $q = NULL) {
$lists = array();
if ($q || ($q = _mailchimp_get_api_object())) {
foreach ((array) $q
->lists() as $list) {
if (!empty($list)) {
foreach ((array) $user->roles as $rid => $info) {
if (variable_get('mailchimp_list_' . $list['id'] . '_role_' . $rid, 0)) {
$lists[] = $list;
}
}
}
}
}
return $lists;
}
function _mailchimp_get_required_lists($lists) {
foreach ($lists as $key => $list) {
if (variable_get('mailchimp_list_' . $list['id'] . '_listtype', '') !== 'required') {
unset($lists[$key]);
}
}
return $lists;
}
function _mailchimp_get_api_object() {
$q = new MCAPI(variable_get('mailchimp_username', ''), variable_get('mailchimp_password', ''));
if ($q->errorCode) {
watchdog('mailchimp', $q->errorMessage, WATCHDOG_ERROR);
return NULL;
}
return $q;
}