Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
adsaudience.php
1<?php
2
4
8
14{
16 protected static $errors = array();
17
19 protected static $isQueueUsed = false;
20
26 public static function canUse()
27 {
28 return Loader::includeModule('seo') && Loader::includeModule('socialservices');
29 }
30
36 public static function useQueue()
37 {
38 self::$isQueueUsed = true;
39 }
40
46 public static function getService()
47 {
48 return Service::getInstance();
49 }
50
59 public static function addAudience($type, $accountId = null, $name = null)
60 {
61 $audience = Service::getAudience($type);
62 if (!$audience)
63 {
64 return null;
65 }
66
67 if (!$name)
68 {
69 self::$errors[] = Loc::getMessage("SEO_RETARGETING_EMPTY_AUDIENCE_NAME");
70 return null;
71 }
72 $audience->setAccountId($accountId);
73 $parameters = array(
74 'NAME' => $name
75 );
76 $addResult = $audience->add($parameters);
77 if ($addResult->isSuccess() && $addResult->getId())
78 {
79 return $addResult->getId();
80 }
81 else
82 {
83 self::$errors = $addResult->getErrorMessages();
84 return null;
85 }
86 }
87
88 public static function addLookalikeAudience($type, $accountId = null, $sourceAudienceId = null, $options = [])
89 {
90 $audience = Service::getAudience($type);
91 if (!$audience)
92 {
93 return null;
94 }
95 $audience->setAccountId($accountId);
96 $addResult = $audience->createLookalike($sourceAudienceId, $options);
97 if ($addResult->isSuccess() && $addResult->getId())
98 {
99 return $addResult->getId();
100 }
101 else
102 {
103 self::$errors = $addResult->getErrorMessages();
104 return null;
105 }
106 }
107
121 public static function getAudiences($type, $accountId = null): array
122 {
123 $result = array();
124
125 $audience = Service::getAudience($type);
126
127 $audience->setAccountId($accountId);
128 $audiencesResult = $audience->getList();
129 if ($audiencesResult->isSuccess())
130 {
131 while ($audienceData = $audiencesResult->fetch())
132 {
133 $audienceData = $audience->normalizeListRow($audienceData);
134 if ($audienceData['ID'])
135 {
136 $result[] = array(
137 'id' => $audienceData['ID'],
138 'isSupportMultiTypeContacts' => $audience->isSupportMultiTypeContacts(),
139 //'isAddingRequireContacts' => $audience->isAddingRequireContacts(),
140 'supportedContactTypes' => $audienceData['SUPPORTED_CONTACT_TYPES'],
141 'status' => $audienceData['STATUS'] ?? '',
142 'name' =>
143 $audienceData['NAME']
144 ?
145 $audienceData['NAME'] . (
146 $audienceData['COUNT_VALID'] > 0 ?
147 ' (' . $audienceData['COUNT_VALID'] . ')'
148 :
149 ''
150 )
151 :
152 $audienceData['ID']
153 );
154 }
155 }
156 }
157 else
158 {
159 self::$errors = $audiencesResult->getErrorMessages();
160 }
161
162 return $result;
163 }
164
165 public static function getAudienceWithNormalizedStatus($type, string $messageCode, $accountId = null): array
166 {
167 $audiences = static::getAudiences($type, $accountId);
168
169 $audienceService = Service::getAudience($type);
170 $audienceStatusNormalizer = AudienceStatusNormalizerFactory::build($audienceService::TYPE_CODE, $messageCode);
171
172 foreach ($audiences as &$audience)
173 {
174 $audienceStatus = $audience['status'];
175
176 $audience['normalizedStatus'] = $audienceStatusNormalizer->getNormalizedStatus($audienceStatus);
177 $audience['normalizedStatusMessage'] = $audienceStatusNormalizer->getNormalizedStatusTranslation($audienceStatus);
178 $audience['isEnabled'] = $audienceStatusNormalizer->isEnabled($audienceStatus);
179 }
180
181 return $audiences;
182 }
183
184 public static function getRegions($type)
185 {
186 $account = Service::getAccount($type);
187 if (!$account)
188 {
189 return [];
190 }
191
192 return $account->getRegionsList();
193 }
194
201 public static function getProviders(array $types = null)
202 {
203 $providers = static::getServiceProviders($types);
204 foreach ($providers as $type => $provider)
205 {
206 $audience = Service::getAudience($type);
207 $providers[$type]['URL_AUDIENCE_LIST'] = $audience->getUrlAudienceList();
208 $providers[$type]['IS_SUPPORT_ACCOUNT'] = $audience->isSupportAccount();
209 $providers[$type]['IS_SUPPORT_REMOVE_CONTACTS'] = $audience->isSupportRemoveContacts();
210 //$providers[$type]['IS_ADDING_REQUIRE_CONTACTS'] = $audience->isAddingRequireContacts();
211 $providers[$type]['IS_SUPPORT_MULTI_TYPE_CONTACTS'] = $audience->isSupportMultiTypeContacts();
212 $providers[$type]['IS_SUPPORT_ADD_AUDIENCE'] = $audience->isSupportAddAudience();
213 $lookalikeAudienceParams = $audience->getLookalikeAudiencesParams();
214 $providers[$type]['IS_SUPPORT_LOOKALIKE_AUDIENCE'] = !!$lookalikeAudienceParams;
215 $providers[$type]['LOOKALIKE_AUDIENCE_PARAMS'] = $lookalikeAudienceParams;
216 $providers[$type]['IS_SUPPORT_CREATE_LOOKALIKE_FROM_SEGMENTS'] = $audience::isSupportCreateLookalikeFromSegments();
217 }
218
219 return $providers;
220 }
221
228 public static function addToAudience(AdsAudienceConfig $config, $contacts)
229 {
230 static $audiences = array();
231 if (!isset($audiences[$config->type]))
232 {
233 if ($config->clientId)
234 {
235 $service = static::getService();
236 $service->setClientId($config->clientId);
237 }
238 $audience = Service::getAudience($config->type);
239 $audiences[$config->type] = $audience;
240 }
241 else
242 {
243 $audience = $audiences[$config->type];
244 }
245
246 $audience->setAccountId($config->accountId);
247 static::$isQueueUsed ? $audience->enableQueueMode() : $audience->disableQueueMode();
248 if ($config->autoRemoveDayNumber)
249 {
250 $audience->enableQueueAutoRemove($config->autoRemoveDayNumber);
251 }
252 else
253 {
254 $audience->disableQueueAutoRemove();
255 }
256
257 $audienceImportResult = $audience->addContacts(
258 $config->audienceId,
259 $contacts,
260 array(
261 'type' => $config->contactType,
262 'parentId' => $config->parentId
263 )
264 );
265
266 self::$errors = $audienceImportResult->getErrorMessages();
267 return $audienceImportResult->isSuccess();
268 }
269
275 public static function getErrors()
276 {
277 return self::$errors;
278 }
279
285 public static function resetErrors()
286 {
287 self::$errors = array();
288 }
289
295 public static function hasErrors()
296 {
297 return count(self::$errors) > 0;
298 }
299
306 public static function removeAuth($type)
307 {
308 static::getService()->getAuthAdapter($type)->removeAuth();
309 }
310
316 public static function getServiceTypes()
317 {
318 if (!static::canUse())
319 {
320 return array();
321 }
322
323 return static::getService()->getTypes();
324 }
325
332 protected static function getServiceProviders(array $types = null)
333 {
334 $typeList = static::getServiceTypes();
335
336 $providers = array();
337 foreach ($typeList as $type)
338 {
339 if ($types && !in_array($type, $types))
340 {
341 continue;
342 }
343
344 $service = static::getService();
345 $authAdapter = $service->getAuthAdapter($type);
346 $account = $service->getAccount($type);
347 $canUserMultiClients = $authAdapter->canUseMultipleClients();
348
349 $providers[$type] = array(
350 'TYPE' => $type,
351 'HAS_AUTH' => $authAdapter->hasAuth(),
352 'AUTH_URL' => $authAdapter->getAuthUrl(),
353 'PROFILE' => $authAdapter->getToken() ? $account->getProfileCached() : false,
354 'ENGINE_CODE' => $service::getEngineCode($type)
355 );
356 if ($canUserMultiClients)
357 {
358 $providers[$type]['CLIENTS'] = static::getClientsProfiles($authAdapter);
359 if (empty($providers[$type]['CLIENTS']))
360 {
361 $providers[$type]['HAS_AUTH'] = false;
362 }
363 }
364
365 // check if no profile, then may be auth was removed in service
366 if ($providers[$type]['HAS_AUTH'] && empty($providers[$type]['PROFILE']))
367 {
368 static::removeAuth($type);
369 if (!$canUserMultiClients)
370 {
371 $providers[$type]['HAS_AUTH'] = false;
372 }
373 }
374 }
375
376 return $providers;
377 }
378
384 protected static function getClientsProfiles(AuthAdapter $authAdapter)
385 {
386 $type = $authAdapter->getType();
387 return array_values(array_filter(array_map(function ($item) use ($type) {
388 $service = new Service();
389 $service->setClientId($item['proxy_client_id']);
390
391 $authAdapter = Service::getAuthAdapter($type);
392 $authAdapter->setService($service);
393
394 $account = Service::getAccount($type);
395 $account->setService($service);
396 $account->getRequest()->setAuthAdapter($authAdapter);
397
398 $profile = $account->getProfileCached();
399 if ($profile)
400 {
401 return $profile;
402 }
403 else
404 {
405 // if no profile, then may be auth was removed in service
406 $authAdapter->removeAuth();
407 }
408 }, $authAdapter->getAuthorizedClientsList())));
409 }
410
417 public static function getAccounts($type)
418 {
419 if (!static::canUse())
420 {
421 return array();
422 }
423
424 $result = array();
425
426 $account = static::getService()->getAccount($type);
427 $accountsResult = $account->getList();
428 if ($accountsResult->isSuccess())
429 {
430 while ($accountData = $accountsResult->fetch())
431 {
432 $accountData = $account->normalizeListRow($accountData);
433 if ($accountData['ID'])
434 {
435 $result[] = array(
436 'id' => $accountData['ID'],
437 'name' => $accountData['NAME'] ? $accountData['NAME'] : $accountData['ID']
438 );
439 }
440 }
441 }
442 else
443 {
444 self::$errors = $accountsResult->getErrorMessages();
445 }
446
447 return $result;
448 }
449}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getClientsProfiles(AuthAdapter $authAdapter)
static addLookalikeAudience($type, $accountId=null, $sourceAudienceId=null, $options=[])
static getAudiences($type, $accountId=null)
static getProviders(array $types=null)
static getAudienceWithNormalizedStatus($type, string $messageCode, $accountId=null)
static addToAudience(AdsAudienceConfig $config, $contacts)
static getServiceProviders(array $types=null)
static addAudience($type, $accountId=null, $name=null)