21Loader::includeModule(
"rest");
30 static::SCOPE => array(
31 "lists.get.iblock.type.id" => array(__CLASS__,
"getIblockTypeId"),
33 "lists.add" => array(__CLASS__,
"addLists"),
34 "lists.get" => array(__CLASS__,
"getLists"),
35 "lists.update" => array(__CLASS__,
"updateLists"),
36 "lists.delete" => array(__CLASS__,
"deleteLists"),
38 "lists.section.add" => array(__CLASS__,
"addSection"),
39 "lists.section.get" => array(__CLASS__,
"getSection"),
40 "lists.section.update" => array(__CLASS__,
"updateSection"),
41 "lists.section.delete" => array(__CLASS__,
"deleteSection"),
43 "lists.field.add" => array(__CLASS__,
"addField"),
44 "lists.field.get" => array(__CLASS__,
"getFields"),
45 "lists.field.update" => array(__CLASS__,
"updateField"),
46 "lists.field.delete" => array(__CLASS__,
"deleteField"),
47 "lists.field.type.get" => array(__CLASS__,
"getFieldTypes"),
49 "lists.element.add" => array(__CLASS__,
"addElement"),
50 "lists.element.get" => array(__CLASS__,
"getElement"),
51 "lists.element.update" => array(__CLASS__,
"updateElement"),
52 "lists.element.delete" => array(__CLASS__,
"deleteElement"),
53 "lists.element.get.file.url" => array(__CLASS__,
"getFileUrl"),
60 $param =
new Param($params);
64 $iblockTypeId = $iblockType->getIblockTypeId();
65 if ($iblockType->hasErrors())
67 self::throwError($iblockType->getErrors());
73 public static function addLists(array $params, $n, \CRestServer $server)
75 $param =
new Param($params);
77 $iblock =
new Iblock($param);
78 if ($iblock->isExist())
85 $rightParam->setUser($USER);
87 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
93 $right->checkPermission(IblockRight::EDIT);
94 if ($right->hasErrors())
96 self::throwError($right->getErrors());
99 $iblockId = $iblock->add();
100 if ($iblock->hasErrors())
102 self::throwError($iblock->getErrors());
108 public static function getLists(array $params, $n, \CRestServer $server)
110 $param =
new Param($params);
114 $rightParam->setUser($USER);
116 if (!\CLists::isListFeatureEnabled((
string)$rightParam->getIblockTypeId()))
122 $right->checkPermission(IblockRight::READ);
123 if ($right->hasErrors())
125 self::throwError($right->getErrors());
128 $iblock =
new Iblock($param);
129 list ($iblocks, $queryObject) = $iblock->get(self::getNavData($n));
130 if (empty($iblocks) || $iblock->hasErrors())
136 return self::setNavData(array_values($iblocks), $queryObject);
140 public static function updateLists(array $params, $n, \CRestServer $server)
142 $param =
new Param($params);
144 $iblock =
new Iblock($param);
145 if (!$iblock->isExist())
152 $rightParam->setUser($USER);
154 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
160 $right->checkPermission(IblockRight::EDIT);
161 if ($right->hasErrors())
163 self::throwError($right->getErrors());
166 if ($iblock->update())
172 self::throwError($iblock->getErrors());
176 public static function deleteLists(array $params, $n, \CRestServer $server)
178 $param =
new Param($params);
180 $iblock =
new Iblock($param);
181 if (!$iblock->isExist())
188 $rightParam->setUser($USER);
190 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
196 $right->checkPermission(IblockRight::EDIT);
197 if ($right->hasErrors())
199 self::throwError($right->getErrors());
202 if ($iblock->delete())
208 self::throwError($iblock->getErrors());
212 public static function addSection(array $params, $n, \CRestServer $server)
214 $param =
new Param($params);
215 $params = $param->getParams();
219 $rightParam->setUser($USER);
220 $rightParam->setEntityId($params[
"IBLOCK_SECTION_ID"]);
222 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
229 if ($right->hasErrors())
231 self::throwError($right->getErrors());
234 $section =
new Section($param);
235 $sectionId = $section->add();
236 if ($section->hasErrors())
238 self::throwError($section->getErrors());
244 public static function getSection(array $params, $n, \CRestServer $server)
246 $param =
new Param($params);
247 $params = $param->getParams();
251 $rightParam->setUser($USER);
252 $rightParam->setEntityId($params[
"IBLOCK_SECTION_ID"]);
254 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
261 if ($right->hasErrors())
263 self::throwError($right->getErrors());
266 $section =
new Section($param);
267 list ($sections, $queryObject) = $section->get(self::getNavData($n));
268 if (empty($sections) || $section->hasErrors())
274 return self::setNavData(array_values($sections), $queryObject);
278 public static function updateSection(array $params, $n, \CRestServer $server)
280 $param =
new Param($params);
281 $params = $param->getParams();
283 $section =
new Section($param);
284 if (!$section->isExist())
291 $rightParam->setUser($USER);
292 $rightParam->setEntityId($params[
"IBLOCK_SECTION_ID"]);
294 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
301 if ($right->hasErrors())
303 self::throwError($right->getErrors());
306 if ($section->update())
312 self::throwError($section->getErrors());
316 public static function deleteSection(array $params, $n, \CRestServer $server)
318 $param =
new Param($params);
319 $params = $param->getParams();
321 $section =
new Section($param);
322 if (!$section->isExist())
329 $rightParam->setUser($USER);
330 $rightParam->setEntityId($params[
"IBLOCK_SECTION_ID"]);
332 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
339 if ($right->hasErrors())
341 self::throwError($right->getErrors());
344 if ($section->delete())
350 self::throwError($section->getErrors());
354 public static function addField(array $params, $n, \CRestServer $server)
356 $param =
new Param($params);
358 $iblock =
new Iblock($param);
359 if (!$iblock->isExist())
366 $rightParam->setUser($USER);
368 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
374 $right->checkPermission(IblockRight::EDIT);
375 if ($right->hasErrors())
377 self::throwError($right->getErrors());
380 $field =
new Field($param);
381 $fieldId = $field->add();
382 if ($field->hasErrors())
384 self::throwError($field->getErrors());
392 public static function getFields(array $params, $n, \CRestServer $server)
394 $param =
new Param($params);
396 $iblock =
new Iblock($param);
397 if (!$iblock->isExist())
404 $rightParam->setUser($USER);
406 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
412 $right->checkPermission();
413 if ($right->hasErrors())
415 self::throwError($right->getErrors());
418 $field =
new Field($param);
419 return $field->get();
422 public static function updateField(array $params, $n, \CRestServer $server)
424 $param =
new Param($params);
426 $iblock =
new Iblock($param);
427 if (!$iblock->isExist())
434 $rightParam->setUser($USER);
436 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
442 $right->checkPermission(IblockRight::EDIT);
443 if ($right->hasErrors())
445 self::throwError($right->getErrors());
448 $field =
new Field($param);
449 if ($field->update())
455 self::throwError($field->getErrors());
459 public static function deleteField(array $params, $n, \CRestServer $server)
461 $param =
new Param($params);
463 $iblock =
new Iblock($param);
464 if (!$iblock->isExist())
471 $rightParam->setUser($USER);
473 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
479 $right->checkPermission(IblockRight::EDIT);
480 if ($right->hasErrors())
482 self::throwError($right->getErrors());
485 $field =
new Field($param);
491 public static function getFieldTypes(array $params, $n, \CRestServer $server)
493 $param =
new Param($params);
495 $iblock =
new Iblock($param);
496 if (!$iblock->isExist())
503 $rightParam->setUser($USER);
505 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
511 $right->checkPermission(IblockRight::EDIT);
512 if ($right->hasErrors())
514 self::throwError($right->getErrors());
517 $field =
new Field($param);
518 return $field->getAvailableTypes();
521 public static function addElement(array $params, $n, \CRestServer $server)
523 $param =
new Param($params);
524 $params = $param->getParams();
526 $iblock =
new Iblock($param);
527 if (!$iblock->isExist())
534 $rightParam->setUser($USER);
535 $rightParam->setEntityId($params[
"IBLOCK_SECTION_ID"]);
537 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
543 $right =
new Right($rightParam, $elementRight);
544 $right->checkPermission(ElementRight::ADD);
545 if ($right->hasErrors())
547 self::throwError($right->getErrors());
550 $element =
new Element($param);
551 if ($element->isExist())
556 $elementId = $element->add();
557 if ($element->hasErrors())
559 self::throwError($element->getErrors());
565 public static function getElement(array $params, $n, \CRestServer $server)
567 $param =
new Param($params);
568 $params = $param->getParams();
570 $iblock =
new Iblock($param);
571 if (!$iblock->isExist())
578 $rightParam->setUser($USER);
579 $rightParam->setEntityId(Utils::getElementId($param->getParams()));
581 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
587 $param->setParam([
"CAN_FULL_EDIT" => ($elementRight->canFullEdit() ?
"Y" :
"N")]);
589 $right =
new Right($rightParam, $elementRight);
590 $right->checkPermission(ElementRight::READ);
591 if ($right->hasErrors())
593 self::throwError($right->getErrors());
596 $element =
new Element($param);
597 if (is_array($params[
"FILTER"]))
599 list($availableFields, $listCustomFields) = $element->getAvailableFields();
600 $element->resultSanitizeFilter = self::getSanitizeFilter(
601 $params[
"FILTER"], $availableFields, $listCustomFields);
603 list ($elements, $queryObject) = $element->get(self::getNavData($n));
606 return self::setNavData(array_values($elements), $queryObject);
614 public static function updateElement(array $params, $n, \CRestServer $server)
616 $param =
new Param($params);
618 $iblock =
new Iblock($param);
619 if (!$iblock->isExist())
626 $rightParam->setUser($USER);
627 $rightParam->setEntityId(Utils::getElementId($param->getParams()));
629 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
635 $right =
new Right($rightParam, $elementRight);
636 $right->checkPermission(ElementRight::EDIT);
637 if ($right->hasErrors())
639 self::throwError($right->getErrors());
642 $element =
new Element($param);
643 if (!$element->isExist())
648 if ($element->update())
654 self::throwError($element->getErrors());
658 public static function deleteElement(array $params, $n, \CRestServer $server)
660 $param =
new Param($params);
662 $iblock =
new Iblock($param);
663 if (!$iblock->isExist())
670 $rightParam->setUser($USER);
671 $rightParam->setEntityId(Utils::getElementId($param->getParams()));
673 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
679 $right =
new Right($rightParam, $elementRight);
680 $right->checkPermission(ElementRight::EDIT);
681 if ($right->hasErrors())
683 self::throwError($right->getErrors());
686 $element =
new Element($param);
687 if (!$element->isExist())
692 $elementRight->canDelete();
693 if ($elementRight->hasErrors())
695 self::throwError($elementRight->getErrors());
698 if ($element->delete())
704 self::throwError($element->getErrors());
708 public static function getFileUrl(array $params, $n, \CRestServer $server)
710 $param =
new Param($params);
712 $iblock =
new Iblock($param);
713 if (!$iblock->isExist())
720 $rightParam->setUser($USER);
721 $rightParam->setEntityId(Utils::getElementId($param->getParams()));
723 if (!\CLists::isListFeatureEnabled($rightParam->getIblockTypeId()))
729 $right =
new Right($rightParam, $elementRight);
730 $right->checkPermission(ElementRight::READ);
731 if ($right->hasErrors())
733 self::throwError($right->getErrors());
736 $element =
new Element($param);
737 if (!$element->isExist())
742 return $element->getFileUrl();
745 private static function throwError(array $errors, $message =
"", $code =
"")
747 $error = end($errors);
749 if ($error instanceof
Error)
751 $message = $error->getMessage();
752 if (is_array($message) && array_key_exists(
"message", $message))
754 $message = $message[
"message"];
758 $message = $message ?:
"Unknown error";
760 throw new RestException($message, $error->getCode());
764 throw new RestException($error, RestException::ERROR_CORE);
766 elseif ($message && $code)
768 throw new RestException($message, $code);
771 throw new RestException(
"Unknown error", RestException::ERROR_NOT_FOUND);
774 private static function getSanitizeFilter($filter, $availableFields, $listCustomFields)
776 return parent::sanitizeFilter(
779 function($field, $value) use ($listCustomFields)
781 if (array_key_exists($field, $listCustomFields))
783 $callback = $listCustomFields[$field];
784 if ($callback instanceof \Closure)
786 return $callback($value);
790 return call_user_func_array($listCustomFields[$field], [[], [
"VALUE" => $value]]);
795 [
"",
"!%",
">=",
"><",
"!><",
">",
"<=",
"<",
"%",
"=",
"*",
"!"]
const ERROR_ELEMENT_NOT_FOUND
const ERROR_ELEMENT_ALREADY_EXISTS
const ERROR_IBLOCK_NOT_FOUND
const ERROR_IBLOCK_ALREADY_EXISTS
const ERROR_SECTION_NOT_FOUND
static addLists(array $params, $n, \CRestServer $server)
const ERROR_PROPERTY_ALREADY_EXISTS
static onRestServiceBuildDescription()
static getFieldTypes(array $params, $n, \CRestServer $server)
static getIblockTypeId(array $params, $n, \CRestServer $server)
const ENTITY_LISTS_CODE_PREFIX
const ERROR_DELETE_ELEMENT
const ERROR_IBLOCK_NOT_FOUND
const ERROR_IBLOCK_ALREADY_EXISTS
static addField(array $params, $n, \CRestServer $server)
static getFields(array $params, $n, \CRestServer $server)
static addElement(array $params, $n, \CRestServer $server)
static getElement(array $params, $n, \CRestServer $server)
static getSection(array $params, $n, \CRestServer $server)
static updateElement(array $params, $n, \CRestServer $server)
static deleteSection(array $params, $n, \CRestServer $server)
static deleteElement(array $params, $n, \CRestServer $server)
static addSection(array $params, $n, \CRestServer $server)
static updateSection(array $params, $n, \CRestServer $server)
static deleteLists(array $params, $n, \CRestServer $server)
static getFileUrl(array $params, $n, \CRestServer $server)
static deleteField(array $params, $n, \CRestServer $server)
static updateField(array $params, $n, \CRestServer $server)
static getLists(array $params, $n, \CRestServer $server)
const ERROR_REQUIRED_PARAMETERS_MISSING
static updateLists(array $params, $n, \CRestServer $server)