• Модуль: rest
  • Путь к файлу: ~/bitrix/modules/rest/classes/general/rest_util.php
  • Класс: CRestUtil
  • Вызов: CRestUtil::InstallApp
static function InstallApp($code)
{
	$result = false;

	if(!BitrixRestOAuthService::getEngine()->isRegistered())
	{
		try
		{
			BitrixRestOAuthService::register();
			BitrixRestOAuthService::getEngine()->getClient()->getApplicationList();
		}
		catch(BitrixMainSystemException $e)
		{
			$result = array('error' => $e->getCode(), 'error_description' => $e->getMessage());
		}
	}

	if(BitrixRestOAuthService::getEngine()->isRegistered())
	{
		$appDetailInfo = BitrixRestMarketplaceClient::getInstall($code);

		if($appDetailInfo)
		{
			$appDetailInfo = $appDetailInfo['ITEMS'];
		}

		if($appDetailInfo)
		{
			$queryFields = array(
				'CLIENT_ID' => $appDetailInfo['APP_CODE'],
				'VERSION' => $appDetailInfo['VER'],
				'BY_SUBSCRIPTION' => $appDetailInfo['BY_SUBSCRIPTION'] === 'Y' ? 'Y' : 'N',
			);

			$installResult = BitrixRestOAuthService::getEngine()
				->getClient()
				->installApplication($queryFields);

			if($installResult['result'])
			{
				$appFields = array(
					'CLIENT_ID' => $installResult['result']['client_id'],
					'CODE' => $appDetailInfo['CODE'],
					'ACTIVE' => BitrixRestAppTable::ACTIVE,
					'INSTALLED' => !empty($appDetailInfo['INSTALL_URL'])
						? BitrixRestAppTable::NOT_INSTALLED
						: BitrixRestAppTable::INSTALLED,
					'URL' => $appDetailInfo['URL'],
					'URL_DEMO' => $appDetailInfo['DEMO_URL'],
					'URL_INSTALL' => $appDetailInfo['INSTALL_URL'],
					'VERSION' => $installResult['result']['version'],
					'SCOPE' => implode(',', $installResult['result']['scope']),
					'STATUS' => $installResult['result']['status'],
					'SHARED_KEY' => $appDetailInfo['SHARED_KEY'],
					'CLIENT_SECRET' => '',
					'APP_NAME' => $appDetailInfo['NAME'],
					'MOBILE' => $appDetailInfo['BXMOBILE'] == 'Y' ? BitrixRestAppTable::ACTIVE : BitrixRestAppTable::INACTIVE,
				);

				if(
					$appFields['STATUS'] === BitrixRestAppTable::STATUS_TRIAL
					|| $appFields['STATUS'] === BitrixRestAppTable::STATUS_PAID
				)
				{
					$appFields['DATE_FINISH'] = BitrixMainTypeDateTime::createFromTimestamp($installResult['result']['date_finish']);
				}
				else
				{
					$appFields['DATE_FINISH'] = '';
				}

				$existingApp = BitrixRestAppTable::getByClientId($appFields['CLIENT_ID']);

				if($existingApp)
				{
					$addResult = BitrixRestAppTable::update($existingApp['ID'], $appFields);
					BitrixRestAppLangTable::deleteByApp($existingApp['ID']);
				}
				else
				{
					$addResult = BitrixRestAppTable::add($appFields);
				}

				if($addResult->isSuccess())
				{
					$appId = $addResult->getId();
					if(is_array($appDetailInfo['MENU_TITLE']))
					{
						foreach($appDetailInfo['MENU_TITLE'] as $lang => $langName)
						{
							BitrixRestAppLangTable::add(array(
								'APP_ID' => $appId,
								'LANGUAGE_ID' => $lang,
								'MENU_NAME' => $langName
							));
						}
					}

					if($appDetailInfo["OPEN_API"] === "Y" && !empty($appFields["URL_INSTALL"]))
					{
						// checkCallback is already called inside checkFields
						$result = BitrixRestEventTable::add(array(
							"APP_ID" => $appId,
							"EVENT_NAME" => "ONAPPINSTALL",
							"EVENT_HANDLER" => $appFields["URL_INSTALL"],
						));
						if($result->isSuccess())
						{
							BitrixRestEventSender::bind('rest', 'OnRestAppInstall');
						}
					}

					BitrixRestAppTable::install($appId);

					$result = true;
				}
			}
		}
	}

	return $result;
}