Index: campaignmonitor.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/campaignmonitor/campaignmonitor.module,v retrieving revision 1.7.2.3 diff -u -p -r1.7.2.3 campaignmonitor.module --- campaignmonitor.module 20 Dec 2008 05:26:38 -0000 1.7.2.3 +++ campaignmonitor.module 16 Jan 2009 21:21:10 -0000 @@ -19,7 +19,7 @@ function campaignmonitor_perm() { /* hook_menu */ function campaignmonitor_menu() { $items = array(); - + $items['admin/settings/campaignmonitor'] = array( 'title' => t('Campaign Monitor'), 'description' => t('Setup Campaign Monitor values.'), @@ -32,11 +32,11 @@ function campaignmonitor_menu() { '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'), + 'access arguments' => array('join newsletter'), 'type' => MENU_LOCAL_TASK, 'weight' => 5, ); @@ -46,17 +46,17 @@ function campaignmonitor_menu() { /* 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, + '#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']; } } @@ -69,12 +69,12 @@ function campaignmonitor_block($op = 'li 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'); @@ -99,8 +99,14 @@ function campaignmonitor_theme() { /* MODULE FUNCTIONS */ function campaignmonitor_admin() { - - $form['campaignmonitor_api_key'] = array( + // enclosing fieldset + $form['campaignmonitor'] = array( + '#type' => 'fieldset', + '#title' => t('API settings'), + '#collapsable' => true, + ); + // API key + $form['campaignmonitor']['campaignmonitor_api_key'] = array( '#type' => 'textfield', '#title' => t('API Key'), '#default_value' => variable_get('campaignmonitor_api_key', ''), @@ -109,8 +115,8 @@ function campaignmonitor_admin() { '#maxlength' => 200, '#description' => t("Your Campaign Monitor API Key.") ); - - $form['campaignmonitor_client_id'] = array( + + $form['campaignmonitor']['campaignmonitor_client_id'] = array( '#type' => 'textfield', '#title' => t('Client ID'), '#default_value' => variable_get('campaignmonitor_client_id', ''), @@ -119,18 +125,35 @@ function campaignmonitor_admin() { '#maxlength' => 200, '#description' => t("Your Campaign Monitor Client ID.") ); - - $form['campaignmonitor_list_id'] = array( + + $form['campaignmonitor']['campaignmonitor_lists'] = array( + '#type' => 'textarea', + '#title' => t('Campaign Monitor lists'), + '#default_value' => variable_get('campaignmonitor_lists', ''), + '#required' => TRUE, + '#cols' => 8, + '#description' => t("Your Campaign Monitor lists and IDs. Enter the campaigns in the format: LISTID|LIST_NAME with a return after each list. This might look like: 12345612312asdf|My newsletter|The description of my newsletter. You can find the API Subscriber List ID under 'edit list name/type' on the Subscriber List page under Manage Subscribers. It's the link next to the list name at the top. Please note, you *must* use plain text format for these values. WSIWYG will get you into trouble.") + ); + + // set timeout value + $form['campaignmonitor']['campaignmonitor_connection_timeout'] = 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.") + '#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.") ); - - $form['campaignmonitor_display_on'] = array( + + // enclosing fieldset + $form['campaignmonitor_display'] = array( + '#type' => 'fieldset', + '#title' => t('Display settings'), + '#collapsable' => true, + ); + + // where should the registration appear? + $form['campaignmonitor_display']['campaignmonitor_display_on'] = array( '#type' => 'checkboxes', '#title' => t('Display on Options'), '#default_value' => variable_get('campaignmonitor_display_on', array()), @@ -140,26 +163,8 @@ function campaignmonitor_admin() { ), '#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( + + $form['campaignmonitor_display']['campaignmonitor_pastcampaignurl'] = array( '#type' => 'textfield', '#title' => t('Past Campaign URL'), '#default_value' => variable_get('campaignmonitor_pastcampaignurl', ''), @@ -167,94 +172,180 @@ function campaignmonitor_admin() { '#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.") + + // display the name field? + $form['campaignmonitor_display']['campaignmonitor_name'] = array( + '#title' => t('Display name field'), + '#type' => 'checkbox', + '#default_value' => variable_get('campaignmonitor_name', null), + '#description' => t('You can request user names when they subscribe.'), + ); + + // display the unsubscribe checbox? + $form['campaignmonitor_display']['campaignmonitor_unsubscribe'] = array( + '#title' => t('Display unsubscribe'), + '#type' => 'checkbox', + '#default_value' => variable_get('campaignmonitor_unsubscribe', null), + '#description' => t('You can add an unsubscribe option to the form.'), + ); + + // display the archive link? + $form['campaignmonitor_display']['campaignmonitor_archive'] = array( + '#title' => t('Display archive link'), + '#type' => 'checkbox', + '#default_value' => variable_get('campaignmonitor_archive', null), + '#description' => t('Do you want to display the link to the archives?.'), ); - return system_settings_form($form); } + +/** + * Builds the generic form for letting users subscribe + * @return unknown_type + */ 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; - } + $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)) { + 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']; + // 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, - ); + + // should we request names for users? + if (variable_get('campaignmonitor_name', null)) { + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Name'), + '#size' => 20, + '#maxlength' => 50, + '#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, + '#type' => 'textfield', + '#title' => t('Email'), + '#size' => 20, + '#maxlength' => 100, + '#required' => TRUE, + '#default_value' => $email, ); + // get all the lists + $lists = campaignmonitor_parse_lists(); + // get unsubscribe display status + $unsubscribe = variable_get('campaignmonitor_unsubscribe', null); + + // if we only have one list, hide the list id in a hidden field + if (! count($lists) > 1) { + $form['list_0'] = array( + '#type' => 'value', + '#value' => $lists[0]['list_id'], + ); + // should we allow users to unsubscribe? + if ($unsubscribe) { + $form['list_0_unsub'] = array( + '#type' => 'checkbox', + '#title' => t('Unsubscribe'), + '#value' => $lists[0]['list_id'], + ); + } + } + else { + $options = array('subscribe' => t('Subscribe')); + // display the unsubscribe options + if ($unsubscribe) { + $options['unsubscribe'] = t('Unsubscribe'); + $type = "radios"; + } + else { + $type = "checkbox"; + } + + foreach ($lists as $id => $list) { + // build the form element + $form['list_'. $list['list_id']] = array( + '#type' => $type, + '#title' => t($list['list_name']), + '#options' => $options, + '#description' => $list['list_description'], + ); + } + } $form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); return $form; } + +/** + * Processes submit data from the general form + * @param $form + * @param $form_state + * @return unknown_type + */ 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); + $actions = array(); + // find all the lists that the user has taken action on + foreach ($form_values as $key => $value) { + // pull the list id out of the form element + preg_match('/list_(.*)/', $key, $matches); + if ($list_id = $matches[1]) { + // add the list id to a set of actions + $actions[$value][] = $list_id; + } } - 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); + + // we need to handle the single list case + if (! $form_values['list_0_unsub'] && $form_values['list_0']) { + $actions['subscribe'][] = $form_values['list_0']; + } + elseif ($form_values['list_0_unsub']) { + $actions['unsubscribe'][] = $form_values['list_0_unsub']; + } + + // We have all of our actions sorted into an array, execute them + foreach ($actions as $action => $lists) { + foreach($lists as $list_id) { + if ($action == 'subscribe') { + // this is the case where they now want to be subscribed, and were not before + _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email); + } + else { + // 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 == '') { @@ -302,14 +393,14 @@ function campaignmonitor_user_form_submi $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; - } + 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; @@ -324,6 +415,15 @@ function campaignmonitor_user_form_submi } } + +/** + * Submit subscribe/request form + * @param $form + * drupal form array + * @param $form_state + * drupal form state + * @return unknown_type + */ function _campaignmonitor_form_submit($form, &$form_state) { $form_values = $form_state['values']; @@ -335,11 +435,35 @@ function _campaignmonitor_form_submit($f // 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); + _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email); + } +} + + +/** + * Builds a list of the current lists enabled + * @return array + * array of lists + */ +function campaignmonitor_parse_lists() { + if ($lists = variable_get('campaignmonitor_lists', null)) { + $return_lists = array(); + // put the lists into an array + $lists = explode("\n", $lists); + // now break out data + foreach ($lists as $list) { + $list = explode('|', $list); + $return_lists[] = array( + 'list_id' => $list[0], + 'list_name' => $list[1], + 'list_descritpion' => $list[2], + ); + } + return $return_lists; } } @@ -352,7 +476,7 @@ function _campaignmonitor_form_submit($f */ 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'); } @@ -372,15 +496,15 @@ function _campaignmonitor_create_client( "style" => SOAP_RPC, "use" => SOAP_LITERAL, "trace" => true, - "connection_timeout" => (int)variable_get('campaignmonitor_connection_timeout', '15'), + "connection_timeout" => (int)variable_get('campaignmonitor_connection_timeout', '15'), )); - return $client; + 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", @@ -391,12 +515,12 @@ function _campaignmonitor_is_subscribed( ), 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'); - } + 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; @@ -405,7 +529,7 @@ function _campaignmonitor_is_subscribed( $retval = true; } } catch (SoapFault $e) { - watchdog('campaignmonitor', 'There was an error checking if joined to newsletter: %fault', array('%fault' => $e->faultstring), WATCHDOG_ERROR); + 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'); } @@ -414,7 +538,7 @@ function _campaignmonitor_is_subscribed( return $retval; } -function _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email, $show_errors = false) { +function _campaignmonitor_add_subscriber($api_key, $list_id, $name = '' , $email, $show_errors = false) { $client = $client = _campaignmonitor_create_client(); try{ @@ -427,19 +551,19 @@ function _campaignmonitor_add_subscriber ), 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'); - } + + 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); + 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'); } @@ -460,19 +584,19 @@ function _campaignmonitor_remove_subscri ), 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'); - } + + 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); + 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'); } @@ -482,7 +606,7 @@ function _campaignmonitor_remove_subscri 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", @@ -493,17 +617,17 @@ function _campaignmonitor_get_subscriber 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'); - } + 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); + 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'); }