You are here

function campaignmonitor_subscribe_form_submit in Campaign Monitor 6.3

Same name and namespace in other branches
  1. 7 campaignmonitor.module \campaignmonitor_subscribe_form_submit()

File

./campaignmonitor.module, line 361
Module that plugs in Campaign Monitor functionality to your Drupal web site. For Campaign Monitor information see: http://www.campaignmonitor.com/

Code

function campaignmonitor_subscribe_form_submit($form, &$form_state) {
  $form_values = $form_state['values'];

  // Replace api_key with your own details
  $api_key = variable_get(CM_API_KEY, '');
  $list_id = $form_values['list_id'];
  $name = check_plain($form_values['name']);
  $email = check_plain($form_values['email']);
  $custom_field_array = array();
  $lists = campaignmonitor_get_available_lists();
  $list = $lists[$list_id];

  // 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
    $custom_fields = _campaignmonitor_get_custom_fields($api_key, $list_id);
    foreach ($custom_fields as $field) {
      $key = str_replace(array(
        '[',
        ']',
      ), '', $field['Key']);
      $saved_cfs = $list->customfields;
      $display_cf = FALSE;
      if ($saved_cfs == NULL) {

        // case that upgraded and haven't gone to admin page yet, make sure to display as that would keep same behavior
        $display_cf = TRUE;
      }
      else {
        $saved_cf = $saved_cfs[$key];
        $display_cf = $saved_cf->display;
      }
      if ($display_cf) {
        $custom_field_array[$field['FieldName']] = $form_values[$key];
      }
    }
    _campaignmonitor_add_subscriber($api_key, $list_id, $name, $email, $custom_field_array);
  }
  elseif ($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);
  }
}