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