Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
deliveryservice.php
1<?php
2
4
9
10if (!Main\Loader::includeModule('rest'))
11{
12 return;
13}
14
20{
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';
28
29 private const ALLOWED_DELIVERY_FIELDS = [
30 'ID',
31 'PARENT_ID',
32 'NAME',
33 'ACTIVE',
34 'DESCRIPTION',
35 'SORT',
36 'LOGOTIP',
37 'CURRENCY',
38 ];
39
45 private static function prepareDeliveryParams(array $data, \CRestServer $server): array
46 {
47 $data = self::prepareIncomingParams($data);
48 $data['APP_ID'] = $server->getClientId();
49
50 return $data;
51 }
52
56 protected static function getIncomingFieldsMap(): array
57 {
58 return [
59 'LOGOTYPE' => 'LOGOTIP',
60 ];
61 }
62
66 protected static function getOutcomingFieldsMap(): array
67 {
68 return [
69 'LOGOTIP' => 'LOGOTYPE',
70 ];
71 }
72
78 private static function checkParamsBeforeDeliveryAdd($params): void
79 {
80 if (empty($params['REST_CODE']))
81 {
82 throw new RestException('Parameter REST_CODE is not defined', self::ERROR_CHECK_FAILURE);
83 }
84
85 if (empty($params['NAME']))
86 {
87 throw new RestException('Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
88 }
89
90 if (empty($params['CONFIG']) || !is_array($params['CONFIG']))
91 {
92 throw new RestException('Parameter CONFIG is not defined', self::ERROR_CHECK_FAILURE);
93 }
94
95 if (empty($params['CURRENCY']))
96 {
97 throw new RestException('Parameter CURRENCY is not defined', self::ERROR_CHECK_FAILURE);
98 }
99
100 $handlerData = self::getHandlerData($params['REST_CODE']);
101 if ($handlerData)
102 {
103 if ($params['APP_ID'] && !empty($handlerData['APP_ID']) && $handlerData['APP_ID'] !== $params['APP_ID'])
104 {
105 throw new AccessException();
106 }
107 }
108 else
109 {
110 throw new RestException(
111 'Handler "' . $params['REST_CODE'] . '" not exists', self::ERROR_HANDLER_NOT_FOUND
112 );
113 }
114 }
115
116 private static function prepareParamsBeforeDeliveryAdd(array $params): array
117 {
118 $params['CONFIG'] = self::prepareIncomingConfig($params['CONFIG'], $params);
119
120 if (isset($params['LOGOTIP']))
121 {
122 $params['LOGOTIP'] = self::saveFile($params['LOGOTIP']);
123 }
124
125 return $params;
126 }
127
133 private static function checkParamsBeforeDeliveryUpdate($params): void
134 {
135 if (empty($params['ID']))
136 {
137 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
138 }
139
140 $data = Delivery\Services\Manager::getById($params['ID']);
141 if (!$data)
142 {
143 throw new RestException('Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
144 }
145
146 if (!self::hasAccessToDelivery($data, $params['APP_ID']))
147 {
148 throw new AccessException();
149 }
150 }
151
157 private static function checkParamsBeforeDeliveryDelete($params): void
158 {
159 if (empty($params['ID']))
160 {
161 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
162 }
163
164 $data = Delivery\Services\Manager::getById($params['ID']);
165 if (!$data)
166 {
167 throw new RestException('Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
168 }
169
170 if (!self::hasAccessToDelivery($data, $params['APP_ID']))
171 {
172 throw new AccessException();
173 }
174 }
175
180 private static function checkParamsBeforeDeliveryConfigGet($params): void
181 {
182 if (empty($params['ID']))
183 {
184 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
185 }
186
187 $data = Delivery\Services\Manager::getById($params['ID']);
188 if (!$data)
189 {
190 throw new RestException('Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
191 }
192
193 if (!self::hasAccessToDelivery($data, $params['APP_ID']))
194 {
195 throw new AccessException();
196 }
197 }
198
199 private static function checkParamsBeforeDeliveryConfigUpdate($params): void
200 {
201 if (empty($params['ID']))
202 {
203 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
204 }
205
206 if (empty($params['CONFIG']) || !is_array($params['CONFIG']))
207 {
208 throw new RestException('Parameter CONFIG is not defined', self::ERROR_CHECK_FAILURE);
209 }
210
211 $data = Delivery\Services\Manager::getById($params['ID']);
212 if (!$data)
213 {
214 throw new RestException('Delivery not found', self::ERROR_DELIVERY_NOT_FOUND);
215 }
216
217 if (!self::hasAccessToDelivery($data, $params['APP_ID']))
218 {
219 throw new AccessException();
220 }
221 }
222
223 private static function saveFile($fileContent)
224 {
225 $file = \CRestUtil::saveFile($fileContent);
226 if ($file)
227 {
228 $file['MODULE_ID'] = 'sale';
229 return \CFile::SaveFile($file, 'sale');
230 }
231
232 return null;
233 }
234
242 public static function addDelivery($query, $n, \CRestServer $server)
243 {
245 $params = self::prepareDeliveryParams($query, $server);
246 self::checkParamsBeforeDeliveryAdd($params);
247
248 $params = self::prepareParamsBeforeDeliveryAdd($params);
249
250 $fields = [
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,
259 ];
260
261 $result = Delivery\Services\Manager::add($fields);
262 if ($result->isSuccess())
263 {
264 $parentDelivery = Delivery\Services\Manager::getList([
265 'select' => self::ALLOWED_DELIVERY_FIELDS,
266 'filter' => ['=ID' => (int)$result->getId()],
267 ])->fetch();
268
269 $profiles = [];
270 $profilesDeliveryList = Delivery\Services\Manager::getList([
271 'select' => self::ALLOWED_DELIVERY_FIELDS,
272 'filter' => ['=PARENT_ID' => (int)$result->getId()],
273 ]);
274 while ($profileDelivery = $profilesDeliveryList->fetch())
275 {
276 $profiles[] = self::prepareOutcomingFields($profileDelivery);
277 }
278
279 return [
280 'parent' => $parentDelivery ? self::prepareOutcomingFields($parentDelivery) : null,
281 'profiles' => $profiles,
282 ];
283 }
284
285 $error = implode("\n", $result->getErrorMessages());
286 throw new RestException($error, self::ERROR_DELIVERY_ADD);
287 }
288
296 public static function updateDelivery($query, $n, \CRestServer $server): bool
297 {
299 $params = self::prepareDeliveryParams($query, $server);
300 self::checkParamsBeforeDeliveryUpdate($params);
301
302 $fields = [];
303 if (isset($params['NAME']))
304 {
305 $fields['NAME'] = $params['NAME'];
306 }
307
308 if (isset($params['ACTIVE']))
309 {
310 $fields['ACTIVE'] = $params['ACTIVE'];
311 }
312
313 if (isset($params['DESCRIPTION']))
314 {
315 $fields['DESCRIPTION'] = $params['DESCRIPTION'];
316 }
317
318 if (isset($params['SORT']))
319 {
320 $fields['SORT'] = $params['SORT'];
321 }
322
323 if (isset($params['CURRENCY']))
324 {
325 $fields['CURRENCY'] = $params['CURRENCY'];
326 }
327
328 if (isset($params['LOGOTIP']))
329 {
330 $fields['LOGOTIP'] = self::saveFile($params['LOGOTIP']);
331 }
332
333 $result = Delivery\Services\Manager::update($params['ID'], $fields);
334 if ($result->isSuccess())
335 {
336 return true;
337 }
338
339 $error = implode("\n", $result->getErrorMessages());
340 throw new RestException($error, self::ERROR_DELIVERY_UPDATE);
341 }
342
350 public static function deleteDelivery($query, $n, \CRestServer $server): bool
351 {
353 $params = self::prepareDeliveryParams($query, $server);
354 self::checkParamsBeforeDeliveryDelete($params);
355
356 $result = Delivery\Services\Manager::delete($params['ID']);
357 if ($result->isSuccess())
358 {
359 return true;
360 }
361
362 $error = implode("\n", $result->getErrorMessages());
363 throw new RestException($error, self::ERROR_DELIVERY_DELETE);
364 }
365
372 public static function getDeliveryList($query, $n, \CRestServer $server): array
373 {
375 $params = self::prepareIncomingParams($query);
376 self::checkParamsBeforeDeliveryListGet($params);
377
378 $select =
379 isset($params['SELECT']) && is_array($params['SELECT'])
380 ? array_flip(self::prepareIncomingParams(array_flip($params['SELECT'])))
381 : self::ALLOWED_DELIVERY_FIELDS
382 ;
383
384 $filter = [];
385 $filterFromParams = isset($params['FILTER']) && is_array($params['FILTER']) ? $params['FILTER'] : [];
386 if ($filterFromParams)
387 {
388 $incomingFieldsMap = self::getIncomingFieldsMap();
389 foreach ($filterFromParams as $rawName => $value)
390 {
391 $filterField = \CSqlUtil::GetFilterOperation($rawName);
392 $fieldName = $incomingFieldsMap[$filterField['FIELD']] ?? $filterField['FIELD'];
393 $filter[$filterField['OPERATION'] . $fieldName] = $value;
394 }
395 }
396
397 $order =
398 isset($params['ORDER']) && is_array($params['ORDER'])
399 ? self::prepareIncomingParams($params['ORDER'])
400 : []
401 ;
402
403 $result = [];
404 $deliveryListResult = Delivery\Services\Manager::getList([
405 'select' => $select,
406 'filter' => $filter,
407 'order' => $order,
408 ]);
409 while ($delivery = $deliveryListResult->fetch())
410 {
411 $result[] = self::prepareOutcomingFields($delivery);
412 }
413
414 return $result;
415 }
416
421 private static function checkParamsBeforeDeliveryListGet(array $params)
422 {
423 $select = isset($params['SELECT']) && is_array($params['SELECT']) ? $params['SELECT'] : [];
424 if ($select)
425 {
426 $select = array_flip(self::prepareIncomingParams(array_flip($select)));
427 $diffSelect = array_diff($select, self::ALLOWED_DELIVERY_FIELDS);
428
429 if ($diffSelect)
430 {
431 throw new RestException(implode(', ', $diffSelect) . ' not allowed for select');
432 }
433 }
434
435 $filter = isset($params['FILTER']) && is_array($params['FILTER']) ? $params['FILTER'] : [];
436 if ($filter)
437 {
438 $filterFields = [];
439 foreach ($filter as $rawName => $value)
440 {
441 $filterField = \CSqlUtil::GetFilterOperation($rawName);
442 if (isset($filterField['FIELD']))
443 {
444 $filterFields[] = $filterField['FIELD'];
445 }
446 }
447
448 $filterFields = array_flip(self::prepareIncomingParams(array_flip($filterFields)));
449 $diffFilter = array_diff($filterFields, self::ALLOWED_DELIVERY_FIELDS);
450 if ($diffFilter)
451 {
452 throw new RestException(implode(', ', $diffFilter) . ' not allowed for filter');
453 }
454 }
455
456 $order =
457 isset($params['ORDER']) && is_array($params['ORDER'])
458 ? self::prepareIncomingParams($params['ORDER'])
459 : []
460 ;
461 if ($order)
462 {
463 $diffOrder = array_diff(array_keys($order), self::ALLOWED_DELIVERY_FIELDS);
464 if ($diffOrder)
465 {
466 throw new RestException(implode(', ', $diffOrder) . ' not allowed for order');
467 }
468 }
469 }
470
478 public static function getConfig($query, $n, \CRestServer $server): array
479 {
481 $params = self::prepareDeliveryParams($query, $server);
482 self::checkParamsBeforeDeliveryConfigGet($params);
483
484 $result = [];
485
486 $delivery = Delivery\Services\Manager::getById($params['ID']);
487 if ($delivery)
488 {
489 if (is_array($delivery['CONFIG']))
490 {
491 $delivery['CONFIG'] = self::prepareOutcomingConfig($delivery['CONFIG']);
492 }
493
494 $result = is_array($delivery['CONFIG']) ? $delivery['CONFIG'] : [];
495 }
496
497 return $result;
498 }
499
507 public static function updateConfig($query, $n, \CRestServer $server): bool
508 {
510 $params = self::prepareDeliveryParams($query, $server);
511 self::checkParamsBeforeDeliveryConfigUpdate($params);
512
513 $data = Delivery\Services\Manager::getById($params['ID']);
514 $handlerCode = self::getRestCodeFromConfig($data['CONFIG']);
515 $params['REST_CODE'] = $handlerCode;
516
517 $result = Delivery\Services\Manager::update(
518 $params['ID'],
519 [
520 'CONFIG' => self::prepareIncomingConfig($params['CONFIG'], $params)
521 ]
522 );
523 if ($result->isSuccess())
524 {
525 return true;
526 }
527
528 $error = implode("\n", $result->getErrorMessages());
529 throw new RestException($error, self::ERROR_DELIVERY_CONFIG_UPDATE);
530 }
531
532 private static function prepareIncomingConfig(array $config, array $params): array
533 {
534 $result = [
535 'MAIN' => [
536 'REST_CODE' => $params['REST_CODE']
537 ],
538 ];
539
540 foreach ($config as $configItem)
541 {
542 $result['MAIN'][$configItem['CODE']] = $configItem['VALUE'];
543 }
544
545 return $result;
546 }
547
552 private static function prepareOutcomingConfig(array $config): array
553 {
554 if (isset($config['MAIN']['REST_CODE']))
555 {
556 unset($config['MAIN']['REST_CODE']);
557 }
558
559 $configItems = isset($config['MAIN']) && is_array($config['MAIN']) ? $config['MAIN'] : [];
560
561 $result = [];
562 foreach ($configItems as $configItemCode => $configItemValue)
563 {
564 $result[] = [
565 'CODE' => $configItemCode,
566 'VALUE' => $configItemValue,
567 ];
568 }
569
570 return $result;
571 }
572}
static includeModule($moduleName)
Definition loader.php:69
static prepareIncomingParams(array $data)
static getRestCodeFromConfig(array $config)
static prepareOutcomingFields(array $data)
static getConfig($query, $n, \CRestServer $server)
static addDelivery($query, $n, \CRestServer $server)
static getDeliveryList($query, $n, \CRestServer $server)
static deleteDelivery($query, $n, \CRestServer $server)
static updateDelivery($query, $n, \CRestServer $server)
static updateConfig($query, $n, \CRestServer $server)