You are here

function mailchimp_campaign_save_campaign in Mailchimp 7.4

Same name and namespace in other branches
  1. 8 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()
  2. 7.5 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()
  3. 7.2 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_save_campaign()
  4. 7.3 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: List settings for the campaign.

object $settings: The subject, from name, reply-to, etc settings for the campaign.

int $template_id: The Mailchimp template ID.

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()
mailchimp_campaign_campaign_form_submit in modules/mailchimp_campaign/includes/mailchimp_campaign.admin.inc
Submit handler for mailchimp_campaign_campaign_form().

File

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

Code

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

  // Allow alter of template and options used in campaign
  drupal_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 = array(
      'sections' => mailchimp_campaign_render_template($template),
    );
  }

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

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

    // Use template sections as campaign content.
    $content_parameters['template'] = (object) array(
      'id' => (int) $template_id,
      'sections' => (object) $content['sections'],
    );
  }
  else {
    if (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 MailchimpException('Cannot create campaign without Mailchimp API. Check API key has been entered.');
      }
      $result = $mc_campaigns
        ->addCampaign(\Mailchimp\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_set_message($e
        ->getMessage(), 'error');
      watchdog('mailchimp_campaign', 'An error occurred while creating this campaign: %message', array(
        '%message' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
      return NULL;
    }
  }
  else {

    // Update an existing campaign.
    try {
      if (!$mc_campaigns) {
        throw new MailchimpException('Cannot update campaign without Mailchimp API. Check API key has been entered.');
      }
      $result = $mc_campaigns
        ->updateCampaign($campaign_id, \Mailchimp\MailchimpCampaigns::CAMPAIGN_TYPE_REGULAR, $recipients, $settings);
      if (!empty($result->id)) {
        $mc_campaigns
          ->setCampaignContent($result->id, $content_parameters);
      }
    } catch (Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
      watchdog('mailchimp_campaign', 'An error occurred while updating this campaign: %message', array(
        '%message' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
      return NULL;
    }
  }
  if (!empty($result->id)) {
    drupal_set_message(t('Campaign %name (%cid) was successfully saved.', array(
      '%name' => $settings->title,
      '%cid' => $campaign_id,
    )));

    // Save the campaign entity:
    $campaign = entity_create('mailchimp_campaign', array(
      'mc_campaign_id' => $campaign_id,
      'template' => $template,
      'is_new' => $is_new,
    ));
    mailchimp_campaign_save($campaign);

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