You are here

function campaignmonitor_user_form_submit in Campaign Monitor 7

Same name and namespace in other branches
  1. 5.2 campaignmonitor.module \campaignmonitor_user_form_submit()
  2. 5 campaignmonitor.module \campaignmonitor_user_form_submit()
  3. 6 campaignmonitor.module \campaignmonitor_user_form_submit()
  4. 6.2 campaignmonitor.module \campaignmonitor_user_form_submit()

Submission form handler.

The information about the selected list will be submitted to Campaign Monitor.

1 string reference to 'campaignmonitor_user_form_submit'
campaignmonitor_user_form in modules/campaignmonitor_user/campaignmonitor_user.module
Builds the selection list for the user profile page.

File

modules/campaignmonitor_user/campaignmonitor_user.module, line 107
Tab to the profile page to select newsletters to subscribe to.

Code

function campaignmonitor_user_form_submit($form, &$form_state) {
  global $user;

  // Get connected to the API and get lists.
  $cm = CampaignMonitor::getConnector();
  $lists_info = $cm
    ->getLists();

  // Loop through the lists.
  foreach ($form_state['values']['lists'] as $list_id => $selected) {
    if ($selected) {

      // If not subscribed, subscribe, else do nothing. The subscribe state is
      // already in the cache, so it cheaper to check then re-subscribe.
      if (!$cm
        ->isSubscribed($list_id, $user->mail)) {
        if (!$cm
          ->subscribe($list_id, $user->mail, $user->name)) {
          form_set_error('', t('You were not subscribed to the list. Please try again later.'));
          $form_state['redirect'] = FALSE;
          return FALSE;
        }

        // Check if the user should be sent to a subscribe page.
        if (isset($lists_info[$list_id]['details']['ConfirmationSuccessPage']) && !empty($lists_info[$list_id]['details']['ConfirmationSuccessPage'])) {
          drupal_goto($lists_info[$list_id]['details']['ConfirmationSuccessPage']);
        }
        else {
          drupal_set_message(t('You are now subscribed to the "@list" list.', [
            '@list' => $lists_info[$list_id]['name'],
          ]), 'status');
        }
      }
    }
    else {

      // Maybe this is an unsubscribe.
      if ($cm
        ->isSubscribed($list_id, $user->mail)) {
        if (!$cm
          ->unsubscribe($list_id, $user->mail)) {
          form_set_error('', t('You were not unsubscribed from the list(s). Please try again later.'));
          $form_state['redirect'] = FALSE;
          return FALSE;
        }

        // Check if the user should be sent to an unsubscribe page.
        if (isset($lists_info[$list_id]['details']['UnsubscribePage']) && !empty($lists_info[$list_id]['details']['UnsubscribePage'])) {
          drupal_goto($lists_info[$list_id]['details']['UnsubscribePage']);
        }
        else {
          drupal_set_message(t('You are now removed from the "@list" list.', [
            '@list' => $lists_info[$list_id]['name'],
          ]), 'status');
        }
      }
    }
  }
}