3namespace Bitrix\Lists\Api\Service\IBlockService;
5use Bitrix\Iblock\SectionTable;
6use Bitrix\Lists\Api\Data\IBlockService\IBlockElementsToGet;
7use Bitrix\Lists\Api\Data\IBlockService\IBlockElementToAdd;
8use Bitrix\Lists\Api\Data\IBlockService\IBlockElementToUpdate;
9use Bitrix\Lists\Api\Data\IBlockService\IBlockToGet;
10use Bitrix\Lists\Api\Data\IBlockService\IBlockToGetById;
11use Bitrix\Lists\Api\Request\IBlockService\AddIBlockElementRequest;
12use Bitrix\Lists\Api\Request\IBlockService\GetIBlockDefaultFieldsRequest;
13use Bitrix\Lists\Api\Request\IBlockService\UpdateIBlockElementRequest;
14use Bitrix\Lists\Api\Response\IBlockService\AddIBlockElementResponse;
15use Bitrix\Lists\Api\Response\IBlockService\GetIBlockByIdResponse;
16use Bitrix\Lists\Api\Response\IBlockService\GetIBlockDefaultFieldsResponse;
17use Bitrix\Lists\Api\Response\IBlockService\GetIBlockElementFieldsResponse;
18use Bitrix\Lists\Api\Response\IBlockService\GetIBlockElementListResponse;
19use Bitrix\Lists\Api\Response\IBlockService\GetIBlockListResponse;
20use Bitrix\Lists\Api\Response\IBlockService\UpdateIBlockElementResponse;
21use Bitrix\Lists\Api\Response\Response;
22use Bitrix\Lists\Service\Param;
23use Bitrix\Lists\Workflow\Starter;
24use Bitrix\Main\ArgumentException;
26use Bitrix\Main\Loader;
27use Bitrix\Main\Localization\Loc;
34 private string $iBlockTypeId;
35 private int $socNetGroupId;
36 private bool $isBpFeatureEnabled;
38 private static array $cache = [];
46 if ($parameters->hasErrors())
48 $firstError = $parameters->
getErrors()[0];
56 $this->iBlockTypeId = $parameters->
getParams()[
'IBLOCK_TYPE_ID'];
57 $this->socNetGroupId = $parameters->
getParams()[
'SOCNET_GROUP_ID'];
59 $this->isBpFeatureEnabled = (
60 Loader::includeModule(
'bizproc')
61 && \CLists::isBpFeatureEnabled($this->iBlockTypeId) === true
71 $response->fillFromResponse($this->accessService->canUserReadIBlock($iBlockToGetById->
getIBlockId()));
74 if (
$response->isSuccess() && Loader::includeModule(
'iblock'))
88 $response->fillFromResponse($this->accessService->canUserReadIBlockList());
91 if (
$response->isSuccess() && Loader::includeModule(
'iblock'))
95 ->setIBLockTypeId($this->iBlockTypeId)
96 ->setSocNetGroupId($this->socNetGroupId)
98 if (!
$filter->hasField(
'CHECK_PERMISSIONS'))
100 $filter->setCheckPermission(!$this->accessService->isAdminPermission(
$response->getPermission()));
121 $elementId =
$filter->getFieldValue(
'ID');
123 if ($elementId ===
null || is_array($elementId) ||
$iBlockId ===
null)
131 $sectionId =
$filter->getFieldValue(
'SECTION_ID') ?? 0;
133 $this->accessService->canUserReadElement((
int)$elementId, (
int)$sectionId, (
int)
$iBlockId)
140 if (!$iBlockElementsToGet->
getFilter()->hasField(
'CHECK_PERMISSIONS'))
142 $iBlockElementsToGet->
getFilter()->setCheckPermission(
false);
145 if ((
int)$elementId === 0)
147 return $response->setElements([[
'ID' => 0]]);
168 if (
$response->isSuccess() && Loader::includeModule(
'iblock'))
170 $filter->setIBlockType($this->iBlockTypeId);
171 if (!
$filter->hasField(
'CHECK_PERMISSIONS'))
173 $filter->setCheckPermission(!$this->accessService->isAdminPermission(
$response->getPermission()));
186 $elementId = (int)$element[
'ID'];
188 $propertiesValues = [];
191 $propertiesValues = $this->loadIBlockElementPropertiesValues(
$iBlockId, $elementId);
194 $workflowStateInfo = [];
197 $workflowStateInfo = $this->loadWorkflowStateInfo($elementId);
200 $elements[] = array_merge($element, $propertiesValues, $workflowStateInfo);
209 private function loadIBlockElementPropertiesValues(
int $iBlockId,
int $elementId):
array
211 if (!Loader::includeModule(
'iblock'))
217 $order = [
'sort' =>
'asc',
'id' =>
'asc',
'enum_sort' =>
'asc',
'value_id' =>
'asc'];
218 $filter = [
'ACTIVE' =>
'Y',
'EMPTY' =>
'N'];
222 $id =
'PROPERTY_' . $property[
'ID'];
223 if (!array_key_exists($id,
$result))
228 $result[$id][$property[
'PROPERTY_VALUE_ID']] = $property[
'VALUE'];
234 private function loadWorkflowStateInfo(
int $elementId):
array
243 'WORKFLOW_STATE' => $documentState ? $documentState[
'STATE_TITLE'] :
'',
244 'STARTED_BY' => $documentState ? $documentState[
'STARTED_BY'] :
'',
245 'WORKFLOW_STATE_ID' => $documentState ? $documentState[
'ID'] :
'',
249 private function getActualElementState(
array $documentId): ?
array
251 if (Loader::includeModule(
'bizproc'))
253 $state = \CBPDocument::getActiveStates($documentId, 1);
256 return array_shift($state);
259 $ids = \CBPStateService::getIdsByDocument($documentId, 1);
262 return \CBPStateService::getWorkflowState(array_shift($ids));
274 if ($isEnableCheckPermissions)
288 foreach ($list->GetFields() as $fieldId => $property)
292 $property[
'ENUM_VALUES'] = [];
294 if ($property[
'TYPE'] ===
'L')
296 $property = $this->loadEnumValuesByTypeL($property);
300 $all[$fieldId] = $property;
301 if ($list->is_field($fieldId))
307 $props[$fieldId] = $property;
314 private function loadEnumValuesByTypeL(
array $property):
array
316 $queryObject = \CIBlockProperty::getPropertyEnum($property[
'ID']);
317 while($enum = $queryObject->fetch())
319 if ($enum[
'DEF'] ===
'Y')
321 if (is_array($property[
'DEFAULT_VALUE']))
323 $property[
'DEFAULT_VALUE'][] = $enum[
'ID'];
325 elseif (empty($property[
'DEFAULT_VALUE']))
327 $property[
'DEFAULT_VALUE'] = $enum[
'ID'];
331 $property[
'DEFAULT_VALUE'] = (
array)$property[
'DEFAULT_VALUE'];
332 $property[
'DEFAULT_VALUE'][] = $enum[
'ID'];
335 $property[
'ENUM_VALUES'][] = $enum;
347 return $response->addError(
new Error(Loc::getMessage(
'LISTS_LIB_API_IBLOCK_SERVICE_ERROR_WRONG_IBLOCK')));
360 if ($iBlock && Loader::includeModule(
'iblock'))
367 if (!isset(self::$cache[
$iBlockId][
'hasSections']))
370 SectionTable::query()
378 self::$cache[
$iBlockId][
'hasSections'] = (bool)$section;
384 && !isset(self::$cache[
$iBlockId][
'sections'])
391 $sectionId = (int)$section[
'ID'];
392 $sections[$sectionId] = $section;
395 self::$cache[
$iBlockId][
'sections'] = $sections;
399 'IBLOCK_SECTION_ID' => [
400 'FIELD_ID' =>
'IBLOCK_SECTION_ID',
401 'NAME' => $iBlock[
'SECTION_NAME'] ??
'',
402 'IS_REQUIRED' =>
'N',
404 'DEFAULT_VALUE' => 0,
407 'SHOW_ADD_FORM' =>
'Y',
408 'SHOW_EDIT_FORM' =>
'Y',
409 'ADD_READ_ONLY_FIELD' =>
'N',
410 'EDIT_READ_ONLY_FIELD' =>
'N',
413 'HAS_SECTIONS' => self::$cache[
$iBlockId][
'hasSections'] ?
'Y' :
'N',
433 $elementToAdd = $this->dataService->getIBlockElementToAddObject(
$request,
$response);
439 $iBlockId = $elementToAdd->getIBlockId();
444 $this->accessService->canUserAddElement($elementToAdd->getSectionId(),
$iBlockId)
448 if (
$response->isSuccess() && Loader::includeModule(
'iblock'))
450 $element = $this->prepareElementValuesToUpsert($elementToAdd,
$response);
459 $iBlockElement = new \CIBlockElement();
460 $id = $iBlockElement->Add($element,
false,
true,
true);
461 $elementId = (int)(is_scalar($id) ? $id : 0);
466 $response->addErrors($this->separateUpsertErrors($iBlockElement->LAST_ERROR));
469 if ($wfStarter && $elementId > 0)
471 $wfStarter->setTimeToStart(
$request->timeToStart)->setElementId($elementId);
472 $startWorkflowResult = $wfStarter->run(
true);
473 $response->addErrors($startWorkflowResult->getErrors());
481 private function getWfStarter(
493 $wfStarter =
new Starter($iBlockInfo, $this->accessService->getUserId());
494 $wfStarter->setElementId($elementId);
496 if (!$wfStarter->isEnabled())
501 $isRunnableWfResult = $wfStarter->isRunnable($modifiedBy);
502 $response->addErrors($isRunnableWfResult->getErrors());
504 if ($isRunnableWfResult->isSuccess())
506 $setParametersResult = $wfStarter->setParameters($parameters);
507 $response->addErrors($setParametersResult->getErrors());
517 $elementToUpdate = $this->dataService->getIBlockElementToUpdateObject(
$request,
$response);
518 if (!$elementToUpdate)
526 $this->accessService->canUserEditElement(
527 $elementToUpdate->getElementId(),
528 $elementToUpdate->getSectionId(),
529 $elementToUpdate->getIBlockId()
536 $element = $this->prepareElementValuesToUpsert($elementToUpdate,
$response);
547 $iBlockElement = new \CIBlockElement();
548 $result = $iBlockElement->Update($elementToUpdate->getElementId(), $element,
false,
true,
true);
557 ->addErrors($this->separateUpsertErrors($iBlockElement->LAST_ERROR))
558 ->setIsSuccessUpdate(
false)
563 if ($wfStarter && (empty($element) ||
$response->getIsSuccessUpdate()))
565 $wfStarter->setTimeToStart(
$request->timeToStart);
567 if ($wfStarter->hasTemplatesOnStartup())
569 $wfStarter->setChangedFields(
570 empty($element) ? [] : $elementToUpdate->getChangedFieldsAfterUpdate()
574 $startWorkflowResult = $wfStarter->run();
575 $response->addErrors($startWorkflowResult->getErrors());
583 private function prepareElementValuesToUpsert(
589 $response->addErrors($iBlockFields->getErrors());
595 $element =
$result->getElementData() ?? [];
598 if (!
$result->getHasChangedFields() && !
$result->getHasChangedProps())
607 private function separateUpsertErrors(
string $errorMessages):
array
611 $messages = explode(
'<br>', $errorMessages);
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
getElementValues(array $fields, array $props)
disableCheckPermissions()
isCheckPermissionsEnabled()
isNeedLoadWorkflowState()
getIBlockDefaultFields(GetIBlockDefaultFieldsRequest $request)
getIBlockElementList(IBlockElementsToGet $iBlockElementsToGet)
getIBlockById(IBlockToGetById $iBlockToGetById)
getIBlockList(IBlockToGet $iBlockToGet)
getElementDetailInfo(IBlockElementsToGet $iBlockElementsToGet)
updateIBlockElement(UpdateIBlockElementRequest $request)
__construct(Param $parameters, AccessService $accessService,)
IBlockDataService $dataService
addIBlockElement(AddIBlockElementRequest $request)
getIBlockFields(int $iBlockId, bool $isEnableCheckPermissions, bool $loadEnumValues)
AccessService $accessService
checkRequiredInputParams(array $requiredInputParams)
static getDocumentComplexId($iblockType, $documentId)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)