2namespace Bitrix\Rest\Api;
5use Bitrix\Main\Application;
6use Bitrix\Main\ArgumentException;
7use Bitrix\Main\ArgumentNullException;
8use Bitrix\Main\Entity\ExpressionField;
10use Bitrix\Main\Loader;
11use Bitrix\Main\Result;
12use Bitrix\Rest\AccessException;
13use Bitrix\Rest\AppTable;
14use Bitrix\Rest\AuthTypeException;
15use Bitrix\Rest\HandlerHelper;
16use Bitrix\Rest\OAuth\Auth;
17use Bitrix\Rest\PlacementLangTable;
18use Bitrix\Rest\PlacementTable;
19use Bitrix\Rest\RestException;
20use Bitrix\Rest\Exceptions;
22use Bitrix\Main\ArgumentTypeException;
31 static::SCOPE_PLACEMENT =>
array(
32 'placement.list' =>
array(
33 'callback' =>
array(__CLASS__,
'getList'),
36 'placement.bind' =>
array(
37 'callback' =>
array(__CLASS__,
'bind'),
40 'placement.unbind' =>
array(
41 'callback' =>
array(__CLASS__,
'unbind'),
44 'placement.get' =>
array(
45 'callback' =>
array(__CLASS__,
'get'),
64 $scopeList =
array(\CRestUtil::GLOBAL_SCOPE);
77 $scopeList = array_keys($serviceDescription);
81 $scopeList = static::getScope($server);
82 $scopeList[] = \CRestUtil::GLOBAL_SCOPE;
85 $placementList = static::getPlacementList($server, $scopeList);
87 foreach($placementList as $placement => $placementInfo)
89 if(!$placementInfo[
'private'])
100 static::checkPermission($server);
104 if(!is_string(
$params[
'PLACEMENT']))
109 $placement = mb_strtoupper(
$params[
'PLACEMENT']);
110 $placementHandler =
$params[
'HANDLER'];
122 if($placementHandler ==
'')
127 $appInfo = static::getApplicationInfo($server);
130 $scopeList = static::getScope($server);
131 $scopeList[] = \CRestUtil::GLOBAL_SCOPE;
133 $placementList = static::getPlacementList($server, $scopeList);
134 $placementInfo = $placementList[$placement];
136 if (is_array($placementInfo) && (!isset($placementInfo[
'private']) || !$placementInfo[
'private']))
138 $placementLangList = [];
139 $paramsOptions =
$params[
'OPTIONS'] ?? [];
140 $placementInfoOptions = $placementInfo[
'options'] ?? [];
142 $placementBind =
array(
143 'APP_ID' => $appInfo[
'ID'],
145 'PLACEMENT' => $placement,
146 'PLACEMENT_HANDLER' => $placementHandler,
147 'OPTIONS' => static::prepareOptions($paramsOptions, $placementInfoOptions),
152 && $placementInfo[
'user_mode'] !==
true
156 'User mode is not available.',
162 $langDefault = reset($langList);
164 if (empty(
$params[
'LANG_ALL']))
168 $placementLangList[$langDefault][
'TITLE'] = trim(
$params[
'TITLE']);
171 if (!empty(
$params[
'DESCRIPTION']))
173 $placementLangList[$langDefault][
'DESCRIPTION'] = trim(
$params[
'DESCRIPTION']);
176 if (!empty(
$params[
'GROUP_NAME']))
178 $placementLangList[$langDefault][
'GROUP_NAME'] = trim(
$params[
'GROUP_NAME']);
188 foreach (
$params[
'LANG_ALL'] as $langCode => $langItem)
190 foreach ($fieldList as $field)
192 $placementLangList[$langCode][$field] = trim($langItem[$field] ??
'');
197 $placementBind[
'LANG_ALL'] = $placementLangList;
199 unset($placementBind[
'LANG_ALL']);
201 if ($placementInfo[
'max_count'] > 0)
204 '=APP_ID' => $placementBind[
'APP_ID'],
205 '=PLACEMENT' => $placementBind[
'PLACEMENT'],
207 if ($placementInfo[
'user_mode'] ===
true)
211 (int)$placementBind[
'USER_ID'],
215 $res = PlacementTable::getList(
229 if (
$result[
'COUNT'] >= $placementInfo[
'max_count'])
232 'Placement max count: ' . $placementInfo[
'max_count'],
240 array_key_exists(
'ICON',
$params)
243 && ($file = \CRestUtil::saveFile(
$params[
'ICON'][
'fileData']))
246 $placementBind[
'ICON'] = $file;
248 if (!empty($placementInfo[
'registerCallback'][
'callback']))
251 $placementInfo[
'registerCallback'][
'moduleId']
252 && Loader::includeModule($placementInfo[
'registerCallback'][
'moduleId'])
253 && is_callable($placementInfo[
'registerCallback'][
'callback'])
256 $resultCallback = call_user_func(
257 $placementInfo[
'registerCallback'][
'callback'],
261 if (!empty($resultCallback[
'error']) && !empty($resultCallback[
'error_description']))
263 return $resultCallback;
268 $lockKey = implode(
'|', [
269 $placementBind[
'APP_ID'],
270 $placementBind[
'PLACEMENT'],
271 $placementBind[
'PLACEMENT_HANDLER']
274 if (Application::getConnection()->lock($lockKey))
276 $result = PlacementTable::add($placementBind);
277 Application::getConnection()->unlock($lockKey);
281 $result = (
new Result())->addError(
new Error(
'Process of binding the handler has already started'));
286 $placementId =
$result->getId();
287 if (empty($placementLangList))
290 if (!empty(
$app[
'APP_NAME']))
292 $placementLangList[$langDefault] = [
293 'TITLE' =>
$app[
'APP_NAME']
297 foreach ($placementLangList as $langId =>
$data)
299 $data[
'PLACEMENT_ID'] = $placementId;
300 $data[
'LANGUAGE_ID'] = $langId;
302 if (!
$res->isSuccess())
306 'Unable to set placements language: ' . implode(
', ',
$errorMessage),
316 'Unable to set placement handler: ' . implode(
', ',
$errorMessage),
325 'Placement not found',
330 private static function prepareOptions($paramsOptions = [], $placementInfoOptions = []):
array
333 if (empty($placementInfoOptions))
337 $requiredOptions = self::getRequiredOptions($placementInfoOptions);
338 $defaultOptions = self::getDefaultOptions($placementInfoOptions);
340 if (!is_array($paramsOptions))
342 if (!empty($requiredOptions))
347 return $defaultOptions;
350 self::checkRequiredOptionsInParamsOptions($paramsOptions, $requiredOptions);
352 foreach ($placementInfoOptions as
$optionName => $optionSetting)
357 if (!is_array($optionSetting))
359 $optionType = $optionSetting;
363 $optionType = $optionSetting[
'type'];
379 throw new ArgumentTypeException(
$optionName,
'array');
397 private static function prepareCompositeOptions(
array $paramOptionData,
array $optionSetting):
array
400 if (!is_array($optionSetting[
'typeValue']))
402 throw new ArgumentTypeException(
'typeValue',
'array');
405 $allowedTypes = [
'integer',
'string',
'array'];
406 $optionSetting[
'typeValue'] = str_replace(
'int',
'integer', $optionSetting[
'typeValue']);
407 $optionSetting[
'typeValue'] = array_intersect($optionSetting[
'typeValue'], $allowedTypes);
410 foreach ($paramOptionData as $keyOption => $valueOption)
412 $typeValue = gettype($valueOption);
414 if (in_array($typeValue, $optionSetting[
'typeValue']) && $typeValue !==
'array')
416 $result[$keyOption] = $valueOption;
421 foreach ($optionSetting as $keySetting => $valueSetting)
424 if ($keySetting ===
'type' || $keySetting ===
'typeValue')
429 $typeValueSetting = gettype($valueSetting);
432 $typeValueSetting ===
'array'
433 && in_array(
'array', $optionSetting[
'typeValue'])
434 && isset($valueSetting[
'type'])
435 && isset($valueSetting[
'typeValue'])
436 && isset($paramOptionData[$keySetting])
439 $result[$keySetting] = self::prepareCompositeOptions($paramOptionData[$keySetting], $valueSetting);
462 private static function getDefaultOptions(
array $placementInfoOptions):
array
466 foreach ($placementInfoOptions as
$optionName => $optionSetting)
468 if (isset($optionSetting[
'default']))
483 private static function checkRequiredOptionsInParamsOptions(
array $paramsOptions,
array $requiredOptions): void
485 foreach ($requiredOptions as $requiredOption)
487 if (!array_key_exists($requiredOption, $paramsOptions))
489 throw new ArgumentNullException($requiredOption);
500 private static function getRequiredOptions(
array $placementInfoOptions):
array
503 foreach ($placementInfoOptions as
$optionName => $optionSettings)
505 if (self::isRequiredOption($optionSettings))
518 private static function isRequiredOption($optionSettings): bool
520 if (!isset($optionSettings[
'require']))
525 return (
bool)$optionSettings[
'require'];
530 static::checkPermission($server);
534 if (!is_string(
$params[
'PLACEMENT']))
539 $placement = mb_strtoupper(
$params[
'PLACEMENT']);
540 $placementHandler =
$params[
'HANDLER'];
542 if ($placement ==
'')
549 $placementList = static::getPlacementList($server);
551 if (array_key_exists($placement, $placementList) && !$placementList[$placement][
'private'])
553 $appInfo = static::getApplicationInfo($server);
556 '=APP_ID' => $appInfo[
"ID"],
557 '=PLACEMENT' => $placement,
560 if (array_key_exists(
'USER_ID',
$params))
565 if($placementHandler <>
'')
567 $filter[
'=PLACEMENT_HANDLER'] = $placementHandler;
574 while($placementHandler =
$dbRes->fetch())
577 $result = PlacementTable::delete($placementHandler[
"ID"]);
585 return array(
'count' => $cnt);
591 static::checkPermission($server);
595 $appInfo = static::getApplicationInfo($server);
599 "=APP_ID" => $appInfo[
"ID"],
606 $placementList = static::getPlacementList($server);
608 foreach (
$dbRes->fetchCollection() as $placement)
611 array_key_exists($placement->getPlacement(), $placementList)
612 && !$placementList[$placement->getPlacement()][
'private']
616 $placement->fillLangAll();
617 if (!is_null($placement->getLangAll()))
619 foreach ($placement->getLangAll() as
$lang)
621 $langList[
$lang->getLanguageId()] = [
622 'TITLE' =>
$lang->getTitle(),
623 'DESCRIPTION' =>
$lang->getDescription(),
624 'GROUP_NAME' =>
$lang->getGroupName(),
629 'placement' => $placement->getPlacement(),
630 'userId' => $placement->getUserId(),
631 'handler' => $placement->getPlacementHandler(),
632 'options' => $placement->getOptions(),
633 'title' => $placement->getTitle(),
634 'description' => $placement->getComment(),
635 'langAll' => $langList,
650 if(!\CRestUtil::isAdmin())
662 $scopeList = explode(
',', $authData[
'scope']);
665 foreach($scopeList as $scope)
667 if(array_key_exists($scope, $serviceDescription))
690 if($scopeList ===
null)
692 $scopeList = array_keys($serviceDescription);
697 foreach($scopeList as $scope)
700 isset($serviceDescription[$scope])
701 && is_array($serviceDescription[$scope][\CRestUtil::PLACEMENTS])
704 $result = array_merge(
$result, $serviceDescription[$scope][\CRestUtil::PLACEMENTS]);
static onRestServiceBuildDescription()
static getScope(\CRestServer $server)
static getPlacementList(\CRestServer $server, $scopeList=null)
static getApplicationInfo(\CRestServer $server)
static checkPermission(\CRestServer $server)
static getList($query, $n, \CRestServer $server)
static unbind($params, $n, \CRestServer $server)
static bind($params, $n, \CRestServer $server)
static getByClientId($clientId)
static checkCallback($handlerUrl, $appInfo=array(), $checkInstallUrl=true)
static mergeFromLangAll($data)
const ERROR_PLACEMENT_MAX_COUNT
const ERROR_PLACEMENT_NOT_FOUND
const ERROR_PLACEMENT_USER_MODE
const DEFAULT_USER_ID_VALUE
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
if(!defined('SITE_ID')) $lang
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']