Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Listener.php
1<?php
2
4
8
10{
11 private static self $instance;
12
13 public function onDocumentStatusChanged(string $status)
14 {
15 $this->pushEvent('documentStatus', ['status' => $status]);
16 }
17
18 public function onWorkflowStatusChanged(string $workflowId, int $status)
19 {
20 $this->pushEvent('workflowStatus', [
21 'workflowId' => $workflowId,
22 'status' => $status,
23 ]);
24 }
25
26 public function onWorkflowEventAdded(string $workflowId, string $eventName)
27 {
28 $session = Session\Manager::getActiveSession();
29 $template = null;
30 foreach ($session->getWorkflowContexts() as $workflowContext)
31 {
32 if ($workflowContext->getWorkflowId() === $workflowId)
33 {
34 $template = Template::createByTpl($workflowContext->fillTemplateShards()->fillTemplate());
35 break;
36 }
37 }
38
39 $robotId = null;
40 if ($template)
41 {
42 foreach ($template->getRobots() as $robot)
43 {
44 if ($robot->getDelayName() === $eventName)
45 {
46 $robotId = $robot->getName();
47 break;
48 }
49 }
50 }
51
52 $this->pushEvent('workflowEventAdd', [
53 'workflowId' => $workflowId,
54 'eventName' => $eventName,
55 'sourceId' => $robotId,
56 ]);
57 }
58
59 public function onWorkflowEventRemoved(string $workflowId, string $eventName)
60 {
61 $this->pushEvent('workflowEventRemove', [
62 'workflowId' => $workflowId,
63 'eventName' => $eventName,
64 ]);
65 }
66
67 public function onDocumentUpdated(array $changedFields)
68 {
69 $documentId = Session\Manager::getActiveSession()->getFixedDocument()->getParameterDocumentId();
70 $documentType = Session\Manager::getActiveSession()->getParameterDocumentType();
71
72 $documentService = \CBPRuntime::GetRuntime(true)->getDocumentService();
73 $documentFields = $documentService->getDocumentFields($documentType);
74 $lazyList = $documentService->GetDocument($documentId);
75
76 $rawValues = [];
77 $values = [];
78 $changedFields[] = 'DATE_MODIFY';
79
80 //TODO - temporary
81 if (in_array('ASSIGNED_BY_ID', $changedFields))
82 {
83 $changedFields[] = 'ASSIGNED_BY_EMAIL';
84 $changedFields[] = 'ASSIGNED_BY_WORK_PHONE';
85 $changedFields[] = 'ASSIGNED_BY_PERSONAL_MOBILE';
86 }
87
88 foreach ($changedFields as $fieldId)
89 {
90 $property = $documentFields[$fieldId] ?? null;
91
92 if (!$property)
93 {
94 continue;
95 }
96
97 $fieldType = $documentService->getFieldTypeObject($documentType, $property);
98
99 if (!$fieldType)
100 {
101 continue;
102 }
103
104 $fieldType->setDocumentId($documentId);
105
106 $rawValues[$fieldId] = $lazyList[$fieldId];
107 $values[$fieldId] = $fieldType->formatValue($rawValues[$fieldId]);
108 }
109
110 $this->pushEvent(
111 'documentValues',
112 [
113 'values' => $values,
114 'rawValues' => $rawValues,
115 ]
116 );
117 }
118
119 public function onDocumentDeleted()
120 {
121 $this->pushEvent('documentDelete', []);
122 }
123
124 public function onTrackWrite($row)
125 {
126 $this->pushEvent('trackRow', ['row' => $row]);
127 }
128
129 public function onSessionFinished(string $sessionId)
130 {
131 $this->pushEvent('sessionFinish', ['sessionId' => $sessionId]);
132 }
133
134 private function canPush()
135 {
136 return Loader::includeModule('pull');
137 }
138
139 private function pushEvent(string $command, array $params)
140 {
141 if ($this->canPush())
142 {
143 \Bitrix\Pull\Event::add(
144 CurrentUser::get()->getId(),
145 [
146 'module_id' => 'bizproc',
147 'command' => $command,
148 'params' => $params,
149 ]
150 );
151 }
152 }
153
154 public static function getInstance(): self
155 {
156 if (!isset(self::$instance))
157 {
158 self::$instance = new self();
159 }
160 return self::$instance;
161 }
162 private function __construct()
163 {
164 }
165 private function __clone()
166 {
167 }
168}
onDocumentUpdated(array $changedFields)
Definition Listener.php:67
onWorkflowStatusChanged(string $workflowId, int $status)
Definition Listener.php:18
onSessionFinished(string $sessionId)
Definition Listener.php:129
onWorkflowEventRemoved(string $workflowId, string $eventName)
Definition Listener.php:59
onDocumentStatusChanged(string $status)
Definition Listener.php:13
onWorkflowEventAdded(string $workflowId, string $eventName)
Definition Listener.php:26