Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messagemarketingfb.php
1<?php
10
20
27{
28 const CODE = Message\iMarketing::CODE_FACEBOOK;
29
31 protected $configuration;
32
33 const STATUS_ACTIVE = 'ACTIVE';
34 const STATUS_PAUSED = 'PAUSED';
35 const STATUS_ARCHIVED = 'ARCHIVED';
36 const STATUS_DELETED = 'DELETED';
37
41 public function __construct()
42 {
43 $this->configuration = new Message\Configuration();
44 }
49 public function getName()
50 {
51 return Loc::getMessage('SENDER_INTEGRATION_SEO_MESSAGE_NAME_ADS_FACEBOOK');
52 }
53
54 protected function setConfigurationOptions()
55 {
56 $this->configuration->setArrayOptions(
57 [
58 [
59 'type' => 'string',
60 'code' => 'CLIENT_ID',
61 'name' => "",
62 'required' => true,
63 ],
64 [
65 'type' => 'title',
66 'code' => 'TITLE',
67 'name' => "",
68 'required' => true,
69 ],
70 [
71 'type' => 'string',
72 'code' => 'ACCOUNT_ID',
73 'name' => Loc::getMessage('SENDER_INTEGRATION_SEO_MESSAGE_CONFIG_ACCOUNT_ID'),
74 'required' => true,
75 ],
76 [
77 'type' => 'string',
78 'code' => 'INSTAGRAM_ACCOUNT_ID',
79 'name' => "",
80 'required' => true,
81 ],
82 [
83 'type' => 'string',
84 'code' => 'PERMALINK',
85 'required' => false,
86 ],
87 [
88 'type' => 'string',
89 'code' => 'TARGET_URL',
90 'required' => true,
91 ],
92 [
93 'type' => 'string',
94 'code' => 'AUDIENCE_ID',
95 ],
96 [
97 'type' => 'string',
98 'code' => 'BODY',
99 'required' => false,
100 ],
101 [
102 'type' => 'string',
103 'code' => 'BUDGET',
104 'required' => true,
105 ],
106 [
107 'type' => 'string',
108 'code' => 'DURATION',
109 'required' => false,
110 ],
111 [
112 'type' => 'string',
113 'code' => 'PAGE_ID',
114 'required' => false,
115 ],
116 [
117 'type' => 'string',
118 'code' => 'AD_SET_ID',
119 'required' => false,
120 ],
121 [
122 'type' => 'string',
123 'code' => 'CAMPAIGN_ID',
124 'required' => false,
125 ],
126 [
127 'type' => 'string',
128 'code' => 'CREATIVE_ID',
129 'required' => false,
130 ],
131 [
132 'type' => 'string',
133 'code' => 'ADS_ID',
134 'required' => false,
135 ],
136 [
137 'type' => 'string',
138 'code' => 'INTERESTS',
139 'required' => false,
140 ],
141 [
142 'type' => 'string',
143 'code' => 'GENDERS',
144 'required' => false,
145 ],
146 [
147 'type' => 'string',
148 'code' => 'REGIONS',
149 'required' => false,
150 ],
151 [
152 'type' => 'string',
153 'code' => 'AGE_FROM',
154 'required' => false,
155 ],
156 [
157 'type' => 'string',
158 'code' => 'AGE_TO',
159 'required' => false,
160 ],
161 [
162 'type' => 'string',
163 'code' => 'MEDIA_ID',
164 'required' => false,
165 ],
166 [
167 'type' => 'string',
168 'code' => 'INSTAGRAM_ACTOR_ID',
169 'required' => false,
170 ],
171 [
172 'type' => 'string',
173 'code' => 'IMAGE_URL',
174 'required' => false,
175 ],
176 [
177 'type' => 'string',
178 'code' => 'AUDIENCE_PHONE_ID',
179 'required' => false,
180 ],
181 [
182 'type' => 'string',
183 'code' => 'AUDIENCE_EMAIL_ID',
184 'required' => false,
185 ],
186 [
187 'type' => 'string',
188 'code' => 'STATUSs',
189 'required' => false,
190 ]
191 ]
192 );
193 }
194 public static function getAdsProvider($adsType, $clientId = null)
195 {
196 $service = Marketing\Configurator::getService();
197 $service->setClientId($clientId);
198 if($adsType === MessageMarketingInstagram::CODE)
199 {
200 $adsType = self::CODE;
201 }
202 $providers = Marketing\Configurator::getProviders([$adsType]);
203 $isFound = false;
204 $provider = array();
205 foreach ($providers as $type => $provider)
206 {
207 if ($type == $adsType)
208 {
209 $isFound = true;
210 break;
211 }
212 }
213
214 if (!$isFound)
215 {
216 return null;
217 }
218
219 return $provider;
220 }
221
229 public function loadConfiguration($id = null)
230 {
231 if (!$this->configuration->hasOptions())
232 {
234 }
235
236 Entity\Message::create()
237 ->setCode($this->getCode())
238 ->loadConfiguration($id, $this->configuration);
239
240
241 $self = $this;
243 $this->configuration->setView(
244 function () use ($self, $configuration)
245 {
246 $containerNodeId = 'seo-ads-' . $configuration->getId();
247 ob_start();
248
249 $provider = static::getAdsProvider(
250 $self->getAdsType(),
251 $configuration->getOption('CLIENT_ID')->getValue()
252 );
253
254 $autoRemoveDays = $configuration->getOption('AUTO_REMOVE_DAY_NUMBER') ?
255 $configuration->getOption('AUTO_REMOVE_DAY_NUMBER')->getValue() : null;
256
257 $GLOBALS['APPLICATION']->IncludeComponent(
258 'bitrix:seo.ads.builder',
259 '',
260 array(
261 'INPUT_NAME_PREFIX' => 'CONFIGURATION_',
262 'CONTAINER_NODE_ID' => $containerNodeId,
263 'PROVIDER' => $provider,
264 'SUBTYPE' => $self->getAdsType(),
265 'ACCOUNT_ID' => $configuration->getOption('ACCOUNT_ID')->getValue(),
266 'CLIENT_ID' => $configuration->getOption('CLIENT_ID')->getValue(),
267 'AUTO_REMOVE_DAY_NUMBER' => $autoRemoveDays,
268 'JS_DESTROY_EVENT_NAME' => '',
269 'TITLE_NODE_SELECTOR' => '[data-role="letter-title"]',
270 'HAS_ACCESS' => true
271 )
272 );
273
274 $result = ob_get_clean();
275 return $result;
276 }
277 );
278
280 }
281
282 public function getCode()
283 {
284 return static::CODE;
285 }
286
287 public function getSupportedTransports()
288 {
289 return array(static::CODE);
290 }
291 protected function getAdsType()
292 {
293 return static::CODE;
294 }
303 {
304 $config = $configuration;
305 $clientId = $config->getOption('CLIENT_ID')->getValue();
306 $body = $config->getOption('BODY')->getValue();
307 $targetUrl = $config->getOption('TARGET_URL')->getValue();
308 $campaignName = $config->getOption('TITLE')->getValue();
309
310 if (!$clientId)
311 {
312 $result = new Result();
313 $result->addError(
314 new Error(Loc::getMessage('SENDER_INTEGRATION_SEO_MESSAGE_ERROR_NO_CLIENT'))
315 );
316
317 return $result;
318 }
319
320 if(!filter_var($targetUrl, FILTER_VALIDATE_URL))
321 {
322 $result = new Result();
323 $result->addError(
324 new Error(Loc::getMessage('SENDER_INTEGRATION_SEO_MESSAGE_ERROR_NO_TARGET_URL'))
325 );
326
327 return $result;
328 }
329
330 $utmMarks = [
331 ['CODE' => 'utm_source', 'VALUE' => 'b24_sender_'.static::CODE],
332 ['CODE' => 'utm_medium', 'VALUE' => 'ads'],
333 ['CODE' => 'utm_campaign', 'VALUE' => $campaignName]
334 ];
335
336 if (!mb_strpos($targetUrl, 'b24_sender_'.static::CODE))
337 {
338 $preparedMarks = [];
339 foreach($utmMarks as $utmMark)
340 {
341 $preparedMarks[$utmMark['CODE']] = $utmMark['VALUE'];
342 }
343
344 $config->getOption('TARGET_URL')->setValue(sprintf('%s/?%s',$targetUrl, http_build_query($preparedMarks)));
345 }
346
347 return Entity\Message::create()
348 ->setCode($this->getCode())
349 ->setUtm($utmMarks)
350 ->saveConfiguration($this->configuration);
351 }
352
359 public function removeConfiguration($id)
360 {
361 $result = Entity\Message::removeById($id);
362 return $result->isSuccess();
363 }
364
371 public function copyConfiguration($id)
372 {
373 return Entity\Message::create()
374 ->setCode($this->getCode())
375 ->copyConfiguration($id);
376 }
377
378 public static function checkSelf($type)
379 {
380 return in_array($type, [iMarketing::CODE_FACEBOOK, iMarketing::CODE_INSTAGRAM]);
381 }
382
383 public function onBeforeStart()
384 : \Bitrix\Main\Result
385 {
386 $result = new EventResult();
387 $result->setSuccess(true);
388 return $result;
389 }
390
391 public function onAfterEnd()
392 : \Bitrix\Main\Result
393 {
394 $result = new EventResult();
395
396 $config = $this->configuration;
397 $clientId = $config->get('CLIENT_ID');
398 $accountId = $config->get('ACCOUNT_ID');
399 $instagramActorId = $config->get('INSTAGRAM_ACTOR_ID');
400 $permalink = $config->get('PERMALINK');
401 $targetUrl = $config->get('TARGET_URL');
402 $campaignId = $config->get('CAMPAIGN_ID');
403 $adSetId = $config->get('AD_SET_ID');
404 $creativeId = $config->get('CREATIVE_ID');
405 $body = $config->get('BODY');
406 $budget = $config->get('BUDGET');
407 $duration = $config->get('DURATION');
408 $adsId = $config->get('ADS_ID');
409 $pageId = $config->get('PAGE_ID');
410 $title = $config->get('TITLE');
411 $imageUrl = $config->get('IMAGE_URL');
412 $audienceId = $config->get('AUDIENCE_ID');
413 $mediaId = $config->get('MEDIA_ID');
414 $phoneAudienceId = $config->get('AUDIENCE_PHONE_ID');
415 $emailAudienceId = $config->get('AUDIENCE_EMAIL_ID');
416 $ageFrom = $config->get('AGE_FROM');
417 $ageTo = $config->get('AGE_TO');
418 $genders = $config->get('GENDERS') ? Json::decode($config->get('GENDERS')) : [];
419 $interests = $config->get('INTERESTS') ? Json::decode($config->get('INTERESTS')) : [];
420 $regions = $config->get('REGIONS') ? Json::decode($config->get('REGIONS')) : [];
421 $service = Marketing\Configurator::getService();
422 $service->setClientId($clientId);
423
424 $response = Marketing\Configurator::createCampaign(
425 Marketing\Services\AdCampaignFacebook::TYPE_CODE,
426 [
427 'accountId' => $accountId,
428 'instagramAccountId' => $instagramActorId,
429 'name' => $title,
430 'permalink' => $permalink,
431 'targetUrl' => $targetUrl,
432 'mediaId' => $mediaId,
433 'imageUrl' => $imageUrl,
434 'audience' => $audienceId,
435 'campaignId' => $campaignId,
436 'adSetId' => $adSetId,
437 'creativeId' => $creativeId,
438 'audienceId' => $audienceId,
439 'phoneAudienceId' => $phoneAudienceId,
440 'emailAudienceId' => $emailAudienceId,
441 'regions' => $regions,
442 'interests' => $interests,
443 'genders' => $genders,
444 'ageTo' => $ageTo,
445 'ageFrom' => $ageFrom,
446 'body' => $body,
447 'budget' => ($budget?: 100) * 100,
448 'duration' => $duration,
449 'type' => static::CODE,
450 'adsId' => $adsId,
451 'pageId' => $pageId,
452 'status' => self::STATUS_ACTIVE
453 ]
454 );
455
456 $config->set('ADS_ID', $response['adsId']);
457 $config->set('CREATIVE_ID', $response['creativeId']);
458 $config->set('CAMPAIGN_ID', $response['campaignId']);
459 $config->set('AD_SET_ID', $response['adSetId']);
460 $config->set('STATUS', self::STATUS_ACTIVE);
461
462 $this->saveConfiguration($config);
463 $result->setSuccess(true);
464
465 if (isset($response['RESULT']))
466 {
467 $responseResult = json_decode($response['RESULT'], true);
468 $result->setSuccess(false);
469
470 $errors[] = new \Bitrix\Main\Error($responseResult['error']['message']?? '');
471 $result->addErrors($errors);
472
473 }
474 $result->setSuccess(true);
475 return $result;
476 }
477}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
saveConfiguration(Message\Configuration $configuration)
$GLOBALS['____1444769544']
Definition license.php:1