function mailchimp_user in Mailchimp 6.2
Same name and namespace in other branches
- 5.2 mailchimp.module \mailchimp_user()
- 5 mailchimp.module \mailchimp_user()
- 6 mailchimp.module \mailchimp_user()
Implementation of hook_user
Required lists are updated on insert, update, and delete. The list form fields are added to the registration and profile forms
1 string reference to 'mailchimp_user'
- mailchimp_update_6201 in ./
mailchimp.install - Implementation of hook_update_N().
File
- ./
mailchimp.module, line 20 - Mailchimp module.
Code
function mailchimp_user($op, &$edit, &$account, $category = NULL) {
// include the form in the user registration if any applicable newsletters
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())) {
// wrap in a fieldset
$form['mailchimp_list_forms'] = array(
'#type' => 'fieldset',
'#title' => t(variable_get('mailchimp_user_settings_title', 'Newsletter Subscriptions')),
);
// container for the list objects we'll need on form submission
$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) {
// is the checkbox for the newsletter selected?
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));
// include interest groups
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) {
// delete a user from MC list
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;
// insert or update a user to/in a MC list
case 'insert':
case 'after_update':
// don't repeat if already managing via cron
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;
// determine if a user is allowed in a given list
$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;
}
}
// they are allowed, update or subscribe
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']);
// They don't *really* need to go in the queue unless they just changed their prefs.
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);
}
}
}
}
}