1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
accessibility.php
См. документацию.
1<?php
2
4
13
15{
16 private array $canSeeNameCache = [];
17 private int $skipEventId = 0;
18
19 public function setSkipEventId(int $curEventId): self
20 {
21 $this->skipEventId = $curEventId;
22
23 return $this;
24 }
25
29 public function getBusyUsersIds(array $userIds, int $timestampFrom, int $timestampTo): array
30 {
31 $dateFromTs = $timestampFrom - ($timestampFrom % \CCalendar::DAY_LENGTH);
32 $dateToTs = $timestampTo - ($timestampTo % \CCalendar::DAY_LENGTH);
33 $accessibility = $this
34 ->getAccessibility($userIds, $dateFromTs, $dateToTs)
35 ;
36
37 $busyUsersList = [];
38 $timezoneName = \CCalendar::GetUserTimezoneName(\CCalendar::GetUserId());
39 $timezoneOffset = Util::getTimezoneOffsetUTC($timezoneName);
40 foreach ($accessibility as $userId => $events)
41 {
42 foreach ($events as $accessibilityItem)
43 {
44 $itemFrom = \CCalendar::TimestampUTC($accessibilityItem['from']);
45 $itemTo = \CCalendar::TimestampUTC($accessibilityItem['to']);
46
47 if ($accessibilityItem['isFullDay'])
48 {
49 $itemFrom -= $timezoneOffset;
50 $itemTo -= $timezoneOffset;
51 }
52
53 if (Util::doIntervalsIntersect($timestampFrom, $timestampTo, $itemFrom, $itemTo))
54 {
55 $busyUsersList[] = $userId;
56 continue 2;
57 }
58 }
59 }
60
61 return $busyUsersList;
62 }
63
67 public function getAccessibility(array $userIds, int $timestampFrom, int $timestampTo): array
68 {
69 $accessibilityTs = $this->getAccessibilityTs($userIds, $timestampFrom, $timestampTo);
70
71 return $this->formatAccessibilityTs($accessibilityTs);
72 }
73
74 private function formatAccessibilityTs(array $accessibilityTs): array
75 {
76 return array_map(
77 fn(array $userAccessibility) => array_map(
78 fn(array $accessibilityItem) => array_merge($accessibilityItem, [
79 'from' => $this->formatTimestamp($accessibilityItem['from'], $accessibilityItem['isFullDay']),
80 'to' => $this->formatTimestamp($accessibilityItem['to'], $accessibilityItem['isFullDay']),
81 ]),
82 $userAccessibility,
83 ),
84 $accessibilityTs,
85 );
86 }
87
88 private function formatTimestamp(int $timestamp, bool $isFullDay): string
89 {
90 if ($isFullDay)
91 {
92 return Util::formatDateTimestampUTC($timestamp);
93 }
94
95 return Util::formatDateTimeTimestampUTC($timestamp);
96 }
97
101 public function getAccessibilityTs(array $userIds, int $timestampFrom, int $timestampTo): array
102 {
103 $accessibility = [];
104
105 if (empty($userIds))
106 {
107 return $accessibility;
108 }
109
110 $events = $this->getEventsTs($userIds, $timestampFrom, $timestampTo);
111 $absences = $this->getAbsencesTs($userIds, $timestampFrom, $timestampTo);
112
113 foreach ($userIds as $userId)
114 {
115 $accessibility[$userId] = array_merge($events[$userId] ?? [], $absences[$userId] ?? []);
116 }
117
118 return $accessibility;
119 }
120
124 public function getEventsTs(array $userIds, int $timestampFrom, int $timestampTo): array
125 {
126 [$from, $to] = $this->formatLimitFromTimestamps($timestampFrom, $timestampTo);
127 $events = \CCalendarEvent::GetList([
128 'arFilter' => [
129 'FROM_LIMIT' => $from,
130 'TO_LIMIT' => $to,
131 'CAL_TYPE' => [
132 Dictionary::CALENDAR_TYPE['user'],
133 Dictionary::CALENDAR_TYPE['open_event'],
134 ],
135 'OWNER_ID' => $userIds,
136 'ACTIVE_SECTION' => 'Y'
137 ],
138 'arSelect' => \CCalendarEvent::$defaultSelectEvent,
139 'getUserfields' => false,
140 'parseRecursion' => true,
141 'fetchAttendees' => false,
142 'fetchSection' => true,
143 'parseDescription' => false,
144 'setDefaultLimit' => false,
145 'checkPermissions' => false,
146 ]);
147
148 $accessibility = $this->initAccessibility($userIds);
149 foreach ($events as $event)
150 {
151 if ((int)$event['ID'] === $this->skipEventId || (int)$event['PARENT_ID'] === $this->skipEventId)
152 {
153 continue;
154 }
155 if ($event['ACCESSIBILITY'] === 'free')
156 {
157 continue;
158 }
159 if ($event['IS_MEETING'] && $event['MEETING_STATUS'] === 'N')
160 {
161 continue;
162 }
163 if (\CCalendarSect::CheckGoogleVirtualSection($event['SECTION_DAV_XML_ID']))
164 {
165 continue;
166 }
167
168 $isFullDay = $event['DT_SKIP_TIME'] === 'Y';
169 if ($isFullDay)
170 {
171 $from = \CCalendar::TimestampUTC($event['DATE_FROM']);
172 $to = \CCalendar::TimestampUTC($event['DATE_TO']) + \CCalendar::GetDayLen();
173 }
174 else
175 {
176 $from = Helper::getEventTimestampUTC(new DateTime($event['DATE_FROM']), $event['TZ_FROM']);
177 $to = Helper::getEventTimestampUTC(new DateTime($event['DATE_TO']), $event['TZ_TO']);
178 }
179 $accessibility[$event['OWNER_ID']][] = [
180 'id' => (int)$event['ID'],
181 'parentId' => (int)$event['PARENT_ID'],
182 'name' => $this->getEventName($event),
183 'from' => $from,
184 'to' => $to,
185 'isFullDay' => $isFullDay,
186 ];
187 }
188
189 return $accessibility;
190 }
191
192 private function getEventName(array $event): string
193 {
194 if (!$this->canSeeName($event))
195 {
196 return '[' . Loc::getMessage('EC_ACCESSIBILITY_BUSY') . ']';
197 }
198
199 return !empty($event['NAME']) ? $event['NAME'] : Loc::getMessage('EC_T_NEW_EVENT');
200 }
201
202 private function canSeeName(array $event): bool
203 {
204 $currentUserId = \CCalendar::GetUserId();
205 $eventId = (int)$event['ID'];
206 $cachedValue = $this->canSeeNameCache[$eventId] ?? null;
207
208 if ($cachedValue === null)
209 {
210 $accessController = new EventAccessController($currentUserId);
211 $eventModel = EventModel::createFromArray($event);
212
213 $canViewTitle = $accessController->check(ActionDictionary::ACTION_EVENT_VIEW_TITLE, $eventModel);
214 $this->canSeeNameCache[$eventId] = !$this->isPrivate($event) && $canViewTitle;
215 }
216
217 return $this->canSeeNameCache[$eventId];
218 }
219
220 private function isPrivate(array $event): bool
221 {
222 $curUserId = \CCalendar::GetUserId();
223
224 return $event['PRIVATE_EVENT'] && $event['CAL_TYPE'] === 'user' && $event['OWNER_ID'] !== $curUserId;
225 }
226
230 public function getAbsencesTs(array $userIds, int $timestampFrom, int $timestampTo): array
231 {
232 if (!\CCalendar::IsIntranetEnabled())
233 {
234 return [];
235 }
236
237 [$from, $to] = $this->formatLimitFromTimestamps($timestampFrom, $timestampTo);
238 $usersAbsence = \CIntranetUtils::GetAbsenceData(
239 array(
240 'DATE_START' => $from,
241 'DATE_FINISH' => $to,
242 'USERS' => $userIds,
243 'PER_USER' => true,
244 ),
245 BX_INTRANET_ABSENCE_HR,
246 );
247
248 $absenceTypes = \Bitrix\Intranet\UserAbsence::getVacationTypes();
249 $vacationTypes = array_filter(
250 $absenceTypes,
251 fn ($type) => in_array($type['ID'], ['VACATION', 'LEAVESICK', 'LEAVEMATERINITY', 'LEAVEUNPAYED'], true),
252 );
253 $vacationTypesIds = array_map(fn ($type) => (int)$type['ENUM_ID'], $vacationTypes);
254
255 $offset = (int)date('Z') + \CCalendar::GetOffset(\CCalendar::GetUserId());
256 $accessibility = $this->initAccessibility($userIds);
257 foreach($usersAbsence as $userId => $absenceData)
258 {
259 foreach($absenceData as $event)
260 {
261 $from = \CCalendar::TimestampUTC($event['DATE_ACTIVE_FROM']);
262 $to = \CCalendar::TimestampUTC($event['DATE_ACTIVE_TO']);
263 $isFullDay = $this->isFullDay($event['DATE_ACTIVE_FROM'], $event['DATE_ACTIVE_TO']);
264
265 if ($this->isDateWithoutTimeOrIsMidnight($event['DATE_ACTIVE_TO']))
266 {
267 $to += \CCalendar::GetDayLen();
268 }
269
270 if (!$isFullDay)
271 {
272 $from -= $offset;
273 $to -= $offset;
274 }
275
276 $accessibility[$userId][] = [
277 'from' => $from,
278 'to' => $to,
279 'isFullDay' => $isFullDay,
280 'name' => $event['PROPERTY_ABSENCE_TYPE_VALUE'] ?? null,
281 'isVacation' => in_array((int)$event['PROPERTY_ABSENCE_TYPE_ENUM_ID'], $vacationTypesIds, true),
282 ];
283 }
284 }
285
286 return $accessibility;
287 }
288
289 private function isFullDay(string $from, string $to): bool
290 {
291 return $this->isDateWithoutTimeOrIsMidnight($from) && $this->isDateWithoutTimeOrIsMidnight($to);
292 }
293
294 private function isDateWithoutTimeOrIsMidnight(string $date): bool
295 {
296 return \CCalendar::TimestampUTC(Util::formatDateTimeTimestampUTC(\CCalendar::TimestampUTC($date)))
297 === \CCalendar::TimestampUTC(Util::formatDateTimestampUTC(\CCalendar::TimestampUTC($date)));
298 }
299
300 private function formatLimitFromTimestamps(int $timestampFrom, int $timestampTo): array
301 {
302 return [
303 Util::formatDateTimeTimestampUTC($timestampFrom),
305 ];
306 }
307
308 private function initAccessibility(array $userIds): array
309 {
310 $accessibility = [];
311 foreach($userIds as $userId)
312 {
313 $accessibility[$userId] = [];
314 }
315
316 return $accessibility;
317 }
318}
$type
Определения options.php:106
$accessController
Определения options.php:23
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
getAccessibilityTs(array $userIds, int $timestampFrom, int $timestampTo)
Определения accessibility.php:101
getEventsTs(array $userIds, int $timestampFrom, int $timestampTo)
Определения accessibility.php:124
setSkipEventId(int $curEventId)
Определения accessibility.php:19
getAccessibility(array $userIds, int $timestampFrom, int $timestampTo)
Определения accessibility.php:67
getAbsencesTs(array $userIds, int $timestampFrom, int $timestampTo)
Определения accessibility.php:230
getBusyUsersIds(array $userIds, int $timestampFrom, int $timestampTo)
Определения accessibility.php:29
Определения util.php:21
static getTimezoneOffsetUTC(string $timezoneName)
Определения util.php:767
static doIntervalsIntersect($from1, $to1, $from2, $to2)
Определения util.php:839
static formatDateTimestampUTC(int $timestamp)
Определения util.php:760
static formatDateTimeTimestampUTC(int $timestamp)
Определения util.php:753
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$event
Определения prolog_after.php:141