Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
IBlockService.php
1<?php
2
4
25
26final class IBlockService
27{
30
31 private string $iBlockTypeId;
32 private int $socNetGroupId;
33 private bool $isBpFeatureEnabled;
34
35 public function __construct(
36 Param $parameters,
38 )
39 {
40 $parameters->checkRequiredInputParams(['IBLOCK_TYPE_ID', 'SOCNET_GROUP_ID']);
41 if ($parameters->hasErrors())
42 {
43 $firstError = $parameters->getErrors()[0];
44
45 throw new ArgumentException($firstError->getMessage());
46 }
47
48 $this->accessService = $accessService;
49 $this->dataService = new IBlockDataService();
50
51 $this->iBlockTypeId = $parameters->getParams()['IBLOCK_TYPE_ID'];
52 $this->socNetGroupId = $parameters->getParams()['SOCNET_GROUP_ID'];
53
54 $this->isBpFeatureEnabled = (
55 Loader::includeModule('bizproc')
56 && \CLists::isBpFeatureEnabled($this->iBlockTypeId) === true
57 );
58 }
59
60 public function getIBlockById(IBlockToGetById $iBlockToGetById): GetIBlockByIdResponse
61 {
62 $response = new GetIBlockByIdResponse();
63
64 if ($iBlockToGetById->needCheckPermissions())
65 {
66 $response->fillFromResponse($this->accessService->canUserReadIBlock($iBlockToGetById->getIBlockId()));
67 }
68
69 if ($response->isSuccess() && Loader::includeModule('iblock'))
70 {
71 $response->setIBlock(\CIBlock::GetArrayByID($iBlockToGetById->getIBlockId()) ?: []);
72 }
73
74 return $response;
75 }
76
77 public function getIBlockList(IBlockToGet $iBlockToGet): GetIBlockListResponse
78 {
79 $response = new GetIBlockListResponse();
80
81 if ($iBlockToGet->needCheckPermissions())
82 {
83 $response->fillFromResponse($this->accessService->canUserReadIBlockList());
84 }
85
86 if ($response->isSuccess() && Loader::includeModule('iblock'))
87 {
88 $filter =
89 $iBlockToGet->getFilter()
90 ->setIBLockTypeId($this->iBlockTypeId)
91 ->setSocNetGroupId($this->socNetGroupId)
92 ;
93 if (!$filter->hasField('CHECK_PERMISSIONS'))
94 {
95 $filter->setCheckPermission(!$this->accessService->isAdminPermission($response->getPermission()));
96 }
97
98 $iBlocks = [];
99 $iterator = \CIBlock::GetList($iBlockToGet->getOrder(), $filter->getOrmFilter());
100 while ($item = $iterator->Fetch())
101 {
102 $iBlocks[] = $item;
103 }
104
105 $response->setIBlocks($iBlocks);
106 }
107
108 return $response;
109 }
110
112 {
113 $response = new GetIBlockElementListResponse();
114 $filter = $iBlockElementsToGet->getFilter();
115
116 $elementId = $filter->getFieldValue('ID');
117 $iBlockId = $filter->getFieldValue('IBLOCK_ID');
118 if ($elementId === null || is_array($elementId) || $iBlockId === null)
119 {
120 // todo: loc
121 return $response->addError(new Error('required parameters'));
122 }
123
124 if ($iBlockElementsToGet->isCheckPermissionsEnabled())
125 {
126 $sectionId = $filter->getFieldValue('SECTION_ID') ?? 0;
127 $response->fillFromResponse(
128 $this->accessService->canUserReadElement((int)$elementId, (int)$sectionId, (int)$iBlockId)
129 );
130 }
131
132 if ($response->isSuccess())
133 {
134 $iBlockElementsToGet->disableCheckPermissions();
135 if (!$iBlockElementsToGet->getFilter()->hasField('CHECK_PERMISSIONS'))
136 {
137 $iBlockElementsToGet->getFilter()->setCheckPermission(false);
138 }
139
140 if ((int)$elementId === 0)
141 {
142 return $response->setElements([['ID' => 0]]);
143 }
144
145 return $this->getIBlockElementList($iBlockElementsToGet);
146 }
147
148 return $response;
149 }
150
152 {
153 $response = new GetIBlockElementListResponse();
154
155 $filter = $iBlockElementsToGet->getFilter();
156 $iBlockId = $filter->hasField('IBLOCK_ID') ? (int)$filter->getFieldValue('IBLOCK_ID') : null;
157 if ($iBlockElementsToGet->isCheckPermissionsEnabled())
158 {
159 $response->fillFromResponse($this->accessService->canUserReadElementList($iBlockId));
160 }
161
162 $elements = [];
163 if ($response->isSuccess() && Loader::includeModule('iblock'))
164 {
165 $filter->setIBlockType($this->iBlockTypeId);
166 if (!$filter->hasField('CHECK_PERMISSIONS'))
167 {
168 $filter->setCheckPermission(!$this->accessService->isAdminPermission($response->getPermission()));
169 }
170
171 $iterator = \CIBlockElement::GetList(
172 $iBlockElementsToGet->getOrder(),
173 $filter->getOrmFilter(),
174 false,
175 $iBlockElementsToGet->getNavigation(),
176 $iBlockElementsToGet->getSelect()
177 );
178
179 while ($element = $iterator->Fetch())
180 {
181 $elementId = (int)$element['ID'];
182
183 $propertiesValues = [];
184 if ($iBlockId && $iBlockElementsToGet->isNeedLoadProps())
185 {
186 $propertiesValues = $this->loadIBlockElementPropertiesValues($iBlockId, $elementId);
187 }
188
189 $workflowStateInfo = [];
190 if ($this->isBpFeatureEnabled && $iBlockElementsToGet->isNeedLoadWorkflowState())
191 {
192 $workflowStateInfo = $this->loadWorkflowStateInfo($elementId);
193 }
194
195 $elements[] = array_merge($element, $propertiesValues, $workflowStateInfo);
196 }
197 }
198
199 $response->setElements($elements);
200
201 return $response;
202 }
203
204 private function loadIBlockElementPropertiesValues(int $iBlockId, int $elementId): array
205 {
206 if (!Loader::includeModule('iblock'))
207 {
208 return [];
209 }
210
211 $result = [];
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())
216 {
217 $id = 'PROPERTY_' . $property['ID'];
218 if (!array_key_exists($id, $result))
219 {
220 $result[$id] = [];
221 }
222
223 $result[$id][$property['PROPERTY_VALUE_ID']] = $property['VALUE'];
224 }
225
226 return $result;
227 }
228
229 private function loadWorkflowStateInfo(int $elementId): array
230 {
231 $documentState =
232 $elementId > 0
233 ? $this->getActualElementState(\BizprocDocument::getDocumentComplexId($this->iBlockTypeId, $elementId))
234 : null
235 ;
236
237 return [
238 'WORKFLOW_STATE' => $documentState ? $documentState['STATE_TITLE'] : '',
239 'STARTED_BY' => $documentState ? $documentState['STARTED_BY'] : '',
240 'WORKFLOW_STATE_ID' => $documentState ? $documentState['ID'] : '',
241 ];
242 }
243
244 private function getActualElementState(array $documentId): ?array
245 {
246 if (Loader::includeModule('bizproc'))
247 {
248 $state = \CBPDocument::getActiveStates($documentId, 1);
249 if ($state)
250 {
251 return array_shift($state);
252 }
253
254 $ids = \CBPStateService::getIdsByDocument($documentId, 1);
255 if ($ids)
256 {
257 return \CBPStateService::getWorkflowState(array_shift($ids));
258 }
259 }
260
261 return null;
262 }
263
264 // todo: VO
265 public function getIBlockFields(int $iBlockId, bool $isEnableCheckPermissions, bool $loadEnumValues): GetIBlockElementFieldsResponse
266 {
267 $response = new GetIBlockElementFieldsResponse();
268
269 if ($isEnableCheckPermissions)
270 {
271 $response->fillFromResponse($this->accessService->canUserReadIBlock($iBlockId));
272 if (!$response->isSuccess())
273 {
274 return $response;
275 }
276 }
277
278 $list = new \CList($iBlockId);
279
280 $fields = [];
281 $props = [];
282 foreach ($list->GetFields() as $fieldId => $property)
283 {
284 if ($loadEnumValues)
285 {
286 $property['ENUM_VALUES'] = [];
287
288 if ($property['TYPE'] === 'L')
289 {
290 $property = $this->loadEnumValuesByTypeL($property);
291 }
292 }
293
294 if ($list->is_field($fieldId))
295 {
296 $fields[$fieldId] = $property;
297 }
298 else
299 {
300 $props[$fieldId] = $property;
301 }
302 }
303
304 return $response->setFields($fields)->setProps($props);
305 }
306
307 private function loadEnumValuesByTypeL(array $property): array
308 {
309 $queryObject = \CIBlockProperty::getPropertyEnum($property['ID']);
310 while($enum = $queryObject->fetch())
311 {
312 if ($enum['DEF'] === 'Y')
313 {
314 if (is_array($property['DEFAULT_VALUE']))
315 {
316 $property['DEFAULT_VALUE'][] = $enum['ID'];
317 }
318 elseif (empty($property['DEFAULT_VALUE']))
319 {
320 $property['DEFAULT_VALUE'] = $enum['ID'];
321 }
322 else
323 {
324 $property['DEFAULT_VALUE'] = (array)$property['DEFAULT_VALUE'];
325 $property['DEFAULT_VALUE'][] = $enum['ID'];
326 }
327 }
328 $property['ENUM_VALUES'][] = $enum;
329 }
330
331 return $property;
332 }
333
335 {
336 $response = new GetIBlockDefaultFieldsResponse();
337 $iBlockId = $request->iBlockId;
338 if ($iBlockId <= 0)
339 {
340 return $response->addError(new Error(Loc::getMessage('LISTS_LIB_API_IBLOCK_SERVICE_ERROR_WRONG_IBLOCK')));
341 }
342
343 if ($request->needCheckPermissions)
344 {
345 $response->fillFromResponse($this->accessService->canUserReadIBlock($iBlockId));
346 }
347
348 if ($response->isSuccess())
349 {
350 $iBlockToGet = (new IBlockToGetById($iBlockId))->disableCheckPermissions();
351 $iBlock = $this->getIBlockById($iBlockToGet)->getIBlock();
352
353 if ($iBlock && Loader::includeModule('iblock'))
354 {
355 $hasSections = SectionTable::getCount(['=IBLOCK_ID' => $iBlockId]) !== 0 ? 'Y' : 'N';
356
357 $fields = [
358 'IBLOCK_SECTION_ID' => [
359 'FIELD_ID' => 'IBLOCK_SECTION_ID',
360 'NAME' => $iBlock['SECTION_NAME'] ?? '',
361 'IS_REQUIRED' => 'N',
362 'MULTIPLE' => 'N',
363 'DEFAULT_VALUE' => 0,
364 'TYPE' => 'G',
365 'SETTINGS' => [
366 'SHOW_ADD_FORM' => 'Y',
367 'SHOW_EDIT_FORM' => 'Y',
368 'ADD_READ_ONLY_FIELD' => 'N',
369 'EDIT_READ_ONLY_FIELD' => 'N',
370 ],
371 'LINK_IBLOCK_ID' => $iBlockId,
372 'HAS_SECTIONS' => $hasSections,
373 ],
374 ];
375
376 $response->setDefaultFields($fields);
377 }
378 }
379
380 return $response;
381 }
382
384 {
385 $response = new AddIBlockElementResponse();
386
387 $elementToAdd = $this->dataService->getIBlockElementToAddObject($request, $response);
388 $iBlockId = $elementToAdd->getIBlockId();
389 $createdBy = $elementToAdd->getCreatedBy();
390
391 if ($response->isSuccess() && $request->needCheckPermissions)
392 {
393 $response->fillFromResponse(
394 $this->accessService->canUserAddElement($elementToAdd->getSectionId(), $iBlockId)
395 );
396 }
397
398 if ($response->isSuccess() && Loader::includeModule('iblock'))
399 {
400 $element = $this->prepareElementValuesToAdd($elementToAdd, $response);
401 $wfStarter =
402 $response->isSuccess() && $request->needStartWorkflows
403 ? $this->getWfStarter($response, $iBlockId, 0, $createdBy, $request->wfParameterValues)
404 : null
405 ;
406
407 if ($response->isSuccess())
408 {
409 $iBlockElement = new \CIBlockElement();
410 $id = $iBlockElement->Add($element, false, true, true);
411 $elementId = (int)(is_scalar($id) ? $id : 0);
412
413 $response->setId($elementId);
414 if ($elementId <= 0)
415 {
416 $response->addError(new Error($iBlockElement->LAST_ERROR));
417 }
418
419 if ($wfStarter && $elementId > 0)
420 {
421 $wfStarter->setTimeToStart($request->timeToStart)->setElementId($elementId);
422 $startWorkflowResult = $wfStarter->run(true);
423 $response->addErrors($startWorkflowResult->getErrors());
424 }
425 }
426 }
427
428 return $response;
429 }
430
431 private function prepareElementValuesToAdd(
432 IBlockElementToAdd $elementToAdd,
434 ): array
435 {
436 $iBlockFields = $this->getIBlockFields($elementToAdd->getIBlockId(), false, false);
437 $response->addErrors($iBlockFields->getErrors());
438
439 $element = [];
440 if ($response->isSuccess())
441 {
442 $result = $elementToAdd->getElementValues($iBlockFields->getFields(), $iBlockFields->getProps());
443 $element = $result->getData()['element'];
444 $response->addErrors($result->getErrors());
445 }
446
447 return $element;
448 }
449
450 private function getWfStarter(
451 Response $response,
452 int $iBlockId,
453 int $elementId,
454 int $createdBy,
455 array $parameters
456 ): Starter
457 {
458 $iBlockInfo = $this->getIBlockById((new IBlockToGetById($iBlockId))->disableCheckPermissions())->getIBlock();
459
460 $wfStarter = new Starter($iBlockInfo, $this->accessService->getUserId());
461 $wfStarter->setElementId($elementId);
462
463 $isRunnableWfResult = $wfStarter->isRunnable($createdBy);
464 $response->addErrors($isRunnableWfResult->getErrors());
465
466 if ($isRunnableWfResult->isSuccess())
467 {
468 $setParametersResult = $wfStarter->setParameters($parameters);
469 $response->addErrors($setParametersResult->getErrors());
470 }
471
472 return $wfStarter;
473 }
474
475 // public function updateIBlockElement(){}
476}
getIBlockDefaultFields(GetIBlockDefaultFieldsRequest $request)
getIBlockElementList(IBlockElementsToGet $iBlockElementsToGet)
getIBlockById(IBlockToGetById $iBlockToGetById)
getElementDetailInfo(IBlockElementsToGet $iBlockElementsToGet)
__construct(Param $parameters, AccessService $accessService,)
addIBlockElement(AddIBlockElementRequest $request)
getIBlockFields(int $iBlockId, bool $isEnableCheckPermissions, bool $loadEnumValues)
checkRequiredInputParams(array $requiredInputParams)
Definition param.php:39
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
isSuccess($internalCall=false)
Definition result.php:52
addErrors(array $errors)
Definition result.php:98
static getDocumentComplexId($iblockType, $documentId)