Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messagelookalike.php
1<?php
10
19
24abstract class MessageLookalike extends MessageBase implements iLookalikeAds, iBeforeAfter
25{
26 public function onBeforeStart():\Bitrix\Main\Result
27 {
28 $result = new Result();
29 $config = $this->loadConfiguration();
30
31 $letter = LetterTable::getList([
32 'filter' => ['=MESSAGE_ID' => $config->getId()],
33 'select' => ['ID', 'TITLE']
34 ])->fetch();
35
36 $service = AdsAudience::getService();
37 $service->setClientId($config->get('CLIENT_ID'));
38
39 $audienceId = AdsAudience::addAudience($this->getAdsType(), $config->get('ACCOUNT_ID'), $letter['TITLE']);
40 if ($audienceId)
41 {
42 $config->set('AUDIENCE_ID', $audienceId);
43 $this->saveConfiguration($this->configuration);
44 }
45 else
46 {
47 $result->addErrors(array_map(
48 function ($errorMessage)
49 {
50 return new Error($errorMessage);
51 },
53 );
54 }
55
56 return $result;
57 }
58
59 public function onAfterEnd():\Bitrix\Main\Result
60 {
61 $result = new EventResult();
62
63 $config = $this->configuration;
64
65 $service = AdsAudience::getService();
66 $service->setClientId($config->get('CLIENT_ID'));
67
68 $audience = Service::getAudience($this->getAdsType());
69 if ($audience->isQueueProcessed('sender:'.$config->getId()))
70 {
71 $audienceId = AdsAudience::addLookalikeAudience($this->getAdsType(), $config->get('ACCOUNT_ID'), $config->get('AUDIENCE_ID'), $this->getLookalikeOptions());
72 if (!$audienceId)
73 {
74 $result->addErrors(array_map(
75 function ($errorMessage)
76 {
77 return new Error($errorMessage);
78 },
80 );
81 $result->setSuccess(true);
82 }
83 }
84 else
85 {
86 $result->setSuccess(false); // disallow finishing the posting
87 }
88 return $result;
89 }
90
91 abstract public function getLookalikeOptions();
92}
saveConfiguration(Message\Configuration $configuration)
static addLookalikeAudience($type, $accountId=null, $sourceAudienceId=null, $options=[])
static addAudience($type, $accountId=null, $name=null)