Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
2
4
5use Bitrix\Bitrix24\Form\AbuseZoneMap;
12use Bitrix\Main;
16use CFile;
17use CUser;
19use CUserOptions;
21
22class Helper
23{
24 private const PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME = 'payAttentionToNewSharingJointFeature';
25 private const PAY_ATTENTION_TO_NEW_FEATURE_JOINT = 'joint-sharing';
26 private const WEEK_TIMESTAMP = 604800; // 86400 * 7
27
28 public const ACTION = 'action';
29 public const ICS = 'ics';
30 public const CANCEL = 'cancel';
31 public const CONFERENCE = 'videoconference';
32 public const OWNER_CREATED = 'ownerCreated';
33 public const ACTION_ICS = '?'.self::ACTION.'='.self::ICS;
34 public const ACTION_CANCEL = '?'.self::ACTION.'='.self::CANCEL;
35 public const ACTION_CONFERENCE = '?'.self::ACTION.'='.self::CONFERENCE;
36
37 protected const ABUSE_SENDER_PAGE = 'page';
38 protected const ABUSE_SENDER_EMAIL = 'email';
39
44 public static function payAttentionToNewSharingFeature(): ?string
45 {
46 $now = time();
47 $defaultValue = 'unset';
48 $optionValue = CUserOptions::getOption(
49 'calendar',
50 self::PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME,
51 $defaultValue
52 );
53
54 if ($optionValue === $defaultValue)
55 {
56 CUserOptions::setOption(
57 'calendar',
58 self::PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME,
59 $now
60 );
61
62 return null;
63 }
64
65 if ($optionValue === 'N')
66 {
67 return null;
68 }
69
70 $timestamp = (int)$optionValue;
71 if ($timestamp && ($now > $timestamp + self::WEEK_TIMESTAMP))
72 {
73 return self::PAY_ATTENTION_TO_NEW_FEATURE_JOINT;
74 }
75
76 return null;
77 }
78
83 public static function disableOptionPayAttentionToNewSharingFeature(): void
84 {
85 $value = 'N';
86 $defaultValue = 'unset';
87 $optionValue = CUserOptions::getOption(
88 'calendar',
89 self::PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME,
90 $defaultValue
91 );
92
93 if ($optionValue === $defaultValue)
94 {
95 $value = time();
96 }
97
98 CUserOptions::setOption(
99 'calendar',
100 self::PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME,
101 $value
102 );
103 }
104
109 public static function isPhoneFeatureEnabled(): bool
110 {
111 return false;
112 }
113
119 public static function isMailFeatureEnabled(): bool
120 {
121 if (Loader::includeModule('bitrix24'))
122 {
123 return \CBitrix24::isEmailConfirmed();
124 }
125
126 return true;
127 }
128
134 public static function getShortUrl(string $url): string
135 {
136 return \CCalendar::GetServerPath() . \CBXShortUri::getShortUri($url);
137 }
138
144 public static function getPersonFullNameLoc(?string $name, ?string $lastName): string
145 {
146 $culture = Main\Application::getInstance()->getContext()->getCulture();
147 $nameFormat = is_null($culture) ? '#NAME# #LAST_NAME#' : $culture->getNameFormat();
148
149 return trim(str_replace(
150 ['#NAME#', '#LAST_NAME#'],
151 [$name ?? '', $lastName ?? ''],
152 $nameFormat,
153 ));
154 }
155
160 public static function getOwnerInfo(int $id): array
161 {
162 $user = CUser::GetByID($id)->Fetch();
163 $arFileTmp = CFile::ResizeImageGet(
164 $user["PERSONAL_PHOTO"],
165 array('width' => 512, 'height' => 512),
166 BX_RESIZE_IMAGE_EXACT,
167 false,
168 false,
169 true,
170 );
171
172 return [
173 'id' => $user['ID'],
174 'name' => $user['NAME'],
175 'lastName' => $user['LAST_NAME'],
176 'photo' => $arFileTmp['src'] ?? null,
177 'gender' => $user['PERSONAL_GENDER'] ?? null,
178 ];
179 }
180
188 public static function formatDate(Date $date): string
189 {
190 $culture = Main\Application::getInstance()->getContext()->getCulture();
191 $dayMonthFormat = Main\Type\Date::convertFormatToPhp($culture->get('DAY_MONTH_FORMAT'));
192 $timeFormat = $culture->get('SHORT_TIME_FORMAT');
193 $weekDayFormat = 'l';
194
195 $timestampUTCWithServerOffset = self::getUserDateTimestamp($date);
196 $dayMonth = FormatDate($dayMonthFormat, $timestampUTCWithServerOffset);
197 $time = FormatDate($timeFormat, $timestampUTCWithServerOffset);
198 $weekDay = mb_strtolower(FormatDate($weekDayFormat, $timestampUTCWithServerOffset));
199
200 return "$dayMonth $time, $weekDay";
201 }
202
209 public static function formatDateWithoutTime(Date $date): string
210 {
211 $culture = Main\Application::getInstance()->getContext()->getCulture();
212 $dayMonthFormat = Main\Type\Date::convertFormatToPhp($culture->get('DAY_MONTH_FORMAT'));
213 $weekDayFormat = 'l';
214
215 $timestamp = $date->getTimestamp();
216 $dayMonth = FormatDate($dayMonthFormat, $timestamp);
217 $weekDay = mb_strtolower(FormatDate($weekDayFormat, $timestamp));
218
219 return "$dayMonth, $weekDay";
220 }
221
228 public static function formatDateShort(Date $date): string
229 {
230 $culture = Main\Application::getInstance()->getContext()->getCulture();
231 $formatShort = Main\Type\Date::convertFormatToPhp($culture->get('FORMAT_DATE'));
232
233 return FormatDate($formatShort, $date->getTimestamp());
234 }
235
242 public static function formatTimeInterval(Date $from, Date $to, bool $isFullDay): string
243 {
244 $isLongDateTimeFormat = false;
245
246 $culture = Context::getCurrent()->getCulture();
247 $formattedDateFrom = FormatDate($culture->getFullDateFormat(), self::getUserDateTimestamp($from));
248 $formattedDateTo = '';
249 $formattedTimeFrom = '';
250 $formattedTimeTo = '';
251
252 if ($to->format('j') !== $from->format('j')
253 || $to->format('Y') !== $from->format('Y')
254 || $to->format('n') !== $from->format('n')
255 )
256 {
257 $isLongDateTimeFormat = true;
258 $formattedDateTo = FormatDate($culture->getFullDateFormat(), self::getUserDateTimestamp($to));
259 }
260
261 if ($isFullDay)
262 {
263 if (!isset($formattedDateTo))
264 {
265 $formattedDateTo = FormatDate($culture->getFullDateFormat(), self::getUserDateTimestamp($to));
266 }
267 }
268 else
269 {
270 $formattedTimeFrom = FormatDate($culture->getShortTimeFormat(), self::getUserDateTimestamp($from));
271 $formattedTimeTo = FormatDate($culture->getShortTimeFormat(), self::getUserDateTimestamp($to));
272 }
273
274 if ($isFullDay)
275 {
276 if ($isLongDateTimeFormat)
277 {
278 return $formattedDateFrom . " - " . $formattedDateTo;
279 }
280
281 return $formattedDateFrom . Loc::getMessage('EC_VIEW_FULL_DAY');
282 }
283
284 if ($isLongDateTimeFormat)
285 {
286 return $formattedDateFrom . ' ' . $formattedTimeFrom . ' - ' . $formattedDateTo . ' ' . $formattedTimeTo;
287 }
288
289 return $formattedDateFrom . ' ' . $formattedTimeFrom . ' - ' . $formattedTimeTo;
290 }
291
297 public static function formatTimezone(DateTimeZone $timezone): string
298 {
299 $utcOffset = "UTC";
300 if ($timezone->getTimeZone()->getOffset(new \DateTime('now')) !== 0)
301 {
302 $time = new \DateTime('now', $timezone->getTimeZone());
303 $utcOffset .= " " . $time->format('P');
304 }
305
306 return "($utcOffset) " . $timezone->toString();
307 }
308
313 public static function getUserDateTimestamp(Date $date): int
314 {
315 $dateTimezone = new \DateTimeZone($date->getFields()['timezone']);
316 $serverTimezone = (new \DateTime())->getTimezone();
317
318 $dateOffset = $dateTimezone->getOffset(new \DateTime());
319 $serverOffset = $serverTimezone->getOffset(new \DateTime());
320 $offset = - $serverOffset + $dateOffset;
321
322 $userDate = clone $date;
323 $userDate->setTime($date->getHour(), $date->getMinutes(), $date->getSeconds() + $offset);
324
325 return $userDate->getTimestamp();
326 }
327
333 public static function getEventTimestampUTC(DateTime $date, ?string $eventTimezone = null): int
334 {
335 return \Bitrix\Calendar\Util::getDateTimestampUtc($date, $eventTimezone);
336 }
337
344 public static function createSharingLinkExpireDate(?DateTime $dateTime, string $linkType): ?DateTime
345 {
346 $result = null;
347
348 if (!$dateTime)
349 {
350 return null;
351 }
352
353 if (array_key_exists($linkType, Sharing\Link\Helper::LIFETIME))
354 {
355 $result = $dateTime->add(Sharing\Link\Helper::LIFETIME[$linkType]);
356 }
357
358 return $result;
359 }
360
366 public static function createSharingJointLinkExpireDate(?DateTime $dateTime): ?DateTime
367 {
368 return $dateTime?->add(Sharing\Link\Helper::LIFETIME[Sharing\Link\Helper::MULTI_LINK_TYPE]);
369 }
370
371 public static function getPageAbuseLink(int $ownerId, string $calendarLink): ?string
372 {
373 return self::getAbuseLink($ownerId, $calendarLink, self::ABUSE_SENDER_PAGE);
374 }
375
376 public static function getEmailAbuseLink(int $ownerId, string $calendarLink): ?string
377 {
378 return self::getAbuseLink($ownerId, $calendarLink, self::ABUSE_SENDER_EMAIL);
379 }
380
381 private static function getAbuseLink(int $ownerId, string $calendarLink, string $senderPage): ?string
382 {
383 if (!Main\Loader::includeModule('bitrix24'))
384 {
385 return null;
386 }
387
388 $owner = self::getOwnerInfo($ownerId);
389
390 $feedbackForm = new FeedbackForm('calendar_sharing_abuse');
391 $presets = $feedbackForm->getPresets();
392 $formParams = [
393 'hostname' => $presets['hostname'],
394 'b24_plan' => $presets['b24_plan'],
395 'sender_page' => $senderPage,
396 'admin_data' => \COption::GetOptionString('main', 'email_from', ''),
397 'user_data' => "id: {$owner['id']}, name: {$owner['name']} {$owner['lastName']}",
398 'calendar_link' => $calendarLink,
399 ];
400
401 $formParamsQuery = http_build_query($formParams);
402
403 $region = Main\Application::getInstance()->getLicense()->getRegion();
404 return AbuseZoneMap::getLink($region) . "?$formParamsQuery";
405 }
406
407 public static function getBitrix24Link(): ?string
408 {
409 if (!Main\Loader::includeModule('bitrix24'))
410 {
411 return null;
412 }
413
414 $region = Main\Application::getInstance()->getLicense()->getRegion();
415 $abuseLink = AbuseZoneMap::getLink($region);
416
417 $parsedUrl = parse_url($abuseLink);
418 $protocol = $parsedUrl['scheme'];
419 $host = $parsedUrl['host'];
420 $parsedUri = new Uri($protocol . '://' . $host);
421
422 return rtrim($parsedUri->getLocator(), '/');
423 }
424
425 public static function setSiteLanguage(): void
426 {
427 $siteDb = Main\SiteTable::getById(SITE_ID);
428 if ($site = $siteDb->fetchObject())
429 {
430 Loc::setCurrentLang($site->getLanguageId());
431 }
432 }
433}
format(string $format=null)
Definition date.php:107
setTime(int $hour, int $minutes, int $seconds)
Definition date.php:162
static getPageAbuseLink(int $ownerId, string $calendarLink)
Definition helper.php:371
static formatDate(Date $date)
Definition helper.php:188
static getOwnerInfo(int $id)
Definition helper.php:160
static getEmailAbuseLink(int $ownerId, string $calendarLink)
Definition helper.php:376
static formatTimeInterval(Date $from, Date $to, bool $isFullDay)
Definition helper.php:242
static disableOptionPayAttentionToNewSharingFeature()
Definition helper.php:83
static formatDateShort(Date $date)
Definition helper.php:228
static getEventTimestampUTC(DateTime $date, ?string $eventTimezone=null)
Definition helper.php:333
static payAttentionToNewSharingFeature()
Definition helper.php:44
static getShortUrl(string $url)
Definition helper.php:134
static getPersonFullNameLoc(?string $name, ?string $lastName)
Definition helper.php:144
static createSharingLinkExpireDate(?DateTime $dateTime, string $linkType)
Definition helper.php:344
static createSharingJointLinkExpireDate(?DateTime $dateTime)
Definition helper.php:366
static formatTimezone(DateTimeZone $timezone)
Definition helper.php:297
static formatDateWithoutTime(Date $date)
Definition helper.php:209
static getUserDateTimestamp(Date $date)
Definition helper.php:313
static getCurrent()
Definition context.php:241
static setCurrentLang($language)
Definition loc.php:95
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
add($interval)
Definition date.php:145