campaignmonitor.user_page.inc in Campaign Monitor 6.3
File
includes/campaignmonitor.user_page.inc
View source
<?php
function campaignmonitor_user_page_form() {
global $user;
$display_on = variable_get(CM_DISPLAY_ON, array());
if ($display_on['userpage']) {
$api_key = variable_get(CM_API_KEY, '');
$client_id = variable_get(CM_CLIENT_ID, '');
$lists = campaignmonitor_get_available_lists();
$options = array();
$default_values = array();
foreach ($lists as $list_id => $list) {
if ($list && $list->onuserpage) {
$default = FALSE;
if (_campaignmonitor_is_subscribed($api_key, $list_id, $user->mail, TRUE)) {
$default = TRUE;
$default_values[] = $list_id;
}
$options[$list_id] = $list->name;
$form['is_subscribed_' . $list_id] = array(
'#type' => 'hidden',
'#default_value' => $default,
);
}
}
if (!empty($options)) {
$form['subscribe_newsletter'] = array(
'#type' => 'checkboxes',
'#title' => t(variable_get(CM_USERPAGE_DISPLAY_TEXT, CM_USERPAGE_DISPLAY_TEXT_DEFAULT)),
'#options' => $options,
'#default_value' => $default_values,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
}
else {
$form['subscribe_newsletter'] = array(
'#value' => t('There are no list avaliable.'),
);
}
}
else {
$form['subscribe_newsletter'] = array(
'#value' => t('You are not allow to subscrib newsletters.'),
);
}
return $form;
}
function campaignmonitor_user_page_form_submit($form, &$form_state) {
global $user;
$api_key = variable_get(CM_API_KEY, '');
$client_id = variable_get(CM_CLIENT_ID, '');
$chosen_lists = campaignmonitor_get_available_lists();
$subscribe = $form_state['values']['subscribe_newsletter'];
foreach ($chosen_lists as $list_id => $list) {
if ($subscribe[$list_id]) {
$account = campaignmonitor_get_field_key_values($user->uid);
$name = isset($account[$list->namekey]) ? $account[$list->namekey] : '';
if (isset($subscribe[$list_id]) && !$form_state['values']['is_subscribed_' . $list_id]) {
_campaignmonitor_add_subscriber($api_key, $list_id, $name, $user->mail);
}
}
elseif (!$subscribe[$list_id] && $form_state['values']['is_subscribed_' . $list_id]) {
_campaignmonitor_remove_subscriber($api_key, $list_id, $user->mail);
}
}
}