31 private string $iBlockTypeId;
32 private int $socNetGroupId;
33 private bool $isBpFeatureEnabled;
41 if ($parameters->hasErrors())
43 $firstError = $parameters->
getErrors()[0];
51 $this->iBlockTypeId = $parameters->
getParams()[
'IBLOCK_TYPE_ID'];
52 $this->socNetGroupId = $parameters->
getParams()[
'SOCNET_GROUP_ID'];
54 $this->isBpFeatureEnabled = (
55 Loader::includeModule(
'bizproc')
56 && \CLists::isBpFeatureEnabled($this->iBlockTypeId) === true
66 $response->fillFromResponse($this->accessService->canUserReadIBlock($iBlockToGetById->
getIBlockId()));
69 if ($response->isSuccess() && Loader::includeModule(
'iblock'))
71 $response->setIBlock(\CIBlock::GetArrayByID($iBlockToGetById->
getIBlockId()) ?: []);
83 $response->fillFromResponse($this->accessService->canUserReadIBlockList());
86 if ($response->isSuccess() && Loader::includeModule(
'iblock'))
90 ->setIBLockTypeId($this->iBlockTypeId)
91 ->setSocNetGroupId($this->socNetGroupId)
93 if (!$filter->hasField(
'CHECK_PERMISSIONS'))
95 $filter->setCheckPermission(!$this->accessService->isAdminPermission($response->getPermission()));
99 $iterator = \CIBlock::GetList($iBlockToGet->
getOrder(), $filter->getOrmFilter());
100 while ($item = $iterator->Fetch())
105 $response->setIBlocks($iBlocks);
114 $filter = $iBlockElementsToGet->
getFilter();
116 $elementId = $filter->getFieldValue(
'ID');
117 $iBlockId = $filter->getFieldValue(
'IBLOCK_ID');
118 if ($elementId ===
null || is_array($elementId) || $iBlockId ===
null)
121 return $response->addError(
new Error(
'required parameters'));
126 $sectionId = $filter->getFieldValue(
'SECTION_ID') ?? 0;
127 $response->fillFromResponse(
128 $this->accessService->canUserReadElement((
int)$elementId, (
int)$sectionId, (
int)$iBlockId)
132 if ($response->isSuccess())
135 if (!$iBlockElementsToGet->
getFilter()->hasField(
'CHECK_PERMISSIONS'))
137 $iBlockElementsToGet->
getFilter()->setCheckPermission(
false);
140 if ((
int)$elementId === 0)
142 return $response->setElements([[
'ID' => 0]]);
155 $filter = $iBlockElementsToGet->
getFilter();
156 $iBlockId = $filter->hasField(
'IBLOCK_ID') ? (int)$filter->getFieldValue(
'IBLOCK_ID') :
null;
159 $response->fillFromResponse($this->accessService->canUserReadElementList($iBlockId));
163 if ($response->isSuccess() && Loader::includeModule(
'iblock'))
165 $filter->setIBlockType($this->iBlockTypeId);
166 if (!$filter->hasField(
'CHECK_PERMISSIONS'))
168 $filter->setCheckPermission(!$this->accessService->isAdminPermission($response->getPermission()));
171 $iterator = \CIBlockElement::GetList(
173 $filter->getOrmFilter(),
179 while ($element = $iterator->Fetch())
181 $elementId = (int)$element[
'ID'];
183 $propertiesValues = [];
186 $propertiesValues = $this->loadIBlockElementPropertiesValues($iBlockId, $elementId);
189 $workflowStateInfo = [];
192 $workflowStateInfo = $this->loadWorkflowStateInfo($elementId);
195 $elements[] = array_merge($element, $propertiesValues, $workflowStateInfo);
199 $response->setElements($elements);
204 private function loadIBlockElementPropertiesValues(
int $iBlockId,
int $elementId): array
206 if (!Loader::includeModule(
'iblock'))
212 $order = [
'sort' =>
'asc',
'id' =>
'asc',
'enum_sort' =>
'asc',
'value_id' =>
'asc'];
213 $filter = [
'ACTIVE' =>
'Y',
'EMPTY' =>
'N'];
214 $iterator = \CIBlockElement::GetProperty($iBlockId, $elementId, $order, $filter);
215 while ($property = $iterator->Fetch())
217 $id =
'PROPERTY_' . $property[
'ID'];
218 if (!array_key_exists($id, $result))
223 $result[$id][$property[
'PROPERTY_VALUE_ID']] = $property[
'VALUE'];
229 private function loadWorkflowStateInfo(
int $elementId): array
238 'WORKFLOW_STATE' => $documentState ? $documentState[
'STATE_TITLE'] :
'',
239 'STARTED_BY' => $documentState ? $documentState[
'STARTED_BY'] :
'',
240 'WORKFLOW_STATE_ID' => $documentState ? $documentState[
'ID'] :
'',
244 private function getActualElementState(array $documentId): ?array
246 if (Loader::includeModule(
'bizproc'))
248 $state = \CBPDocument::getActiveStates($documentId, 1);
251 return array_shift($state);
254 $ids = \CBPStateService::getIdsByDocument($documentId, 1);
257 return \CBPStateService::getWorkflowState(array_shift($ids));
269 if ($isEnableCheckPermissions)
271 $response->fillFromResponse($this->accessService->canUserReadIBlock($iBlockId));
272 if (!$response->isSuccess())
278 $list = new \CList($iBlockId);
282 foreach ($list->GetFields() as $fieldId => $property)
286 $property[
'ENUM_VALUES'] = [];
288 if ($property[
'TYPE'] ===
'L')
290 $property = $this->loadEnumValuesByTypeL($property);
294 if ($list->is_field($fieldId))
296 $fields[$fieldId] = $property;
300 $props[$fieldId] = $property;
304 return $response->setFields($fields)->setProps($props);
307 private function loadEnumValuesByTypeL(array $property): array
309 $queryObject = \CIBlockProperty::getPropertyEnum($property[
'ID']);
310 while($enum = $queryObject->fetch())
312 if ($enum[
'DEF'] ===
'Y')
314 if (is_array($property[
'DEFAULT_VALUE']))
316 $property[
'DEFAULT_VALUE'][] = $enum[
'ID'];
318 elseif (empty($property[
'DEFAULT_VALUE']))
320 $property[
'DEFAULT_VALUE'] = $enum[
'ID'];
324 $property[
'DEFAULT_VALUE'] = (array)$property[
'DEFAULT_VALUE'];
325 $property[
'DEFAULT_VALUE'][] = $enum[
'ID'];
328 $property[
'ENUM_VALUES'][] = $enum;
337 $iBlockId = $request->iBlockId;
340 return $response->addError(
new Error(
Loc::getMessage(
'LISTS_LIB_API_IBLOCK_SERVICE_ERROR_WRONG_IBLOCK')));
343 if ($request->needCheckPermissions)
345 $response->fillFromResponse($this->accessService->canUserReadIBlock($iBlockId));
348 if ($response->isSuccess())
350 $iBlockToGet = (
new IBlockToGetById($iBlockId))->disableCheckPermissions();
353 if ($iBlock && Loader::includeModule(
'iblock'))
355 $hasSections = SectionTable::getCount([
'=IBLOCK_ID' => $iBlockId]) !== 0 ?
'Y' :
'N';
358 'IBLOCK_SECTION_ID' => [
359 'FIELD_ID' =>
'IBLOCK_SECTION_ID',
360 'NAME' => $iBlock[
'SECTION_NAME'] ??
'',
361 'IS_REQUIRED' =>
'N',
363 'DEFAULT_VALUE' => 0,
366 'SHOW_ADD_FORM' =>
'Y',
367 'SHOW_EDIT_FORM' =>
'Y',
368 'ADD_READ_ONLY_FIELD' =>
'N',
369 'EDIT_READ_ONLY_FIELD' =>
'N',
371 'LINK_IBLOCK_ID' => $iBlockId,
372 'HAS_SECTIONS' => $hasSections,
376 $response->setDefaultFields($fields);
387 $elementToAdd = $this->dataService->getIBlockElementToAddObject($request, $response);
388 $iBlockId = $elementToAdd->getIBlockId();
389 $createdBy = $elementToAdd->getCreatedBy();
391 if ($response->isSuccess() && $request->needCheckPermissions)
393 $response->fillFromResponse(
394 $this->accessService->canUserAddElement($elementToAdd->getSectionId(), $iBlockId)
398 if ($response->isSuccess() && Loader::includeModule(
'iblock'))
400 $element = $this->prepareElementValuesToAdd($elementToAdd, $response);
402 $response->isSuccess() && $request->needStartWorkflows
403 ? $this->getWfStarter($response, $iBlockId, 0, $createdBy, $request->wfParameterValues)
407 if ($response->isSuccess())
409 $iBlockElement = new \CIBlockElement();
410 $id = $iBlockElement->Add($element,
false,
true,
true);
411 $elementId = (int)(is_scalar($id) ? $id : 0);
413 $response->setId($elementId);
416 $response->addError(
new Error($iBlockElement->LAST_ERROR));
419 if ($wfStarter && $elementId > 0)
421 $wfStarter->setTimeToStart($request->timeToStart)->setElementId($elementId);
422 $startWorkflowResult = $wfStarter->run(
true);
423 $response->addErrors($startWorkflowResult->getErrors());
431 private function prepareElementValuesToAdd(
437 $response->
addErrors($iBlockFields->getErrors());
442 $result = $elementToAdd->
getElementValues($iBlockFields->getFields(), $iBlockFields->getProps());
443 $element = $result->getData()[
'element'];
444 $response->
addErrors($result->getErrors());
450 private function getWfStarter(
458 $iBlockInfo = $this->
getIBlockById((
new IBlockToGetById($iBlockId))->disableCheckPermissions())->getIBlock();
460 $wfStarter =
new Starter($iBlockInfo, $this->accessService->getUserId());
461 $wfStarter->setElementId($elementId);
463 $isRunnableWfResult = $wfStarter->isRunnable($createdBy);
464 $response->addErrors($isRunnableWfResult->getErrors());
466 if ($isRunnableWfResult->isSuccess())
468 $setParametersResult = $wfStarter->setParameters($parameters);
469 $response->addErrors($setParametersResult->getErrors());
static getMessage($code, $replace=null, $language=null)
isSuccess($internalCall=false)