function constant_contact_user in Constant Contact 6.3
Same name and namespace in other branches
- 6.2 constant_contact.module \constant_contact_user()
This method adds extra functionality to the user methods Create, Update, Delete
File
- ./
constant_contact.module, line 334
Code
function constant_contact_user($op, &$edit, &$account, $category = NULL) {
global $user;
$action_type = 'contact';
if (isset($user->uid) && $user->uid) {
$uid = $user->uid;
$allowed = array(
'Administrator',
'Editor',
);
foreach ($user->roles as $role) {
if (in_array($role, $allowed)) {
$action_type = 'customer';
}
}
}
$subscribe_method = variable_get('cc_register_page_method', CC_REGISTER_PAGE_METHOD);
$list_format = variable_get('cc_list_selection_format', CC_LIST_SELECTION_FORMAT);
$show_format_choice = variable_get('cc_show_format_choice', CC_SHOW_FORMAT_CHOICE);
$default_subscribe_format = variable_get('cc_subscribe_format', CC_SUBSCRIBE_FORMAT);
$lists = variable_get('cc_lists', '');
$selected_lists = array();
if ($subscribe_method == 'none') {
return;
}
// Fixes a bug: http://drupal.org/node/697400
if ($op == 'insert' || $op == 'after_update') {
if ($category == 'account' && (!isset($edit['cc_newsletter_lists']) and !isset($edit['cc_newsletter']))) {
return;
}
if (!isset($edit['cc_newsletter']) && !isset($account->cc_newsletter) && !count($edit['cc_newsletter_lists']) && !count($account->cc_newsletter_lists)) {
return;
}
}
if ($op == 'form' && $category == 'account') {
// alter the user edit form
$selected_lists = array();
$cc = constant_contact_create_object();
if (!is_object($cc)) {
return;
}
// get contact and selected lists
$contact = $cc
->query_contacts($account->mail);
$contact = $cc
->get_contact($contact['id']);
if ($contact['lists']) {
$selected_lists = $contact['lists'];
}
$form['constant_contact'] = array(
'#type' => 'fieldset',
'#title' => t('Newsletter'),
'#collapsible' => TRUE,
'#collapsed' => false,
'#tree' => false,
'#description' => t('Manage your newsletter subscription'),
);
// check with the returned API data if they are active or not
// if syncing is disabled this will mean their status is reflected correctly
if ($contact) {
if ($contact['Status'] == 'Active') {
$account->cc_newsletter = 1;
}
else {
$account->cc_newsletter = 0;
}
$default_subscribe_format = $contact['EmailType'];
}
if ($subscribe_method == 'lists') {
$exclude_lists = variable_get('cc_lists', array());
$lists = constant_contact_get_lists($cc);
$options = array();
foreach ($lists as $list_id => $list_name) {
if (!in_array($list_id, $exclude_lists)) {
$options[$list_id] = $list_name;
}
}
if (count($options) > 0) {
if ($list_format == 'select') {
$field_type = 'select';
}
else {
$field_type = 'checkboxes';
}
$form['constant_contact']['cc_newsletter_lists'] = array(
'#type' => $field_type,
'#title' => variable_get('cc_signup_title', CC_SIGNUP_TITLE),
'#description' => variable_get('cc_signup_description', CC_SIGNUP_DESCRIPTION),
'#options' => $options,
'#default_value' => $selected_lists,
'#weight' => 11,
);
if ($list_format == 'select') {
$field_size = $options && count($options) > 25 ? 25 : count($options);
$form['constant_contact']['cc_newsletter_lists']['#multiple'] = true;
$form['constant_contact']['cc_newsletter_lists']['#size'] = count($options);
}
}
}
else {
$form['constant_contact']['cc_newsletter'] = array(
'#type' => 'checkbox',
'#title' => variable_get('cc_signup_title', CC_SIGNUP_TITLE),
'#description' => variable_get('cc_signup_description', CC_SIGNUP_DESCRIPTION),
'#weight' => 10,
'#default_value' => $account->cc_newsletter,
);
}
if ($show_format_choice) {
$form['constant_contact']['cc_email_format'] = array(
'#type' => 'radios',
'#title' => t('Email Format'),
'#description' => 'You can receive emails in Text or HTML format',
'#weight' => 12,
'#default_value' => $default_subscribe_format,
'#options' => array(
'Text' => 'Text',
'HTML' => 'HTML',
),
);
}
return $form;
}
elseif ($op == 'insert') {
$cc = constant_contact_create_object();
if (!is_object($cc)) {
return;
}
$fields = variable_get('cc_extra_fields', array());
$field_mappings = constant_contact_build_field_mappings();
if ($subscribe_method == 'checkbox') {
if (!$edit['cc_newsletter']) {
return;
}
$lists = variable_get('cc_lists', array());
}
else {
$lists = $edit['cc_newsletter_lists'];
$newlists = array();
foreach ($lists as $list_id => $enabled) {
if (intval($enabled) !== 0) {
$list = $cc
->get_list($list_id);
$newlists[$list_id] = $list['Name'];
}
}
$lists = $newlists;
}
if (!count($lists)) {
return;
}
//module_invoke_all('constant_contact', 'user_inserted', $edit, $lists);
$edit['cc_newsletter'] = 1;
/* they may not have this flag set but may be subscribed */
// parse custom fields
$extra_fields = array();
if (is_array($fields)) {
foreach ($fields as $field) {
$fieldname = str_replace(' ', '', $field);
if (isset($field_mappings[$fieldname]) && isset($_POST[$field_mappings[$fieldname]])) {
$extra_fields[$fieldname] = $_POST[$field_mappings[$fieldname]];
// CCK fields / multi-value fields - strip down array to first value.
// structure is probably $field[$delta][$value], but try to work for any array
if (is_array($extra_fields[$fieldname])) {
while (is_array($extra_fields[$fieldname])) {
$extra_fields[$fieldname] = array_shift($extra_fields[$fieldname]);
}
}
}
}
}
// find contact
$contact = $cc
->query_contacts($edit['mail']);
$cc
->set_action_type($action_type);
$email_format = $contact ? $contact['EmailType'] : $default_subscribe_format;
if ($show_format_choice && isset($edit['cc_email_format'])) {
$email_format = $edit['cc_email_format'];
}
$extra_fields['EmailType'] = $email_format;
if ($contact) {
$contact = $cc
->get_contact($contact['id']);
// merge contact lists user is already subscribed to
if ($lists && $contact['lists']) {
foreach ($contact['lists'] as $list_id) {
if (!isset($lists[$list_id])) {
$list = $cc
->get_list($list_id);
$lists[$list_id] = $list['Name'];
}
}
}
$status = $cc
->update_contact($contact['id'], $edit['mail'], array_keys($lists), $extra_fields);
}
else {
$status = $cc
->create_contact($edit['mail'], array_keys($lists), $extra_fields);
}
if (!$status) {
drupal_set_message(t(constant_contact_display_last_error($cc->http_response_code)), 'error');
}
}
elseif ($op == 'delete') {
$cc = constant_contact_create_object();
if (!is_object($cc)) {
return;
}
if ($account->cc_newsletter) {
// find contact
$contact = $cc
->query_contacts($account->mail);
/* important, this tells CC that the contact or customer made this action */
$cc
->set_action_type($action_type);
if ($contact) {
$cc
->delete_contact($contact['id']);
}
}
}
elseif ($op == 'after_update') {
$cc = constant_contact_create_object();
if (!is_object($cc)) {
return;
}
$fields = variable_get('cc_extra_fields', array());
$field_mappings = constant_contact_build_field_mappings();
if ($subscribe_method == 'lists') {
if ($category == 'account') {
$lists = $edit['cc_newsletter_lists'];
}
else {
$lists = $account->cc_newsletter_lists;
}
$newlists = array();
foreach ($lists as $list_id => $enabled) {
if (intval($enabled) !== 0) {
$list = $cc
->get_list($list_id);
$newlists[$list_id] = $list['Name'];
}
}
$lists = $newlists;
}
else {
if ($category == 'account') {
$edit['cc_newsletter_lists'] = array();
}
else {
$account->cc_newsletter_lists = array();
}
$lists = constant_contact_get_lists($cc);
$enabled_lists = variable_get('cc_lists', array());
$newlists = array();
foreach ($lists as $list_id => $list_name) {
if (in_array($list_id, $enabled_lists)) {
$newlists[$list_id] = $list_name;
}
}
$lists = $newlists;
}
// parse custom fields
$extra_fields = array();
foreach ($fields as $field) {
$fieldname = str_replace(' ', '', $field);
if (isset($field_mappings[$fieldname])) {
$actual_fieldname = $field_mappings[$fieldname];
// the data we receive here could be from 2 different forms so handle them both
if ($category == 'account') {
// if the fieldname has been posted add to our custom CC fields array
if (isset($edit[$actual_fieldname])) {
$extra_fields[$fieldname] = $edit[$actual_fieldname];
// CCK fields / multi-value fields - strip down array to first value.
// structure is probably $field[$delta][$value], but try to work for any array
if (is_array($extra_fields[$fieldname])) {
while (is_array($extra_fields[$fieldname])) {
$extra_fields[$fieldname] = array_shift($extra_fields[$fieldname]);
}
}
}
}
else {
if (isset($account->{$actual_fieldname})) {
$extra_fields[$fieldname] = $account->{$actual_fieldname};
// CCK fields / multi-value fields - strip down array to first value.
// structure is probably $field[$delta][$value], but try to work for any array
if (is_array($extra_fields[$fieldname])) {
while (is_array($extra_fields[$fieldname])) {
$extra_fields[$fieldname] = array_shift($extra_fields[$fieldname]);
}
}
}
}
}
}
// find contact
if ($category == 'account') {
$contact = $cc
->query_contacts($edit['mail']);
}
else {
$contact = $cc
->query_contacts($account->mail);
}
$cc
->set_action_type('contact');
$status = true;
if ($contact) {
$email_format = $contact['EmailType'];
if ($show_format_choice) {
if ($category == 'account') {
$email_format = $edit['cc_email_format'];
}
else {
$email_format = $account->cc_email_format;
}
}
$extra_fields['EmailType'] = $email_format;
$contact = $cc
->get_contact($contact['id']);
// merge contact lists user is already subscribed to
if ($lists && $contact['lists']) {
foreach ($contact['lists'] as $list_id) {
if (!isset($lists[$list_id])) {
$list = $cc
->get_list($list_id);
$lists[$list_id] = $list['Name'];
}
}
}
if ($category == 'account') {
if ($subscribe_method == 'checkbox' && $edit['cc_newsletter']) {
$status = $cc
->update_contact($contact['id'], $edit['mail'], array_keys($lists), $extra_fields);
}
elseif ($subscribe_method == 'checkbox' && !$edit['cc_newsletter']) {
$status = $cc
->update_contact($contact['id'], $edit['mail'], array(), $extra_fields);
}
elseif ($subscribe_method == 'lists' && count($lists) > 0) {
$status = $cc
->update_contact($contact['id'], $edit['mail'], array_keys($lists), $extra_fields);
$edit['cc_newsletter'] = 1;
}
elseif ($subscribe_method == 'lists' && !count($lists)) {
$status = $cc
->update_contact($contact['id'], $edit['mail'], array(), $extra_fields);
}
}
else {
if ($subscribe_method == 'checkbox' && $account->cc_newsletter) {
$status = $cc
->update_contact($contact['id'], $account->mail, array_keys($lists), $extra_fields);
}
elseif ($subscribe_method == 'checkbox' && !$account->cc_newsletter) {
$status = $cc
->update_contact($contact['id'], $account->mail, array(), $extra_fields);
}
elseif ($subscribe_method == 'lists' && count($lists) > 0) {
$status = $cc
->update_contact($contact['id'], $account->mail, array_keys($lists), $extra_fields);
$account->cc_newsletter = 1;
}
elseif ($subscribe_method == 'lists' && !count($lists)) {
$status = $cc
->update_contact($contact['id'], $account->mail, array(), $extra_fields);
}
}
}
elseif ($subscribe_method == 'checkbox' && $edit['cc_newsletter']) {
$status = $cc
->create_contact($edit['mail'], array_keys($lists), $extra_fields);
}
elseif ($subscribe_method == 'lists' && count($lists) > 0) {
$status = $cc
->create_contact($edit['mail'], array_keys($lists), $extra_fields);
$edit['cc_newsletter'] = 1;
}
if (!$status) {
drupal_set_message(t(constant_contact_display_last_error($cc->http_response_code)), 'error');
}
}
}