function mailchimp_user in Mailchimp 5
Same name and namespace in other branches
- 5.2 mailchimp.module \mailchimp_user()
- 6.2 mailchimp.module \mailchimp_user()
- 6 mailchimp.module \mailchimp_user()
File
- ./
mailchimp.module, line 11
Code
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") {
// subscribe the user if they are not previously subscribed and want to be
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;
}
}