1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
handlerservice.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\Cashbox\Rest;
4
5use Bitrix\Main;
6use Bitrix\Rest\AccessException;
7use Bitrix\Rest\RestException;
8use Bitrix\Sale\Cashbox\CashboxRest;
9use Bitrix\Sale\Cashbox\Manager;
10use Bitrix\Sale\Internals\CashboxRestHandlerTable;
11use Bitrix\Sale\Helpers;
12
13if (!Main\Loader::includeModule('rest'))
14{
15 return;
16}
17
23{
24 private const ERROR_HANDLER_ALREADY_EXISTS = 'ERROR_HANDLER_ALREADY_EXIST';
25 private const ERROR_HANDLER_NOT_FOUND = 'ERROR_HANDLER_NOT_FOUND';
26 private const ERROR_HANDLER_ADD = 'ERROR_HANDLER_ADD';
27 private const ERROR_HANDLER_UPDATE = 'ERROR_HANDLER_UPDATE';
28 private const ERROR_HANDLER_DELETE = 'ERROR_HANDLER_DELETE';
29
34 private static function checkParamsBeforeAddHandler($params)
35 {
36 if (empty($params['CODE']))
37 {
38 throw new RestException('Parameter CODE is not defined', self::ERROR_CHECK_FAILURE);
39 }
40
41 if (empty($params['NAME']))
42 {
43 throw new RestException('Parameter NAME is not defined', self::ERROR_CHECK_FAILURE);
44 }
45
46 if (empty($params['SETTINGS']) || !is_array($params['SETTINGS']))
47 {
48 throw new RestException('Parameter SETTINGS is not defined or empty', self::ERROR_CHECK_FAILURE);
49 }
50
51 self::checkHandlerSettingsBeforeAdd($params['SETTINGS']);
52
54 'filter' => [
55 '=CODE' => $params['CODE']
56 ]
57 ])->fetch();
58 if ($handler)
59 {
60 throw new RestException('Handler already exists!', self::ERROR_HANDLER_ALREADY_EXISTS);
61 }
62 }
63
69 private static function checkParamsBeforeUpdateHandler($params)
70 {
71 if (empty($params['ID']))
72 {
73 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
74 }
75
77 'filter' => [
78 'ID' => $params['ID']
79 ]
80 ])->fetch();
81 if (!$handler)
82 {
83 throw new RestException('Handler not found', self::ERROR_HANDLER_NOT_FOUND);
84 }
85
86 if ($params['APP_ID'] && !empty($handler['APP_ID']) && $handler['APP_ID'] !== $params['APP_ID'])
87 {
88 throw new AccessException();
89 }
90
91 if (empty($params['FIELDS']) || !is_array($params['FIELDS']))
92 {
93 throw new RestException('Parameter FIELDS is not defined', self::ERROR_CHECK_FAILURE);
94 }
95
96 if (isset($params['FIELDS']['SETTINGS']))
97 {
98 self::checkHandlerSettingsBeforeUpdate($params['FIELDS']['SETTINGS']);
99 }
100 }
101
107 private static function checkParamsBeforeDeleteHandler($params)
108 {
109 if (empty($params['ID']))
110 {
111 throw new RestException('Parameter ID is not defined', self::ERROR_CHECK_FAILURE);
112 }
113
115 'filter' => [
116 'ID' => $params['ID']
117 ]
118 ])->fetch();
119 if (!$handler)
120 {
121 throw new RestException('Handler not found', self::ERROR_HANDLER_NOT_FOUND);
122 }
123
124 if ($params['APP_ID'] && !empty($handler['APP_ID']) && $handler['APP_ID'] !== $params['APP_ID'])
125 {
126 throw new AccessException();
127 }
128
129 $cashboxListResult = Manager::getList([
130 'select' => ['ID', 'HANDLER', 'SETTINGS'],
131 'filter' => [
132 '=HANDLER' => '\\'.CashboxRest::class,
133 ],
134 ]);
135
136 $cashboxIdList = [];
137 while ($cashbox = $cashboxListResult->fetch())
138 {
139 if ($cashbox['SETTINGS']['REST']['REST_CODE'] === $handler['CODE'])
140 {
141 $cashboxIdList[] = $cashbox['ID'];
142 }
143 }
144
145 if ($cashboxIdList)
146 {
147 throw new RestException(
148 'There are cashboxes with this handler: '.implode(', ', $cashboxIdList),
149 self::ERROR_CHECK_FAILURE
150 );
151 }
152 }
153
157 private static function checkHandlerSettingsBeforeAdd($settings): void
158 {
159 self::checkRequiredSettingsFields($settings, ['PRINT_URL', 'CHECK_URL', 'CONFIG']);
160 self::checkSettingsFieldValues($settings, ['HTTP_VERSION', 'CONFIG']);
161 }
162
166 private static function checkHandlerSettingsBeforeUpdate($settings): void
167 {
168 self::checkSettingsFieldValues($settings, ['PRINT_URL', 'CHECK_URL', 'CONFIG', 'HTTP_VERSION']);
169 }
170
176 private static function checkRequiredSettingsFields(array $settings, array $requiredFields): void
177 {
178 foreach ($requiredFields as $fieldName)
179 {
180 if (empty($settings[$fieldName]))
181 {
182 throw new RestException('Parameter SETTINGS[' . $fieldName . '] is not defined', self::ERROR_CHECK_FAILURE);
183 }
184 }
185 }
186
192 private static function checkSettingsFieldValues(array $settings, array $fields): void
193 {
194 foreach ($fields as $fieldName)
195 {
196 if ($fieldName === 'HTTP_VERSION' && array_key_exists('HTTP_VERSION', $settings))
197 {
198 $version = $settings['HTTP_VERSION'];
199 if (
200 $version !== Main\Web\HttpClient::HTTP_1_0
201 && $version !== Main\Web\HttpClient::HTTP_1_1
202 )
203 {
204 throw new RestException('The value of SETTINGS[HTTP_VERSION] is not valid', self::ERROR_CHECK_FAILURE);
205 }
206 }
207 elseif ($fieldName === 'CONFIG' && array_key_exists('CONFIG', $settings))
208 {
209 self::checkSettingsConfig($settings['CONFIG']);
210 }
211 elseif (array_key_exists($fieldName, $settings) && empty($settings[$fieldName]))
212 {
213 throw new RestException('The value of SETTINGS[' . $fieldName . '] is not valid', self::ERROR_CHECK_FAILURE);
214 }
215 }
216 }
217
222 private static function checkSettingsConfig(array $config): void
223 {
224 foreach ($config as $group => $block)
225 {
226 foreach ($block['ITEMS'] as $code => $item)
227 {
228 try
229 {
230 \Bitrix\Sale\Internals\Input\Manager::getEditHtml('SETTINGS['.$group.']['.$code.']', $item);
231 }
232 catch (\Exception $exception)
233 {
234 throw new RestException('The config provided in SETTINGS[CONFIG] is not valid', self::ERROR_CHECK_FAILURE);
235 }
236 }
237 }
238 }
239
245 private static function mergeHandlerSettings($cashboxId, array $newSettings): array
246 {
247 $dbResult = $existingSettings = CashboxRestHandlerTable::getList([
248 'select' => ['SETTINGS'],
249 'filter' => ['=ID' => $cashboxId],
250 'limit' => 1,
251 ])->fetch();
252
253 if (!$dbResult)
254 {
255 return $newSettings;
256 }
257
258 $existingSettings = $dbResult['SETTINGS'];
259 if (!$existingSettings)
260 {
261 return $newSettings;
262 }
263
264 return array_replace_recursive($existingSettings, $newSettings);
265 }
266
273 public static function addHandler($params, $page, \CRestServer $server)
274 {
277 self::checkParamsBeforeAddHandler($params);
278
279 if (!isset($params['SETTINGS']['SUPPORTS_FFD105']))
280 {
281 $params['SETTINGS']['SUPPORTS_FFD105'] = 'N';
282 }
283
285 'NAME' => $params['NAME'],
286 'CODE' => $params['CODE'],
287 'SORT' => $params['SORT'] ?: 100,
288 'SETTINGS' => $params['SETTINGS'],
289 'APP_ID' => $params['APP_ID'],
290 ]);
291
292 if ($result->isSuccess())
293 {
294 return $result->getId();
295 }
296
297 $errors = implode("\n", $result->getErrorMessages());
298 throw new RestException($errors, self::ERROR_HANDLER_ADD);
299 }
300
308 public static function updateHandler($params, $page, \CRestServer $server)
309 {
312 self::checkParamsBeforeUpdateHandler($params);
313
314 $handlerFields = $params['FIELDS'];
315 if ($handlerFields['SETTINGS'])
316 {
317 $handlerFields['SETTINGS'] = self::mergeHandlerSettings($params['ID'], $handlerFields['SETTINGS']);
318 }
319
320 $result = CashboxRestHandlerTable::update($params['ID'], $handlerFields);
321 if ($result->isSuccess())
322 {
323 return true;
324 }
325
326 $errors = implode("\n", $result->getErrorMessages());
327 throw new RestException($errors, self::ERROR_HANDLER_UPDATE);
328 }
329
335 public static function deleteHandler($params, $page, \CRestServer $server)
336 {
339 self::checkParamsBeforeDeleteHandler($params);
340
342 if ($result->isSuccess())
343 {
344 return true;
345 }
346
347 $errors = implode("\n", $result->getErrorMessages());
348 throw new RestException($errors, self::ERROR_HANDLER_DELETE);
349 }
350
354 public static function getHandlerList($params, $page, \CRestServer $server)
355 {
357
358 $result = [];
359
361 'select' => ['ID', 'NAME', 'CODE', 'SORT', 'SETTINGS'],
362 ]);
363 while ($item = $dbRes->fetch())
364 {
365 $result[] = $item;
366 }
367
368 return $result;
369 }
370}
static includeModule($moduleName)
Определения loader.php:67
static getList(array $parameters=array())
Определения datamanager.php:431
static delete($primary)
Определения datamanager.php:1644
static add(array $data)
Определения datamanager.php:877
static update($primary, array $data)
Определения datamanager.php:1256
static getList(array $parameters=[])
Определения manager.php:135
static updateHandler($params, $page, \CRestServer $server)
Определения handlerservice.php:308
static addHandler($params, $page, \CRestServer $server)
Определения handlerservice.php:273
static getHandlerList($params, $page, \CRestServer $server)
Определения handlerservice.php:354
static deleteHandler($params, $page, \CRestServer $server)
Определения handlerservice.php:335
static prepareHandlerParams($data, \CRestServer $server)
Определения restservice.php:143
static getEditHtml($name, array $input, $value=null)
Определения input.php:88
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$errors
Определения iblock_catalog_edit.php:74
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения cookie.php:3
$settings
Определения product_settings.php:43
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69
$page
Определения order_form.php:33
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$dbResult
Определения updtr957.php:3
$dbRes
Определения yandex_detail.php:168
$fields
Определения yandex_run.php:501