Source of file Campaigns.php
Size: 12,208 Bytes - Last Modified: 2021-12-23T10:39:01+00:00
/var/www/docs.ssmods.com/process/src/src/Api/Traits/Campaigns.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 | <?php namespace Sunnysideup\CampaignMonitor\Api\Traits; use SilverStripe\Control\Email\Email; use SilverStripe\Core\Config\Config; use SilverStripe\SiteConfig\SiteConfig; use Sunnysideup\CampaignMonitor\Model\CampaignMonitorCampaign; trait Campaigns { /** * @return mixed A successful response will be an object of the form * array( * { * 'WebVersionURL' => The web version url of the campaign * 'WebVersionTextURL' => The web version url of the text version of the campaign * 'CampaignID' => The id of the campaign * 'Subject' => The campaign subject * 'Name' => The name of the campaign * 'FromName' => The from name for the campaign * 'FromEmail' => The from email address for the campaign * 'ReplyTo' => The reply to email address for the campaign * 'SentDate' => The sent data of the campaign * 'TotalRecipients' => The number of recipients of the campaign * } */ public function getCampaigns() { //require_once '../../csrest_clients.php'; require_once BASE_PATH . '/vendor/campaignmonitor/createsend-php/csrest_clients.php'; $wrap = new \CS_REST_Clients($this->Config()->get('client_id'), $this->getAuth()); $result = $wrap->get_campaigns(); return $this->returnResult( $result, 'GET /api/v3.1/clients/{id}/campaigns', 'Got sent campaigns' ); } /** * @return mixed */ public function getDrafts() { //require_once '../../csrest_clients.php'; require_once BASE_PATH . '/vendor/campaignmonitor/createsend-php/csrest_clients.php'; $wrap = new \CS_REST_Clients($this->Config()->get('client_id'), $this->getAuth()); $result = $wrap->get_drafts(); return $this->returnResult( $result, 'GET /api/v3.1/clients/{id}/drafts', 'Got draft campaigns' ); } public function getScheduled() { user_error('This method is still to be implemented, see samples for an example'); } // create campaigns /** * @param array $listIDs * @param array $segmentIDs * @param string $templateID - OPTIONAL! * @param array $templateContent - OPTIONAL! */ public function createCampaign( CampaignMonitorCampaign $campaignMonitorCampaign, $listIDs = [], $segmentIDs = [], $templateID = '', $templateContent = [] ) { //require_once '../../csrest_lists.php'; require_once BASE_PATH . '/vendor/campaignmonitor/createsend-php/csrest_lists.php'; $siteConfig = SiteConfig::current_site_config(); $subject = $campaignMonitorCampaign->Subject; if (! $subject) { $subject = 'no subject set'; } $name = $campaignMonitorCampaign->Name; if (! $name) { $name = 'no name set'; } $fromName = $campaignMonitorCampaign->FromName; if (! $fromName) { $fromName = $siteConfig->Title; } $fromEmail = $campaignMonitorCampaign->FromEmail; if (! $fromEmail) { $fromEmail = Config::inst()->get(Email::class, 'admin_email'); } $replyTo = $campaignMonitorCampaign->ReplyTo; if (! $replyTo) { $replyTo = $fromEmail; } $page = $campaignMonitorCampaign->Pages()->first(); if ($page) { $listID = $campaignMonitorCampaign->Pages()->first()->ListID; $wrap = new \CS_REST_Campaigns(null, $this->getAuth()); if ($templateID) { $result = $wrap->create_from_template( $this->Config()->get('client_id'), [ 'Subject' => $subject, 'Name' => $name, 'FromName' => $fromName, 'FromEmail' => $fromEmail, 'ReplyTo' => $replyTo, 'ListIDs' => [$listID], 'SegmentIDs' => [], 'TemplateID' => $templateID, 'TemplateContent' => $templateContent, ] ); } else { $result = $wrap->create( $this->Config()->get('client_id'), [ 'Subject' => $subject, 'Name' => $name, 'FromName' => $fromName, 'FromEmail' => $fromEmail, 'ReplyTo' => $replyTo, 'HtmlUrl' => $campaignMonitorCampaign->PreviewLink(), 'TextUrl' => $campaignMonitorCampaign->PreviewLink('textonly'), 'ListIDs' => [$listID], ] ); } if (null !== $result->http_status_code && (201 === $result->http_status_code || 201 === $result->http_status_code)) { $code = $result->response; $campaignMonitorCampaign->CreateFromWebsite = false; $campaignMonitorCampaign->CreatedFromWebsite = true; $campaignMonitorCampaign->CampaignID = $code; } else { $campaignMonitorCampaign->CreateFromWebsite = false; $campaignMonitorCampaign->CreatedFromWebsite = false; $code = 'Error'; if (is_object($result->response)) { $code = $result->response->Code . ':' . $result->response->Message; } $campaignMonitorCampaign->MessageFromNewsletterServer = $code; } $campaignMonitorCampaign->write(); } else { $result = 'ERROR: no campagn monitor page with list id created yet.'; } return $this->returnResult( $result, 'CREATE /api/v3/campaigns/{clientID}', 'Created Campaign' ); } public function deleteCampaign($campaignID) { $wrap = new \CS_REST_Campaigns($campaignID, $this->getAuth()); $result = $wrap->delete(); return $this->returnResult( $result, 'DELETE /api/v3/campaigns/{id}', 'Deleted Campaign' ); } // information about the campaigns public function getBounces() { user_error('This method is still to be implemented, see samples for an example'); } public function getClicks() { user_error('This method is still to be implemented, see samples for an example'); } /** * Gets a summary of all campaign reporting statistics. * * @param int $campaignID * * @return mixed A successful response will be an object of the form * { * 'Recipients' => The total recipients of the campaign * 'TotalOpened' => The total number of opens recorded * 'Clicks' => The total number of recorded clicks * 'Unsubscribed' => The number of recipients who unsubscribed * 'Bounced' => The number of recipients who bounced * 'UniqueOpened' => The number of recipients who opened * 'WebVersionURL' => The url of the web version of the campaign * 'WebVersionTextURL' => The url of the web version of the text version of the campaign * 'WorldviewURL' => The public Worldview URL for the campaign * 'Forwards' => The number of times the campaign has been forwarded to a friend * 'Likes' => The number of times the campaign has been 'liked' on Facebook * 'Mentions' => The number of times the campaign has been tweeted about * 'SpamComplaints' => The number of recipients who marked the campaign as spam * } */ public function getSummary($campaignID) { $wrap = new \CS_REST_Campaigns($campaignID, $this->getAuth()); $result = $wrap->get_summary(); return $this->returnResult( $result, 'GET /api/v3.1/campaigns/{id}/summary', 'Got Summary' ); } /** * Gets the email clients that subscribers used to open the campaign. * * @param int $campaignID * * @return mixed A successful response will be an object of the form * array( * { * Client => The email client name * Version => The email client version * Percentage => The percentage of subscribers who used this email client * Subscribers => The actual number of subscribers who used this email client * } * ) */ public function getEmailClientUsage($campaignID) { $wrap = new \CS_REST_Campaigns($campaignID, $this->getAuth()); $result = $wrap->get_email_client_usage(); return $this->returnResult( $result, 'GET /api/v3.1/campaigns/{id}/emailclientusage', 'Got email client usage' ); } public function getOpens() { user_error('This method is still to be implemented, see samples for an example'); } public function getRecipients() { user_error('This method is still to be implemented, see samples for an example'); } public function getSpam() { user_error('This method is still to be implemented, see samples for an example'); } /** * Gets all unsubscribes recorded for a campaign since the provided date. * * @param int $campaignID ID of the Campaign * @param int $daysAgo The date to start getting subscribers from * @param int $page The page number to get * @param int $pageSize The number of records per page * @param string $sortByField ('EMAIL', 'NAME', 'DATE') * @param string $sortDirection ('ASC', 'DESC') * * @return mixed A successful response will be an object of the form * { * 'ResultsOrderedBy' => The field the results are ordered by * 'OrderDirection' => The order direction * 'PageNumber' => The page number for the result set * 'PageSize' => The page size used * 'RecordsOnThisPage' => The number of records returned * 'TotalNumberOfRecords' => The total number of records available * 'NumberOfPages' => The total number of pages for this collection * 'Results' => array( * { * 'EmailAddress' => The email address of the subscriber who unsubscribed * 'ListID' => The list id of the list containing the subscriber * 'Date' => The date of the unsubscribe * 'IPAddress' => The ip address where the unsubscribe originated * } * ) * } */ public function getUnsubscribes( int $campaignID, ?int $daysAgo = 3650, ?int $page = 1, ?int $pageSize = 999, ?string $sortByField = 'EMAIL', ?string $sortDirection = 'ASC' ) { //require_once '../../csrest_campaigns.php'; require_once BASE_PATH . '/vendor/campaignmonitor/createsend-php/csrest_campaigns.php'; $wrap = new \CS_REST_Campaigns($campaignID, $this->getAuth()); $result = $wrap->get_unsubscribes( date('Y-m-d', strtotime('-' . $daysAgo . ' days')), $page, $pageSize, $sortByField, $sortDirection ); return $this->returnResult( $result, 'GET /api/v3.1/campaigns/{id}/unsubscribes', 'Got unsubscribes' ); } } |