You are here

public function CampaignMonitor::getCampaigns in Campaign Monitor 7

Get basic information about campaigns in the form of a keyed array.

The information is stored locally in a temporary cache.

The array is formatted like this:

$campaigns[$id] => array( 'Name' => 'Campaign Name', 'Subject' => 'Campaign subject line', 'Sent' => 'Unix timestamp', 'Recipients' => 'The number of recipients', 'Link' => 'Online URL to the campaign', );

Return value

array|bool An array with the campaigns or FALSE on failure.

File

lib/campaignmonitor.class.inc, line 746
Implementation of the CampaignMonitor class.

Class

CampaignMonitor
Implementation of the CampaignMonitor class.

Code

public function getCampaigns() {
  if (empty($this->campaigns)) {
    if (($cache = cache_get('campaignmonitor_campaigns')) && !empty($cache->data)) {

      // Cache information found.
      $this->campaigns = $cache->data;
    }
    else {
      if ($obj = $this
        ->createClientObj()) {
        $result = $obj
          ->get_campaigns();
        if ($result
          ->was_successful()) {

          // Build an array for each campaign returned.
          foreach ($result->response as $campaign) {
            $this->campaigns[$campaign->CampaignID] = [
              'Name' => $campaign->Name,
              'Subject' => $campaign->Subject,
              'Sent' => strtotime($campaign->SentDate),
              'Recipients' => $campaign->TotalRecipients,
              'Link' => $campaign->WebVersionURL,
            ];
          }

          // Save campaigns in the cache.
          cache_set('campaignmonitor_campaigns', $this->campaigns, 'cache', $this
            ->getCacheTimeout());
        }
        else {
          $this
            ->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
          return FALSE;
        }
      }
      else {
        return FALSE;
      }
    }
  }
  return $this->campaigns;
}