1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
groupinvite.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Internals\Counter\Processor;
4
5use Bitrix\Calendar\Core\Event\Tools\Dictionary;
6use Bitrix\Calendar\Integration\Pull\PushCommand;
7use Bitrix\Calendar\Integration\Pull\PushService;
8use Bitrix\Calendar\Integration\SocialNetwork\Collab\counter\CollabListener;
9use Bitrix\Calendar\Integration\SocialNetwork\UserGroupService;
10use Bitrix\Calendar\Internals\Counter;
11use Bitrix\Calendar\Internals\Counter\CounterDictionary;
12use Bitrix\Calendar\Internals\Counter\Event\Event;
13use Bitrix\Calendar\Internals\Counter\Event\EventCollection;
14use Bitrix\Calendar\Internals\Counter\Event\EventDictionary;
15use Bitrix\Calendar\Internals\EventTable;
16use Bitrix\Main\ArgumentException;
17use Bitrix\Main\ObjectPropertyException;
18use Bitrix\Main\ORM\Fields\ExpressionField;
19use Bitrix\Main\SystemException;
20
21final class GroupInvite implements Base
22{
23 private const SUPPORTED_EVENTS = [
24 EventDictionary::EVENT_ATTENDEES_UPDATED,
25 ];
26
27 public function process(): void
28 {
29 $events = (EventCollection::getInstance())->list();
30
31 foreach ($events as $event)
32 {
33 /* @var $event Event */
34 $eventType = $event->getType();
35
36 if (in_array($eventType, self::SUPPORTED_EVENTS, true))
37 {
38 $eventData = $event->getData();
39 $affectedUserIds = $eventData['user_ids'] ?? [];
40 $affectedEventIds = $eventData['event_ids'] ?? [];
41 $affectedGroupIds = $eventData['group_ids'] ?? [];
42 if (!$affectedUserIds || (!$affectedEventIds && !$affectedGroupIds))
43 {
44 continue;
45 }
46
47 $this->recountGroupEventsInvites($affectedUserIds, $affectedEventIds, $affectedGroupIds);
48 }
49 }
50 }
51
60 private function recountGroupEventsInvites(array $userIds, array $eventIds, array $groupIds): void
61 {
62 $groupsToRecount = [];
63
64 if (!empty($eventIds))
65 {
66 $parentIdsOfAffectedEvents = $this->getParentIdsOfAffectedEvents($eventIds);
67 $groupsToRecount = $this->getAffectedGroupIds($parentIdsOfAffectedEvents);
68 }
69
70 if (!empty($groupIds))
71 {
72 $groupsToRecount = array_unique([...$groupsToRecount, ...$groupIds]);
73 }
74
75 if (empty($groupsToRecount))
76 {
77 return;
78 }
79
80 // filter affected groups, keep those to which affected users belong
81 $userAffectedGroups = [];
82 foreach ($userIds as $userId)
83 {
84 if ($userId <= 0)
85 {
86 continue;
87 }
88
89 // get all user groups and check if user belongs any affected group
90 $userGroups = array_keys(UserGroupService::getInstance()->getUserGroups($userId));
91 $affectedGroupsForUser = array_intersect($userGroups, $groupsToRecount);
92 if (!$userGroups || !$affectedGroupsForUser)
93 {
94 continue;
95 }
96
97 $userAffectedGroups[$userId] = $affectedGroupsForUser;
98 }
99
100 // get unique group ids which needs to recount for some of affected users
101 $groupsToRecountForAffectedUsers = array_unique(array_values(array_merge(...$userAffectedGroups)));
102 if (!$groupsToRecountForAffectedUsers)
103 {
104 return;
105 }
106
107 $groupsToNotify = [];
108
109 // preload all actual events from affected groups and sort it by groupId
110 $eventsByGroup = $this->prepareGroupEvents($groupsToRecountForAffectedUsers);
111
112 foreach ($userIds as $userId)
113 {
114 if ($userId <= 0 || !($userAffectedGroups[$userId] ?? []))
115 {
116 continue;
117 }
118
119 foreach ($userAffectedGroups[$userId] as $groupId)
120 {
121 $parentGroupEvents = $eventsByGroup[$groupId] ?? [];
122 if (!$parentGroupEvents)
123 {
124 $this->cleanGroupCounter($userId, $groupId);
125
126 continue;
127 }
128
129 $groupsToNotify[$groupId] ??= [];
130 $groupsToNotify[$groupId][] = $userId;
131
132 $this->updateGroupCounter($userId, $groupId, $parentGroupEvents);
133 }
134 }
135
136 if (!empty($groupsToNotify))
137 {
138 (new CollabListener())->notify($groupsToNotify);
139 }
140 }
141
152 private function cleanGroupCounter(int $userId, int $groupId): void
153 {
154 \CUserCounter::Set(
155 user_id: $userId,
156 code: $this->getGroupCounterCode($groupId),
157 value: 0,
158 site_id: '**',
159 sendPull: false,
160 );
161
162 $this->sendGroupCountersPush($userId, $groupId);
163 }
164
175 private function updateGroupCounter(int $userId, int $groupId, array $parentIds): void
176 {
177 $actualValue = $this->getActualUserInvitesCount($userId, $parentIds);
178
179 $counterCode = $this->getGroupCounterCode($groupId);
180 $storedValue = \CUserCounter::GetValue(
181 user_id: $userId,
182 code: $counterCode,
183 );
184
185 if ((!$actualValue && !$storedValue) || $actualValue === $storedValue)
186 {
187 return;
188 }
189
190 \CUserCounter::Set(
191 user_id: $userId,
192 code: $counterCode,
193 value: $actualValue,
194 site_id: '**',
195 sendPull: false,
196 );
197
198 $this->sendGroupCountersPush($userId, $groupId);
199 }
200
209 private function getParentIdsOfAffectedEvents(array $eventIds): array
210 {
211 return array_unique(
212 EventTable::query()
213 ->whereIn('ID', $eventIds)
214 ->where('IS_MEETING', 1)
215 ->setSelect(['PARENT_ID'])
216 ->fetchCollection()
217 ->getParentIdList()
218 );
219 }
220
229 private function getAffectedGroupIds(array $eventIds): array
230 {
231 return array_unique(
232 EventTable::query()
233 ->whereIn('ID', $eventIds)
234 ->where('CAL_TYPE', Dictionary::CALENDAR_TYPE['group'])
235 ->where('IS_MEETING', 1)
236 ->setSelect(['OWNER_ID'])
237 ->fetchCollection()
238 ->getOwnerIdList()
239 );
240 }
241
250 private function prepareGroupEvents(array $allGroupIds): array
251 {
252 $groupEvents = $this->getGroupsEvents($allGroupIds);
253 $eventsByGroup = [];
254
255 foreach ($groupEvents as $eventId => $groupId)
256 {
257 $eventsByGroup[(int)$groupId][] = $eventId;
258 }
259
260 return $eventsByGroup;
261 }
262
271 private function getGroupsEvents(array $groupIds): array
272 {
273 [$fromTimestamp, $toTimestamp] = $this->getPeriod();
274
275 $result = EventTable::query()
276 ->whereIn('OWNER_ID', $groupIds)
277 ->where('CAL_TYPE', Dictionary::CALENDAR_TYPE['group'])
278 ->where('DATE_FROM_TS_UTC', '>=', $fromTimestamp)
279 ->where('DATE_TO_TS_UTC', '<=', $toTimestamp)
280 ->where('IS_MEETING', 1)
281 ->where('MEETING_STATUS', 'H')
282 ->setSelect(['ID', 'OWNER_ID'])
283 ->fetchCollection();
284
285 return array_combine($result->getIdList(), $result->getOwnerIdList());
286 }
287
297 private function getActualUserInvitesCount(int $userId, array $parentIds): int
298 {
299 [$fromTimestamp, $toTimestamp] = $this->getPeriod();
300
301 return (int)EventTable::query()
302 ->where('OWNER_ID', $userId)
303 ->whereIn('PARENT_ID', $parentIds)
304 ->where('CAL_TYPE', Dictionary::CALENDAR_TYPE['user'])
305 ->where('DATE_FROM_TS_UTC', '>=', $fromTimestamp)
306 ->where('DATE_TO_TS_UTC', '<=', $toTimestamp)
307 ->where('IS_MEETING', 1)
308 ->where('MEETING_STATUS', 'Q')
309 ->where('DELETED', 'N')
310 ->registerRuntimeField('COUNT', new ExpressionField('COUNT', 'COUNT(*)'))
311 ->setSelect(['COUNT'])
312 ->fetch()['COUNT']
313 ;
314 }
315
316 private function getPeriod(): array
317 {
318 $fromTimestamp = (int)\CCalendar::Timestamp(\CCalendar::Date(time(), false), false);
319 $toTimestamp = (int)\CCalendar::Timestamp(\CCalendar::Date(time() + \CCalendar::DAY_LENGTH * 90, false), false)
320 + \CCalendar::GetDayLen()
321 - 1;
322
323 return [$fromTimestamp, $toTimestamp];
324 }
325
336 private function sendGroupCountersPush(int $userId, int $groupId): void
337 {
338 PushService::addEvent($userId, [
339 'module_id' => PushService::MODULE_ID,
340 'command' => PushCommand::UpdateGroupCounters->value,
341 'params' => [
342 'groupId' => $groupId,
343 'counters' => [
345 ],
346 ],
347 ]);
348 }
349
355 private function getGroupCounterCode(int $groupId): string
356 {
357 return sprintf(CounterDictionary::COUNTER_GROUP_INVITES_TPL, $groupId);
358 }
359}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getInstance()
Определения application.php:98
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$event
Определения prolog_after.php:141