27 static::SCOPE_PLACEMENT => array(
28 'placement.list' => array(
29 'callback' => array(__CLASS__,
'getList'),
32 'placement.bind' => array(
33 'callback' => array(__CLASS__,
'bind'),
36 'placement.unbind' => array(
37 'callback' => array(__CLASS__,
'unbind'),
40 'placement.get' => array(
41 'callback' => array(__CLASS__,
'get'),
49 public static function getList($query, $n, \CRestServer $server)
51 if($server->getAuthType() !== Auth::AUTH_TYPE)
58 $serviceDescription = $server->getServiceDescription();
60 $scopeList = array(\CRestUtil::GLOBAL_SCOPE);
62 $query = array_change_key_case($query, CASE_UPPER);
64 if(isset($query[
'SCOPE']))
66 if($query[
'SCOPE'] !=
'')
68 $scopeList = array($query[
'SCOPE']);
71 elseif($query[
'FULL'] ==
true)
73 $scopeList = array_keys($serviceDescription);
77 $scopeList = static::getScope($server);
78 $scopeList[] = \CRestUtil::GLOBAL_SCOPE;
81 $placementList = static::getPlacementList($server, $scopeList);
83 foreach($placementList as $placement => $placementInfo)
85 if(!$placementInfo[
'private'])
87 $result[] = $placement;
94 public static function bind($params, $n, \CRestServer $server)
96 static::checkPermission($server);
98 $params = array_change_key_case($params, CASE_UPPER);
100 $placement = toUpper($params[
'PLACEMENT']);
101 $placementHandler = $params[
'HANDLER'];
113 if($placementHandler ==
'')
118 $appInfo = static::getApplicationInfo($server);
121 $scopeList = static::getScope($server);
122 $scopeList[] = \CRestUtil::GLOBAL_SCOPE;
124 $placementList = static::getPlacementList($server, $scopeList);
125 $placementInfo = $placementList[$placement];
127 if (is_array($placementInfo) && (!isset($placementInfo[
'private']) || !$placementInfo[
'private']))
129 $placementLangList = [];
130 $paramsOptions = $params[
'OPTIONS'] ?? [];
131 $placementInfoOptions = $placementInfo[
'options'] ?? [];
133 $placementBind = array(
134 'APP_ID' => $appInfo[
'ID'],
136 'PLACEMENT' => $placement,
137 'PLACEMENT_HANDLER' => $placementHandler,
138 'OPTIONS' => static::prepareOptions($paramsOptions, $placementInfoOptions),
143 && $placementInfo[
'user_mode'] !==
true
147 'User mode is not available.',
153 $langDefault = reset($langList);
155 if (empty($params[
'LANG_ALL']))
157 if (!empty($params[
'TITLE']))
159 $placementLangList[$langDefault][
'TITLE'] = trim($params[
'TITLE']);
162 if (!empty($params[
'DESCRIPTION']))
164 $placementLangList[$langDefault][
'DESCRIPTION'] = trim($params[
'DESCRIPTION']);
167 if (!empty($params[
'GROUP_NAME']))
169 $placementLangList[$langDefault][
'GROUP_NAME'] = trim($params[
'GROUP_NAME']);
179 foreach ($params[
'LANG_ALL'] as $langCode => $langItem)
181 foreach ($fieldList as $field)
183 $placementLangList[$langCode][$field] = trim($langItem[$field] ??
'');
188 $placementBind[
'LANG_ALL'] = $placementLangList;
190 unset($placementBind[
'LANG_ALL']);
192 if ($placementInfo[
'max_count'] > 0)
195 '=APP_ID' => $placementBind[
'APP_ID'],
196 '=PLACEMENT' => $placementBind[
'PLACEMENT'],
198 if ($placementInfo[
'user_mode'] ===
true)
200 $filter[
'=USER_ID'] = [
202 (int)$placementBind[
'USER_ID'],
206 $res = PlacementTable::getList(
213 new ExpressionField(
'COUNT',
'COUNT(*)'),
218 if ($result = $res->fetch())
220 if ($result[
'COUNT'] >= $placementInfo[
'max_count'])
223 'Placement max count: ' . $placementInfo[
'max_count'],
231 array_key_exists(
'ICON', $params)
232 && is_array($params[
'ICON'])
233 && $params[
'ICON'][
'fileData']
234 && ($file = \CRestUtil::saveFile($params[
'ICON'][
'fileData']))
237 $placementBind[
'ICON'] = $file;
239 if (!empty($placementInfo[
'registerCallback'][
'callback']))
242 $placementInfo[
'registerCallback'][
'moduleId']
243 && Loader::includeModule($placementInfo[
'registerCallback'][
'moduleId'])
244 && is_callable($placementInfo[
'registerCallback'][
'callback'])
247 $resultCallback = call_user_func(
248 $placementInfo[
'registerCallback'][
'callback'],
252 if (!empty($resultCallback[
'error']) && !empty($resultCallback[
'error_description']))
254 return $resultCallback;
259 $result = PlacementTable::add($placementBind);
260 if ($result->isSuccess())
262 $placementId = $result->getId();
263 if (empty($placementLangList))
266 if (!empty($app[
'APP_NAME']))
268 $placementLangList[$langDefault] = [
269 'TITLE' => $app[
'APP_NAME']
273 foreach ($placementLangList as $langId => $data)
275 $data[
'PLACEMENT_ID'] = $placementId;
276 $data[
'LANGUAGE_ID'] = $langId;
278 if (!$res->isSuccess())
280 $errorMessage = $res->getErrorMessages();
282 'Unable to set placements language: ' . implode(
', ', $errorMessage),
290 $errorMessage = $result->getErrorMessages();
292 'Unable to set placement handler: ' . implode(
', ', $errorMessage),
301 'Placement not found',
306 private static function prepareOptions($paramsOptions = [], $placementInfoOptions = []): array
309 if (empty($placementInfoOptions))
313 $requiredOptions = self::getRequiredOptions($placementInfoOptions);
314 $defaultOptions = self::getDefaultOptions($placementInfoOptions);
316 if (!is_array($paramsOptions))
318 if (!empty($requiredOptions))
323 return $defaultOptions;
326 self::checkRequiredOptionsInParamsOptions($paramsOptions, $requiredOptions);
328 foreach ($placementInfoOptions as $optionName => $optionSetting)
330 $optionValue = $paramsOptions[$optionName] ?? $defaultOptions[$optionName] ??
null;
333 if (!is_array($optionSetting))
335 $optionType = $optionSetting;
339 $optionType = $optionSetting[
'type'];
345 $result[$optionName] = (int)$optionValue;
349 $result[$optionName] = (string)$optionValue;
353 if (!is_array($optionValue))
355 throw new ArgumentTypeException($optionName,
'array');
357 $result[$optionName] = self::prepareCompositeOptions($optionValue, $optionSetting);
373 private static function prepareCompositeOptions(array $paramOptionData, array $optionSetting): array
376 if (!is_array($optionSetting[
'typeValue']))
378 throw new ArgumentTypeException(
'typeValue',
'array');
381 $allowedTypes = [
'integer',
'string',
'array'];
382 $optionSetting[
'typeValue'] = str_replace(
'int',
'integer', $optionSetting[
'typeValue']);
383 $optionSetting[
'typeValue'] = array_intersect($optionSetting[
'typeValue'], $allowedTypes);
386 foreach ($paramOptionData as $keyOption => $valueOption)
388 $typeValue = gettype($valueOption);
390 if (in_array($typeValue, $optionSetting[
'typeValue']) && $typeValue !==
'array')
392 $result[$keyOption] = $valueOption;
397 foreach ($optionSetting as $keySetting => $valueSetting)
400 if ($keySetting ===
'type' || $keySetting ===
'typeValue')
405 $typeValueSetting = gettype($valueSetting);
408 $typeValueSetting ===
'array'
409 && in_array(
'array', $optionSetting[
'typeValue'])
410 && isset($valueSetting[
'type'])
411 && isset($valueSetting[
'typeValue'])
412 && isset($paramOptionData[$keySetting])
415 $result[$keySetting] = self::prepareCompositeOptions($paramOptionData[$keySetting], $valueSetting);
438 private static function getDefaultOptions(array $placementInfoOptions): array
442 foreach ($placementInfoOptions as $optionName => $optionSetting)
444 if (isset($optionSetting[
'default']))
446 $result[$optionName] = $optionSetting[
'default'];
459 private static function checkRequiredOptionsInParamsOptions(array $paramsOptions, array $requiredOptions): void
461 foreach ($requiredOptions as $requiredOption)
463 if (!array_key_exists($requiredOption, $paramsOptions))
465 throw new ArgumentNullException($requiredOption);
476 private static function getRequiredOptions(array $placementInfoOptions): array
479 foreach ($placementInfoOptions as $optionName => $optionSettings)
481 if (self::isRequiredOption($optionSettings))
483 $result[] = $optionName;
494 private static function isRequiredOption($optionSettings): bool
496 if (!isset($optionSettings[
'require']))
501 return (
bool)$optionSettings[
'require'];
504 public static function unbind($params, $n, \CRestServer $server)
506 static::checkPermission($server);
508 $params = array_change_key_case($params, CASE_UPPER);
510 if (!is_string($params[
'PLACEMENT']))
515 $placement = toUpper($params[
'PLACEMENT']);
516 $placementHandler = $params[
'HANDLER'];
518 if ($placement ==
'')
525 $placementList = static::getPlacementList($server);
527 if (array_key_exists($placement, $placementList) && !$placementList[$placement][
'private'])
529 $appInfo = static::getApplicationInfo($server);
532 '=APP_ID' => $appInfo[
"ID"],
533 '=PLACEMENT' => $placement,
536 if (array_key_exists(
'USER_ID', $params))
538 $filter[
'USER_ID'] = (int)$params[
'USER_ID'];
541 if($placementHandler <>
'')
543 $filter[
'=PLACEMENT_HANDLER'] = $placementHandler;
546 $dbRes = PlacementTable::getList(array(
550 while($placementHandler = $dbRes->fetch())
553 $result = PlacementTable::delete($placementHandler[
"ID"]);
554 if($result->isSuccess())
561 return array(
'count' => $cnt);
565 public static function get($params, $n, \CRestServer $server)
567 static::checkPermission($server);
571 $appInfo = static::getApplicationInfo($server);
573 $dbRes = PlacementTable::getList(array(
575 "=APP_ID" => $appInfo[
"ID"],
582 $placementList = static::getPlacementList($server);
584 foreach ($dbRes->fetchCollection() as $placement)
587 array_key_exists($placement->getPlacement(), $placementList)
588 && !$placementList[$placement->getPlacement()][
'private']
592 $placement->fillLangAll();
593 if (!is_null($placement->getLangAll()))
595 foreach ($placement->getLangAll() as $lang)
597 $langList[$lang->getLanguageId()] = [
598 'TITLE' => $lang->getTitle(),
599 'DESCRIPTION' => $lang->getDescription(),
600 'GROUP_NAME' => $lang->getGroupName(),
605 'placement' => $placement->getPlacement(),
606 'userId' => $placement->getUserId(),
607 'handler' => $placement->getPlacementHandler(),
608 'options' => $placement->getOptions(),
609 'title' => $placement->getTitle(),
610 'description' => $placement->getComment(),
611 'langAll' => $langList,
621 if($server->getAuthType() !== Auth::AUTH_TYPE)
626 if(!\CRestUtil::isAdmin())
632 protected static function getScope(\CRestServer $server)
636 $authData = $server->getAuthData();
638 $scopeList = explode(
',', $authData[
'scope']);
640 $serviceDescription = $server->getServiceDescription();
641 foreach($scopeList as $scope)
643 if(array_key_exists($scope, $serviceDescription))
654 if($server->getAuthType() !== Auth::AUTH_TYPE)
664 $serviceDescription = $server->getServiceDescription();
666 if($scopeList ===
null)
668 $scopeList = array_keys($serviceDescription);
673 foreach($scopeList as $scope)
676 isset($serviceDescription[$scope])
677 && is_array($serviceDescription[$scope][\CRestUtil::PLACEMENTS])
680 $result = array_merge($result, $serviceDescription[$scope][\CRestUtil::PLACEMENTS]);