26 private static function getAppId()
28 if(!Loader::includeModule(
'socialservices'))
31 if(!self::isLinkedToNet())
34 $interface = new \CBitrix24NetOAuthInterface();
35 return $interface->getAppID();
45 private static function linkToNet()
47 if(!Loader::includeModule(
'socialservices'))
50 if(self::isLinkedToNet())
54 $request = \Bitrix\Main\Context::getCurrent()->getRequest();
55 $host = ($request->isHttps() ?
'https://' :
'http://').$request->getHttpHost();
56 $registerResult = \CSocServBitrix24Net::registerSite($host);
58 if(is_array($registerResult) && isset($registerResult[
"client_id"]) && isset($registerResult[
"client_secret"]))
60 Option::set(
'socialservices',
'bitrix24net_domain', $host);
61 Option::set(
'socialservices',
'bitrix24net_id', $registerResult[
"client_id"]);
62 Option::set(
'socialservices',
'bitrix24net_secret', $registerResult[
"client_secret"]);
73 private static function isLinkedToNet()
75 return Option::get(
'socialservices',
'bitrix24net_id',
'') !==
'';
84 public static function delete()
88 if(!Loader::includeModule(
'socialservices'))
90 $result->addError(
new Error(
'Module socialservices is not installed'));
94 if($connection = self::getFields())
96 $res = ApTable::delete($connection[
'ID']);
98 if(!$res->isSuccess())
99 $result->addErrors($res->getErrors());
101 $dbRes = ButtonTable::getList(array(
103 '=APP_ID' => $connection[
'ID']
107 while($but = $dbRes->fetch())
109 $res = ButtonTable::delete($but[
'ID']);
111 if(!$res->isSuccess())
112 $result->addErrors($res->getErrors());
129 $class =
'connector-btn-blue';
130 $href =
'javascript:void(0)';
131 $moduleAccess = $APPLICATION->GetGroupRight(
'b24connector');
136 if(!Loader::includeModule(
'socialservices') || $moduleAccess <=
"R")
138 $class .=
' connector-btn-blue-disabled';
142 if(!self::isLinkedToNet())
145 $hosts = self::getHostsList();
149 $urlTeml = self::getUrl(
'##HOST##');
153 $onclick =
'BX.B24Connector.showPortalChoosingDialog(\''.\CUtil::JSEscape($urlTeml).
'\',
'.\CUtil::PhpToJSObject($hosts).');
';
157 $onclick = 'alert(\
''.
Loc::getMessage(
'B24C_CONN_CONNECT_ERROR').
'\');
';
162 $href = self::getUrlNet();
166 $result = '<a href=
"'.htmlspecialcharsbx($href).'"'.
167 ($onclick <> '' ? ' onclick=
"'.$onclick.'"' : '').
168 ' class=
"'.$class.'" >
'.
180 public static function getOptionButtonHtml($title)
185 if(!\Bitrix\Main\Loader::includeModule('socialservices
'))
191 if(!self::isLinkedToNet())
194 $hosts = self::getHostsList();
198 $urlTeml = self::getUrl('##HOST##
');
202 $onclick = 'BX.B24Connector.showPortalChoosingDialog(\
''.\CUtil::JSEscape($urlTeml).
'\',
'.\CUtil::PhpToJSObject($hosts).');
';
206 $onclick = 'alert(\
''.\CUtil::JSEscape(
Loc::getMessage(
'B24C_CONN_CONNECT_ERROR')).
'\');
';
211 $onclick = 'window.location.href=
"'.\CUtil::JSEscape(self::getUrlNet()).'"';
215 return '<input type=
"button" onclick=
"'.htmlspecialcharsbx($onclick).'" value=
"'.$title.'"'.($disabled ? ' disabled
' : '').'>
';
222 public static function getFields()
224 static $result = null;
230 if(Loader::includeModule('socialservices
'))
231 $result = ApTable::getConnection();
234 return is_array($result) ? $result : array();
241 public static function getDomain()
243 $fields = self::getFields();
244 return !empty($fields['DOMAIN
']) ? $fields['DOMAIN
'] : '';
251 public static function isExist()
253 $fields = self::getFields();
254 return !empty($fields);
262 public static function isRestAvailable(): bool
264 static $result = null;
266 if ($result === null)
268 if (!Loader::includeModule('socialservices
'))
272 elseif (!$client = ApClient::init())
278 $cacheId = 'b24connector_rest_status
';
279 $cached = Cache::remember($cacheId, self::DEFAULT_CACHE_TTL, function () use ($client)
281 $response = $client->call('app.info
');
282 return (isset($response['result
']) && empty($response['error
']));
284 $result = (bool)$cached;
296 private static function getUrl($host)
303 if(!Loader::includeModule("socialservices"))
307 $appId = self::getAppID();
311 $result = $host.'apconnect/?client_id=
'.urlencode($appId).'&preset=ap&state=
'.urlencode(http_build_query(array(
312 'check_key
' => \CSocServAuthManager::GetUniqueKey(),
314 'backurl
' => $APPLICATION->GetCurPageParam(),
325 private static function getUrlNet()
327 if(!Loader::includeModule("socialservices"))
331 $appId = self::getAppID();
336 $result = \CBitrix24NetOAuthInterface::NET_URL.'/oauth/select/?preset=ap&client_id=
'.urlencode($appId).'&state=
'.urlencode(http_build_query(array(
337 'check_key
' => \CSocServAuthManager::GetUniqueKey(),
339 'backurl
' => $APPLICATION->GetCurPageParam('', array('apaction
', 'apID
')),
350 private static function getHostsList()
352 if(!Loader::includeModule('socialservices
'))
356 $query = \CBitrix24NetTransport::init();
359 $result = $query->call('admin.profile.list
', array());
361 return !empty($result['result
']) && is_array($result['result
']) ? $result['result
'] : array();
367 public static function getOpenLinesConfigUrl()
369 $res = self::getDataFromRest('imopenlines.config.path.get
', array('result
'));
371 if(is_array($res) && !empty($res['SERVER_ADDRESS
']) && !empty($res['PUBLIC_PATH
']))
373 $result = $res['SERVER_ADDRESS
'].$res['PUBLIC_PATH
'];
377 $domain = self::getDomain();
393 return self::getDataFromRest(
'voximplant.url.get', array(
'result',
'lines'),
'/telephony/lines.php');
401 return self::getDataFromRest(
'crm.webform.configuration.get', array(
'result',
'URL'),
'/crm/webform/');
409 return self::getDataFromRest(
'crm.sitebutton.configuration.get', array(
'result',
'URL'),
'/crm/button/');
412 private static function getDataFromRest($method, $pathToData, $defaultPath =
'')
414 if(!Loader::includeModule(
'socialservices'))
421 $result = $client->call($method);
423 if(is_array($result))
425 foreach($pathToData as $idx)
427 if(!empty($result[$idx]))
429 $result = $result[$idx];
440 if(is_array($result))
450 $result =
'https://'.htmlspecialcharsbx($domain).$defaultPath;
static get($moduleId, $name, $default="", $siteId=false)
static set($moduleId, $name, $value="", $siteId="")
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)