View source
<?php
require_once 'constant_contact.config.php';
function constant_contact_help($path, $arg) {
switch ($path) {
case 'admin/help#constant_contact':
return '<p>' . t('
<p>This module works with the new <a target="_blank" href="@api">Constant Contact REST API</a></p>
<p><a target="_blank" href="@cc">Signup for a free 60-day trial</a> if you do not already have an account.</p>
<p>To setup a custom signup form please follow the instructions below:</p>
<ol>
<li>First you need to <a target="_blank" href="@dev">Register for an API key</a> with constant contact</li>
<li>Next you should visit the <a href="@settings">settings page</a>, enter your account username, password and API key then save the page.</li>
<li>Now you can return to the <a href="@settings">settings page</a> and configure the options such as which contact lists the subscribers are added to or excluded from.</li>
<li>If you want to display a signup checkbox on the register page simply check the appropriate checkbox, if you want to use the form block method see <a href="@blocks">this page</a>.</li>
</ol>
<p>Detailed instructions, screen shots and videos will be available in a future release.</p>
<h6>Known Issues</h6>
<ul>
<li>If you want to mass delete users you probably need to mass unsubscribe them first.</li>
<li>Custom fields may not work on your specific setup, we have tested with the profile module only, this feature is still under development.</li>
<li>Updating custom fields will not work if they are displayed in a different section to the main form.</li>
</ul>
', array(
'@api' => 'http://developer.constantcontact.com/doc/reference',
'@dev' => 'http://developer.constant_contact.com/license/login',
'@cc' => 'http://www.jdoqocy.com/click-3699287-10296666',
'@settings' => url('admin/constant_contact/settings'),
'@blocks' => url('admin/build/block'),
)) . '</p>';
case 'admin/constant_contact/lists':
return t('<p>You can manage your contact lists below, this saves having to visit the constant contact website if you want to edit a list name</p><p>Editing a list name will not break your registration form, deleting a list will break it but only if you are using that list in the signup form, you should edit the settings to remove the list from the signup form after you have deleted it here.</p>');
case 'admin/constant_contact/settings':
return t('<p>Edit your Constant Contact account settings and configure your settings.</p>');
case 'admin/constant_contact/lists/add':
return t('<p>This will add a new contact list to your Constant Contact account.</p>');
case 'admin/constant_contact/lists/edit/%':
return t('<p>This will update the contact list with your Constant Contact account.</p>');
case 'admin/constant_contact/lists/delete/%':
return t('<p>This will delete the contact list from your Constant Contact account, users subscribed to the list will be unsubscribed from the list first but will remain subscribed to other lists.</p>');
}
}
function constant_contact_menu() {
$items = array();
$items['admin/constant_contact'] = array(
'title' => 'Constant contact',
'description' => 'Setup and configure your Constant Contact signup form',
'page callback' => 'constant_contact_intro',
'access arguments' => array(
'administer constant_contact',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'system.admin.inc',
);
$items['admin/constant_contact/settings'] = array(
'title' => 'Change settings',
'description' => 'Change your constant contact settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'constant_contact_settings',
),
'access arguments' => array(
'administer constant_contact',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'system.admin.inc',
);
$items['admin/constant_contact/lists'] = array(
'title' => 'Contact Lists',
'description' => 'Manage your contact lists',
'page callback' => 'constant_contact_manage_lists',
'access arguments' => array(
'administer constant_contact',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'contactlists.admin.inc',
);
$items['admin/constant_contact/lists/add'] = array(
'title' => 'Add List',
'description' => 'Add a new contact list to constant contact',
'page callback' => 'constant_contact_add_list',
'access arguments' => array(
'administer constant_contact',
),
'type' => MENU_CALLBACK,
'file' => 'contactlists.admin.inc',
);
$items['admin/constant_contact/lists/edit/%'] = array(
'title' => 'Edit List',
'description' => 'Edit a contact list',
'page callback' => 'constant_contact_edit_list',
'page arguments' => array(
4,
),
'access arguments' => array(
'administer constant_contact',
),
'type' => MENU_CALLBACK,
'file' => 'contactlists.admin.inc',
);
$items['admin/constant_contact/lists/delete/%'] = array(
'title' => 'Delete List',
'description' => 'Delete a contact list',
'page callback' => 'constant_contact_delete_list',
'page arguments' => array(
4,
),
'access arguments' => array(
'administer constant_contact',
),
'type' => MENU_CALLBACK,
'file' => 'contactlists.admin.inc',
);
return $items;
}
function constant_contact_perm() {
return array(
'access constant_contact',
'administer constant_contact',
);
}
function constant_contact_form_user_register_alter(&$form, &$form_state) {
$show_on_register = variable_get('constant_contact_show_on_register', CONSTANT_CONTACT_SHOW_ON_REGISTER);
$show_lists_selection = variable_get('constant_contact_show_list_selection', CONSTANT_CONTACT_SHOW_LIST_SELECTION);
if (!$show_on_register) {
return;
}
$cc = constant_contact_create_object();
if (!is_object($cc)) {
drupal_set_message(t("Failed to create constant contact object, please check your account details are correctly entered on the settings page"), 'error');
return;
}
$lists = $cc
->get_lists();
if ($lists) {
foreach ($lists as $k => $v) {
$selected_lists[$k] = $v['id'];
}
}
$form['account']['cc_newsletter'] = array(
'#type' => 'checkbox',
'#title' => variable_get('constant_contact_signup_title', CONSTANT_CONTACT_SIGNUP_TITLE),
'#description' => variable_get('constant_contact_signup_description', CONSTANT_CONTACT_SIGNUP_DESCRIPTION),
'#weight' => 10,
);
$form['account']['cc_newsletter']['#default_value'] = 1;
if ($show_lists_selection) {
$exclude_lists = variable_get('constant_contact_lists', '');
$lists = $cc
->get_lists();
if ($lists) {
foreach ($lists as $k => $v) {
$lists[$k] = $v['id'];
}
}
if (!is_array($exclude_lists)) {
$exclude_lists = array();
}
$options = array();
foreach ($lists as $list_id) {
if (!in_array($list_id, $exclude_lists)) {
$list = $cc
->get_list($list_id);
$options[$list['id']] = $list['Name'];
}
}
if (count($options) > 0) {
$form['account']['cc_newsletter_lists'] = array(
'#type' => 'select',
'#description' => variable_get('constant_contact_signup_lists_description', CONSTANT_CONTACT_SIGNUP_LISTS_DESCRIPTION),
'#options' => $options,
'#multiple' => true,
'#size' => count($options),
'#default_value' => $selected_lists,
'#weight' => 11,
);
}
}
return $form;
}
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';
}
}
}
$show_on_register = variable_get('constant_contact_show_on_register', CONSTANT_CONTACT_SHOW_ON_REGISTER);
$show_lists_selection = variable_get('constant_contact_show_list_selection', CONSTANT_CONTACT_SHOW_LIST_SELECTION);
$lists = variable_get('constant_contact_lists', '');
$selected_lists = array();
if (!$show_on_register) {
return;
}
$valid_ops = array(
'form',
'delete',
'after_update',
'insert',
);
if (in_array($op, $valid_ops)) {
$cc = constant_contact_create_object();
if (!is_object($cc)) {
drupal_set_message(t("Failed to create constant contact object, please check your account details are correctly entered on the settings page"), 'error');
return;
}
}
if ($op == 'form') {
$selected_lists = array();
if (!$show_on_register) {
return;
}
$cc = constant_contact_create_object();
if (!is_object($cc)) {
drupal_set_message(t("Failed to create constant contact object, please check your account details are correctly entered on the settings page"), 'error');
return;
}
$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' => true,
'#tree' => false,
'#description' => t('Manage your newsletter subscription'),
);
$form['constant_contact']['cc_newsletter'] = array(
'#type' => 'checkbox',
'#title' => variable_get('constant_contact_signup_title', CONSTANT_CONTACT_SIGNUP_TITLE),
'#description' => variable_get('constant_contact_signup_description', CONSTANT_CONTACT_SIGNUP_DESCRIPTION),
'#weight' => 10,
);
$form['constant_contact']['cc_newsletter']['#default_value'] = $account->cc_newsletter;
if ($show_lists_selection) {
$exclude_lists = variable_get('constant_contact_lists', '');
$lists = $cc
->get_lists();
if ($lists) {
foreach ($lists as $k => $v) {
$lists[$k] = $v['id'];
}
}
if (!is_array($exclude_lists)) {
$exclude_lists = array();
}
$options = array();
foreach ($lists as $list_id) {
if (!in_array($list_id, $exclude_lists)) {
$list = $cc
->get_list($list_id);
$options[$list['id']] = $list['Name'];
}
}
if (count($options) > 0) {
$form['constant_contact']['cc_newsletter_lists'] = array(
'#type' => 'select',
'#description' => variable_get('constant_contact_signup_lists_description', CONSTANT_CONTACT_SIGNUP_LISTS_DESCRIPTION),
'#options' => $options,
'#multiple' => true,
'#size' => count($options),
'#default_value' => $selected_lists,
'#weight' => 11,
);
}
}
return $form;
}
elseif ($op == 'insert') {
if (isset($edit['cc_newsletter']) && $edit['cc_newsletter']) {
$lists = variable_get('constant_contact_lists', '');
$fields = variable_get('constant_contact_custom_fields', CONSTANT_CONTACT_CUSTOM_FIELDS);
if ($show_lists_selection && is_array($lists)) {
$lists = $edit['cc_newsletter_lists'];
}
elseif (!$show_lists_selection) {
$_lists = $cc
->get_lists();
if ($_lists) {
foreach ($_lists as $k => $v) {
$_lists[$k] = $v['id'];
}
}
$newlists = array();
foreach ($_lists as $list_id) {
if (in_array($list_id, $lists)) {
$list = $cc
->get_list($list_id);
$newlists[$list['id']] = $list['Name'];
}
}
$lists = $newlists;
}
if ($lists) {
$fields_bits = explode(',', $fields);
$fields = array();
if ($fields_bits) {
foreach ($fields_bits as $bit) {
$field_bit = explode(':', trim($bit));
if (is_array($field_bit) && isset($field_bit[0], $field_bit[1])) {
$cc_fieldname = $field_bit[0];
$fieldname = $field_bit[1];
if (isset($_POST[$fieldname])) {
$fields[$cc_fieldname] = $_POST[$fieldname];
}
}
}
}
$contact = $cc
->query_contacts($edit['mail']);
$cc
->set_action_type($action_type);
if ($contact) {
$contact = $cc
->get_contact($contact['id']);
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), $fields);
}
else {
$status = $cc
->create_contact($edit['mail'], array_keys($lists), $fields);
}
if (!$status) {
drupal_set_message(t("Sorry we encountered an error: {$cc->last_error}"), 'error');
}
}
}
}
elseif ($op == 'delete') {
if ($account->cc_newsletter) {
$contact = $cc
->query_contacts($account->mail);
$cc
->set_action_type($action_type);
if ($contact) {
$cc
->delete_contact($contact['id']);
}
}
}
elseif ($op == 'after_update') {
$fields = variable_get('constant_contact_custom_fields', CONSTANT_CONTACT_CUSTOM_FIELDS);
if ($show_lists_selection) {
$lists = $edit['cc_newsletter_lists'];
}
else {
$_lists = $cc
->get_lists();
if ($_lists) {
foreach ($_lists as $k => $v) {
$_lists[$k] = $v['id'];
}
}
$newlists = array();
foreach ($_lists as $list_id) {
if (in_array($list_id, $lists)) {
$list = $cc
->get_list($list_id);
$newlists[$list['id']] = $list['Name'];
}
}
$lists = $newlists;
}
$fields_bits = explode(',', $fields);
$fields = array();
if ($fields_bits) {
foreach ($fields_bits as $bit) {
$field_bit = explode(':', trim($bit));
if (is_array($field_bit) && isset($field_bit[0], $field_bit[1])) {
$cc_fieldname = $field_bit[0];
$fieldname = $field_bit[1];
if (isset($edit[$fieldname])) {
$fields[$cc_fieldname] = $edit[$fieldname];
}
}
}
}
$contact = $cc
->query_contacts($edit['mail']);
$cc
->set_action_type('contact');
$status = true;
if ($contact) {
$contact = $cc
->get_contact($contact['id']);
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 (isset($edit['cc_newsletter']) && $edit['cc_newsletter']) {
$status = $cc
->update_contact($contact['id'], $edit['mail'], array_keys($lists), $fields);
}
else {
$status = $cc
->update_contact($contact['id'], $edit['mail'], array(), $fields);
}
}
elseif (isset($edit['cc_newsletter']) && $edit['cc_newsletter']) {
$status = $cc
->create_contact($edit['mail'], array_keys($lists), $fields);
}
}
}
function constant_contact_user_operations() {
$operations = array(
'unsubscribe' => array(
'label' => t('Unsubscribe the selected users'),
'callback' => 'constant_contact_user_operations_unsubscribe',
),
);
return $operations;
}
function constant_contact_user_operations_unsubscribe($accounts) {
$cc = constant_contact_create_object();
foreach ($accounts as $uid) {
$account = user_load(array(
'uid' => (int) $uid,
));
if ($account !== FALSE && $account->cc_newsletter) {
user_save($account, array(
'cc_newsletter' => 0,
));
$contact = $cc
->query_contacts($account->mail);
if ($contact) {
$cc
->delete_contact($contact['id']);
}
}
}
}
function constant_contact_block($op = 'view', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('Constant Contact Signup Form'),
);
return $blocks;
case 'configure':
$form = array();
return;
case 'save':
return;
case 'view':
switch ($delta) {
case 0:
$block['subject'] = t('Signup');
$block['content'] = drupal_get_form('constant_contact_signup_form');
break;
}
return $block;
}
}
function constant_contact_signup_form(&$form_state) {
$cc = constant_contact_create_object();
if (!$cc) {
return;
}
$form = array();
$show_firstname = variable_get('constant_contact_show_firstname', CONSTANT_CONTACT_SHOW_FIRSTNAME);
$show_lastname = variable_get('constant_contact_show_lastname', CONSTANT_CONTACT_SHOW_LASTNAME);
$show_job_title = variable_get('constant_contact_show_job_title', CONSTANT_CONTACT_SHOW_JOB_TITLE);
$show_company_name = variable_get('constant_contact_show_company_name', CONSTANT_CONTACT_SHOW_COMPANY_NAME);
$show_lists_selection = variable_get('constant_contact_show_list_selection', CONSTANT_CONTACT_SHOW_LIST_SELECTION);
if ($show_firstname) {
$form['cc_firstname'] = array(
'#type' => 'textfield',
'#title' => t('Firstname'),
'#size' => 30,
'#required' => TRUE,
'#default_value' => $form_state['values']['cc_firstname'],
);
}
if ($show_lastname) {
$form['cc_lastname'] = array(
'#type' => 'textfield',
'#title' => t('Lastname'),
'#size' => 30,
'#required' => TRUE,
'#default_value' => $form_state['values']['cc_lastname'],
);
}
if ($show_company_name) {
$form['cc_company_name'] = array(
'#type' => 'textfield',
'#title' => t('Company Name'),
'#size' => 30,
'#required' => TRUE,
'#default_value' => $form_state['values']['cc_company_name'],
);
}
if ($show_job_title) {
$form['cc_job_title'] = array(
'#type' => 'textfield',
'#title' => t('Job Title'),
'#size' => 30,
'#required' => TRUE,
'#default_value' => $form_state['values']['cc_job_title'],
);
}
$form['cc_email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#size' => 30,
'#required' => TRUE,
);
if ($show_lists_selection) {
$exclude_lists = variable_get('constant_contact_lists', '');
$lists = $cc
->get_lists();
if ($lists) {
foreach ($lists as $k => $v) {
$lists[$k] = $v['id'];
}
}
if (!is_array($exclude_lists)) {
$exclude_lists = array();
}
$options = array();
foreach ($lists as $list_id) {
if (!in_array($list_id, $exclude_lists)) {
$list = $cc
->get_list($list_id);
$options[$list['id']] = $list['Name'];
}
}
if (count($options) > 0) {
$form['cc_newsletter_lists'] = array(
'#type' => 'select',
'#description' => variable_get('constant_contact_signup_lists_description', CONSTANT_CONTACT_SIGNUP_LISTS_DESCRIPTION),
'#options' => $options,
'#multiple' => true,
'#size' => count($options),
);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Signup'),
);
return $form;
}
function constant_contact_signup_form_submit($form, &$form_state) {
$cc = constant_contact_create_object();
$lists = variable_get('constant_contact_lists', '');
$show_lists_selection = variable_get('constant_contact_show_list_selection', CONSTANT_CONTACT_SHOW_LIST_SELECTION);
if ($cc) {
$fields = array();
if ($show_lists_selection && is_array($lists)) {
$lists = $form_state['values']['cc_newsletter_lists'];
}
elseif (!$show_lists_selection) {
$_lists = $cc
->get_lists();
if ($_lists) {
foreach ($_lists as $k => $v) {
$_lists[$k] = $v['id'];
}
}
$newlists = array();
foreach ($_lists as $list_id) {
if (in_array($list_id, $lists)) {
$list = $cc
->get_list($list_id);
$newlists[$list['id']] = $list['Name'];
}
}
$lists = $newlists;
}
else {
$lists = array();
}
$show_firstname = variable_get('constant_contact_show_firstname', CONSTANT_CONTACT_SHOW_FIRSTNAME);
$show_lastname = variable_get('constant_contact_show_lastname', CONSTANT_CONTACT_SHOW_LASTNAME);
$show_job_title = variable_get('constant_contact_show_job_title', CONSTANT_CONTACT_SHOW_JOB_TITLE);
$show_company_name = variable_get('constant_contact_show_company_name', CONSTANT_CONTACT_SHOW_COMPANY_NAME);
$custom_fields = variable_get('constant_contact_custom_fields', CONSTANT_CONTACT_CUSTOM_FIELDS);
$custom_fields_bits = explode(',', $custom_fields);
if ($show_firstname) {
$fields['FirstName'] = $form_state['values']['cc_firstname'];
}
if ($show_lastname) {
$fields['LastName'] = $form_state['values']['cc_lastname'];
}
if ($show_company_name) {
$fields['CompanyName'] = $form_state['values']['cc_company_name'];
}
if ($show_job_title) {
$fields['JobTitle'] = $form_state['values']['cc_job_title'];
}
$user_email = $form_state['values']['cc_email'];
$cc
->set_action_type('contact');
$contact = $cc
->query_contacts($user_email);
if ($contact) {
$contact = $cc
->get_contact($contact['id']);
if ($lists && $contact['lists']) {
foreach ($contact['lists'] as $list_id => $list_name) {
if (!isset($lists[$list_id])) {
$list = $cc
->get_list($list_id);
$lists[$list_id] = $list['Name'];
}
}
}
$status = $cc
->update_contact($contact['id'], $user_email, array_keys($lists), $fields);
if ($status) {
drupal_set_message(t('Success, we have updated your subscription'));
}
else {
drupal_set_message(t('Sorry, there was a problem, please ensure your email is valid and try again'), 'error');
}
}
else {
$status = $cc
->create_contact($user_email, array_keys($lists), $fields);
if ($status) {
drupal_set_message(t('Success, you are now subscribed to our mailing list'));
}
else {
drupal_set_message(t('Sorry, there was a problem, please ensure your email is valid and try again'), 'error');
}
}
}
else {
drupal_set_message(t('The username, password or API key data could not be found'), 'error');
}
$form_state['redirect'] = '';
return;
}
function constant_contact_create_object() {
$username = variable_get('constant_contact_username', '');
$password = variable_get('constant_contact_password', '');
$api_key = variable_get('constant_contact_api_key', '');
if ($username && $password && $api_key) {
require_once dirname(__FILE__) . '/class.cc.php';
$cc = new cc($username, $password, $api_key);
if ($cc
->get_service_description()) {
return $cc;
}
}
return false;
}