1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
EventDispatcher.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Collab\Entity\Event;
6
7use Bitrix\Im\V2\Integration\Socialnetwork\Collab\Collab;
8use Bitrix\Main\Engine\CurrentUser;
9use Bitrix\Main\DI\ServiceLocator;
10use Bitrix\Main\Event;
11use Bitrix\Main\Loader;
12use Bitrix\SocialNetwork\Collab\Access\CollabAccessController;
13use Bitrix\SocialNetwork\Collab\Access\CollabDictionary;
14use Bitrix\Socialnetwork\Collab\Activity\LastActivityTrigger;
15use Bitrix\Socialnetwork\Collab\Control\Event\CollabUpdateEvent;
16use Bitrix\Socialnetwork\Collab\Entity\CollabEntity;
17use Bitrix\Socialnetwork\Collab\Entity\CollabEntityFactory;
18use Bitrix\Socialnetwork\Collab\Entity\Type\EntityType;
19use Bitrix\Socialnetwork\Collab\Integration\IM\ActionType;
20use Bitrix\Socialnetwork\Collab\Integration\IM\ActionMessageFactory;
21use Bitrix\Socialnetwork\Collab\Internals\CollabLogTable;
22use Bitrix\Socialnetwork\Collab\Log\Entry\AddCalendarEventLogEntry;
23use Bitrix\Socialnetwork\Collab\Log\Entry\AddFileLogEntry;
24use Bitrix\Socialnetwork\Collab\Log\Entry\AddFilePublicLinkLogEntry;
25use Bitrix\Socialnetwork\Collab\Log\Entry\AddTaskLogEntry;
26use Bitrix\Socialnetwork\Collab\Log\Entry\CompleteTaskLogEntry;
27use Bitrix\Socialnetwork\Collab\Log\Entry\DeleteCalendarEventLogEntry;
28use Bitrix\Socialnetwork\Collab\Log\Entry\DeleteFileLogEntry;
29use Bitrix\Socialnetwork\Collab\Log\Entry\DeleteTaskLogEntry;
30use Bitrix\Socialnetwork\Collab\Log\Entry\MoveFileToRecyclebinLogEntry;
31use Bitrix\Socialnetwork\Collab\Provider\CollabProvider;
32use Bitrix\Socialnetwork\Collab\Log\Entry\CopyInvitationLinkLogEntry;
33use Bitrix\Socialnetwork\Collab\Registry\CollabRegistry;
34use Bitrix\Socialnetwork\Collab\User\Stepper\SendCollabLeaveMessageStepper;
35use Bitrix\Socialnetwork\ComponentHelper;
36use Bitrix\Socialnetwork\Integration\Disk\ObjectType;
37use Bitrix\Socialnetwork\Integration\Tasks\TaskStatus;
38
39final class EventDispatcher
40{
41 public static function onCountersRecount(int $collabId, array $counters, string $entityType): void
42 {
43 if (Loader::includeModule('im'))
44 {
45 Collab::onEntityCountersUpdate($collabId, $counters, $entityType);
46 }
47 }
48
49 public static function onTaskAdd(int $taskId, array $fields = [], array $parameters = []): void
50 {
51 $entity = CollabEntityFactory::getById($taskId, EntityType::Task->value);
52 if ($entity === null)
53 {
54 return;
55 }
56
57 $collabId = $entity->getCollab()->getId();
58 $executorId = (int)($parameters['USER_ID'] ?? 0);
59
60 self::sendAddEvent($entity);
61 LastActivityTrigger::execute($executorId, $collabId);
62
63 $logEntry = new AddTaskLogEntry(userId: $executorId, collabId: $collabId, collabEntity: $entity);
64 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
65 $service->save($logEntry);
66 }
67
68 public static function onTaskUpdate(int $taskId, array $changes = [], array $fields = [], array $parameters = []): void
69 {
70 // in collab now
71 $entity = CollabEntityFactory::getById($taskId, EntityType::Task->value);
72 if ($entity !== null)
73 {
74 self::sendUpdateEvent($changes, $entity);
75
76 if (($changes['STATUS'] ?? null) === TaskStatus::getCompletedStatus())
77 {
78 $collabId = $entity->getCollab()->getId();
79 $executorId = (int)($changes['STATUS_CHANGED_BY'] ?? null);
80
81 if ($executorId <= 0)
82 {
83 return;
84 }
85
86 $logEntry = new CompleteTaskLogEntry(userId: $executorId, collabId: $collabId, collabEntity: $entity);
87 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
88 $service->save($logEntry);
89 }
90
91 return;
92 }
93
94 $oldGroupId = (int)($fields['GROUP_ID'] ?? 0);
95
96 // no collab changes
97 if ($oldGroupId <= 0)
98 {
99 return;
100 }
101
102 // removed from collab
103 $isCollab = CollabRegistry::getInstance()->get($oldGroupId) !== null;
104 if ($isCollab)
105 {
106 self::sendUpdateEvent($changes);
107 }
108 }
109
110 public static function onTaskDelete(int $taskId, array $parameters): void
111 {
112 $entity = CollabEntityFactory::getById($taskId, EntityType::Task->value);
113 if ($entity === null)
114 {
115 return;
116 }
117
118 self::sendDeleteEvent($entity);
119
120 $userId = $parameters['USER_ID'] ?? null;
121
122 if ($userId <=0)
123 {
124 return;
125 }
126
127 $collabId = $entity->getCollab()->getId();
128 $logEntry = new DeleteTaskLogEntry(userId: $userId, collabId: $collabId, collabEntity: $entity);
129 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
130 $service->save($logEntry);
131 }
132
133 public static function OnAfterCommentAdd(Event $event): void
134 {
135 $message = $event->getParameter(2)['MESSAGE'] ?? null;
136 $messageId = (int)($message['ID'] ?? null);
137 if (empty($messageId))
138 {
139 return;
140 }
141
142 $entity = CollabEntityFactory::getById($messageId, EntityType::Comment->value);
143 if ($entity === null)
144 {
145 return;
146 }
147
148 $collabId = $entity->getCollab()->getId();
149 $executorId = (int)($message['AUTHOR_ID'] ?? 0);
150
151 self::sendAddEvent($entity);
152 LastActivityTrigger::execute($executorId, $collabId);
153 }
154
155 public static function OnAfterCalendarEventEdit(array $fields, bool $isNew, int $userId): void
156 {
157 $eventId = (int)($fields['ID'] ?? null);
158 if (empty($eventId) || !$isNew)
159 {
160 return;
161 }
162
163 $entity = CollabEntityFactory::getById($eventId, EntityType::CalendarEvent->value);
164 if ($entity === null)
165 {
166 return;
167 }
168
169 $collabId = $entity->getCollab()->getId();
170
171 self::sendAddEvent($entity);
172 LastActivityTrigger::execute($userId, $collabId);
173
174 $collabId = $entity->getCollab()->getId();
175 $logEntry = new AddCalendarEventLogEntry(userId: $userId, collabId: $collabId, collabEntity: $entity);
176 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
177 $service->save($logEntry);
178 }
179
180 public static function onAfterUserDelete(int $userId): void
181 {
182 if ($userId <= 0)
183 {
184 return;
185 }
186
187 LastActivityTrigger::remove(userIds: [$userId]);
188
189 $userCollabs = CollabProvider::getInstance()->getListByUserId($userId)->getIdList();
190
191 // firedId, whoFiredId, collabs
192 SendCollabLeaveMessageStepper::bind(0, [$userId, (int)CurrentUser::get()->getId(), serialize($userCollabs),]);
193 }
194
195 public static function onAfterAddFile(Event $event): void
196 {
197 $file = $event->getParameter(0);
198
199 $fileId = (int)$file->getFileId();
200 $userId = (int)$file->getCreateUser()->getId();
201
202 if (empty($fileId) || empty($userId))
203 {
204 return;
205 }
206
207 $entity = CollabEntityFactory::getByInternalObject($file, EntityType::File->value);
208 if ($entity === null)
209 {
210 return;
211 }
212
213 $collabId = $entity->getCollab()->getId();
214
215 self::sendAddEvent($entity);
216 LastActivityTrigger::execute($userId, $collabId);
217
218 $logEntry = new AddFileLogEntry(userId: $userId, collabId: $collabId);
219 $logEntry->setEntity($entity);
220 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
221 $service->save($logEntry);
222 }
223
224 public static function onAfterDeleteFile(Event $event): void
225 {
226 $params = $event->getParameters();
227
228 $fileId = (int)($params[0] ?? null);
229 $deletedBy = (int)($params[1] ?? null);
230
231 if ($fileId <= 0 || $deletedBy <= 0)
232 {
233 return;
234 }
235
236 $fileLog = CollabLogTable::query()
237 ->setSelect(['ID', 'COLLAB_ID'])
238 ->where('ENTITY_ID', $fileId)
239 ->where('ENTITY_TYPE', EntityType::File->value)
240 ->fetchObject()
241 ;
242
243 if (!$fileLog)
244 {
245 return;
246 }
247
248 $logEntry = new DeleteFileLogEntry(userId: $deletedBy, collabId: $fileLog->getCollabId());
249 $logEntry->setFileId($fileId);
250 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
251 $service->save($logEntry);
252 }
253
254 public static function onAfterAddExternalLinkToObject(Event $event): void
255 {
256 [$file, $data] = $event->getParameters();
257
258 $userId = (int)($data['CREATED_BY'] ?? null);
259
260 $type = (int)$file?->getType();
261 $isFile = $type > 0 && $type === (new ObjectType())->getFileType();
262
263 if ($userId <= 0 || !$isFile)
264 {
265 return;
266 }
267
268 $entity = CollabEntityFactory::getByInternalObject($file, EntityType::File->value);
269 if ($entity === null)
270 {
271 return;
272 }
273
274 $collabId = $entity->getCollab()->getId();
275
276 $logEntry = new AddFilePublicLinkLogEntry(userId: $userId, collabId: $collabId);
277 $logEntry->setEntity($entity);
278 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
279 $service->save($logEntry);
280 }
281
282 public static function onAfterMarkDeletedObject(Event $event): void
283 {
284 [$file, $deletedBy, $deletedType] = $event->getParameters();
285 $deletedBy = (int)$deletedBy;
286 $type = (int)$file?->getType();
287
288 $isFile = $type > 0 && $type === (new ObjectType())->getFileType();
289
290 if ($deletedBy <= 0 || !$isFile)
291 {
292 return;
293 }
294
295 $entity = CollabEntityFactory::getByInternalObject($file, EntityType::File->value);
296 if ($entity === null)
297 {
298 return;
299 }
300
301 $collabId = $entity->getCollab()->getId();
302
303 $logEntry = new MoveFileToRecyclebinLogEntry(userId: $deletedBy, collabId: $collabId);
304 $logEntry->setEntity($entity);
305 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
306 $service->save($logEntry);
307 }
308
309 public static function onAfterCalendarEventDelete(int $eventId, array $entryFields, int $userId): void
310 {
311 $entity = CollabEntityFactory::getById($eventId, EntityType::CalendarEvent->value);
312
313 if ($entity === null)
314 {
315 return;
316 }
317
318 $collabId = $entity->getCollab()->getId();
319 $logEntry = new DeleteCalendarEventLogEntry(userId: $userId, collabId: $collabId, collabEntity: $entity);
320 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
321 $service->save($logEntry);
322 }
323
324 public static function onCollabUpdate(CollabUpdateEvent $event): void
325 {
326 $afterMembers = $event->getCollabAfter()->getUserMemberIds();
327 $beforeMembers = $event->getCollabBefore()->getUserMemberIds();
328 $excludedMembers = array_values(array_diff($beforeMembers, $afterMembers));
329
330 if (!empty($excludedMembers))
331 {
332 LastActivityTrigger::remove($event->getCollabAfter()->getId(), $excludedMembers);
333 }
334 }
335
336 public static function onCopyCollabInviteLink(Event $event): void
337 {
338 $userId = (int)$event->getParameter('userId');
339 $collabId = (int)$event->getParameter('collabId');
340
341 if (!CollabAccessController::can($userId, CollabDictionary::COPY_LINK, $collabId))
342 {
343 return;
344 }
345
346 ActionMessageFactory::getInstance()
347 ->getActionMessage(ActionType::CopyLink, $collabId, $userId)
348 ->send()
349 ;
350
351 $logEntry = new CopyInvitationLinkLogEntry(userId: $userId, collabId: $collabId);
352 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
353 $service->save($logEntry);
354 }
355
356 public static function onRegenerateCollabInviteLink(Event $event): void
357 {
358 $userId = (int)$event->getParameter('userId');
359 $collabId = (int)$event->getParameter('collabId');
360
361 if ($userId <= 0 || $collabId <= 0)
362 {
363 return;
364 }
365
366 ActionMessageFactory::getInstance()
367 ->getActionMessage(ActionType::RegenerateLink, $collabId, $userId)
368 ->send()
369 ;
370 }
371
372 private static function sendAddEvent(CollabEntity $entity): void
373 {
375
376 $event->send();
377 }
378
379 private static function sendUpdateEvent(array $changes, ?CollabEntity $entity = null): void
380 {
381 $event = new CollabEntityUpdateEvent($entity, $changes);
382
383 $event->send();
384 }
385
386 private static function sendDeleteEvent(CollabEntity $entity): void
387 {
388 $event = new CollabEntityDeleteEvent($entity);
389
390 $event->send();
391 }
392}
$type
Определения options.php:106
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
Определения event.php:5
static getByInternalObject(mixed $internalObject, EntityType|string $type)
Определения CollabEntityFactory.php:25
static getById(int $id, EntityType|string $type)
Определения CollabEntityFactory.php:20
static onAfterAddExternalLinkToObject(Event $event)
Определения EventDispatcher.php:254
static onCollabUpdate(CollabUpdateEvent $event)
Определения EventDispatcher.php:324
static onCountersRecount(int $collabId, array $counters, string $entityType)
Определения EventDispatcher.php:41
static onRegenerateCollabInviteLink(Event $event)
Определения EventDispatcher.php:356
static onTaskAdd(int $taskId, array $fields=[], array $parameters=[])
Определения EventDispatcher.php:49
static OnAfterCalendarEventEdit(array $fields, bool $isNew, int $userId)
Определения EventDispatcher.php:155
static onTaskUpdate(int $taskId, array $changes=[], array $fields=[], array $parameters=[])
Определения EventDispatcher.php:68
static onTaskDelete(int $taskId, array $parameters)
Определения EventDispatcher.php:110
static onAfterCalendarEventDelete(int $eventId, array $entryFields, int $userId)
Определения EventDispatcher.php:309
Определения AddCalendarEventLogEntry.php:8
Определения AddFileLogEntry.php:8
Определения AddFilePublicLinkLogEntry.php:8
Определения AddTaskLogEntry.php:8
Определения CompleteTaskLogEntry.php:8
Определения CopyInvitationLinkLogEntry.php:8
Определения DeleteCalendarEventLogEntry.php:8
Определения DeleteFileLogEntry.php:9
Определения DeleteTaskLogEntry.php:8
Определения MoveFileToRecyclebinLogEntry.php:8
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$entity
$service
Определения payment.php:18
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$counters
Определения options.php:100
$fields
Определения yandex_run.php:501