You are here

public function CampaignMonitorManager::getDrafts in Campaign Monitor 8.2

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

The information is stored locally in a temporary cache. The array is formatted like this:.

$drafts[$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 drafts or FALSE on failure.

File

src/CampaignMonitorManager.php, line 820

Class

CampaignMonitorManager
Manager for Campaignmonitor.

Namespace

Drupal\campaignmonitor

Code

public function getDrafts() {
  if (empty($this->drafts)) {
    if (($cache = $this->cacheBackend
      ->get('campaignmonitor_drafts')) && !empty($cache->data)) {

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

          // Build an array for each campaign returned.
          foreach ($result->response as $draft) {
            $this->drafts[$draft->CampaignID] = [
              'Name' => $draft->Name,
              'Subject' => $draft->Subject,
              'Created' => strtotime($draft->DateCreated),
              'From' => $draft->FromName,
              'Link' => $draft->PreviewURL,
            ];
          }

          // Save drafts in the cache.
          $this->cacheBackend
            ->set('campaignmonitor_drafts', $this->drafts, $this
            ->getCacheTimeout());
        }
        else {
          $this
            ->addError($result->response->Message, $result->http_status_code);
          return FALSE;
        }
      }
      else {
        return FALSE;
      }
    }
  }
  return $this->drafts;
}