Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ServiceFactory.php
1<?php
2
4
32
33abstract class ServiceFactory
34{
38
39 public static function getServiceByIBlockTypeId(
40 string $iBlockTypeId,
41 int $currentUserId,
42 int $socNetGroupId = 0,
44 {
45 if (empty($iBlockTypeId) || $currentUserId <= 0 || $socNetGroupId < 0)
46 {
47 return null;
48 }
49
50 $param = new Param([
51 'IBLOCK_TYPE_ID' => $iBlockTypeId,
52 'IBLOCK_ID' => false,
53 'SOCNET_GROUP_ID' => $socNetGroupId,
54 ]);
55
56 $accessService = new AccessService($currentUserId, $param);
58 $param,
59 new \Bitrix\Lists\Api\Service\IBlockService\AccessService($currentUserId, $param)
60 );
62
63 if ($iBlockTypeId === ListService::getIBlockTypeId())
64 {
66 }
67
68 if ($iBlockTypeId === ProcessService::getIBlockTypeId())
69 {
71 }
72
73 if ($socNetGroupId > 0 && $iBlockTypeId === SocNetListService::getIBlockTypeId())
74 {
75 return (
77 ->setSocNetGroupId($socNetGroupId)
78 );
79 }
80
81 $listsPermissions = \CLists::GetPermission();
82 if (array_key_exists($iBlockTypeId, $listsPermissions))
83 {
84 return (
86 ->setIBlockTypeId($iBlockTypeId)
87 );
88 }
89
90 return null;
91 }
92
93 abstract public static function getIBlockTypeId(): string;
94
95 private function __construct(
99 )
100 {
101 $this->accessService = $accessService;
102 $this->iBlockService = $iBlockService;
103 $this->dataService = $dataService;
104 }
105
106 public function getInnerIBlockTypeId(): string
107 {
108 return static::getIBlockTypeId();
109 }
110
112 {
113 return $this->accessService->checkIBlockTypePermission();
114 }
115
117 {
118 $response = new GetCatalogResponse();
119
120 $checkPermissionResponse = $this->accessService->canUserReadCatalog();
121 $response->fillFromResponse($checkPermissionResponse);
122
123 if ($response->isSuccess())
124 {
125 $filter =
126 (new IBlockListFilter())
127 ->setActive(true)
128 ->setIBLockTypeId($this->getInnerIBlockTypeId())
129 ->setCheckPermission(!$this->accessService->isAdminPermission($response->getPermission()))
130 ;
131 $this->fillCatalogFilter($filter);
132
133 $iBlockToGet =
134 (new IBlockToGet($filter))
135 ->disableCheckPermissions()
136 ;
137
138 $iBlockListResult = $this->iBlockService->getIBlockList($iBlockToGet);
139 $response
140 ->addErrors($iBlockListResult->getErrors())
141 ->setCatalog($iBlockListResult->getIBlocks())
142 ;
143 }
144
145 return $response;
146 }
147
148 abstract protected function fillCatalogFilter(IBlockListFilter $filter): void;
149
151 {
152 $response = new GetListResponse();
153
154 $checkPermissionResult = $this->accessService->canUserGetElementList();
155 $response->fillFromResponse($checkPermissionResult);
156
157 if ($response->isSuccess())
158 {
159 $filter =
160 (IBlockElementFilter::initializeFromArray($request->filter->getOrmFilter()))
161 ->setIBlockType($this->getInnerIBlockTypeId())
162 ->setCheckPermission(!$this->accessService->isCanReadPermission($response->getPermission()))
163 ;
164 $this->fillElementListFilter($filter);
165
166 $iBlockElementsToGet =
168 $filter,
169 $request->sort,
170 $request->offset,
171 $request->limit,
172 $request->additionalSelectFields
173 ))
174 ->disableCheckPermissions()
175 ->setIsNeedLoadWorkflowStateInfo() // todo: to GetListOptions
176 ;
177
178 $getIBlockElementListResult = $this->iBlockService->getIBlockElementList($iBlockElementsToGet);
179 $response
180 ->addErrors($getIBlockElementListResult->getErrors())
181 ->setElements($getIBlockElementListResult->getElements())
182 ;
183 }
184
185 return $response;
186 }
187
188 abstract protected function fillElementListFilter(IBlockElementFilter $filter): void;
189
191 {
192 $response = new GetIBlockInfoResponse();
193
194 if ($request->iBlockId <= 0)
195 {
196 return $response->addError($this->dataService::getWrongIBlockError());
197 }
198
199 if ($request->needCheckPermissions)
200 {
201 $checkPermissionResult = $this->accessService->canUserReadIBlock($request->iBlockId);
202 $response->fillFromResponse($checkPermissionResult);
203 }
204
205 if ($response->isSuccess())
206 {
207 $iBlockToGetById =
208 (new IBlockToGetById($request->iBlockId))
209 ->disableCheckPermissions()
210 ;
211
212 $iBlockResponse = $this->iBlockService->getIBlockById($iBlockToGetById);
213 $response->addErrors($iBlockResponse->getErrors());
214
215 if ($iBlockResponse->isSuccess())
216 {
217 $response->setIBlock($iBlockResponse->getIBlock());
218 }
219 }
220
221 return $response;
222 }
223
225 {
226 $response = new GetElementDetailInfoResponse();
227
228 $elementToGetDetailInfo = $this->dataService->getElementToGetDetailInfoObject($request, $response);
229 if ($elementToGetDetailInfo)
230 {
231 $elementId = $elementToGetDetailInfo->getElementId();
232
233 if ($elementToGetDetailInfo->isNeedCheckPermissions())
234 {
235 $sectionId = $elementToGetDetailInfo->getSectionId();
236 $checkElementPermission = $this->accessService->canUserReadElement($elementId, $sectionId);
237 $response->fillFromResponse($checkElementPermission);
238 }
239
240 if ($response->isSuccess())
241 {
242 $filter =
243 (new IBlockElementFilter())
244 ->setIBlockType($this->getInnerIBlockTypeId())
245 ->setIBlockId($elementToGetDetailInfo->getIBlockId())
246 ->setId($elementId)
247 ->setShowNew(true)
248 ;
249 $this->fillElementDetailInfoFilter($filter);
250
251 $iBlockElementsToGet =
253 filter: $filter,
254 limit: 1,
255 additionalSelectFields: $elementToGetDetailInfo->getAdditionalSelectFields()
256 ))
257 ->disableCheckPermissions()
258 ->setIsNeedLoadWorkflowStateInfo(false)
259 ;
260
261 $elementListResponse = $this->iBlockService->getElementDetailInfo($iBlockElementsToGet);
262 $response->addErrors($elementListResponse->getErrors());
263
264 if ($response->isSuccess())
265 {
266 $response
267 ->setInfo($elementListResponse->hasElements() ? $elementListResponse->getElements()[0] : [])
268 ;
269 }
270 }
271 }
272
273 return $response;
274 }
275
276 abstract protected function fillElementDetailInfoFilter(IBlockElementFilter $filter): void;
277
279 {
280 $response = new GetIBlockFieldsResponse();
281 $iBlockId = $request->iBlockId;
282
283 if ($iBlockId <= 0)
284 {
285 return $response->addError($this->dataService::getWrongIBlockError());
286 }
287
288 if ($request->needCheckPermissions)
289 {
290 $checkPermissionsResponse = $this->accessService->canUserReadIBlock($iBlockId);
291 $response->fillFromResponse($checkPermissionsResponse);
292 }
293
294 if ($response->isSuccess())
295 {
296 $iBlockFieldsResponse = $this->iBlockService->getIBlockFields(
297 $iBlockId,
298 false,
299 $request->loadEnumValues
300 );
301 $response->addErrors($iBlockFieldsResponse->getErrors());
302
303 $fields = $iBlockFieldsResponse->getFields();
304 if ($request->loadDefaultFields)
305 {
306 $iBlockDefaultFieldsRequest = new GetIBlockDefaultFieldsRequest($iBlockId, false);
307 $defaultFieldsRequest = $this->iBlockService->getIBlockDefaultFields($iBlockDefaultFieldsRequest);
308 $response->addErrors($defaultFieldsRequest->getErrors());
309
310 $defaultFields = $defaultFieldsRequest->getDefaultFields();
311 if ($defaultFields)
312 {
313 $fields = array_merge($fields, $defaultFields);
314 }
315 }
316
317 $response->setFields($fields);
318 $response->setProps($iBlockFieldsResponse->getProps());
319 }
320
321 return $response;
322 }
323
325 {
326 $response = new AddElementResponse();
327
328 $elementToAdd = $this->dataService->getElementToAddObject($request, $response);
329 if ($elementToAdd)
330 {
331 if ($request->needCheckPermission)
332 {
333 $checkPermissionsResponse = $this->accessService->canUserAddElement(
334 $elementToAdd->getSectionId(),
335 $elementToAdd->getIBlockId()
336 );
337 $response->fillFromResponse($checkPermissionsResponse);
338 }
339
340 if ($response->isSuccess())
341 {
342 $addRequest = new AddIBlockElementRequest(
343 $elementToAdd->getIBlockId(),
344 $elementToAdd->getSectionId(),
345 $elementToAdd->getValues(),
346 $elementToAdd->getCreatedBy(),
347 $request->needStartWorkflows,
348 false,
349 $request->wfParameterValues,
350 $request->timeToStart,
351 );
352
353 $addResponse = $this->iBlockService->addIBlockElement($addRequest);
354 $response
355 ->addErrors($addResponse->getErrors())
356 ->setId((int)$addResponse->getId())
357 ;
358 }
359 }
360
361 return $response;
362 }
363
370 {
371 // todo: rights?
373
374 if (!\CLists::isBpFeatureEnabled($this->getInnerIBlockTypeId()) || !Loader::includeModule('bizproc'))
375 {
376 // todo: localization
377 return $response->addError(new Error('not supported'));
378 }
379
380 $timeToGet = $this->dataService->getAverageTemplateDurationToGetObject($request, $response);
381 if (!$timeToGet)
382 {
383 return $response;
384 }
385
386 $templates = $this->getTemplatesByIBlockId($timeToGet->getIBlockId(), $timeToGet->getAutoExecuteType());
387 if (!$templates)
388 {
389 // todo: localization
390 return $response->addError(new Error('no templates'));
391 }
392
393 $workflowStateService = new WorkflowStateService();
394 $averageTimeResult = $workflowStateService->getAverageWorkflowDuration(
395 new GetAverageWorkflowDurationRequest($templates[0]['ID'])
396 );
397
398 $response->addErrors($averageTimeResult->getErrors());
399 if ($averageTimeResult->isSuccess() && $averageTimeResult->getAverageDuration())
400 {
401 $response->setAverageDuration($averageTimeResult->getAverageDuration());
402 }
403
404 return $response;
405 }
406
407 private function getTemplatesByIBlockId(int $iBlockId, int $autoExecuteType): array
408 {
409 $documentType = \BizprocDocument::generateDocumentComplexType($this->getInnerIBlockTypeId(), $iBlockId);
410
411 if (Loader::includeModule('bizproc'))
412 {
413 return \CBPWorkflowTemplateLoader::searchTemplatesByDocumentType($documentType, $autoExecuteType);
414 }
415
416 return [];
417 }
418
419}
static getServiceByIBlockTypeId(string $iBlockTypeId, int $currentUserId, int $socNetGroupId=0,)
getAverageIBlockTemplateDuration(GetAverageIBlockTemplateDurationRequest $request)
getElementDetailInfo(GetElementDetailInfoRequest $request)
fillElementDetailInfoFilter(IBlockElementFilter $filter)