Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
transportmarketingfb.php
1<?php
10
17
18Loc::loadMessages(__FILE__);
19
25{
26 const CODE = 'facebook';
27 const STATUS_ACTIVE = 'ACTIVE';
28 const STATUS_PAUSED = 'PAUSED';
29 const STATUS_ARCHIVED = 'ARCHIVED';
30 const STATUS_DELETED = 'DELETED';
31
32 const SEND_STATES = [
33 'AUDIENCE_CREATED' => 'AUDIENCE_CREATED',
34 'AUDIENCE_UPLOADING' => 'AUDIENCE_UPLOADING',
35 'AUDIENCE_UPLOADED'=>'AUDIENCE_UPLOADED'
36 ];
37
39 protected $adsConfig;
40
41
43 protected $configuration;
44
45 public function getName()
46 {
47 return 'Ads';
48 }
49
53 public function __construct()
54 {
55 $this->configuration = new Message\Configuration();
56 }
57
58 public function getCode()
59 {
60 return static::CODE;
61 }
62
64 {
65 return array(Recipient\Type::EMAIL, Recipient\Type::PHONE);
66 }
67
68 public function loadConfiguration()
69 {
71 }
72
74 {
75 $this->configuration = $configuration;
76 }
77
78 public function start()
79 {
80 $authAdapter = Marketing\Service::getAuthAdapter(self::CODE);
81 if (!$authAdapter->hasAuth())
82 {
83 return false;
84 }
85
86 $this->adsConfig = new Marketing\AdsAudienceConfig();
87
88 return true;
89 }
90
91 public function send(Message\Adapter $message)
92 {
93 $config = $message->getConfiguration();
94 $clientId = $config->get('CLIENT_ID');
95 $accountId = $config->get('ACCOUNT_ID');
96 $duration = $config->get('DURATION');
97 $audienceId = $config->get('AUDIENCE_ID');
98 $status = $config->get('STATUS');
99
100 $service = Marketing\Configurator::getService();
101 $service->setClientId($clientId);
102
103 if(!$message->getRecipientCode())
104 {
105 $status = $config->set('STATUS', self::SEND_STATES['AUDIENCE_UPLOADED']);
106 $message->saveConfiguration($config);
107 }
108
109 if (!$audienceId && $message->getRecipientCode())
110 {
111 $audiences = Marketing\Configurator::createAudience(
112 Marketing\Services\AdCampaignFacebook::TYPE_CODE,
113 [
114 'accountId' => $accountId,
115 'duration' => $duration
116 ]
117 );
118 $config->set('AUDIENCE_ID', $audiences['audienceId']);
119 $config->set('AUDIENCE_PHONE_ID', $audiences['phoneAudienceId']);
120 $config->set('AUDIENCE_EMAIL_ID', $audiences['emailAudienceId']);
121
122 $status = $config->set('STATUS', self::SEND_STATES['AUDIENCE_CREATED']);
123 $message->saveConfiguration($config);
124 }
125
126 if($status && in_array($status, [self::SEND_STATES['AUDIENCE_CREATED'],
127 self::SEND_STATES['AUDIENCE_UPLOADING']]))
128 {
129 return parent::send($message);
130 }
131 }
132
133 protected function addToAudience($clientId, $contacts)
134 {
135 $service = AdsAudience::getService();
136 $service->setClientId($clientId);
138 $this->adsConfig->type = Marketing\Services\AdCampaignFacebook::TYPE_CODE;
139
140 return AdsAudience::addToAudience($this->adsConfig, $contacts);
141 }
142
143 public function end()
144 {
145 }
146}
static loadMessages($file)
Definition loc.php:64
saveConfiguration(Message\Configuration $configuration)
static addToAudience(AdsAudienceConfig $config, $contacts)