1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
userfieldtype.php
См. документацию.
1<?php
2namespace Bitrix\Rest\Api;
3
4use Bitrix\Rest\AccessException;
5use Bitrix\Rest\AppTable;
6use Bitrix\Rest\AuthTypeException;
7use Bitrix\Rest\HandlerHelper;
8use Bitrix\Rest\OAuth\Auth;
9use Bitrix\Rest\PlacementLangTable;
10use Bitrix\Rest\PlacementTable;
11use Bitrix\Rest\RestException;
12use Bitrix\Rest\UserField\Callback;
13use Bitrix\Rest\Lang;
14use Bitrix\Rest\Exceptions;
15
17{
18 const SCOPE_USERFIELDTYPE = 'placement';
19 const PLACEMENT_UF_TYPE = 'USERFIELD_TYPE';
20
21 public static function onRestServiceBuildDescription()
22 {
23 return array(
24 static::SCOPE_USERFIELDTYPE => array(
25 'userfieldtype.list' => array(
26 'callback' => array(__CLASS__, 'getList'),
27 'options' => array()
28 ),
29 'userfieldtype.add' => array(
30 'callback' => array(__CLASS__, 'add'),
31 'options' => array()
32 ),
33 'userfieldtype.update' => array(
34 'callback' => array(__CLASS__, 'update'),
35 'options' => array()
36 ),
37 'userfieldtype.delete' => array(
38 'callback' => array(__CLASS__, 'delete'),
39 'options' => array()
40 ),
41 \CRestUtil::PLACEMENTS => array(
42 static::PLACEMENT_UF_TYPE => array(
43 'private' => true
44 ),
45 ),
46 ),
47 );
48 }
49
50 public static function getList($param, $nav, \CRestServer $server)
51 {
52 static::checkPermission($server);
53
54 $navParams = static::getNavData($nav, true);
55
56 $dbRes = PlacementTable::getList(array(
57 'filter' => array(
58 '=PLACEMENT' => static::PLACEMENT_UF_TYPE,
59 '=REST_APP.CLIENT_ID' => $server->getClientId(),
60 ),
61 'select' => array(
62 'USER_TYPE_ID' => 'ADDITIONAL',
63 'HANDLER' => 'PLACEMENT_HANDLER',
64 'TITLE' => 'TITLE',
65 'DESCRIPTION' => 'COMMENT'
66 ),
67
68 'limit' => $navParams['limit'],
69 'offset' => $navParams['offset'],
70 'count_total' => true,
71 ));
72
73 $result = array();
74 while($handler = $dbRes->fetch())
75 {
76 $result[] = $handler;
77 }
78
79 return static::setNavData(
80 $result,
81 array(
82 "count" => $dbRes->getCount(),
83 "offset" => $navParams['offset']
84 )
85 );
86 }
87
88 private static function prepareOption($option): array
89 {
90 $result = [];
91 if (is_array($option))
92 {
93 $option = array_change_key_case($option, CASE_LOWER);
94 if ($option['height'])
95 {
96 $result['height'] = (int)$option['height'];
97 }
98 }
99
100 return $result;
101 }
102
103 public static function add($param, $n, \CRestServer $server): bool
104 {
105 static::checkPermission($server);
106
107 $param = array_change_key_case($param, CASE_UPPER);
108
109 $userTypeId = mb_strtolower($param['USER_TYPE_ID'] ?? '');
110 $placementHandler = $param['HANDLER'] ?? '';
111
112 if ($userTypeId == '')
113 {
114 throw new Exceptions\ArgumentNullException("USER_TYPE_ID");
115 }
116
117 if ($placementHandler == '')
118 {
119 throw new Exceptions\ArgumentNullException("HANDLER");
120 }
121
122 $appInfo = AppTable::getByClientId($server->getClientId());;
123
124 HandlerHelper::checkCallback($placementHandler, $appInfo);
125
126 $placementBind = array(
127 'APP_ID' => $appInfo['ID'],
128 'PLACEMENT' => static::PLACEMENT_UF_TYPE,
129 'PLACEMENT_HANDLER' => $placementHandler,
130 'TITLE' => $userTypeId,
131 'ADDITIONAL' => $userTypeId,
132 'OPTIONS' => static::prepareOption($param['OPTIONS'] ?? null),
133 );
134
135 $placementBind = array_merge(
136 $placementBind,
138 $param,
139 [
140 'TITLE',
141 'DESCRIPTION',
142 ],
143 [
144 'TITLE' => $placementBind['TITLE'] ?? null
145 ]
146 )
147 );
148 $langAll = [];
149 if ($placementBind['LANG_ALL'])
150 {
151 $langAll = $placementBind['LANG_ALL'];
152 }
153 unset($placementBind['LANG_ALL']);
154
155 $result = PlacementTable::add($placementBind);
156 if (!$result->isSuccess())
157 {
158 $errorMessage = $result->getErrorMessages();
159 throw new RestException(
160 'Unable to set placement handler: '.implode(', ', $errorMessage),
162 );
163 }
164 else
165 {
166 $placementId = $result->getId();
167 foreach ($langAll as $lang => $item)
168 {
169 $item['PLACEMENT_ID'] = $placementId;
170 $item['LANGUAGE_ID'] = $lang;
172 if (!$res->isSuccess())
173 {
174 throw new RestException(
175 'Error: ' . implode(', ', $res->getErrorMessages()),
177 );
178 }
179 }
181 'ID' => $placementId,
182 'APP_ID' => $appInfo['ID'],
183 'ADDITIONAL' => $userTypeId,
184 ));
185 }
186
187 return true;
188 }
189
190 public static function update($param, $n, \CRestServer $server)
191 {
192 static::checkPermission($server);
193
194 $param = array_change_key_case($param, CASE_UPPER);
195
196 $userTypeId = mb_strtolower($param['USER_TYPE_ID'] ?? '');
197
198 if($userTypeId == '')
199 {
200 throw new Exceptions\ArgumentNullException("USER_TYPE_ID");
201 }
202
203 $updateFields = array();
204 if(!empty($param['HANDLER']))
205 {
206 $appInfo = AppTable::getByClientId($server->getClientId());;
207 HandlerHelper::checkCallback($param['HANDLER'], $appInfo);
208
209 $updateFields['PLACEMENT_HANDLER'] = $param['HANDLER'];
210 }
211
212 if (array_key_exists('OPTIONS', $param))
213 {
214 $updateFields['OPTIONS'] = static::prepareOption($param['OPTIONS']);
215 }
216
217 $updateFields = array_merge(
218 $updateFields,
220 $param,
221 [
222 'TITLE',
223 'DESCRIPTION',
224 ],
225 [
226 'TITLE' => $updateFields['TITLE'] ?? null
227 ]
228 )
229 );
230 $langAll = [];
231 if ($updateFields['LANG_ALL'])
232 {
233 $langAll = $updateFields['LANG_ALL'];
234 }
235 unset($updateFields['LANG_ALL']);
236
237 if(count($updateFields) > 0)
238 {
239 $dbRes = PlacementTable::getList(array(
240 'filter' => array(
241 '=REST_APP.CLIENT_ID' => $server->getClientId(),
242 '=ADDITIONAL' => $userTypeId
243 ),
244 'select' => array('ID', 'APP_ID', 'ADDITIONAL')
245 ));
246 $placementInfo = $dbRes->fetch();
247 if($placementInfo)
248 {
249 $updateResult = PlacementTable::update($placementInfo['ID'], $updateFields);
250 if($updateResult->isSuccess())
251 {
252 PlacementLangTable::deleteByPlacement($placementInfo['ID']);
253 foreach ($langAll as $lang => $item)
254 {
255 $item['PLACEMENT_ID'] = $placementInfo['ID'];
256 $item['LANGUAGE_ID'] = $lang;
258 if (!$res->isSuccess())
259 {
260 throw new RestException(
261 'Error: ' . implode(', ', $res->getErrorMessages()),
263 );
264 }
265 }
266 // rebind handler for failover reasons
267 Callback::bind($placementInfo);
268 }
269 else
270 {
271 $errorMessage = $updateResult->getErrorMessages();
272 throw new RestException(
273 'Unable to update User Field Type: '.implode(', ', $errorMessage),
275 );
276 }
277 }
278 else
279 {
280 throw new RestException('User Field Type not found', RestException::ERROR_NOT_FOUND);
281 }
282 }
283 else
284 {
285 throw new Exceptions\ArgumentNullException('HANDLER|TITLE|DESCRIPTION');
286 }
287
288 return true;
289 }
290
291 public static function delete($param, $n, \CRestServer $server)
292 {
293 static::checkPermission($server);
294
295 $param = array_change_key_case($param, CASE_UPPER);
296
297 $userTypeId = mb_strtolower($param['USER_TYPE_ID'] ?? '');
298
299 if($userTypeId == '')
300 {
301 throw new Exceptions\ArgumentNullException("USER_TYPE_ID");
302 }
303
304 $dbRes = PlacementTable::getList(array(
305 'filter' => array(
306 '=REST_APP.CLIENT_ID' => $server->getClientId(),
307 '=ADDITIONAL' => $userTypeId
308 ),
309 'select' => array('ID', 'APP_ID', 'ADDITIONAL')
310 ));
311 $placementInfo = $dbRes->fetch();
312 if($placementInfo)
313 {
314 $deleteResult = PlacementTable::delete($placementInfo['ID']);
315 if($deleteResult->isSuccess())
316 {
317 Callback::unbind($placementInfo);
318 }
319 else
320 {
321 $errorMessage = $deleteResult->getErrorMessages();
322 throw new RestException(
323 'Unable to delete User Field Type: '.implode(', ', $errorMessage),
325 );
326 }
327 }
328 else
329 {
330 throw new RestException('User Field Type not found', RestException::ERROR_NOT_FOUND);
331 }
332
333 return true;
334 }
335
336 protected static function checkPermission(\CRestServer $server)
337 {
338 if($server->getAuthType() !== Auth::AUTH_TYPE)
339 {
340 throw new AuthTypeException("Application context required");
341 }
342
343 if(!\CRestUtil::isAdmin())
344 {
345 throw new AccessException();
346 }
347 }
348}
static add(array $data)
Определения datamanager.php:877
static onRestServiceBuildDescription()
Определения userfieldtype.php:21
const SCOPE_USERFIELDTYPE
Определения userfieldtype.php:18
const PLACEMENT_UF_TYPE
Определения userfieldtype.php:19
static getList($param, $nav, \CRestServer $server)
Определения userfieldtype.php:50
static add($param, $n, \CRestServer $server)
Определения userfieldtype.php:103
static update($param, $n, \CRestServer $server)
Определения userfieldtype.php:190
static checkPermission(\CRestServer $server)
Определения userfieldtype.php:336
static getByClientId($clientId)
Определения app.php:967
static checkCallback($handlerUrl, $appInfo=array(), $checkInstallUrl=true)
Определения handlerhelper.php:31
static fillCompatibility(array $param, array $fieldList, array $defaultValues=[])
Определения lang.php:100
static deleteByPlacement(int $placementId)
Определения placementlang.php:194
const ERROR_CORE
Определения restexception.php:15
const ERROR_NOT_FOUND
Определения restexception.php:17
static unbind($fields)
Определения callback.php:46
static bind($fields)
Определения callback.php:34
Определения rest.php:24
getAuthType()
Определения rest.php:347
getClientId()
Определения rest.php:362
Определения rest.php:896
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
$navParams
Определения csv_new_run.php:35
if(!defined('SITE_ID')) $lang
Определения include.php:91
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$option
Определения options.php:1711
$n
Определения update_log.php:107
$dbRes
Определения yandex_detail.php:168