Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
service.php
1<?
2
4
6
9
11{
12 const GROUP = 'analytics';
13
14 const TYPE_FACEBOOK = 'facebook';
15 const TYPE_INSTAGRAM = 'instagram';
16 const TYPE_VKONTAKTE = 'vkontakte';
17 const TYPE_VKADS = 'vkads';
18 const TYPE_GOOGLE = 'google';
19 const TYPE_YANDEX = 'yandex';
20
22 protected static $errors = [];
23
24 protected $clientId;
25
31 public static function getInstance()
32 {
33 static $instance = null;
34 if ($instance === null)
35 {
36 $instance = new static();
37 }
38
39 return $instance;
40 }
41
47 public static function canUse()
48 {
49 return Loader::includeModule('seo') && Loader::includeModule('socialservices');
50 }
51
57 public static function canUseMultipleClients()
58 {
59 return true;
60 }
61
66 public static function getEngineCode($type)
67 {
68 return static::GROUP . '.' . $type;
69 }
70
77 public static function getAccount($type)
78 {
79 return Account::create($type, null, static::getInstance());
80 }
81
85 public static function getTypes()
86 {
87 return array(
88 static::TYPE_FACEBOOK,
89 static::TYPE_VKONTAKTE,
90 static::TYPE_VKADS,
91 static::TYPE_GOOGLE,
92 static::TYPE_INSTAGRAM,
93 static::TYPE_YANDEX,
94 );
95 }
96
103 public static function getAuthAdapter($type)
104 {
105 return Retargeting\AuthAdapter::create($type, static::getInstance());
106 }
107
114 public static function getProviders(array $types = null)
115 {
116 $providers = static::getServiceProviders($types);
117
118 return $providers;
119 }
120
121 protected static function isRegionRussian(bool $onlyRu = false): bool
122 {
123 $regions = $onlyRu ? ['ru'] : ['ru', 'kz', 'by'];
124
125 $region = \Bitrix\Main\Application::getInstance()->getLicense()->getRegion() ?: 'ru';
126 return in_array($region, $regions);
127 }
128
129 protected static function getServiceProviders(array $types = null)
130 {
131 $typeList = static::getTypes();
132
133 $providers = array();
134 foreach ($typeList as $type)
135 {
136 if ($types && !in_array($type, $types))
137 {
138 continue;
139 }
140
141 if (in_array($type, [static::TYPE_FACEBOOK, static::TYPE_INSTAGRAM]) && self::isRegionRussian(true))
142 {
143 continue;
144 }
145
146 $authAdapter = static::getInstance()->getAuthAdapter($type);
147 $account = static::getInstance()->getAccount($type);
148
149 $providers[$type] = array(
150 'TYPE' => $type,
151 'HAS_AUTH' => $authAdapter->hasAuth(),
152 'AUTH_URL' => $authAdapter->getAuthUrl(),
153 'HAS_ACCOUNTS' => $account->hasAccounts(),
154 'PROFILE' => $account->getProfileCached(),
155 'ENGINE_CODE' => static::getEngineCode($type),
156 'CLIENTS' => static::getClientsProfiles($authAdapter)
157 );
158
159 // check if no profile, then may be auth was removed in service
160 if ($providers[$type]['HAS_AUTH'] && empty($providers[$type]['PROFILE']))
161 {
162 static::removeAuth($type);
163 $providers[$type]['HAS_AUTH'] = false;
164 }
165 }
166
167 return $providers;
168 }
169
176 public static function getAccounts($type)
177 {
178 if (!static::canUse())
179 {
180 return array();
181 }
182
183 $result = array();
184
185 $account = static::getAccount($type);
186 $accountsResult = $account->getList();
187 if ($accountsResult->isSuccess())
188 {
189 while ($accountData = $accountsResult->fetch())
190 {
191 $accountData = $account->normalizeListRow($accountData);
192 if ($accountData['ID'])
193 {
194 $result[] = array(
195 'id' => $accountData['ID'],
196 'name' => $accountData['NAME'] ? $accountData['NAME'] : $accountData['ID']
197 );
198 }
199 }
200 }
201 else
202 {
203 self::$errors = $accountsResult->getErrorMessages();
204 }
205
206 return $result;
207 }
208
215 public static function removeAuth($type)
216 {
217 static::getInstance()->getAuthAdapter($type)->removeAuth();
218 }
219
225 public static function getErrors()
226 {
227 return self::$errors;
228 }
229
235 public static function resetErrors()
236 {
237 self::$errors = array();
238 }
239
245 public static function hasErrors()
246 {
247 return count(self::$errors) > 0;
248 }
249
254 public function getClientId()
255 {
256 return $this->clientId;
257 }
263 public function setClientId($clientId)
264 {
265 $this->clientId = $clientId;
266 return $this;
267 }
268
274 public static function getClientsProfiles(Retargeting\AuthAdapter $authAdapter)
275 {
276 $type = $authAdapter->getType();
277 return array_values(array_filter(array_map(function ($item) use ($type) {
278 $service = new static();
279 $service->setClientId($item['proxy_client_id']);
280
281 $authAdapter = Retargeting\AuthAdapter::create($type)->setService($service);
282
283 $account = Account::create($type)->setService($service);
284 $account->getRequest()->setAuthAdapter($authAdapter);
285
286 $profile = $account->getProfileCached();
287 if ($profile)
288 {
289 return $profile;
290 }
291 else
292 {
293 // if no profile, then may be auth was removed in service
294 $authAdapter->removeAuth();
295 return null;
296 }
297 }, $authAdapter->getAuthorizedClientsList())));
298 }
299
303 public static function getTypeByEngine(string $engineCode): ?string
304 {
305 foreach (static::getTypes() as $type)
306 {
307 if($engineCode == static::getEngineCode($type))
308 {
309 return $type;
310 }
311 }
312 return null;
313 }
314
318 public static function canUseAsInternal(): bool
319 {
320 return true;
321 }
322
326 public static function getMethodPrefix(): string
327 {
328 return 'analytics';
329 }
330}
static includeModule($moduleName)
Definition loader.php:69
static getClientsProfiles(Retargeting\AuthAdapter $authAdapter)
Definition service.php:274
static isRegionRussian(bool $onlyRu=false)
Definition service.php:121
static getProviders(array $types=null)
Definition service.php:114
static getEngineCode($type)
Definition service.php:66
static getTypeByEngine(string $engineCode)
Definition service.php:303
static getServiceProviders(array $types=null)
Definition service.php:129
static getAuthAdapter($type)
Definition service.php:103