You are here

protected function CampaignMonitorSubscriptionManager::subscribeProcess in Campaign Monitor 8.2

Process to immediately subscribe.

1 call to CampaignMonitorSubscriptionManager::subscribeProcess()
CampaignMonitorSubscriptionManager::userSubscribe in src/CampaignMonitorSubscriptionManager.php
Subscribe user to CM list in real time or by adding to the queue.

File

src/CampaignMonitorSubscriptionManager.php, line 471

Class

CampaignMonitorSubscriptionManager
Manager for Campaignmonitor subscriptions.

Namespace

Drupal\campaignmonitor

Code

protected function subscribeProcess($list_id, $email, $name, $customFields = NULL, $interests = [], $double_optin = FALSE, $format = 'html') {
  $result = FALSE;
  try {
    $custom_fields = [];
    if (isset($customFields)) {
      foreach ($customFields as $key => $field) {
        if (is_array($field)) {

          // Filter out non-selected values.
          $field = array_filter($field);

          // Transform two level array into one level.
          foreach ($field as $value) {
            $custom_fields[] = [
              'Key' => $key,
              'Value' => $value,
            ];
          }
        }
        else {

          // Add non-array custom fields.
          $custom_fields[] = [
            'Key' => $key,
            'Value' => $field,
          ];
        }
      }
    }

    // Update subscriber information or add new subscriber to the list.
    if ($this->campaignMonitorManager
      ->subscribe($list_id, $email, $name, $custom_fields)) {
      $this->campaignMonitorManager
        ->removeSubscriberFromCache($list_id, $email);
      $this->moduleHandler
        ->invokeAll('campaignmonitor_subscribe', [
        $list_id,
        $email,
      ]);
      return TRUE;
    }
    else {
      return FALSE;
    }

    /*// Check if the user should be sent to a subscribe page.
      $lists = $cm->getLists();
      if (isset($lists[$list_id]['details']['ConfirmationSuccessPage'])
      && !empty($lists[$list_id]['details']['ConfirmationSuccessPage'])) {
      drupal_goto($lists[$list_id]['details']['ConfirmationSuccessPage']);
      }
      else {
      drupal_set_message(
      $this->t('You are now subscribed to the "@list" list.',
      ['@list' => $lists[$list_id]['name']]), 'status');
      }.*/
  } catch (Exception $e) {
    $this
      ->logger('campaignmonitor')
      ->error('An error occurred subscribing {email} to list {list}. "{message}"', [
      'email' => $email,
      'list' => $list_id,
      'message' => $e
        ->getMessage(),
    ]);
  }
  return $result;
}