21 private const ERROR_CHECK_FAILURE =
'ERROR_CHECK_FAILURE';
22 private const ERROR_HANDLER_NOT_FOUND =
'ERROR_HANDLER_NOT_FOUND';
23 private const ERROR_DELIVERY_ADD =
'ERROR_DELIVERY_ADD';
24 private const ERROR_DELIVERY_UPDATE =
'ERROR_DELIVERY_UPDATE';
25 private const ERROR_DELIVERY_DELETE =
'ERROR_DELIVERY_DELETE';
26 private const ERROR_DELIVERY_CONFIG_UPDATE =
'ERROR_DELIVERY_CONFIG_UPDATE';
27 private const ERROR_DELIVERY_NOT_FOUND =
'ERROR_DELIVERY_NOT_FOUND';
29 private const ALLOWED_DELIVERY_FIELDS = [
45 private static function prepareDeliveryParams(array $data, \CRestServer $server): array
48 $data[
'APP_ID'] = $server->getClientId();
59 'LOGOTYPE' =>
'LOGOTIP',
69 'LOGOTIP' =>
'LOGOTYPE',
78 private static function checkParamsBeforeDeliveryAdd($params): void
80 if (empty($params[
'REST_CODE']))
82 throw new RestException(
'Parameter REST_CODE is not defined', self::ERROR_CHECK_FAILURE);
85 if (empty($params[
'NAME']))
87 throw new RestException(
'Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
90 if (empty($params[
'CONFIG']) || !is_array($params[
'CONFIG']))
92 throw new RestException(
'Parameter CONFIG is not defined', self::ERROR_CHECK_FAILURE);
95 if (empty($params[
'CURRENCY']))
97 throw new RestException(
'Parameter CURRENCY is not defined', self::ERROR_CHECK_FAILURE);
103 if ($params[
'APP_ID'] && !empty($handlerData[
'APP_ID']) && $handlerData[
'APP_ID'] !== $params[
'APP_ID'])
105 throw new AccessException();
110 throw new RestException(
111 'Handler "' . $params[
'REST_CODE'] .
'" not exists', self::ERROR_HANDLER_NOT_FOUND
116 private static function prepareParamsBeforeDeliveryAdd(array $params): array
118 $params[
'CONFIG'] = self::prepareIncomingConfig($params[
'CONFIG'], $params);
120 if (isset($params[
'LOGOTIP']))
122 $params[
'LOGOTIP'] = self::saveFile($params[
'LOGOTIP']);
133 private static function checkParamsBeforeDeliveryUpdate($params): void
135 if (empty($params[
'ID']))
137 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
140 $data = Delivery\Services\Manager::getById($params[
'ID']);
143 throw new RestException(
'Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
146 if (!self::hasAccessToDelivery($data, $params[
'APP_ID']))
148 throw new AccessException();
157 private static function checkParamsBeforeDeliveryDelete($params): void
159 if (empty($params[
'ID']))
161 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
164 $data = Delivery\Services\Manager::getById($params[
'ID']);
167 throw new RestException(
'Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
170 if (!self::hasAccessToDelivery($data, $params[
'APP_ID']))
172 throw new AccessException();
180 private static function checkParamsBeforeDeliveryConfigGet($params): void
182 if (empty($params[
'ID']))
184 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
187 $data = Delivery\Services\Manager::getById($params[
'ID']);
190 throw new RestException(
'Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
193 if (!self::hasAccessToDelivery($data, $params[
'APP_ID']))
195 throw new AccessException();
199 private static function checkParamsBeforeDeliveryConfigUpdate($params): void
201 if (empty($params[
'ID']))
203 throw new RestException(
'Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
206 if (empty($params[
'CONFIG']) || !is_array($params[
'CONFIG']))
208 throw new RestException(
'Parameter CONFIG is not defined', self::ERROR_CHECK_FAILURE);
211 $data = Delivery\Services\Manager::getById($params[
'ID']);
214 throw new RestException(
'Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
217 if (!self::hasAccessToDelivery($data, $params[
'APP_ID']))
219 throw new AccessException();
223 private static function saveFile($fileContent)
225 $file = \CRestUtil::saveFile($fileContent);
228 $file[
'MODULE_ID'] =
'sale';
229 return \CFile::SaveFile($file,
'sale');
242 public static function addDelivery($query, $n, \CRestServer $server)
245 $params = self::prepareDeliveryParams($query, $server);
246 self::checkParamsBeforeDeliveryAdd($params);
248 $params = self::prepareParamsBeforeDeliveryAdd($params);
251 'NAME' => $params[
'NAME'],
252 'DESCRIPTION' => $params[
'DESCRIPTION'] ??
'',
253 'CLASS_NAME' =>
'\\' . \Sale\Handlers\Delivery\RestHandler::class,
254 'CURRENCY' => $params[
'CURRENCY'],
255 'SORT' => $params[
'SORT'] ?? 100,
256 'ACTIVE' => $params[
'ACTIVE'] ??
'Y',
257 'CONFIG' => $params[
'CONFIG'],
258 'LOGOTIP' => $params[
'LOGOTIP'] ??
null,
261 $result = Delivery\Services\Manager::add($fields);
262 if ($result->isSuccess())
264 $parentDelivery = Delivery\Services\Manager::getList([
265 'select' => self::ALLOWED_DELIVERY_FIELDS,
266 'filter' => [
'=ID' => (
int)$result->getId()],
270 $profilesDeliveryList = Delivery\Services\Manager::getList([
271 'select' => self::ALLOWED_DELIVERY_FIELDS,
272 'filter' => [
'=PARENT_ID' => (
int)$result->getId()],
274 while ($profileDelivery = $profilesDeliveryList->fetch())
281 'profiles' => $profiles,
285 $error = implode(
"\n", $result->getErrorMessages());
299 $params = self::prepareDeliveryParams($query, $server);
300 self::checkParamsBeforeDeliveryUpdate($params);
303 if (isset($params[
'NAME']))
305 $fields[
'NAME'] = $params[
'NAME'];
308 if (isset($params[
'ACTIVE']))
310 $fields[
'ACTIVE'] = $params[
'ACTIVE'];
313 if (isset($params[
'DESCRIPTION']))
315 $fields[
'DESCRIPTION'] = $params[
'DESCRIPTION'];
318 if (isset($params[
'SORT']))
320 $fields[
'SORT'] = $params[
'SORT'];
323 if (isset($params[
'CURRENCY']))
325 $fields[
'CURRENCY'] = $params[
'CURRENCY'];
328 if (isset($params[
'LOGOTIP']))
330 $fields[
'LOGOTIP'] = self::saveFile($params[
'LOGOTIP']);
333 $result = Delivery\Services\Manager::update($params[
'ID'], $fields);
334 if ($result->isSuccess())
339 $error = implode(
"\n", $result->getErrorMessages());
340 throw new RestException($error, self::ERROR_DELIVERY_UPDATE);
353 $params = self::prepareDeliveryParams($query, $server);
354 self::checkParamsBeforeDeliveryDelete($params);
356 $result = Delivery\Services\Manager::delete($params[
'ID']);
357 if ($result->isSuccess())
362 $error = implode(
"\n", $result->getErrorMessages());
363 throw new RestException($error, self::ERROR_DELIVERY_DELETE);
376 self::checkParamsBeforeDeliveryListGet($params);
379 isset($params[
'SELECT']) && is_array($params[
'SELECT'])
380 ? array_flip(self::prepareIncomingParams(array_flip($params[
'SELECT'])))
381 : self::ALLOWED_DELIVERY_FIELDS
385 $filterFromParams = isset($params[
'FILTER']) && is_array($params[
'FILTER']) ? $params[
'FILTER'] : [];
386 if ($filterFromParams)
389 foreach ($filterFromParams as $rawName => $value)
391 $filterField = \CSqlUtil::GetFilterOperation($rawName);
392 $fieldName = $incomingFieldsMap[$filterField[
'FIELD']] ?? $filterField[
'FIELD'];
393 $filter[$filterField[
'OPERATION'] . $fieldName] = $value;
398 isset($params[
'ORDER']) && is_array($params[
'ORDER'])
404 $deliveryListResult = Delivery\Services\Manager::getList([
409 while ($delivery = $deliveryListResult->fetch())
421 private static function checkParamsBeforeDeliveryListGet(array $params)
423 $select = isset($params[
'SELECT']) && is_array($params[
'SELECT']) ? $params[
'SELECT'] : [];
426 $select = array_flip(self::prepareIncomingParams(array_flip($select)));
427 $diffSelect = array_diff($select, self::ALLOWED_DELIVERY_FIELDS);
431 throw new RestException(implode(
', ', $diffSelect) .
' not allowed for select');
435 $filter = isset($params[
'FILTER']) && is_array($params[
'FILTER']) ? $params[
'FILTER'] : [];
439 foreach ($filter as $rawName => $value)
441 $filterField = \CSqlUtil::GetFilterOperation($rawName);
442 if (isset($filterField[
'FIELD']))
444 $filterFields[] = $filterField[
'FIELD'];
448 $filterFields = array_flip(self::prepareIncomingParams(array_flip($filterFields)));
449 $diffFilter = array_diff($filterFields, self::ALLOWED_DELIVERY_FIELDS);
452 throw new RestException(implode(
', ', $diffFilter) .
' not allowed for filter');
457 isset($params[
'ORDER']) && is_array($params[
'ORDER'])
463 $diffOrder = array_diff(array_keys($order), self::ALLOWED_DELIVERY_FIELDS);
466 throw new RestException(implode(
', ', $diffOrder) .
' not allowed for order');
478 public static function getConfig($query, $n, \CRestServer $server): array
481 $params = self::prepareDeliveryParams($query, $server);
482 self::checkParamsBeforeDeliveryConfigGet($params);
486 $delivery = Delivery\Services\Manager::getById($params[
'ID']);
489 if (is_array($delivery[
'CONFIG']))
491 $delivery[
'CONFIG'] = self::prepareOutcomingConfig($delivery[
'CONFIG']);
494 $result = is_array($delivery[
'CONFIG']) ? $delivery[
'CONFIG'] : [];
507 public static function updateConfig($query, $n, \CRestServer $server): bool
510 $params = self::prepareDeliveryParams($query, $server);
511 self::checkParamsBeforeDeliveryConfigUpdate($params);
513 $data = Delivery\Services\Manager::getById($params[
'ID']);
515 $params[
'REST_CODE'] = $handlerCode;
517 $result = Delivery\Services\Manager::update(
520 'CONFIG' => self::prepareIncomingConfig($params[
'CONFIG'], $params)
523 if ($result->isSuccess())
528 $error = implode(
"\n", $result->getErrorMessages());
529 throw new RestException($error, self::ERROR_DELIVERY_CONFIG_UPDATE);
532 private static function prepareIncomingConfig(array $config, array $params): array
536 'REST_CODE' => $params[
'REST_CODE']
540 foreach ($config as $configItem)
542 $result[
'MAIN'][$configItem[
'CODE']] = $configItem[
'VALUE'];
552 private static function prepareOutcomingConfig(array $config): array
554 if (isset($config[
'MAIN'][
'REST_CODE']))
556 unset($config[
'MAIN'][
'REST_CODE']);
559 $configItems = isset($config[
'MAIN']) && is_array($config[
'MAIN']) ? $config[
'MAIN'] : [];
562 foreach ($configItems as $configItemCode => $configItemValue)
565 'CODE' => $configItemCode,
566 'VALUE' => $configItemValue,
static includeModule($moduleName)