You are here

function mailchimp_campaign_save_campaign in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()
  2. 7.2 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()
  3. 7.3 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()
  4. 7.4 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()
  5. 2.x modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()

Save a campaign in Mailchimp and as a Drupal entity.

Parameters

array $template: Associative array of template content indexed by section IDs.

object $recipients: Associative array of template values.

string $campaign_id: The ID of the campaign to save, if updating.

Return value

string New or existing campaign ID.

1 call to mailchimp_campaign_save_campaign()
MailchimpCampaignForm::validateForm in modules/mailchimp_campaign/src/Form/MailchimpCampaignForm.php
Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level…

File

modules/mailchimp_campaign/mailchimp_campaign.module, line 79
Module file for mailchimp_campaign.

Code

function mailchimp_campaign_save_campaign(array $template, $recipients, $settings, $template_id, $campaign_id = NULL) {

  // Allow alter of template and options used in campaign.
  \Drupal::moduleHandler()
    ->alter('mailchimp_campaign', $recipients, $template, $campaign_id);

  // Convert template to content by running through formatter.
  if (isset($template['html'])) {
    $content = mailchimp_campaign_render_template($template);
  }
  else {
    $content = [
      'sections' => mailchimp_campaign_render_template($template),
    ];
  }

  // Test for valid list segment, if selected.
  if (isset($recipients->segment_opts)) {
    if (!isset($recipients->segment_opts->saved_segment_id) || mailchimp_test_list_segment($recipients->list_id, $recipients->segment_opts->saved_segment_id) === NULL && !isset($recipients->segment_opts->match, $recipients->segment_opts->conditions)) {
      return NULL;
    }
  }

  // Build content parameters.
  $content_parameters = [];
  if (!empty($template_id)) {

    // Use template sections as campaign content.
    $content_parameters['template'] = (object) [
      'id' => (int) $template_id,
      'sections' => (object) $content['sections'],
    ];
  }
  elseif (isset($content['html'])) {

    // Use HTML as campaign content.
    $content_parameters['html'] = $content['html'];
  }

  /* @var \Mailchimp\MailchimpCampaigns $mc_campaigns */
  $mc_campaigns = mailchimp_get_api_object('MailchimpCampaigns');

  // Save campaign to Mailchimp. (Only regular campaigns are supported).
  $is_new = empty($campaign_id);
  if ($is_new) {
    try {
      if (!$mc_campaigns) {
        throw new Exception('Cannot create campaign without Mailchimp API. Check API key has been entered.');
      }
      $result = $mc_campaigns
        ->addCampaign(MailchimpCampaigns::CAMPAIGN_TYPE_REGULAR, $recipients, $settings);
      if (!empty($result->id)) {
        $campaign_id = $result->id;
        $mc_campaigns
          ->setCampaignContent($campaign_id, $content_parameters);
      }
    } catch (\Exception $e) {
      \Drupal::messenger()
        ->addError($e
        ->getMessage());
      \Drupal::logger('mailchimp_campaign')
        ->error('An error occurred while creating this campaign: {message}', [
        'message' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
  }
  else {

    // Updates must be sent one type at a time.
    try {
      if (!$mc_campaigns) {
        throw new Exception('Cannot update campaign without Mailchimp API. Check API key has been entered.');
      }
      $result = $mc_campaigns
        ->updateCampaign($campaign_id, MailchimpCampaigns::CAMPAIGN_TYPE_REGULAR, $recipients, $settings);
      if (!empty($result->id)) {
        $mc_campaigns
          ->setCampaignContent($result->id, $content_parameters);
      }
    } catch (\Exception $e) {
      \Drupal::messenger()
        ->addError($e
        ->getMessage());
      \Drupal::logger('mailchimp_campaign')
        ->error('An error occurred while updating this campaign: @msg', [
        '@msg' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
  }
  if (!empty($result->id)) {
    \Drupal::messenger()
      ->addStatus(t('Campaign %name (%cid) was successfully saved.', [
      '%name' => $settings->title,
      '%cid' => $campaign_id,
    ]));

    // Clear cached data for this campaign.
    mailchimp_campaign_get_campaigns([
      $campaign_id,
    ], TRUE);
  }
  return $campaign_id;
}