You are here

public function CampaignMonitor::getCampaigns in Campaign Monitor 8

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

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

File

src/CampaignMonitor.php, line 614
Implementation of the CampaignMonitor class, which is a wrapper class for Campaign Monitor v3 API. It's implemented as a Singleton class and instances are created using the account variables and the static function getConnector(). An example:.

Class

CampaignMonitor

Namespace

Drupal\campaignmonitor

Code

public function getCampaigns() {
  if (empty($this->campaigns)) {
    if (($cache = \Drupal::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.
          \Drupal::cache()
            ->set('campaignmonitor_campaigns', $this->campaigns, $this
            ->getCacheTimeout());
        }
        else {
          $this
            ->addError(WATCHDOG_ERROR, $result->response->Message, $result->http_status_code);
          return FALSE;
        }
      }
      else {
        return FALSE;
      }
    }
  }
  return $this->campaigns;
}