View source
<?php
define('MAILCHIMP_LISTTYPE_REQUIRED', 'required');
define('MAILCHIMP_LISTTYPE_OPTIN', 'optin');
define('MAILCHIMP_LISTTYPE_OPTOUT', 'optout');
define('MAILCHIMP_USERSTATUS_PENDING', 'pending');
define('MAILCHIMP_USERSTATUS_CURRENT', 'current');
function mailchimp_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'register' && variable_get('mailchimp_user_register', FALSE)) {
$account->roles = array(
2 => 'authenticated',
);
$lists = _mailchimp_get_available_lists($account);
if (!empty($lists) && ($q = _mailchimp_get_api_object())) {
$form['mailchimp_list_forms'] = array(
'#type' => 'fieldset',
'#title' => t(variable_get('mailchimp_user_settings_title', 'Newsletter Subscriptions')),
);
$form['mailchimp_lists'] = array(
'#type' => 'value',
);
foreach ($lists as $list) {
if ($list->listtype !== MAILCHIMP_LISTTYPE_REQUIRED) {
mailchimp_auth_newsletter_form($form['mailchimp_list_forms'], $list, NULL, $q);
$form['mailchimp_lists']['#value'][] = $list;
}
}
return $form;
}
}
if ($op == 'insert' && variable_get('mailchimp_user_register', FALSE) && ($q = _mailchimp_get_api_object())) {
foreach ((array) $edit['mailchimp_lists'] as $list) {
if (isset($edit['mailchimp_list_' . $list->id]) && $edit['mailchimp_list_' . $list->id]) {
$merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list->id, $q
->listMergeVars($list->id));
if (isset($edit['interest_groups_' . $list->id]) && is_array($edit['interest_groups_' . $list->id])) {
foreach ($edit['interest_groups_' . $list->id] as $key => $group) {
$merge_vars['GROUPINGS'][] = array(
'id' => $key,
'groups' => _mailchimp_implode_interest_groups($group),
);
}
}
$ret = _mailchimp_subscribe_user($list, $account->mail, $merge_vars, TRUE, $q);
if (!$ret) {
watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
'%errormsg' => $q->errorMessage,
), WATCHDOG_ERROR);
}
}
}
}
if (in_array($op, array(
'insert',
'delete',
'update',
'after_update',
)) && ($q = _mailchimp_get_api_object())) {
foreach ((array) _mailchimp_get_required_lists() as $list) {
$action_taken = FALSE;
switch ($op) {
case "delete":
db_query('DELETE FROM {mailchimp_user} WHERE uid = %d', $account->uid);
$ret = _mailchimp_unsubscribe_user($list, $account->mail, FALSE, $q);
$action_taken = TRUE;
break;
case 'update':
$_SESSION['prev_email'] = $account->mail;
break;
case 'insert':
case 'after_update':
if (isset($_SESSION['prev_email'])) {
$email_update = $_SESSION['prev_email'] == $account->mail ? FALSE : TRUE;
}
else {
$email_update = FALSE;
}
if (!variable_get('mailchimp_cron', FALSE) || $email_update == TRUE) {
$action_taken = TRUE;
$is_allowed = FALSE;
$roles = empty($edit['roles']) ? $account->roles : $edit['roles'];
foreach ($list->roles as $key => $value) {
if (array_key_exists($key, $roles)) {
$is_allowed = TRUE;
break;
}
}
if ($is_allowed && $account->status == 1) {
$userinfo = _mailchimp_load_user_list_mergevars($account->uid, $list->id, $q
->listMergeVars($list->id));
$userinfo['EMAIL'] = $edit['mail'];
$email_data = isset($_SESSION['prev_email']) ? $_SESSION['prev_email'] : $userinfo['EMAIL'];
$ret = _mailchimp_subscribe_user($list, $email_data, $userinfo, FALSE, $q);
unset($_SESSION['prev_email']);
}
else {
$ret = _mailchimp_unsubscribe_user($list, $account->mail, FALSE, $q);
}
}
else {
unset($_SESSION['prev_email']);
if ($op == 'insert') {
db_query("INSERT INTO {mailchimp_user} (uid, status) VALUES (%d, '%s')", $account->uid, MAILCHIMP_USERSTATUS_PENDING);
}
else {
db_query("UPDATE {mailchimp_user} SET status = '%s' WHERE uid = %d", MAILCHIMP_USERSTATUS_PENDING, $account->uid);
}
}
unset($_SESSION['prev_email']);
break;
}
if ($action_taken) {
if ($ret) {
watchdog('mailchimp', 'MailChimp: %email updated in list %list on action %action', array(
'%email' => $account->mail,
'%list' => $list->name,
'%action' => $op,
), WATCHDOG_NOTICE);
}
else {
watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
'%errormsg' => $q->errorMessage,
), WATCHDOG_ERROR);
}
}
}
}
}
function mailchimp_subscribe_auth_form($form_state, $account, $standalone = TRUE, $list = NULL) {
$form = array();
if ($q = _mailchimp_get_api_object()) {
if ($account->uid) {
$form['uid'] = array(
'#type' => 'hidden',
'#value' => $account->uid,
);
}
else {
$account->roles = array(
2 => 'authenticated',
);
}
$lists = array();
if (empty($list)) {
$lists = _mailchimp_get_available_lists($account);
}
else {
$lists[$list->id] = $list;
}
foreach ($lists as $list) {
mailchimp_auth_newsletter_form($form, $list, $account, $q);
}
$form['lists'] = array(
'#type' => 'value',
'#value' => $lists,
);
if ($standalone) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('save settings'),
'#weight' => 100,
);
$form['#submit'][] = 'mailchimp_subscribe_auth_form_submit';
}
}
return $form;
}
function mailchimp_forms($form_id, $args) {
if (strpos($form_id, 'mailchimp_subscribe_auth_form') !== FALSE) {
$forms[$form_id] = array(
'callback' => 'mailchimp_subscribe_auth_form',
);
return $forms;
}
else {
if (strpos($form_id, 'mailchimp_subscribe_anon_form') !== FALSE) {
$forms[$form_id] = array(
'callback' => 'mailchimp_subscribe_anon_form',
);
return $forms;
}
}
}
function mailchimp_auth_newsletter_form(&$form, $list, $account, $q) {
$is_subscribed = FALSE;
$member_interests = array();
if ($list->listtype !== MAILCHIMP_LISTTYPE_REQUIRED) {
if ($account && $account->uid) {
$memberinfo = $q
->listMemberInfo($list->id, $account->mail);
$is_subscribed = $memberinfo['status'] == 'subscribed';
$member_interests = isset($memberinfo['merges']['GROUPINGS']) ? $memberinfo['merges']['GROUPINGS'] : array();
$default_value = $is_subscribed;
}
else {
$default_value = $is_subscribed = $list->listtype == MAILCHIMP_LISTTYPE_OPTOUT ? TRUE : FALSE;
}
$form['wrapper' . $list->id] = array(
'#prefix' => '<div class="mailchimp-newsletter-wrapper">',
'#suffix' => '</div>',
);
$form['wrapper' . $list->id]['mailchimp_list_' . $list->id] = array(
'#type' => 'checkbox',
'#title' => $list->label ? t($list->label) : t('Subscribe to the @newsletter newsletter', array(
'@newsletter' => $list->name,
)),
'#default_value' => $default_value,
'#description' => t($list->description),
'#attributes' => array(
'class' => 'mailchimp-newsletter-checkbox-' . $list->id,
),
);
if (variable_get('mailchimp_interest_groups_user_forms', FALSE)) {
$interests = _mailchimp_interest_groups_element($q, $list->id, $member_interests, $is_subscribed);
if (!empty($interests)) {
$form['wrapper' . $list->id] = array_merge($form['wrapper' . $list->id], $interests);
drupal_add_js('Drupal.behaviors.mailchimp = function(context) {
$(".mailchimp-newsletter-checkbox-' . $list->id . '").click(function(){
Drupal.toggleFieldset(".mailchimp-newsletter-interests-' . $list->id . '");
});
}', 'inline');
}
}
}
return $form;
}
function mailchimp_subscribe_auth_form_submit($form, &$form_state) {
if ($q = _mailchimp_get_api_object()) {
$account = new stdClass();
if (!empty($form_state['values']['uid'])) {
$account = user_load(array(
'uid' => $form_state['values']['uid'],
));
}
else {
global $user;
$account = $user;
}
$lists = $form_state['values']['lists'];
foreach ($lists as $list) {
if ($list->listtype !== MAILCHIMP_LISTTYPE_REQUIRED) {
$is_subscribed = _mailchimp_is_subscribed($list->id, $account->mail, $q);
$ret = TRUE;
$selected = @$form_state['values']['mailchimp_list_' . $list->id];
if ($is_subscribed && !$selected && $form['#id'] != 'user-register') {
$ret = _mailchimp_unsubscribe_user($list, $account->mail, TRUE, $q);
}
else {
if ($selected) {
$merge_vars = _mailchimp_load_user_list_mergevars($account->uid, $list->id, $q
->listMergeVars($list->id));
if (!empty($form_state['values']['mail']) && $is_subscribed) {
$merge_vars['EMAIL'] = $form_state['values']['mail'];
}
if (isset($form_state['values']['interest_groups_' . $list->id]) && is_array($form_state['values']['interest_groups_' . $list->id])) {
foreach ($form_state['values']['interest_groups_' . $list->id] as $key => $group) {
$merge_vars['GROUPINGS'][] = array(
'id' => $key,
'groups' => _mailchimp_implode_interest_groups($group),
);
}
}
$ret = _mailchimp_subscribe_user($list, $account->mail, $merge_vars, TRUE, $q);
}
}
if (!$ret) {
watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
'%errormsg' => $q->errorMessage,
), WATCHDOG_ERROR);
}
}
}
}
}
function mailchimp_cron() {
if (variable_get('mailchimp_cron', FALSE) && ($q = _mailchimp_get_api_object())) {
$sql = "SELECT mu.uid FROM {mailchimp_user} mu LEFT OUTER JOIN {users} u ON mu.uid = u.uid WHERE mu.status = '%s' AND u.status = 1";
$result = db_query_range($sql, array(
MAILCHIMP_USERSTATUS_PENDING,
), 0, variable_get('mailchimp_batch_limit', 100));
if ($result) {
$lists = _mailchimp_get_required_lists();
foreach ($lists as $key => $list) {
$lists[$key]->batch = array();
$lists[$key]->listMergeVars = $q
->listMergeVars($list->id);
$lists[$key]->unsubscribe = array();
}
while ($row = db_fetch_object($result)) {
if ($account = user_load(array(
'uid' => $row->uid,
))) {
db_query('UPDATE {mailchimp_user} SET status = \'%s\' WHERE uid = %d', MAILCHIMP_USERSTATUS_CURRENT, $account->uid);
foreach ((array) $lists as $key => $list) {
$is_allowed = FALSE;
foreach ((array) $account->roles as $rid => $info) {
if ($list->roles[$rid]) {
$lists[$key]->batch[] = _mailchimp_load_user_list_mergevars($row->uid, $list->id, $lists[$key]->listMergeVars);
$is_allowed = TRUE;
break;
}
}
if (!$is_allowed) {
$lists[$key]->unsubscribe[] = $account->mail;
}
}
}
else {
db_query('DELETE FROM {mailchimp_user} WHERE uid = %d', $account->uid);
}
}
$count = 0;
$unsub_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', 'MCAPI Error: %errormsg', array(
'%errormsg' => $error['message'],
), WATCHDOG_ERROR);
}
}
$count += $ret['success_count'];
}
if (count($lists[$key]->unsubscribe)) {
$ret = $q
->listBatchUnsubscribe($list->id, $lists[$key]->unsubscribe, FALSE, FALSE);
if ($ret['error_count'] > 0) {
foreach ((array) $ret['errors'] as $error) {
watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
'%errormsg' => $error['message'],
), WATCHDOG_ERROR);
}
}
$unsub_count += $ret['success_count'];
}
}
watchdog('mailchimp', 'Updated !count records in MailChimp and unsubscribed !unsub emails.', array(
'!count' => $count,
'!unsub' => $unsub_count,
), WATCHDOG_NOTICE);
}
}
}
function mailchimp_menu() {
$items = array();
$items['user/%user/mailchimp'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mailchimp_subscribe_auth_form',
1,
),
'title callback' => 'mailchimp_user_settings_page_title',
'type' => MENU_LOCAL_TASK,
'access callback' => 'mailchimp_user_edit_access',
'access arguments' => array(
1,
),
'tab_parent' => 'user/%/edit',
'weight' => 10,
);
$items['mailchimp/subscribe'] = array(
'page callback' => 'mailchimp_subscribe_page',
'title callback' => 'mailchimp_subscribe_page_title',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array(
'access content',
),
);
$items['mailchimp/unsubscribe'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mailchimp_unsubscribe_form',
),
'title callback' => 'mailchimp_unsubscribe_page_title',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
$items['admin/settings/mailchimp'] = array(
'title' => 'MailChimp Configuration',
'description' => 'Manage MailChimp Settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'mailchimp_admin_settings',
),
'access arguments' => array(
'administer mailchimp',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'mailchimp.admin.inc',
);
$items['admin/settings/mailchimp/rebuild'] = array(
'title' => 'Rebuild Users Table',
'page callback' => 'mailchimp_rebuild_users',
'access arguments' => array(
'administer mailchimp',
),
'weight' => 10,
);
return $items;
}
function mailchimp_user_edit_access($user) {
if (variable_get('mailchimp_user_edit', TRUE) && user_edit_access($user)) {
return TRUE;
}
else {
return FALSE;
}
}
function mailchimp_perm() {
return array(
'administer mailchimp',
);
}
function mailchimp_subscribe_page() {
global $user;
$ret = '';
if ($user->uid) {
$ret = drupal_get_form('mailchimp_subscribe_auth_form', $user, TRUE, NULL);
}
elseif ($q = _mailchimp_get_api_object()) {
$ret = drupal_get_form('mailchimp_subscribe_anon_form_all', $q);
}
return $ret;
}
function mailchimp_subscribe_anon_form_all($form_state, $q) {
$form = array(
'#tree' => TRUE,
);
global $user;
$lists = _mailchimp_get_available_lists($user);
foreach ($lists as $list) {
$form['mailchimp_lists'][$list->id] = array(
'#type' => 'fieldset',
'#title' => t($list->name),
'#description' => t($list->description),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
_mailchimp_subscribe_anon_form($list, $q, $form['mailchimp_lists'][$list->id]);
if ($form['mailchimp_lists'][$list->id]['EMAIL']) {
$form['mailchimp_lists'][$list->id]['EMAIL']['#required'] = FALSE;
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sign Up!'),
'#weight' => 100,
);
$form['#submit'][] = 'mailchimp_subscribe_anon_form_submit';
return $form;
}
function mailchimp_subscribe_anon_form_submit($form, &$form_state) {
if ($q = _mailchimp_get_api_object()) {
$lists = $form_state['values']['mailchimp_lists'];
foreach ($lists as $list) {
if (!empty($list['EMAIL'])) {
$success = FALSE;
if (isset($list['interest_groups_' . $list['list']->id]) && is_array($list['interest_groups_' . $list['list']->id])) {
foreach ($list['interest_groups_' . $list['list']->id] as $key => $group) {
$list['GROUPINGS'][] = array(
'id' => $key,
'groups' => _mailchimp_implode_interest_groups($group),
);
}
}
$success = _mailchimp_subscribe_user($list['list'], $list['EMAIL'], $list, TRUE, $q);
if ($success) {
$msg = t(variable_get('mailchimp_subscription_success_message', 'Thank you, you have been successfully subscribed.'));
}
else {
$msg = t(variable_get('mailchimp_subscription_failure_message', 'We were unable to subscribe you at this time. Please try again later.'));
}
drupal_set_message($msg);
}
}
}
}
function mailchimp_subscribe_page_title() {
return t(variable_get('mailchimp_subscribe_page_title', 'Newsletter Subscription'));
}
function mailchimp_unsubscribe_page_title() {
return t(variable_get('mailchimp_unsubscribe_page_title', 'Newsletter Unsubscribe'));
}
function mailchimp_user_settings_page_title() {
$title = t(variable_get('mailchimp_user_settings_title', 'Newsletter Subscriptions'));
return $title;
}
function mailchimp_subscribe_anon_form($form_state, $list, $q) {
$form = array(
'#tree' => TRUE,
'#id' => 'mailchimp_subscribe_anon_form_' . $list->id,
);
$form['description'] = array(
'#value' => $list->description,
);
_mailchimp_subscribe_anon_form($list, $q, $form['mailchimp_lists'][$list->id]);
$form['#submit'][] = 'mailchimp_subscribe_anon_form_submit';
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Sign Up!'),
'#weight' => 100,
);
return $form;
}
function _mailchimp_subscribe_anon_form($list, $q, &$form) {
$form['list'] = array(
'#type' => 'value',
'#value' => $list,
);
global $user;
$mergevalues = NULL;
if ($user->uid) {
$mergevalues = _mailchimp_load_user_list_mergevars($user->uid, $list->id, $q
->listMergeVars($list->id));
}
foreach ((array) $q
->listMergeVars($list->id) as $mergevar) {
if ($mergevalues && $mergevalues[$mergevar['tag']]) {
$mergevar['default'] = $mergevalues[$mergevar['tag']];
}
$form[$mergevar['tag']] = _mailchimp_insert_drupal_form_tag($mergevar);
}
if (variable_get('mailchimp_interest_groups_user_forms', FALSE)) {
$form = array_merge($form, _mailchimp_interest_groups_element($q, $list->id, array(), TRUE));
}
}
function _mailchimp_interest_groups_element($q, $list_id, $groupings = array(), $is_subscribed = FALSE) {
$element = array();
if ($groups = $q
->listInterestGroupings($list_id)) {
$element['interest_groups_' . $list_id] = array(
'#type' => 'fieldset',
'#title' => t('Interest Groups'),
'#description' => t(''),
'#collapsible' => TRUE,
'#collapsed' => !$is_subscribed,
'#tree' => TRUE,
'#attributes' => array(
'class' => 'mailchimp-newsletter-interests-' . $list_id,
),
);
foreach ($groups as $group) {
if ($group['form_field'] != 'hidden') {
$default = array();
if (!empty($groupings)) {
foreach ($groupings as $grouping) {
if ($grouping['id'] == $group['id']) {
$default = _mailchimp_explode_interest_groups($grouping['groups']);
}
}
}
$field_type = '';
switch ($group['form_field']) {
case 'radio':
$field_type = 'radios';
break;
case 'dropdown':
$field_type = 'select';
break;
default:
$field_type = $group['form_field'];
}
$options = array();
foreach ((array) $group['groups'] as $item) {
$options[$item['name']] = $item['name'];
}
$element['interest_groups_' . $list_id][$group['id']] = array(
'#type' => $field_type,
'#title' => t('@name', array(
'@name' => $group['name'],
)),
'#multiple' => FALSE,
'#options' => $options,
'#default_value' => $default,
);
}
}
}
return $element;
}
function _mailchimp_insert_drupal_form_tag($mergevar) {
$input = array(
'#title' => $mergevar['name'],
'#weight' => $mergevar['order'],
'#required' => $mergevar['req'],
'#default_value' => $mergevar['default'],
);
switch ($mergevar['field_type']) {
case 'dropdown':
$input['#type'] = 'select';
foreach ($mergevar['choices'] as $choice) {
$choices[$choice] = $choice;
}
$input['#options'] = $choices;
break;
case 'radio':
$input['#type'] = 'radios';
foreach ($mergevar['choices'] as $choice) {
$choices[$choice] = $choice;
}
$input['#options'] = $choices;
break;
default:
$input['#type'] = 'textfield';
$input['#size'] = $mergevar['size'];
break;
}
if ($mergevar['public'] == FALSE) {
$input['#type'] = 'hidden';
}
return $input;
}
function mailchimp_unsubscribe_form($form_values = array()) {
$form = array();
$form['intro'] = array(
'#type' => 'markup',
'#value' => t(variable_get('mailchimp_unsubscribe_page_description', 'Use this form to unsubscribe from all of our newsletters.')),
);
$form['EMAIL'] = array(
'#type' => 'textfield',
'#title' => t('Email Address'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Unsubscribe'),
);
return $form;
}
function mailchimp_unsubscribe_form_submit($form, &$form_state) {
if ($q = _mailchimp_get_api_object()) {
$success = FALSE;
$count = 0;
global $user;
foreach ((array) _mailchimp_get_available_lists($user) as $list) {
$success = _mailchimp_unsubscribe_user($list, $form_state['values']['EMAIL'], FALSE, $q);
if ($success) {
$count++;
drupal_set_message(t('Thank you, you have been successfully unsubscribed from !list.', array(
'!list' => '<em>' . $list->name . '</em>',
)));
}
}
if ($count == 0) {
drupal_set_message(t('There were no lists to unsubscribe from at this time.'));
}
}
}
function mailchimp_mailchimp_merge_keys() {
$out = array(
'' => '<none>',
);
$user_fields = array(
'name' => t('Username'),
'mail' => t('Email Address'),
'uid' => t('User ID'),
'signature' => t("User's Signature"),
'created' => t("User's Creation Date"),
'access' => t("User's Last Access Date"),
);
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']);
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(array(
'user',
'order',
));
if (is_array($tokens['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;
foreach ((array) $user as $key => $value) {
if (is_array($value) && isset($value['month'])) {
$out[$key] = date('Y-m-d', strtotime(implode('/', $value)));
}
else {
if (in_array($key, array(
'login',
'access',
'created',
))) {
$out[$key] = date('c', $value);
}
else {
if ($key != 'roles') {
$out[$key] = $value;
}
}
}
}
$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) {
$values = array();
if ($user = user_load(array(
'uid' => $uid,
))) {
$values = module_invoke_all('mailchimp_merge_values', $user);
}
return $values;
}
function _mailchimp_load_user_list_mergevars($uid, $list_id, $mergevars) {
$account = mailchimp_get_merge_values($uid);
$lists = variable_get('mailchimp_lists', array());
$list = $lists[$list_id];
$uservars = array(
'EMAIL' => $account['mail'],
);
foreach ($mergevars as $mergevar) {
if (!empty($list->mergevars[$mergevar['tag']]) && ($key = $list->mergevars[$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, $email, $merge_vars, $message = TRUE, $q = NULL) {
if ($q || ($q = _mailchimp_get_api_object())) {
$double_optin = $list->doublein;
if (empty($merge_vars)) {
$merge_vars = '';
}
if (_mailchimp_is_subscribed($list->id, $email, $q)) {
$action = 'updated in';
$success = $q
->listUpdateMember($list->id, $email, $merge_vars);
if ($success && $message) {
drupal_set_message(t('You have updated %email\'s settings in the %list list.', array(
'%email' => $email,
'%list' => $list->name,
)));
}
}
else {
$action = 'subscribed to';
$success = $q
->listSubscribe($list->id, $email, $merge_vars, 'html', $double_optin, TRUE);
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,
)));
}
elseif ($message && $success) {
drupal_set_message(t('You have subscribed %email to the %list list.', array(
'%email' => $email,
'%list' => $list->name,
)));
}
}
}
if ($success) {
watchdog('mailchimp', '@email was @action list @list.', array(
'@action' => $action,
'@email' => $merge_vars['EMAIL'],
'@list' => $list->name,
), WATCHDOG_NOTICE);
}
else {
watchdog('mailchimp', '@email has failed to subscribed to @list.', array(
'@email' => $merge_vars['EMAIL'],
'@list' => $list->name,
), WATCHDOG_WARNING);
}
return $success;
}
function _mailchimp_unsubscribe_user($list, $mail, $message = TRUE, $q = NULL) {
if ($q || ($q = _mailchimp_get_api_object())) {
$success = FALSE;
if (_mailchimp_is_subscribed($list->id, $mail, $q)) {
$success = $q
->listUnsubscribe($list->id, $mail, FALSE, FALSE, FALSE);
if ($message && $success) {
drupal_set_message(t('You have unsubscribed %email from the %list list.', array(
'%email' => $mail,
'%list' => $list->name,
)));
}
}
}
return $success;
}
function _mailchimp_get_available_lists($user) {
$all_lists = variable_get('mailchimp_lists', array());
$available_lists = array();
if (!empty($all_lists)) {
foreach ($all_lists as $key => $list) {
foreach ($user->roles as $rid => $info) {
if (!empty($list->roles[$rid])) {
$available_lists[$list->id] = $list;
break;
}
}
}
}
return $available_lists;
}
function _mailchimp_get_required_lists() {
$lists = variable_get('mailchimp_lists', array());
if (!is_array($lists)) {
$lists = array();
}
else {
if (!empty($lists)) {
foreach ($lists as $key => $list) {
if ($list->listtype !== MAILCHIMP_LISTTYPE_REQUIRED) {
unset($lists[$key]);
}
}
}
}
return $lists;
}
function _mailchimp_get_api_object() {
module_load_include('php', 'mailchimp', 'MCAPI.class');
$q = new MCAPI(variable_get('mailchimp_api_key', ''));
$q
->setTimeout(60);
if ($q->errorCode) {
watchdog('mailchimp', 'MCAPI Error: %errormsg', array(
'%errormsg' => $q->errorMessage,
), WATCHDOG_ERROR);
return NULL;
}
return $q;
}
function mailchimp_block($op = 'list', $delta = 0) {
if ($op == "list") {
$blocks = array();
$all_lists = variable_get('mailchimp_lists', array());
if (!empty($all_lists)) {
foreach ($all_lists as $key => $list) {
if ($list->listtype !== MAILCHIMP_LISTTYPE_REQUIRED) {
$blocks[$list->id] = array(
'info' => t('Mailchimp Subscription Form: @name', array(
'@name' => $list->name,
)),
'cache' => BLOCK_CACHE_PER_USER,
);
}
}
}
return $blocks;
}
else {
if ($op == 'view' && arg(2) != 'mailchimp' && arg(0) != 'mailchimp') {
$block = array();
global $user;
$lists = _mailchimp_get_available_lists($user);
if (!empty($lists)) {
if (!empty($lists[$delta]) && ($q = _mailchimp_get_api_object())) {
$list = $lists[$delta];
$block['subject'] = t('Subscribe to @title', array(
'@title' => $list->name,
));
if ($user->uid) {
$block['content'] = drupal_get_form('mailchimp_subscribe_auth_form_' . $list->id, $user, 1, $list);
}
else {
$block['content'] = drupal_get_form('mailchimp_subscribe_anon_form_' . $list->id, $list, $q);
}
}
}
return $block;
}
}
}
function mailchimp_rebuild_users() {
db_query("DELETE FROM {mailchimp_user}");
db_query("INSERT INTO {mailchimp_user} (uid, status) SELECT uid, '%s' FROM {users} WHERE uid > 0", MAILCHIMP_USERSTATUS_PENDING);
drupal_set_message('The MailChimp User\'s Table has been rebuilt with all user\'s status set to pending.', 'status');
drupal_goto('admin/settings/mailchimp');
}
function _mailchimp_implode_interest_groups($groups) {
if (!is_array($groups)) {
$groups = array(
$groups,
);
}
else {
$groups = array_filter($groups);
}
foreach ($groups as $key => $group) {
$groups[$key] = str_replace(',', '\\,', $group);
}
$groups = implode(',', $groups);
return $groups;
}
function _mailchimp_explode_interest_groups($groups) {
$groups = preg_split('/(?<!\\\\)\\,\\s?/', $groups);
foreach ($groups as $key => $group) {
$groups[$key] = str_replace('\\,', ',', $group);
}
return $groups;
}