Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
adsaudience.php
1<?php
2
4
7
13{
15 protected static $errors = array();
16
18 protected static $isQueueUsed = false;
19
25 public static function canUse()
26 {
27 return Loader::includeModule('seo') && Loader::includeModule('socialservices');
28 }
29
35 public static function useQueue()
36 {
37 self::$isQueueUsed = true;
38 }
39
45 public static function getService()
46 {
47 return Service::getInstance();
48 }
49
58 public static function addAudience($type, $accountId = null, $name = null)
59 {
60 $audience = Service::getAudience($type);
61 if (!$audience)
62 {
63 return null;
64 }
65
66 if (!$name)
67 {
68 self::$errors[] = Loc::getMessage("SEO_RETARGETING_EMPTY_AUDIENCE_NAME");
69 return null;
70 }
71 $audience->setAccountId($accountId);
72 $parameters = array(
73 'NAME' => $name
74 );
75 $addResult = $audience->add($parameters);
76 if ($addResult->isSuccess() && $addResult->getId())
77 {
78 return $addResult->getId();
79 }
80 else
81 {
82 self::$errors = $addResult->getErrorMessages();
83 return null;
84 }
85 }
86
87 public static function addLookalikeAudience($type, $accountId = null, $sourceAudienceId = null, $options = [])
88 {
89 $audience = Service::getAudience($type);
90 if (!$audience)
91 {
92 return null;
93 }
94 $audience->setAccountId($accountId);
95 $addResult = $audience->createLookalike($sourceAudienceId, $options);
96 if ($addResult->isSuccess() && $addResult->getId())
97 {
98 return $addResult->getId();
99 }
100 else
101 {
102 self::$errors = $addResult->getErrorMessages();
103 return null;
104 }
105 }
106
114 public static function getAudiences($type, $accountId = null)
115 {
116 $result = array();
117
118 $audience = Service::getAudience($type);
119
120 $audience->setAccountId($accountId);
121 $audiencesResult = $audience->getList();
122 if ($audiencesResult->isSuccess())
123 {
124 while ($audienceData = $audiencesResult->fetch())
125 {
126 $audienceData = $audience->normalizeListRow($audienceData);
127 if ($audienceData['ID'])
128 {
129 $result[] = array(
130 'id' => $audienceData['ID'],
131 'isSupportMultiTypeContacts' => $audience->isSupportMultiTypeContacts(),
132 //'isAddingRequireContacts' => $audience->isAddingRequireContacts(),
133 'supportedContactTypes' => $audienceData['SUPPORTED_CONTACT_TYPES'],
134 'name' =>
135 $audienceData['NAME']
136 ?
137 $audienceData['NAME'] . (
138 $audienceData['COUNT_VALID'] > 0 ?
139 ' (' . $audienceData['COUNT_VALID'] . ')'
140 :
141 ''
142 )
143 :
144 $audienceData['ID']
145 );
146 }
147 }
148 }
149 else
150 {
151 self::$errors = $audiencesResult->getErrorMessages();
152 }
153
154 return $result;
155 }
156
157 public static function getRegions($type)
158 {
159 $account = Service::getAccount($type);
160 if (!$account)
161 {
162 return [];
163 }
164
165 return $account->getRegionsList();
166 }
167
174 public static function getProviders(array $types = null)
175 {
176 $providers = static::getServiceProviders($types);
177 foreach ($providers as $type => $provider)
178 {
179 $audience = Service::getAudience($type);
180 $providers[$type]['URL_AUDIENCE_LIST'] = $audience->getUrlAudienceList();
181 $providers[$type]['IS_SUPPORT_ACCOUNT'] = $audience->isSupportAccount();
182 $providers[$type]['IS_SUPPORT_REMOVE_CONTACTS'] = $audience->isSupportRemoveContacts();
183 //$providers[$type]['IS_ADDING_REQUIRE_CONTACTS'] = $audience->isAddingRequireContacts();
184 $providers[$type]['IS_SUPPORT_MULTI_TYPE_CONTACTS'] = $audience->isSupportMultiTypeContacts();
185 $providers[$type]['IS_SUPPORT_ADD_AUDIENCE'] = $audience->isSupportAddAudience();
186 $lookalikeAudienceParams = $audience->getLookalikeAudiencesParams();
187 $providers[$type]['IS_SUPPORT_LOOKALIKE_AUDIENCE'] = !!$lookalikeAudienceParams;
188 $providers[$type]['LOOKALIKE_AUDIENCE_PARAMS'] = $lookalikeAudienceParams;
189 }
190
191 return $providers;
192 }
193
200 public static function addToAudience(AdsAudienceConfig $config, $contacts)
201 {
202 static $audiences = array();
203 if (!isset($audiences[$config->type]))
204 {
205 if ($config->clientId)
206 {
207 $service = static::getService();
208 $service->setClientId($config->clientId);
209 }
210 $audience = Service::getAudience($config->type);
211 $audiences[$config->type] = $audience;
212 }
213 else
214 {
215 $audience = $audiences[$config->type];
216 }
217
218 $audience->setAccountId($config->accountId);
219 static::$isQueueUsed ? $audience->enableQueueMode() : $audience->disableQueueMode();
220 if ($config->autoRemoveDayNumber)
221 {
222 $audience->enableQueueAutoRemove($config->autoRemoveDayNumber);
223 }
224 else
225 {
226 $audience->disableQueueAutoRemove();
227 }
228
229 $audienceImportResult = $audience->addContacts(
230 $config->audienceId,
231 $contacts,
232 array(
233 'type' => $config->contactType,
234 'parentId' => $config->parentId
235 )
236 );
237
238 self::$errors = $audienceImportResult->getErrorMessages();
239 return $audienceImportResult->isSuccess();
240 }
241
247 public static function getErrors()
248 {
249 return self::$errors;
250 }
251
257 public static function resetErrors()
258 {
259 self::$errors = array();
260 }
261
267 public static function hasErrors()
268 {
269 return count(self::$errors) > 0;
270 }
271
278 public static function removeAuth($type)
279 {
280 static::getService()->getAuthAdapter($type)->removeAuth();
281 }
282
288 public static function getServiceTypes()
289 {
290 if (!static::canUse())
291 {
292 return array();
293 }
294
295 return static::getService()->getTypes();
296 }
297
304 protected static function getServiceProviders(array $types = null)
305 {
306 $typeList = static::getServiceTypes();
307
308 $providers = array();
309 foreach ($typeList as $type)
310 {
311 if ($types && !in_array($type, $types))
312 {
313 continue;
314 }
315
316 $service = static::getService();
317 $authAdapter = $service->getAuthAdapter($type);
318 $account = $service->getAccount($type);
319 $canUserMultiClients = $authAdapter->canUseMultipleClients();
320
321 $providers[$type] = array(
322 'TYPE' => $type,
323 'HAS_AUTH' => $authAdapter->hasAuth(),
324 'AUTH_URL' => $authAdapter->getAuthUrl(),
325 'PROFILE' => $authAdapter->getToken() ? $account->getProfileCached() : false,
326 );
327 if ($canUserMultiClients)
328 {
329 $providers[$type]['CLIENTS'] = static::getClientsProfiles($authAdapter);
330 if (empty($providers[$type]['CLIENTS']))
331 {
332 $providers[$type]['HAS_AUTH'] = false;
333 }
334 }
335
336 // check if no profile, then may be auth was removed in service
337 if ($providers[$type]['HAS_AUTH'] && empty($providers[$type]['PROFILE']))
338 {
339 static::removeAuth($type);
340 if (!$canUserMultiClients)
341 {
342 $providers[$type]['HAS_AUTH'] = false;
343 }
344 }
345 }
346
347 return $providers;
348 }
349
355 protected static function getClientsProfiles(AuthAdapter $authAdapter)
356 {
357 $type = $authAdapter->getType();
358 return array_values(array_filter(array_map(function ($item) use ($type) {
359 $service = new Service();
360 $service->setClientId($item['proxy_client_id']);
361
362 $authAdapter = Service::getAuthAdapter($type);
363 $authAdapter->setService($service);
364
365 $account = Service::getAccount($type);
366 $account->setService($service);
367 $account->getRequest()->setAuthAdapter($authAdapter);
368
369 $profile = $account->getProfileCached();
370 if ($profile)
371 {
372 return $profile;
373 }
374 else
375 {
376 // if no profile, then may be auth was removed in service
377 $authAdapter->removeAuth();
378 }
379 }, $authAdapter->getAuthorizedClientsList())));
380 }
381
388 public static function getAccounts($type)
389 {
390 if (!static::canUse())
391 {
392 return array();
393 }
394
395 $result = array();
396
397 $account = static::getService()->getAccount($type);
398 $accountsResult = $account->getList();
399 if ($accountsResult->isSuccess())
400 {
401 while ($accountData = $accountsResult->fetch())
402 {
403 $accountData = $account->normalizeListRow($accountData);
404 if ($accountData['ID'])
405 {
406 $result[] = array(
407 'id' => $accountData['ID'],
408 'name' => $accountData['NAME'] ? $accountData['NAME'] : $accountData['ID']
409 );
410 }
411 }
412 }
413 else
414 {
415 self::$errors = $accountsResult->getErrorMessages();
416 }
417
418 return $result;
419 }
420}
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 addToAudience(AdsAudienceConfig $config, $contacts)
static getServiceProviders(array $types=null)
static addAudience($type, $accountId=null, $name=null)
static getAuthAdapter($type)
Definition service.php:205