Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Starter.php
1<?php
2
4
9
10final class Starter
11{
12 private WorkflowService $workflowService;
13 private int $currentUserId;
14 private int $elementId = 0;
15 private array $parameters = [];
16 private array $changedFields = [];
17 private ?int $timeToStart = null;
18
19 private bool $hasTemplatesOnStartup = false;
20
21 public function __construct(array $iBlockInfo, int $currentUserId)
22 {
23 $this->workflowService = new WorkflowService($iBlockInfo);
24 $this->currentUserId = max($currentUserId, 0);
25 }
26
27 public function setElementId(int $elementId): void
28 {
29 if ($elementId > 0)
30 {
31 $this->elementId = $elementId;
32 }
33 }
34
35 public function setTimeToStart(?int $timeToStart = null): self
36 {
37 $this->timeToStart = $timeToStart;
38
39 return $this;
40 }
41
42 public function setParameters(array $parameters): Result
43 {
44 $result = new Result();
45
46 $parameterValuesResponse = $this->workflowService->getParameterValuesFromRequest($parameters, $this->elementId);
47 if ($parameterValuesResponse->isSuccess())
48 {
49 $this->parameters = $parameterValuesResponse->getParameters();
50 $this->hasTemplatesOnStartup = $this->elementId > 0 && $parameterValuesResponse->hasTemplatesOnStartup();
51 }
52
53 return (
54 $result
55 ->addErrors($parameterValuesResponse->getErrors())
56 ->setData(['parameters' => $parameterValuesResponse->getParameters()])
57 );
58 }
59
60 public function setChangedFields(array $changedFields): void
61 {
62 $this->changedFields = $changedFields;
63 }
64
65 public function isEnabled(): bool
66 {
67 return $this->workflowService->isBpEnabled();
68 }
69
70 public function isRunnable(int $createdBy): Result
71 {
72 $result = new Result();
73
74 if (!$this->isEnabled())
75 {
76 return $result->addError(new Error('not available'));
77 }
78
79 $currentUserGroups = $this->getCurrentUserGroups($this->currentUserId > 0 && $this->currentUserId === $createdBy);
80 if (!$this->workflowService->canUserWriteDocument($this->elementId, $this->currentUserId, $currentUserGroups))
81 {
82 return $result->addError(new Error('cant write document'));
83 }
84
85 if (!$this->workflowService->isConstantsTuned())
86 {
87 return $result->addError(new Error('constants are not configured'));
88 }
89
90 return $result;
91 }
92
93 private function getCurrentUserGroups(bool $isAuthor = false): array
94 {
95 $userGroups = $this->currentUserId ? \CUser::GetUserGroup($this->currentUserId) : [];
96 if ($isAuthor && is_array($userGroups))
97 {
98 $userGroups[] = 'author';
99 }
100
101 return is_array($userGroups) ? $userGroups : [];
102 }
103
104 public function run(bool $isNewElement = false): Result
105 {
106 $result = new Result();
107
108 $startWorkflowResponse = $this->workflowService->startWorkflows(
110 $this->elementId,
111 $this->currentUserId,
112 $this->parameters,
113 $this->hasTemplatesOnStartup && !$isNewElement ? $this->changedFields : [],
114 $isNewElement,
115 $this->timeToStart,
116 )
117 );
118
119 return (
120 $result
121 ->addErrors($startWorkflowResponse->getErrors())
122 ->setData(['workflowIds' => $startWorkflowResponse->getWorkflowIds()])
123 );
124 }
125}
run(bool $isNewElement=false)
Definition Starter.php:104
setChangedFields(array $changedFields)
Definition Starter.php:60
setTimeToStart(?int $timeToStart=null)
Definition Starter.php:35
setElementId(int $elementId)
Definition Starter.php:27
setParameters(array $parameters)
Definition Starter.php:42
__construct(array $iBlockInfo, int $currentUserId)
Definition Starter.php:21
isRunnable(int $createdBy)
Definition Starter.php:70