21 private const ERROR_HANDLER_ADD =
'ERROR_HANDLER_ADD';
22 private const ERROR_HANDLER_UPDATE =
'ERROR_HANDLER_UPDATE';
23 private const ERROR_HANDLER_DELETE =
'ERROR_HANDLER_DELETE';
24 private const ERROR_CHECK_FAILURE =
'ERROR_CHECK_FAILURE';
25 private const ERROR_HANDLER_ALREADY_EXIST =
'ERROR_HANDLER_ALREADY_EXIST';
26 private const ERROR_HANDLER_NOT_FOUND =
'ERROR_HANDLER_NOT_FOUND';
33 private static function prepareHandlerParams(array $data, \CRestServer $server): array
36 $data[
'APP_ID'] = $server->getClientId();
48 private static function checkParamsOnAddHandler(array $params):
void
50 if (empty($params[
'NAME']))
52 throw new RestException(
'Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
55 if (empty($params[
'CODE']))
57 throw new RestException(
'Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
60 self::checkSettings($params[
'SETTINGS']);
61 self::checkProfiles($params[
'PROFILES']);
63 $deliveryRestHandler = Internals\DeliveryRestHandlerTable::getList([
65 '=CODE' => $params[
'CODE']
68 if ($deliveryRestHandler)
70 throw new RestException(
'Handler already exists!', self::ERROR_HANDLER_ALREADY_EXIST);
79 private static function checkParamsOnUpdateHandler(array $params):
void
81 if (empty($params[
'ID']))
83 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
86 if (array_key_exists(
'NAME', $params) && empty($params[
'NAME']))
88 throw new RestException(
'Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
91 if (array_key_exists(
'CODE', $params) && empty($params[
'CODE']))
93 throw new RestException(
'Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
96 if (array_key_exists(
'SETTINGS', $params))
98 self::checkSettings($params[
'SETTINGS']);
101 if (array_key_exists(
'PROFILES', $params))
103 self::checkProfiles($params[
'PROFILES']);
106 $deliveryRestHandler = Internals\DeliveryRestHandlerTable::getList(array(
108 'ID' => $params[
'ID']
111 if (!$deliveryRestHandler)
113 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
116 if ($params[
'APP_ID'] && !empty($deliveryRestHandler[
'APP_ID']) && $deliveryRestHandler[
'APP_ID'] !== $params[
'APP_ID'])
127 private static function checkParamsOnDeleteHandler($params):
void
129 if (empty($params[
'ID']))
131 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
134 $deliveryRestHandler = Internals\DeliveryRestHandlerTable::getList(array(
136 'ID' => $params[
'ID']
139 if (!$deliveryRestHandler)
141 throw new RestException(
'Handler not found', self::ERROR_HANDLER_NOT_FOUND);
144 if ($params[
'APP_ID'] && !empty($deliveryRestHandler[
'APP_ID']) && $deliveryRestHandler[
'APP_ID'] !== $params[
'APP_ID'])
149 $deliveryListResult = Sale\Delivery\Services\Manager::getList([
150 'select' => [
'ID',
'CLASS_NAME',
'CONFIG'],
155 $deliveryIdList = [];
156 while ($delivery = $deliveryListResult->fetch())
158 if ($delivery[
'CONFIG'][
'MAIN'][
'REST_CODE'] === $deliveryRestHandler[
'CODE'])
160 $deliveryIdList[] = $delivery[
'ID'];
167 'There are deliveries with this handler: '.implode(
', ', $deliveryIdList),
168 self::ERROR_HANDLER_DELETE
180 public static function addHandler($query, $n, \CRestServer $server)
183 $params = self::prepareHandlerParams($query, $server);
184 self::checkParamsOnAddHandler($params);
186 if (isset($params[
'SETTINGS'][
'CONFIG']) && is_array($params[
'SETTINGS'][
'CONFIG']))
188 $params[
'SETTINGS'][
'CONFIG'] = [
189 'ITEMS' => self::convertArrayForSaving($params[
'SETTINGS'][
'CONFIG'],
'[SETTINGS][CONFIG][]'),
194 'NAME' => $params[
'NAME'],
195 'CODE' => $params[
'CODE'],
196 'SORT' => $params[
'SORT'] ?: 100,
197 'DESCRIPTION' => $params[
'DESCRIPTION'],
198 'SETTINGS' => $params[
'SETTINGS'],
199 'PROFILES' => self::convertArrayForSaving($params[
'PROFILES'],
'[PROFILES][]'),
200 'APP_ID' => $params[
'APP_ID'],
203 $result = Internals\DeliveryRestHandlerTable::add($data);
204 if ($result->isSuccess())
206 return $result->getId();
209 $error = implode(
"\n", $result->getErrorMessages());
220 public static function updateHandler($query, $n, \CRestServer $server): bool
223 $params = self::prepareHandlerParams($query, $server);
225 self::checkParamsOnUpdateHandler($params);
228 foreach ([
'CODE',
'NAME',
'DESCRIPTION',
'SETTINGS',
'PROFILES'] as $field)
230 if (!array_key_exists($field, $params))
235 if ($field ===
'PROFILES')
237 $value = self::convertArrayForSaving($params[$field],
'[PROFILES][]');
239 elseif ($field ===
'SETTINGS')
241 if (isset($params[$field][
'CONFIG']) && is_array($params[$field][
'CONFIG']))
243 $params[$field][
'CONFIG'] = [
244 'ITEMS' => self::convertArrayForSaving($params[$field][
'CONFIG'],
'[SETTINGS][CONFIG][]'),
248 $value = $params[$field];
252 $value = $params[$field];
255 $fields[$field] = $value;
258 $result = Internals\DeliveryRestHandlerTable::update($params[
'ID'], $fields);
259 if ($result->isSuccess())
264 $error = implode(
"\n", $result->getErrorMessages());
275 public static function deleteHandler($query, $n, \CRestServer $server): bool
278 $params = self::prepareHandlerParams($query, $server);
280 self::checkParamsOnDeleteHandler($params);
282 $result = Internals\DeliveryRestHandlerTable::delete($params[
'ID']);
283 if ($result->isSuccess())
288 $error = implode(
"\n", $result->getErrorMessages());
304 $handlersList = array_values(
Sale\
Delivery\Services\Manager::getRestHandlerList());
305 foreach ($handlersList as $handler)
311 if (isset($handler[
'PROFILES']) && is_array($handler[
'PROFILES']))
313 $profiles = self::convertArrayForOutput($handler[
'PROFILES']);
315 $handler[
'PROFILES'] = $profiles;
320 $settings = $handler[
'SETTINGS'];
322 if (isset($settings[
'CONFIG'][
'ITEMS']) && is_array($settings[
'CONFIG'][
'ITEMS']))
324 $config = self::convertArrayForOutput($settings[
'CONFIG'][
'ITEMS']);
326 $settings[
'CONFIG'] = $config;
327 $handler[
'SETTINGS'] = $settings;
329 $result[] = $handler;
339 private static function checkProfiles($profiles): void
341 if (empty($profiles) || !is_array($profiles))
343 throw new RestException(
'Parameter PROFILES is not defined', self::ERROR_CHECK_FAILURE);
351 private static function checkSettings($settings): void
353 if (empty($settings) || !is_array($settings))
355 throw new RestException(
'Parameter SETTINGS is not defined', self::ERROR_CHECK_FAILURE);
358 if (empty($settings[
'CALCULATE_URL']))
360 throw new RestException(
361 'Parameter SETTINGS[CALCULATE_URL] is not defined',
362 self::ERROR_CHECK_FAILURE
365 elseif (!is_string($settings[
'CALCULATE_URL']))
367 throw new RestException(
368 'Parameter SETTINGS[CALCULATE_URL] must be of string type',
369 self::ERROR_CHECK_FAILURE
374 !empty($settings[
'CREATE_DELIVERY_REQUEST_URL'])
375 && !is_string($settings[
'CREATE_DELIVERY_REQUEST_URL'])
378 throw new RestException(
379 'Parameter SETTINGS[CREATE_DELIVERY_REQUEST_URL] must be of string type',
380 self::ERROR_CHECK_FAILURE
385 !empty($settings[
'CANCEL_DELIVERY_REQUEST_URL'])
386 && !is_string($settings[
'CANCEL_DELIVERY_REQUEST_URL'])
389 throw new RestException(
390 'Parameter SETTINGS[CANCEL_DELIVERY_REQUEST_URL] must be of string type',
391 self::ERROR_CHECK_FAILURE
395 if (empty($settings[
'CONFIG']) || !is_array($settings[
'CONFIG']))
397 throw new RestException(
'Parameter SETTINGS[CONFIG] is not defined', self::ERROR_CHECK_FAILURE);
407 private static function convertArrayForSaving(array $items,
string $path =
''): array
411 foreach ($items as $item)
413 if (!isset($item[
'CODE']))
415 throw new RestException(
416 ($path ===
'' ?
'' : $path .
' ') .
'Item CODE is not specified',
417 self::ERROR_CHECK_FAILURE
421 $profileCode = $item[
'CODE'];
422 unset($item[
'CODE']);
424 $result[$profileCode] = $item;
434 private static function convertArrayForOutput(array $items): array
438 foreach ($items as $profileCode => $item)
440 $item[
'CODE'] = $profileCode;
static includeModule($moduleName)