You are here

function cc::create_campaign in Constant Contact 7.3

Same name and namespace in other branches
  1. 6.3 class.cc.php \cc::create_campaign()
  2. 6.2 class.cc.php \cc::create_campaign()

Creates a new campaign.

@access public

File

./class.cc.php, line 1306
Constant Contact PHP Class

Class

cc
@file Constant Contact PHP Class

Code

function create_campaign($title, $contact_lists = array(), $options = array()) {
  $email = isset($options['EmailAddress']) ? $options['EmailAddress'] : '';
  unset($options['EmailAddress']);
  $xml_post = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
  <link href="/ws/customers/' . $this->api_username . '/campaigns" rel="edit" />
  <id>' . $this
    ->get_http_api_url() . 'campaigns</id>
  <title type="text">' . $title . '</title>
  <updated>2009-10-19T18:34:53.105Z</updated>
  <author>
    <name>Constant Contact</name>
  </author>
  <content type="application/vnd.ctct+xml">
    <Campaign xmlns="http://ws.constantcontact.com/ns/1.0/"
id="' . $this
    ->get_http_api_url() . 'campaigns/1100546096289">
      <Name>' . $title . '</Name>
      <Status>Draft</Status>
      <Date>2009-10-19T18:34:53.105Z</Date>
';
  if (!is_array($options)) {
    trigger_error('Third argument to create_campaign() should be an array', E_USER_ERROR);
  }
  foreach ($options as $fieldname => $fieldvalue) {
    if (isset($options[$fieldname])) {
      $xml_post .= "<{$fieldname}>{$fieldvalue}</{$fieldname}>\n";
    }
  }
  if (is_array($contact_lists)) {
    $xml_post .= '<ContactLists>';
    foreach ($contact_lists as $id) {
      $xml_post .= '
				<ContactList id="' . $this
        ->get_list_url($id) . '">
				<link xmlns="http://www.w3.org/2005/Atom" href="' . $this
        ->get_list_url($id, 0) . '" rel="self" />
				</ContactList>
			  ';
    }
    $xml_post .= '</ContactLists>';
  }
  if ($email) {
    $email = $this
      ->get_emails($email);
    if (!isset($email['id'])) {
      $this->last_error = 'Invalid Email Address, the email address must exist in your constant contact account to be able to send an email from this address';
      return FALSE;
    }
    $xml_post .= '
			<FromEmail>
			<Email id="' . $this
      ->get_http_api_url() . 'settings/emailaddresses/' . $email['id'] . '">
			<link xmlns="http://www.w3.org/2005/Atom" href="' . $this->api_uri . 'settings/emailaddresses/' . $email['id'] . '" rel="self" />
			</Email>
			<EmailAddress>' . $email['EmailAddress'] . '</EmailAddress>
			</FromEmail>
			<ReplyToEmail>
			<Email id="' . $this
      ->get_http_api_url() . 'settings/emailaddresses/' . $email['id'] . '">
			<link xmlns="http://www.w3.org/2005/Atom" href="' . $this->api_uri . 'settings/emailaddresses/' . $email['id'] . '" rel="self" />
			</Email>
			<EmailAddress>' . $email['EmailAddress'] . '</EmailAddress>
			</ReplyToEmail>
			';
  }
  $xml_post .= '
	</Campaign>
	</content>
	<source>
		<id>' . $this
    ->get_http_api_url() . 'campaigns</id>
		<title type="text">Campaigns for customer: ' . $this->api_username . '</title>
		<link href="campaigns" />
		<link href="campaigns" rel="self" />
		<author>
			<name>' . $this->api_username . '</name>
		</author>
		<updated>2009-10-19T19:36:12.622Z</updated>
	</source>
</entry>';
  $this
    ->http_set_content_type('application/atom+xml');
  $xml = $this
    ->load_url("campaigns", 'post', $xml_post, 201);
  if ($xml) {
    return TRUE;
  }

  /*
  if(isset($this->http_response_headers['Location']) && trim($this->http_response_headers['Location']) != ''){
      return $this->get_id_from_link($this->http_response_headers['Location']);
  }
  */
  return FALSE;
}