1,513c1,623
< <?php
< /* $Id: campaignmonitor.module,v 1.7.2.3 2008/12/20 05:26:38 ssherriff Exp $ */
< 
< /* DRUPAL HOOKS */
< /* hook_help */
< function campaignmonitor_help($path, $arg) {
<   switch ($path) {
<     case 'admin/settings/campaignmonitor':
<       return '<p>' . t('Use your API key and other keys to have users register for a mailing list setup through Campaign Monitor.') . '</p>';
<   }
< }
< 
< 
< /* hook_perm */
< function campaignmonitor_perm() {
<   return array('administer campaignmonitor', 'access archive', 'join newsletter');
< }
< 
< /* hook_menu */
< function campaignmonitor_menu() {
<   $items = array();
<   
<   $items['admin/settings/campaignmonitor'] = array(
<     'title' => t('Campaign Monitor'),
<     'description' => t('Setup Campaign Monitor values.'),
<     'page callback' => 'drupal_get_form',
<     'page arguments' => array('campaignmonitor_admin'),
<     'access arguments' => array('administer campaignmonitor'),
<     'type' => MENU_NORMAL_ITEM);
<   $items['newsletter_archive'] = array(
<     'title' => t('Newsletter Archive'),
<     'page callback' => 'campaignmonitor_newsletter_archive',
<     'access arguments' => array('access archive'),
<     'type' => MENU_SUGGESTED_ITEM);
< 	  
<   $items['user/%user/newsletters'] = array(
<     'title' => t('My Newsletters'),
<     'page callback' => 'campaignmonitor_user_page',
< 	'access arguments' => array('join newsletter'),
<     'type' => MENU_LOCAL_TASK,
<     'weight' => 5,
<   );
<   return $items;
< }
< 
< /* hook_form_alter */
< function campaignmonitor_form_alter(&$form, $form_state, $form_id) {
<   $display_on = variable_get('campaignmonitor_display_on', array());
<   
<   if (('contact_mail_page' == $form_id && $display_on['contact'] == 'contact') || ('user_register' == $form_id && $display_on['registration'] == 'registration') && user_access('join newsletter')) {
<     $form['subscribe_newsletter'] = array(
<       '#type'          => 'checkbox',
<       '#title'         => t(variable_get('campaignmonitor_checkboxdisplaytext', 'Join our Newsletter?')),
<       '#weight'        => 99,
<       '#default_value' => 1,
<     );
<   
<     $form['submit']['#weight'] = 100;
<     
<     $form['#submit'] = array('_campaignmonitor_form_submit' => array()) + (array)$form['#submit'];
<   }
< }
< 
< /**
<  * Implementation of hook_block()
<  */
< function campaignmonitor_block($op = 'list', $delta = 0, $edit = array()) {
<   switch ($op) {
<     case 'list':
<       $blocks[0] = array('info' => t('Join newsletter'));
<       return $blocks;
<     
<     case 'configure':
<     case 'save':
<       // no additional configuration options
<       break;
<     
<     case 'view':
<     default:
<       $block['subject'] = t('Join Newsletter');
<       $block['content'] = theme('campaignmonitor_block_content', $delta);
<       return $block;
<   }
< }
< 
< /**
<  * Implementation of hook_theme()
<  */
< function campaignmonitor_theme() {
<   return array(
<     'campaignmonitor_block_content' => array(
<       'template' => 'campaignmonitor-block-content',
<     ),
<   );
< }
< 
< /* END DRUPAL HOOKS */
< 
< /* MODULE FUNCTIONS */
< 
< function campaignmonitor_admin() {
< 
<   $form['campaignmonitor_api_key'] = array(
<     '#type' => 'textfield',
<     '#title' => t('API Key'),
<     '#default_value' => variable_get('campaignmonitor_api_key', ''),
<     '#required' => TRUE,
<     '#size' => 50,
<     '#maxlength' => 200,
<     '#description' => t("Your Campaign Monitor API Key.")
<   );
<   
<   $form['campaignmonitor_client_id'] = array(
<     '#type' => 'textfield',
<     '#title' => t('Client ID'),
<     '#default_value' => variable_get('campaignmonitor_client_id', ''),
<     '#required' => TRUE,
<     '#size' => 50,
<     '#maxlength' => 200,
<     '#description' => t("Your Campaign Monitor Client ID.")
<   );
<   
<   $form['campaignmonitor_list_id'] = array(
<     '#type' => 'textfield',
<     '#title' => t('List ID'),
<     '#default_value' => variable_get('campaignmonitor_list_id', ''),
<   '#required' => TRUE,
<     '#size' => 50,
<     '#maxlength' => 200,
<     '#description' => t("Your Campaign Monitor List ID.")
<   );
<   
<   $form['campaignmonitor_display_on'] = array(
<     '#type' => 'checkboxes',
<     '#title' => t('Display on Options'),
<     '#default_value' => variable_get('campaignmonitor_display_on', array()),
<     '#options' => array(
<       'contact' => t('Contact Page'),
<       'registration' => t('Registration Page')
<     ),
<     '#description' => t('Choose which forms you want to display the Join Newsletter checkbox.')
<   );
<   
<   $form['campaignmonitor_checkboxisplaytext'] = array(
<     '#type' => 'textfield',
<     '#title' => t('Display Text for Checkbox'),
<     '#default_value' => variable_get('campaignmonitor_checkboxdisplaytext', 'Join our Newsletter?'),
<     '#size' => 50,
<     '#maxlength' => 50,
<     '#description' => t("This text will display next to the checkbox on the selected forms.")
<   );
<   
<   $form['campaignmonitor_userpagedisplaytext'] = array(
<     '#type' => 'textfield',
<     '#title' => t('Display Text for User Page'),
<     '#default_value' => variable_get('campaignmonitor_userpagedisplaytext', 'Newsletter'),
<     '#size' => 50,
<     '#maxlength' => 50,
<     '#description' => t("This text will display next to the checkbox on the user profile page.")
<   );
<   
<   $form['campaignmonitor_pastcampaignurl'] = array(
<     '#type' => 'textfield',
<     '#title' => t('Past Campaign URL'),
<     '#default_value' => variable_get('campaignmonitor_pastcampaignurl', ''),
<     '#size' => 100,
<     '#maxlength' => 100,
<     '#description' => t("This is required if you want to use the page that displays past campaigns. You can find this value if you go to Manage Clients, click on the client, go to the link that tells you how to display past campaigns, then copy the URL ONLY from the html given. The URL is in between the src=\"\" value.")
<   );
<   
<   $form['campaignmonitor_connection_timeout'] = array(
<     '#type' => 'textfield',
<     '#title' => t('Connection timeout'),
<     '#default_value' => variable_get('campaignmonitor_connection_timeout', 15),
<     '#size' => 10,
<     '#maxlength' => 10,
<     '#description' => t("If your server can't get through to the API, or the API server is down, this is the amount of time until the connection times out in seconds. Default is 15 seconds.")
<   );
<   
<   return system_settings_form($form);
< }
< 
< function campaignmonitor_general_form() {
<   global $user;
< 	$name = '';
< 	$email = '';
< 	$default = false;
< 	
< 	// Check if the profile module is installed. If it is, use the name element of the profile
< 	if (module_exists("profile")) {
<   	profile_load_profile($user->uid);
< 		$name = $user->profile_name;
< 	}
<   if ($user->uid != 0) {
<     $email = $user->mail;
<     if (_campaignmonitor_is_subscribed(variable_get('campaignmonitor_api_key', ''), 
< 		  variable_get('campaignmonitor_list_id', '') , $email)) {
<       $default = true;
< 			// Also if subscribed get name
< 			$subscriber = _campaignmonitor_get_subscriber(variable_get('campaignmonitor_api_key', ''),
< 			  variable_get('campaignmonitor_list_id', ''), $email);
< 			$name = $subscriber['name'];
<     }
<     else {
<       $default = false;
<     }
<   
<   }
< 	
<   $form['name'] = array(
<     '#type'          => 'textfield',
<     '#title'         => t('Name'),
<     '#size'          => 20,
<     '#maxlength'     => 50,
<     '#required'      => TRUE,
< 		'#default_value' => $name,
<   );
<   $form['email'] = array(
<     '#type'          => 'textfield',
<     '#title'         => t('Email'),
<     '#size'          => 20,
<     '#maxlength'     => 100,
<     '#required'      => TRUE,
< 		'#default_value' => $email,
<   );
<   
<   $form['unsubscribe_newsletter'] = array(
<     '#type'          => 'checkbox',
<     '#title'         => t('Unsubscribe'),
<     '#default_value' => $default,
<   );
< 
<   $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
<   return $form;
< }
< 
< function campaignmonitor_general_form_submit($form, &$form_state) {
<   $form_values = $form_state['values'];
<   
<   // Replace api_key and list_id with your own details
<   $api_key = variable_get('campaignmonitor_api_key', '');
<   $list_id = variable_get('campaignmonitor_list_id', '');
<   
<   $name = $form_values['name'];
<   $email = $form_values['email'];
< 
<   // any cases other then these are when things are unchanged
<   if (!$form_values['unsubscribe_newsletter']) {
<     // this is the case where they now want to be subscribed, and weren't before
<     _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email);
<   }
<   else if ($form_values['unsubscribe_newsletter']) {
<     // this is the case where they don't want to be subscribed, and were before
<     _campaignmonitor_remove_subscriber($api_key, $list_id, $email);
<   }
< }
< 
< function campaignmonitor_newsletter_archive() {
<   $url = variable_get('campaignmonitor_pastcampaignurl', '');
<   if ($url == '') {
<     $content = '<p>The past campaign URL has not been set. Please set this in the administration pages.</p>';
<   }
<   else{
<     $content = '<script type="text/javascript" src="'. variable_get('campaignmonitor_pastcampaignurl', '') .'"></script>';
<   }
<   return $content;
< }
< 
< function campaignmonitor_user_page() {
<   return drupal_get_form('campaignmonitor_user_form');
< }
< 
< function campaignmonitor_user_form() {
<   global $user;
<   // Replace api_key and list_id with your own details
<   $api_key = variable_get('campaignmonitor_api_key', '');
<   $list_id = variable_get('campaignmonitor_list_id', '');
< 
<   $email = $user->mail;
< 
<   if (_campaignmonitor_is_subscribed($api_key, $list_id, $email, true)) {
<     $default = true;
<   }
<   else {
<     $default = false;
<   }
< 
<   $form['subscribe_newsletter'] = array(
<     '#type'          => 'checkbox',
<     '#title'         => variable_get('campaignmonitor_user_pagedisplaytext', 'Newsletter'),
<     '#default_value' => $default,
<   );
<   $form['is_subscribed'] = array(
<     '#type'          => 'hidden',
<     '#default_value' => $default,
<   );
<   $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
<   return $form;
< }
< 
< function campaignmonitor_user_form_submit($form, &$form_state)  {
<   $form_values = $form_state['values'];
<   global $user;
<   // Check if the profile module is installed. If it is, use the name element of the profile
< 	if (module_exists("profile")) {
<   	profile_load_profile($user->uid);
< 		$profile_name = $user->profile_name;
< 	}
<   // Replace api_key and list_id with your own details
<   $api_key = variable_get('campaignmonitor_api_key', '');
<   $list_id = variable_get('campaignmonitor_list_id', '');
<   
<   $name = $profile_name;
<   $email = $user->mail;
< 
<   // any cases other then these are when things are unchanged
<   if ($form_values['subscribe_newsletter'] && !$form_values['is_subscribed']) {
<     // this is the case where they now want to be subscribed, and weren't before
<     _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email);
<   }
<   else if (!$form_values['subscribe_newsletter'] && $form_values['is_subscribed']) {
<     // this is the case where they don't want to be subscribed, and were before
<     _campaignmonitor_remove_subscriber($api_key, $list_id, $email);
<   }
< }
< 
< function _campaignmonitor_form_submit($form, &$form_state) {
<   $form_values = $form_state['values'];
< 
<   if ($form_values['subscribe_newsletter']) {
<     if ('contact_mail_page' == $form_id) {
<       $form_values['message'] .= "\n\n" . t('Subscribed to newsletter.');
<     }
< 
<     // Replace api_key and list_id with your own details
<     $api_key = variable_get('campaignmonitor_api_key', '');
<     $list_id = variable_get('campaignmonitor_list_id', '');
<   
<     $email = $form_values['mail'];
<     $name = $form_values['name'];
< 
<  	_campaignmonitor_add_subscriber($api_key, $list_id, $name, $email);
<   }
< }
< 
< /* END MODULE FUNCTIONS */
< 
< /* TEMPLATE FUNCTIONS */
< 
< /**
<  * Block template preprocessor
<  */
< function template_preprocess_campaignmonitor_block_content(&$variables) {
<   $variables['form'] = drupal_get_form('campaignmonitor_general_form');
<   
<   if (user_access('access archive')) {
<     $variables['archive_link'] =  l('Newsletter Archive', 'newsletter_archive');
<   }
<   else {
<     $variables['archive_link'] = '';
<   }
< }
< 
< /* END TEMPLATE FUNCTIONS */
< 
< /* SOAP CALLS AND HELPERS */
< 
< function _campaignmonitor_create_client() {
<   $client = new SoapClient(null,
<     array("location" => "http://api.createsend.com/api/api.asmx",
<       "uri"  => "http://api.createsend.com/api/",
<       "style"  => SOAP_RPC,
<       "use"  => SOAP_LITERAL,
<       "trace"  => true,
< 	  "connection_timeout" => (int)variable_get('campaignmonitor_connection_timeout', '15'),
<     ));
< 	return $client;
< }
< 
< function _campaignmonitor_is_subscribed($api_key, $list_id, $email, $show_errors = false) {
<   $retval = false;
<   $client = _campaignmonitor_create_client();
<   
<   try{
<     //make soap call
<     $result = $client->__soapCall("Subscribers.GetIsSubscribed",
<         array(new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
<           new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
<           new SoapVar($email, XSD_STRING, null, null, "Email", "http://api.createsend.com/api/"),
<           new SoapVar($name, XSD_STRING, null, null, "Name", "http://api.createsend.com/api/"),
<         ),
<         array("soapaction" => "http://api.createsend.com/api/Subscribers.GetIsSubscribed")
<     );
< 	if ($result->Code != 0) {
<       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
< 	  			array('%code' => $result->Code, '%message' => $result->Message));
< 	  if ($show_errors) {
< 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
< 	  }
<     }
<     else if ($result == 'False') {
<       $retval = false;
<     }
<     else if ($result == 'True') {
<       $retval = true;
<     }
<   } catch (SoapFault $e) {
<   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
<     if ($show_errors) {
<       drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
<     }
<     $retval = false;
<   }
<   return $retval;
< }
< 
< function _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email, $show_errors = false) {
<   $client = $client = _campaignmonitor_create_client();
< 
<   try{
<     //make soap call
<     $result = $client->__soapCall("Subscriber.AddAndResubscribe",
<       array(new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
<         new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
<         new SoapVar($email, XSD_STRING, null, null, "Email", "http://api.createsend.com/api/"),
<         new SoapVar($name, XSD_STRING, null, null, "Name", "http://api.createsend.com/api/"),
<       ),
<       array("soapaction"  => "http://api.createsend.com/api/Subscriber.AddAndResubscribe")
<     );
< 	
< 	if ($result->Code != 0) {
<       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
< 	  			array('%code' => $result->Code, '%message' => $result->Message));
< 	  if ($show_errors) {
< 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
< 	  }
<     }
<     else {
<       drupal_set_message("You have successfully been added.", 'status');
<     }
<   } catch (SoapFault $e) {
<   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
<     if ($show_errors) {
<       drupal_set_message("There is an error with the newsletter server. Please try again later.", 'error');
<     }
<   }
< }
< 
< function _campaignmonitor_remove_subscriber($api_key, $list_id, $email, $show_errors = false) {
<   $client = $client = _campaignmonitor_create_client();
< 
<   try{
<     //make soap call
<     $result = $client->__soapCall("Subscriber.Unsubscribe",
<       array(
<         new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
<         new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
<         new SoapVar($email, XSD_STRING, null, null, "Email", "http://api.createsend.com/api/"),
<         new SoapVar($name, XSD_STRING, null, null, "Name", "http://api.createsend.com/api/"),
<       ),
<       array("soapaction" => "http://api.createsend.com/api/Subscriber.Unsubscribe")
<     );
< 	
< 	if ($result->Code != 0) {
<       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
< 	  			array('%code' => $result->Code, '%message' => $result->Message));
< 	  if ($show_errors) {
< 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
< 	  }
<     }
<     else {
<       drupal_set_message("You have successfully been unsubscribed.", 'status');
<     }
<   } catch (SoapFault $e) {
<   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
<     if ($show_errors) {
<       drupal_set_message("There is an error with the newsletter server. Please try again later.", 'error');
<     }
<   }
< }
< 
< function _campaignmonitor_get_subscriber($api_key, $list_id, $email, $show_errors = false) {
<   $retval = array("name" => '', "email" => $email);
<   $client = $client = _campaignmonitor_create_client();
<   
<   try{
<     //make soap call
<     $result = $client->__soapCall("Subscribers.GetSingleSubscriber",
<         array(new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
<           new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
<           new SoapVar($email, XSD_STRING, null, null, "EmailAddress", "http://api.createsend.com/api/"),
<         ),
<         array("soapaction" => "http://api.createsend.com/api/Subscribers.GetSingleSubscriber")
<     );
<     if ($result->Code != 0) {
<       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
< 	  			array('%code' => $result->Code, '%message' => $result->Message));
< 	  if ($show_errors) {
< 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
< 	  }
<     }
<     else {
<       $retval['name'] = $result->Name;
<     }
<   } catch (SoapFault $e) {
<   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
<     if ($show_errors) {
<       drupal_set_message("There is an error with the newsletter server. Please try again later.", 'error');
<     }
<   }
<   return $retval;
< }
< /* END SOAP CALLS AND HELPERS */
\ No newline at end of file
---
> <?php
> /* $Id: campaignmonitor.module,v 1.7.2.3 2008/12/20 05:26:38 ssherriff Exp $ */
> 
> /* DRUPAL HOOKS */
> /* hook_help */
> function campaignmonitor_help($path, $arg) {
>   switch ($path) {
>     case 'admin/settings/campaignmonitor':
>       return '<p>' . t('Use your API key and other keys to have users register for a mailing list setup through Campaign Monitor.') . '</p>';
>   }
> }
> 
> 
> /* hook_perm */
> function campaignmonitor_perm() {
>   return array('administer campaignmonitor', 'access archive', 'join newsletter');
> }
> 
> /* hook_menu */
> function campaignmonitor_menu() {
>   $items = array();
>   
>   $items['admin/settings/campaignmonitor'] = array(
>     'title' => t('Campaign Monitor'),
>     'description' => t('Setup Campaign Monitor values.'),
>     'page callback' => 'drupal_get_form',
>     'page arguments' => array('campaignmonitor_admin'),
>     'access arguments' => array('administer campaignmonitor'),
>     'type' => MENU_NORMAL_ITEM);
>   $items['newsletter_archive'] = array(
>     'title' => t('Newsletter Archive'),
>     'page callback' => 'campaignmonitor_newsletter_archive',
>     'access arguments' => array('access archive'),
>     'type' => MENU_SUGGESTED_ITEM);
> 	  
>   $items['user/%user/newsletters'] = array(
>     'title' => t('My Newsletters'),
>     'page callback' => 'campaignmonitor_user_page',
> 	'access arguments' => array('join newsletter'),
>     'type' => MENU_LOCAL_TASK,
>     'weight' => 5,
>   );
> 	
> 	$items['campaignmonitor/js'] = array(
> 		'title' => 'Javascript Lists',
> 		'page callback' => 'campaignmonitor_lists_js',
> 		'access arguments' => array('access content'),
> 		'type' => MENU_CALLBACK,
> 	);
> 	return $items;
> }
> 
> /* hook_form_alter */
> function campaignmonitor_form_alter(&$form, $form_state, $form_id) {
>   $display_on = variable_get('campaignmonitor_display_on', array());
>   
>   if (('contact_mail_page' == $form_id && $display_on['contact'] == 'contact') || ('user_register' == $form_id && $display_on['registration'] == 'registration') && user_access('join newsletter')) {
>     $form['subscribe_newsletter'] = array(
>       '#type'          => 'checkbox',
>       '#title'         => t(variable_get('campaignmonitor_checkboxdisplaytext', 'Join our Newsletter?')),
>       '#weight'        => 99,
>       '#default_value' => 1,
>     );
>   
>     $form['submit']['#weight'] = 100;
>     
>     $form['#submit'] = array('_campaignmonitor_form_submit' => array()) + (array)$form['#submit'];
>   }
> }
> 
> /**
>  * Implementation of hook_block()
>  */
> function campaignmonitor_block($op = 'list', $delta = 0, $edit = array()) {
>   switch ($op) {
>     case 'list':
>       $blocks[0] = array('info' => t('Join newsletter'));
>       return $blocks;
>     
>     case 'configure':
>     case 'save':
>       // no additional configuration options
>       break;
>     
>     case 'view':
>     default:
>       $block['subject'] = t('Join Newsletter');
>       $block['content'] = theme('campaignmonitor_block_content', $delta);
>       return $block;
>   }
> }
> 
> /**
>  * Implementation of hook_theme()
>  */
> function campaignmonitor_theme() {
>   return array(
>     'campaignmonitor_block_content' => array(
>       'template' => 'campaignmonitor-block-content',
>     ),
>   );
> }
> 
> /* END DRUPAL HOOKS */
> 
> /* MODULE FUNCTIONS */
> 
> function campaignmonitor_admin() {
> 
>   $form['campaignmonitor_api_key'] = array(
>     '#type' => 'textfield',
>     '#title' => t('API Key'),
>     '#default_value' => variable_get('campaignmonitor_api_key', ''),
>     '#required' => TRUE,
>     '#size' => 50,
>     '#maxlength' => 200,
>     '#description' => t("Your Campaign Monitor API Key.")
>   );
>   
>   $form['campaignmonitor_client_id'] = array(
>     '#type' => 'textfield',
>     '#title' => t('Client ID'),
>     '#default_value' => variable_get('campaignmonitor_client_id', ''),
>     '#required' => TRUE,
>     '#size' => 50,
>     '#maxlength' => 200,
>     '#description' => t("Your Campaign Monitor Client ID.")
>   );
> 	
> 	// Add a wrapper for the lists and selection radio buttons.
> 	$form['campaignmonitor_lists_wrapper'] = array(
> 		'#tree' => FALSE,
> 		'#prefix' => '<div class="clear-block" id="campaignmonitor-lists-wrapper">',
> 		'#suffix' => '</div>',
> 	);
> 	$form['campaignmonitor_lists_wrapper']['fetch_lists'] = array(
> 		'#title' => t('Choose which lists you wish to make available'),
> 		'#type' => 'radios',
> 		'#options' => array(
> 			'all' => 'All lists for this client',
> 			'some' => 'I want to select specific lists for this client',
> 		),
> 		'#value' => t('Fetch all list for this client'),
> 		'#button_type' => 'button',
> 		'#description' => t('Please enter Client ID and select which lists you wish to make available.'),
> 		'#submit' => array('_campaignmonitor_fetch_lists_submit'), // if no JS
> 		'#ahah' => array(
> 			'path' => 'campaignmonitor/js',
> 			'wrapper' => 'campaignmonitor-lists',
> 			'method' => 'replace',
> 			'effect' => 'fade',
> 		),
> 	);
> 	// Container for the list of lists
> 	$form['campaignmonitor_lists_wrapper']['lists'] = array(
> 		'#type' => 'checkboxes',
> 		'#prefix' => '<div id="campaignmonitor-lists">',
> 		'#value' => NULL,
> 		'#options' => array(),
> 		'#suffix' => '</div>',
> 	);
> 
>   /*$form['campaignmonitor_list_id'] = array(
>     '#type' => 'textfield',
>     '#title' => t('List ID'),
>     '#default_value' => variable_get('campaignmonitor_list_id', ''),
>   '#required' => TRUE,
>     '#size' => 50,
>     '#maxlength' => 200,
>     '#description' => t("Your Campaign Monitor List ID.")
>   );*/
>   
>   $form['campaignmonitor_display_on'] = array(
>     '#type' => 'checkboxes',
>     '#title' => t('Display on Options'),
>     '#default_value' => variable_get('campaignmonitor_display_on', array()),
>     '#options' => array(
>       'contact' => t('Contact Page'),
>       'registration' => t('Registration Page')
>     ),
>     '#description' => t('Choose which forms you want to display the Join Newsletter checkbox.')
>   );
>   
>   $form['campaignmonitor_checkboxisplaytext'] = array(
>     '#type' => 'textfield',
>     '#title' => t('Display Text for Checkbox'),
>     '#default_value' => variable_get('campaignmonitor_checkboxdisplaytext', 'Join our Newsletter?'),
>     '#size' => 50,
>     '#maxlength' => 50,
>     '#description' => t("This text will display next to the checkbox on the selected forms.")
>   );
>   
>   $form['campaignmonitor_userpagedisplaytext'] = array(
>     '#type' => 'textfield',
>     '#title' => t('Display Text for User Page'),
>     '#default_value' => variable_get('campaignmonitor_userpagedisplaytext', 'Newsletter'),
>     '#size' => 50,
>     '#maxlength' => 50,
>     '#description' => t("This text will display next to the checkbox on the user profile page.")
>   );
>   
>   $form['campaignmonitor_pastcampaignurl'] = array(
>     '#type' => 'textfield',
>     '#title' => t('Past Campaign URL'),
>     '#default_value' => variable_get('campaignmonitor_pastcampaignurl', ''),
>     '#size' => 100,
>     '#maxlength' => 100,
>     '#description' => t("This is required if you want to use the page that displays past campaigns. You can find this value if you go to Manage Clients, click on the client, go to the link that tells you how to display past campaigns, then copy the URL ONLY from the html given. The URL is in between the src=\"\" value.")
>   );
>   
>   $form['campaignmonitor_connection_timeout'] = array(
>     '#type' => 'textfield',
>     '#title' => t('Connection timeout'),
>     '#default_value' => variable_get('campaignmonitor_connection_timeout', 15),
>     '#size' => 10,
>     '#maxlength' => 10,
>     '#description' => t("If your server can't get through to the API, or the API server is down, this is the amount of time until the connection times out in seconds. Default is 15 seconds.")
>   );
>   
>   return system_settings_form($form);
> }
> 
> function campaignmonitor_general_form() {
>   global $user;
> 	$name = '';
> 	$email = '';
> 	$default = false;
> 	
> 	// Check if the profile module is installed. If it is, use the name element of the profile
> 	if (module_exists("profile")) {
>   	profile_load_profile($user->uid);
> 		$name = $user->profile_name;
> 	}
>   if ($user->uid != 0) {
>     $email = $user->mail;
>     if (_campaignmonitor_is_subscribed(variable_get('campaignmonitor_api_key', ''), 
> 		  variable_get('campaignmonitor_list_id', '') , $email)) {
>       $default = true;
> 			// Also if subscribed get name
> 			$subscriber = _campaignmonitor_get_subscriber(variable_get('campaignmonitor_api_key', ''),
> 			  variable_get('campaignmonitor_list_id', ''), $email);
> 			$name = $subscriber['name'];
>     }
>     else {
>       $default = false;
>     }
>   
>   }
> 	
>   $form['name'] = array(
>     '#type'          => 'textfield',
>     '#title'         => t('Name'),
>     '#size'          => 20,
>     '#maxlength'     => 50,
>     '#required'      => TRUE,
> 		'#default_value' => $name,
>   );
>   $form['email'] = array(
>     '#type'          => 'textfield',
>     '#title'         => t('Email'),
>     '#size'          => 20,
>     '#maxlength'     => 100,
>     '#required'      => TRUE,
> 		'#default_value' => $email,
>   );
>   
>   $form['unsubscribe_newsletter'] = array(
>     '#type'          => 'checkbox',
>     '#title'         => t('Unsubscribe'),
>     '#default_value' => $default,
>   );
> 
>   $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
>   return $form;
> }
> 
> function campaignmonitor_general_form_submit($form, &$form_state) {
>   $form_values = $form_state['values'];
>   
>   // Replace api_key and list_id with your own details
>   $api_key = variable_get('campaignmonitor_api_key', '');
>   $list_id = variable_get('campaignmonitor_list_id', '');
>   
>   $name = $form_values['name'];
>   $email = $form_values['email'];
> 
>   // any cases other then these are when things are unchanged
>   if (!$form_values['unsubscribe_newsletter']) {
>     // this is the case where they now want to be subscribed, and weren't before
>     _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email);
>   }
>   else if ($form_values['unsubscribe_newsletter']) {
>     // this is the case where they don't want to be subscribed, and were before
>     _campaignmonitor_remove_subscriber($api_key, $list_id, $email);
>   }
> }
> 
> function campaignmonitor_newsletter_archive() {
>   $url = variable_get('campaignmonitor_pastcampaignurl', '');
>   if ($url == '') {
>     $content = '<p>The past campaign URL has not been set. Please set this in the administration pages.</p>';
>   }
>   else{
>     $content = '<script type="text/javascript" src="'. variable_get('campaignmonitor_pastcampaignurl', '') .'"></script>';
>   }
>   return $content;
> }
> 
> function campaignmonitor_user_page() {
>   return drupal_get_form('campaignmonitor_user_form');
> }
> 
> function campaignmonitor_user_form() {
>   global $user;
>   // Replace api_key and list_id with your own details
>   $api_key = variable_get('campaignmonitor_api_key', '');
>   $list_id = variable_get('campaignmonitor_list_id', '');
> 
>   $email = $user->mail;
> 
>   if (_campaignmonitor_is_subscribed($api_key, $list_id, $email, true)) {
>     $default = true;
>   }
>   else {
>     $default = false;
>   }
> 
>   $form['subscribe_newsletter'] = array(
>     '#type'          => 'checkbox',
>     '#title'         => variable_get('campaignmonitor_user_pagedisplaytext', 'Newsletter'),
>     '#default_value' => $default,
>   );
>   $form['is_subscribed'] = array(
>     '#type'          => 'hidden',
>     '#default_value' => $default,
>   );
>   $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
>   return $form;
> }
> 
> function campaignmonitor_user_form_submit($form, &$form_state)  {
>   $form_values = $form_state['values'];
>   global $user;
>   // Check if the profile module is installed. If it is, use the name element of the profile
> 	if (module_exists("profile")) {
>   	profile_load_profile($user->uid);
> 		$profile_name = $user->profile_name;
> 	}
>   // Replace api_key and list_id with your own details
>   $api_key = variable_get('campaignmonitor_api_key', '');
>   $list_id = variable_get('campaignmonitor_list_id', '');
>   
>   $name = $profile_name;
>   $email = $user->mail;
> 
>   // any cases other then these are when things are unchanged
>   if ($form_values['subscribe_newsletter'] && !$form_values['is_subscribed']) {
>     // this is the case where they now want to be subscribed, and weren't before
>     _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email);
>   }
>   else if (!$form_values['subscribe_newsletter'] && $form_values['is_subscribed']) {
>     // this is the case where they don't want to be subscribed, and were before
>     _campaignmonitor_remove_subscriber($api_key, $list_id, $email);
>   }
> }
> 
> function _campaignmonitor_form_submit($form, &$form_state) {
>   $form_values = $form_state['values'];
> 
>   if ($form_values['subscribe_newsletter']) {
>     if ('contact_mail_page' == $form_id) {
>       $form_values['message'] .= "\n\n" . t('Subscribed to newsletter.');
>     }
> 
>     // Replace api_key and list_id with your own details
>     $api_key = variable_get('campaignmonitor_api_key', '');
>     $list_id = variable_get('campaignmonitor_list_id', '');
>   
>     $email = $form_values['mail'];
>     $name = $form_values['name'];
> 
>  	_campaignmonitor_add_subscriber($api_key, $list_id, $name, $email);
>   }
> }
> 
> /* END MODULE FUNCTIONS */
> 
> /* TEMPLATE FUNCTIONS */
> 
> /**
>  * Block template preprocessor
>  */
> function template_preprocess_campaignmonitor_block_content(&$variables) {
>   $variables['form'] = drupal_get_form('campaignmonitor_general_form');
>   
>   if (user_access('access archive')) {
>     $variables['archive_link'] =  l('Newsletter Archive', 'newsletter_archive');
>   }
>   else {
>     $variables['archive_link'] = '';
>   }
> }
> 
> /* END TEMPLATE FUNCTIONS */
> 
> /* SOAP CALLS AND HELPERS */
> 
> function _campaignmonitor_create_client() {
>   $client = new SoapClient(null,
>     array("location" => "http://api.createsend.com/api/api.asmx",
>       "uri"  => "http://api.createsend.com/api/",
>       "style"  => SOAP_RPC,
>       "use"  => SOAP_LITERAL,
>       "trace"  => true,
> 	  "connection_timeout" => (int)variable_get('campaignmonitor_connection_timeout', '15'),
>     ));
> 	return $client;
> }
> 
> function _campaignmonitor_is_subscribed($api_key, $list_id, $email, $show_errors = false) {
>   $retval = false;
>   $client = _campaignmonitor_create_client();
>   
>   try{
>     //make soap call
>     $result = $client->__soapCall("Subscribers.GetIsSubscribed",
>         array(new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
>           new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
>           new SoapVar($email, XSD_STRING, null, null, "Email", "http://api.createsend.com/api/"),
>           new SoapVar($name, XSD_STRING, null, null, "Name", "http://api.createsend.com/api/"),
>         ),
>         array("soapaction" => "http://api.createsend.com/api/Subscribers.GetIsSubscribed")
>     );
> 	if ($result->Code != 0) {
>       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
> 	  			array('%code' => $result->Code, '%message' => $result->Message));
> 	  if ($show_errors) {
> 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
> 	  }
>     }
>     else if ($result == 'False') {
>       $retval = false;
>     }
>     else if ($result == 'True') {
>       $retval = true;
>     }
>   } catch (SoapFault $e) {
>   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
>     if ($show_errors) {
>       drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
>     }
>     $retval = false;
>   }
>   return $retval;
> }
> 
> function _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email, $show_errors = false) {
>   $client = $client = _campaignmonitor_create_client();
> 
>   try{
>     //make soap call
>     $result = $client->__soapCall("Subscriber.AddAndResubscribe",
>       array(new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
>         new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
>         new SoapVar($email, XSD_STRING, null, null, "Email", "http://api.createsend.com/api/"),
>         new SoapVar($name, XSD_STRING, null, null, "Name", "http://api.createsend.com/api/"),
>       ),
>       array("soapaction"  => "http://api.createsend.com/api/Subscriber.AddAndResubscribe")
>     );
> 	
> 	if ($result->Code != 0) {
>       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
> 	  			array('%code' => $result->Code, '%message' => $result->Message));
> 	  if ($show_errors) {
> 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
> 	  }
>     }
>     else {
>       drupal_set_message("You have successfully been added.", 'status');
>     }
>   } catch (SoapFault $e) {
>   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
>     if ($show_errors) {
>       drupal_set_message("There is an error with the newsletter server. Please try again later.", 'error');
>     }
>   }
> }
> 
> function _campaignmonitor_remove_subscriber($api_key, $list_id, $email, $show_errors = false) {
>   $client = $client = _campaignmonitor_create_client();
> 
>   try{
>     //make soap call
>     $result = $client->__soapCall("Subscriber.Unsubscribe",
>       array(
>         new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
>         new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
>         new SoapVar($email, XSD_STRING, null, null, "Email", "http://api.createsend.com/api/"),
>         new SoapVar($name, XSD_STRING, null, null, "Name", "http://api.createsend.com/api/"),
>       ),
>       array("soapaction" => "http://api.createsend.com/api/Subscriber.Unsubscribe")
>     );
> 	
> 	if ($result->Code != 0) {
>       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
> 	  			array('%code' => $result->Code, '%message' => $result->Message));
> 	  if ($show_errors) {
> 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
> 	  }
>     }
>     else {
>       drupal_set_message("You have successfully been unsubscribed.", 'status');
>     }
>   } catch (SoapFault $e) {
>   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
>     if ($show_errors) {
>       drupal_set_message("There is an error with the newsletter server. Please try again later.", 'error');
>     }
>   }
> }
> 
> function _campaignmonitor_get_subscriber($api_key, $list_id, $email, $show_errors = false) {
>   $retval = array("name" => '', "email" => $email);
>   $client = $client = _campaignmonitor_create_client();
>   
>   try{
>     //make soap call
>     $result = $client->__soapCall("Subscribers.GetSingleSubscriber",
>         array(new SoapVar($api_key, XSD_STRING, null, null, "ApiKey", "http://api.createsend.com/api/"),
>           new SoapVar($list_id, XSD_STRING, null, null, "ListID", "http://api.createsend.com/api/"),
>           new SoapVar($email, XSD_STRING, null, null, "EmailAddress", "http://api.createsend.com/api/"),
>         ),
>         array("soapaction" => "http://api.createsend.com/api/Subscribers.GetSingleSubscriber")
>     );
>     if ($result->Code != 0) {
>       watchdog('campaignmonitor', 'Code - %code, Message - %message', 
> 	  			array('%code' => $result->Code, '%message' => $result->Message));
> 	  if ($show_errors) {
> 	    drupal_set_message('There is an error with the newsletter server. Please try again later.', 'error');
> 	  }
>     }
>     else {
>       $retval['name'] = $result->Name;
>     }
>   } catch (SoapFault $e) {
>   	watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR);
>     if ($show_errors) {
>       drupal_set_message("There is an error with the newsletter server. Please try again later.", 'error');
>     }
>   }
>   return $retval;
> }
> 
> function _campaignmonitor_get_lists($api_key, $client_id) {
> 	// @todo: dummy function which returns lists
> 	return array(
> 		'listid1' => t('List 1'),
> 		'listid2' => t('List 2'),
> 		'listid3' => t('List 3'),
> 	);
> }
> 
> function _campaignmonitor_fetch_lists_submit($form, &$form_state) {
> 	// @todo:
> }
> 
> /* END SOAP CALLS AND HELPERS */
> 
> 
> function campaignmonitor_lists_js() {
>   $form_state = array(
> 		'storage' => NULL,
> 		'submitted' => FALSE,
> 	);
> 	$form_build_id = $_POST['form_build_id'];
> 	// Get the form from the cache.
> 	$form = form_get_cache($form_build_id, $form_state);
> 	$args = $form['#parameters'];
> 	$form_id = array_shift($args);
> 	// We will run some of the submit handlers so we need to disable redirecting.
> 	$form['#redirect'] = FALSE;
> 	// We need to process the form, prepare for that by setting a few internal variables.
> 	$form['#post'] = $_POST;
> 	$form['#programmed'] = FALSE;
> 	$form_state['post'] = $_POST;
> 	// Build, validate and if possible, submit the form.
> 	drupal_process_form($form_id, $form, $form_state);
> 	// This call recreates the form relying solely on the form_state that the
> 	// drupal_process_form set up.
> 	$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
> 	// Render the new output.
> 	$lists_form = $form['campaignmonitor_lists_wrapper']['lists'];
>   unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent duplicate wrappers.
> 	
> 	$lists_type = $form_state['post']['fetch_lists'];
> 	switch ($lists_type) {
> 		case 'all':
> 			$output = theme('status_messages') . t('All the lists from this client will be available to the users.');
> 			break;
> 		
> 		case 'some':
> 			// Make sure the API key and Client ID are filled in.
> 			if (empty($form_state['post']['campaignmonitor_client_id']) || empty($form_state['post']['campaignmonitor_api_key'])) {
> 				form_set_error('fetch_lists', t('Please enter the API key and client ID first.'));
> 				$output = theme('status_messages');
> 			}
> 			else {
> 				$lists = _campaignmonitor_get_lists($form_state['post']['campaignmonitor_client_id'], $form_state['post']['campaignmonitor_api_key']);
> 				$output = theme('status_messages') . '<div class="form-item"><div class="form-checkboxes">';
> 				$i = 0;
> 				foreach ($lists as $key => $value) {
> 					$output .= '<div id="edit-lists-' . $i . '-wrapper" class="form-item">'
> 											. '<label class="option"><input type="checkbox" name="lists[' . $i . ']" value="'.$key.'" /></label> ' . $value
> 										. '</div>';
> 					$i++;
> 				}
> 				$output .= '</div></div>';
> 			}
> 			break;
> 	}
> 	
> 	drupal_json(array('status' => TRUE, 'data' => $output));
> }
\ No newline at end of file
