1C-Bitrix 25.700.0
helper.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sharing;
4
5use Bitrix\Bitrix24\Form\AbuseZoneMap;
6use Bitrix\Calendar\Core\Base\Date;
7use Bitrix\Calendar\Core\Base\DateTimeZone;
8use Bitrix\Calendar\Integration\SocialNetwork\GroupService;
9use Bitrix\Main\Web\Uri;
10use Bitrix\UI\Form\FeedbackForm;
11use Bitrix\Main;
12use Bitrix\Main\Loader;
13use Bitrix\Main\Localization\Loc;
14use Bitrix\Main\Type\DateTime;
15use CFile;
16use CUser;
17use Bitrix\Main\Context;
18use CUserOptions;
19use Bitrix\Calendar\Sharing;
20
21class Helper
22{
23 private const PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME = 'payAttentionToNewSharingJointFeature';
24 private const PAY_ATTENTION_TO_NEW_FEATURE_JOINT = 'joint-sharing';
25 private const WEEK_TIMESTAMP = 604800; // 86400 * 7
26
27 public const ACTION = 'action';
28 public const ICS = 'ics';
29 public const CANCEL = 'cancel';
30 public const CONFERENCE = 'videoconference';
31 public const OPENED = 'opened';
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 public const ACTION_OPENED = '?'.self::ACTION.'='.self::OPENED;
37
38 protected const ABUSE_SENDER_PAGE = 'page';
39 protected const ABUSE_SENDER_EMAIL = 'email';
40
41 private static array $shortUrl = [];
42
46 public static function payAttentionToNewSharingFeature(): ?string
47 {
48 return null;
49 }
50
55 public static function disableOptionPayAttentionToNewSharingFeature(): void
56 {
57 $value = 'N';
58 $defaultValue = 'unset';
59 $optionValue = CUserOptions::getOption(
60 'calendar',
61 self::PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME,
63 );
64
66 {
67 $value = time();
68 }
69
70 CUserOptions::setOption(
71 'calendar',
72 self::PAY_ATTENTION_TO_NEW_SHARING_JOINT_FEATURE_OPTION_NAME,
73 $value,
74 );
75 }
76
81 public static function isPhoneFeatureEnabled(): bool
82 {
83 return false;
84 }
85
91 public static function isMailFeatureEnabled(): bool
92 {
93 if (Loader::includeModule('bitrix24'))
94 {
95 return \CBitrix24::isEmailConfirmed();
96 }
97
98 return true;
99 }
100
106 public static function getShortUrl(string $url): string
107 {
108 if (isset(self::$shortUrl[$url]))
109 {
110 return self::$shortUrl[$url];
111 }
112
113 self::$shortUrl[$url] = \CCalendar::GetServerPath() . \CBXShortUri::getShortUri($url);
114
115 return self::$shortUrl[$url];
116 }
117
123 public static function getPersonFullNameLoc(?string $name, ?string $lastName): string
124 {
125 $culture = Main\Application::getInstance()->getContext()->getCulture();
126 $nameFormat = is_null($culture) ? '#NAME# #LAST_NAME#' : $culture->getNameFormat();
127
128 return trim(str_replace(
129 ['#NAME#', '#LAST_NAME#'],
130 [$name ?? '', $lastName ?? ''],
132 ));
133 }
134
139 public static function getOwnerInfo(int $id): array
140 {
141 return self::getUserInfo($id);
142 }
143
144 public static function getUserInfo(int $userId): array
145 {
146 $user = CUser::getById($userId)->fetch();
147
148 $personalPhoto = CFile::resizeImageGet(
149 $user['PERSONAL_PHOTO'],
150 ['width' => 512, 'height' => 512],
152 false,
153 false,
154 true,
155 );
156
157 return [
158 'id' => $user['ID'],
159 'name' => $user['NAME'],
160 'lastName' => $user['LAST_NAME'],
161 'photo' => $personalPhoto['src'] ?? null,
162 'gender' => $user['PERSONAL_GENDER'] ?? null,
163 ];
164 }
165
166 public static function getGroupOwnerInfo(int $groupId): array
167 {
168 $group = GroupService::getInstance()->getGroup($groupId);
169 $groupInfo = (new Sharing\Link\Member\Group($group))->toArray();
170
171 return [
172 'id' => $groupInfo['id'],
173 'name' => $groupInfo['name'],
174 'lastName' => null,
175 'photo' => $groupInfo['avatar'],
176 'gender' => null,
177 ];
178 }
179
180 public static function getLinkOwnerInfo(array $link): array
181 {
182 return match ($link['type']) {
183 Sharing\Link\Helper::GROUP_SHARING_TYPE => self::getGroupOwnerInfo($link['groupId']),
184 default => self::getOwnerInfo($link['userId']),
185 };
186 }
187
195 public static function formatDate(Date $date): string
196 {
197 $culture = Main\Application::getInstance()->getContext()->getCulture();
198 $dayMonthFormat = Main\Type\Date::convertFormatToPhp($culture->get('DAY_MONTH_FORMAT'));
199 $timeFormat = $culture->get('SHORT_TIME_FORMAT');
200 $weekDayFormat = 'l';
201
202 $timestampUTCWithServerOffset = self::getUserDateTimestamp($date);
203 $dayMonth = FormatDate($dayMonthFormat, $timestampUTCWithServerOffset);
204 $time = FormatDate($timeFormat, $timestampUTCWithServerOffset);
205 $weekDay = mb_strtolower(FormatDate($weekDayFormat, $timestampUTCWithServerOffset));
206
207 return "$dayMonth $time, $weekDay";
208 }
209
216 public static function formatDateWithoutTime(Date $date): string
217 {
218 $culture = Main\Application::getInstance()->getContext()->getCulture();
219 $dayMonthFormat = Main\Type\Date::convertFormatToPhp($culture->get('DAY_MONTH_FORMAT'));
220 $weekDayFormat = 'l';
221
222 $timestamp = $date->getTimestamp();
223 $dayMonth = FormatDate($dayMonthFormat, $timestamp);
224 $weekDay = mb_strtolower(FormatDate($weekDayFormat, $timestamp));
225
226 return "$dayMonth, $weekDay";
227 }
228
235 public static function formatDateShort(Date $date): string
236 {
237 $culture = Main\Application::getInstance()->getContext()->getCulture();
238 $formatShort = Main\Type\Date::convertFormatToPhp($culture->get('FORMAT_DATE'));
239
240 return FormatDate($formatShort, $date->getTimestamp());
241 }
242
249 public static function formatTimeInterval(Date $from, Date $to, bool $isFullDay): string
250 {
251 $isLongDateTimeFormat = false;
252
253 $culture = Context::getCurrent()->getCulture();
254 $formattedDateFrom = FormatDate($culture->getFullDateFormat(), self::getUserDateTimestamp($from));
255 $formattedDateTo = '';
256 $formattedTimeFrom = '';
257 $formattedTimeTo = '';
258
259 if ($to->format('j') !== $from->format('j')
260 || $to->format('Y') !== $from->format('Y')
261 || $to->format('n') !== $from->format('n')
262 )
263 {
264 $isLongDateTimeFormat = true;
265 $formattedDateTo = FormatDate($culture->getFullDateFormat(), self::getUserDateTimestamp($to));
266 }
267
268 if ($isFullDay)
269 {
270 if (!isset($formattedDateTo))
271 {
272 $formattedDateTo = FormatDate($culture->getFullDateFormat(), self::getUserDateTimestamp($to));
273 }
274 }
275 else
276 {
277 $formattedTimeFrom = FormatDate($culture->getShortTimeFormat(), self::getUserDateTimestamp($from));
278 $formattedTimeTo = FormatDate($culture->getShortTimeFormat(), self::getUserDateTimestamp($to));
279 }
280
281 if ($isFullDay)
282 {
283 if ($isLongDateTimeFormat)
284 {
285 return $formattedDateFrom . " - " . $formattedDateTo;
286 }
287
288 return $formattedDateFrom . Loc::getMessage('EC_VIEW_FULL_DAY');
289 }
290
291 if ($isLongDateTimeFormat)
292 {
293 return $formattedDateFrom . ' ' . $formattedTimeFrom . ' - ' . $formattedDateTo . ' ' . $formattedTimeTo;
294 }
295
296 return $formattedDateFrom . ' ' . $formattedTimeFrom . ' - ' . $formattedTimeTo;
297 }
298
304 public static function formatTimezone(DateTimeZone $timezone): string
305 {
306 $utcOffset = "UTC";
307 if ($timezone->getTimeZone()->getOffset(new \DateTime('now')) !== 0)
308 {
309 $time = new \DateTime('now', $timezone->getTimeZone());
310 $utcOffset .= " " . $time->format('P');
311 }
312
313 return "($utcOffset) " . $timezone->toString();
314 }
315
320 public static function getUserDateTimestamp(Date $date): int
321 {
322 $dateTimezone = new \DateTimeZone($date->getFields()['timezone']);
323 $serverTimezone = (new \DateTime())->getTimezone();
324
325 $dateOffset = $dateTimezone->getOffset(new \DateTime());
326 $serverOffset = $serverTimezone->getOffset(new \DateTime());
327 $offset = - $serverOffset + $dateOffset;
328
329 $userDate = clone $date;
330 $userDate->setTime($date->getHour(), $date->getMinutes(), $date->getSeconds() + $offset);
331
332 return $userDate->getTimestamp();
333 }
334
340 public static function getEventTimestampUTC(DateTime $date, ?string $eventTimezone = null): int
341 {
342 return \Bitrix\Calendar\Util::getDateTimestampUtc($date, $eventTimezone);
343 }
344
351 public static function createSharingLinkExpireDate(?DateTime $dateTime, string $linkType): ?DateTime
352 {
353 $result = null;
354
355 if (!$dateTime)
356 {
357 return null;
358 }
359
360 if (array_key_exists($linkType, Sharing\Link\Helper::LIFETIME))
361 {
362 $result = $dateTime->add(Sharing\Link\Helper::LIFETIME[$linkType]);
363 }
364
365 return $result;
366 }
367
373 public static function createSharingJointLinkExpireDate(?DateTime $dateTime): ?DateTime
374 {
375 return $dateTime?->add(Sharing\Link\Helper::LIFETIME[Sharing\Link\Helper::MULTI_LINK_TYPE]);
376 }
377
378 public static function getPageAbuseLink(int $ownerId, string $calendarLink): ?string
379 {
380 return self::getAbuseLink($ownerId, $calendarLink, self::ABUSE_SENDER_PAGE);
381 }
382
383 public static function getEmailAbuseLink(int $ownerId, string $calendarLink): ?string
384 {
385 return self::getAbuseLink($ownerId, $calendarLink, self::ABUSE_SENDER_EMAIL);
386 }
387
388 private static function getAbuseLink(int $ownerId, string $calendarLink, string $senderPage): ?string
389 {
390 if (!Main\Loader::includeModule('bitrix24'))
391 {
392 return null;
393 }
394
395 $owner = self::getOwnerInfo($ownerId);
396
397 $feedbackForm = new FeedbackForm('calendar_sharing_abuse');
398 $presets = $feedbackForm->getPresets();
399 $formParams = [
400 'hostname' => $presets['hostname'],
401 'b24_plan' => $presets['b24_plan'],
402 'sender_page' => $senderPage,
403 'admin_data' => \COption::GetOptionString('main', 'email_from', ''),
404 'user_data' => "id: {$owner['id']}, name: {$owner['name']} {$owner['lastName']}",
405 'calendar_link' => $calendarLink,
406 ];
407
408 $formParamsQuery = http_build_query($formParams);
409
410 $region = Main\Application::getInstance()->getLicense()->getRegion();
411 return AbuseZoneMap::getLink($region) . "?$formParamsQuery";
412 }
413
414 public static function getBitrix24Link(): ?string
415 {
416 if (!Main\Loader::includeModule('bitrix24'))
417 {
418 return null;
419 }
420
421 $region = Main\Application::getInstance()->getLicense()->getRegion();
422 $abuseLink = AbuseZoneMap::getLink($region);
423
424 $parsedUrl = parse_url($abuseLink);
425 $protocol = $parsedUrl['scheme'];
426 $host = $parsedUrl['host'];
427 $parsedUri = new Uri($protocol . '://' . $host);
428
429 return rtrim($parsedUri->getLocator(), '/');
430 }
431
432 public static function setSiteLanguage(): void
433 {
435 if ($site = $siteDb->fetchObject())
436 {
437 Loc::setCurrentLang($site->getLanguageId());
438 }
439 }
440}
if($_SERVER $defaultValue['REQUEST_METHOD']==="GET" &&!empty($RestoreDefaults) && $bizprocPerms==="W" &&check_bitrix_sessid())
Определения options.php:32
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
const ACTION_CANCEL
Определения helper.php:34
const CANCEL
Определения helper.php:29
const ABUSE_SENDER_EMAIL
Определения helper.php:39
const ACTION_OPENED
Определения helper.php:36
static getPageAbuseLink(int $ownerId, string $calendarLink)
Определения helper.php:378
static formatDate(Date $date)
Определения helper.php:195
static getOwnerInfo(int $id)
Определения helper.php:139
static getEmailAbuseLink(int $ownerId, string $calendarLink)
Определения helper.php:383
static isPhoneFeatureEnabled()
Определения helper.php:81
const ABUSE_SENDER_PAGE
Определения helper.php:38
const OPENED
Определения helper.php:31
static formatTimeInterval(Date $from, Date $to, bool $isFullDay)
Определения helper.php:249
static disableOptionPayAttentionToNewSharingFeature()
Определения helper.php:55
const ICS
Определения helper.php:28
const CONFERENCE
Определения helper.php:30
static formatDateShort(Date $date)
Определения helper.php:235
const OWNER_CREATED
Определения helper.php:32
static getEventTimestampUTC(DateTime $date, ?string $eventTimezone=null)
Определения helper.php:340
static payAttentionToNewSharingFeature()
Определения helper.php:46
static getShortUrl(string $url)
Определения helper.php:106
static getPersonFullNameLoc(?string $name, ?string $lastName)
Определения helper.php:123
static getBitrix24Link()
Определения helper.php:414
static isMailFeatureEnabled()
Определения helper.php:91
static createSharingLinkExpireDate(?DateTime $dateTime, string $linkType)
Определения helper.php:351
static createSharingJointLinkExpireDate(?DateTime $dateTime)
Определения helper.php:373
static getGroupOwnerInfo(int $groupId)
Определения helper.php:166
static setSiteLanguage()
Определения helper.php:432
static formatTimezone(DateTimeZone $timezone)
Определения helper.php:304
static getLinkOwnerInfo(array $link)
Определения helper.php:180
static getUserInfo(int $userId)
Определения helper.php:144
static formatDateWithoutTime(Date $date)
Определения helper.php:216
const ACTION_CONFERENCE
Определения helper.php:35
static getUserDateTimestamp(Date $date)
Определения helper.php:320
const ACTION_ICS
Определения helper.php:33
const ACTION
Определения helper.php:27
static getInstance()
Определения application.php:98
static getById($id)
Определения datamanager.php:364
Определения date.php:9
add($interval)
Определения date.php:145
format($format)
Определения date.php:110
getTimestamp()
Определения date.php:218
static convertFormatToPhp($format)
Определения date.php:309
Определения uri.php:17
$nameFormat
Определения discount_coupon_list.php:278
</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
$protocol
Определения .description.php:9
$region
Определения .description.php:13
const BX_RESIZE_IMAGE_EXACT
Определения constants.php:12
$culture
Определения include.php:61
FormatDate($format="", $timestamp=false, $now=false, ?string $languageId=null)
Определения tools.php:871
$name
Определения menu_edit.php:35
$user
Определения mysql_to_pgsql.php:33
$host
Определения mysql_to_pgsql.php:32
$time
Определения payment.php:61
$optionValue
Определения options.php:3512
const SITE_ID
Определения sonet_set_content_view.php:12
$url
Определения iframe.php:7
$site
Определения yandex_run.php:614