/modules/calendar/lib/util.php
PHP | 672 lines | 504 code | 68 blank | 100 comment | 72 complexity | ec9a27adc9ebf9e6b27b1e1ad9928aa5 MD5 | raw file
- <?php
- namespace Bitrix\Calendar;
- use Bitrix\Calendar\Sync\Util\MsTimezoneConverter;
- use Bitrix\Main;
- use Bitrix\Main\Loader;
- use Bitrix\Main\Localization\LanguageTable;
- use Bitrix\Main\Type\Date;
- use Bitrix\Main\Type\DateTime;
- class Util
- {
- public const USER_SELECTOR_CONTEXT = "CALENDAR";
- public const LIMIT_NUMBER_BANNER_IMPRESSIONS = 3;
- public const DATETIME_PHP_FORMAT = 'Y-m-d H:i:sP';
- private static $requestUid = '';
- private static $userAccessCodes = [];
- private static $pathCache = [];
- /**
- * @param $managerId
- * @param $userId
- * @return bool
- */
- public static function isManagerForUser($managerId, $userId): bool
- {
- return in_array('IU'.$userId, self::getUserAccessCodes($managerId));
- }
- /**
- * @return bool
- * @throws Main\ArgumentNullException
- * @throws Main\ArgumentOutOfRangeException
- */
- public static function isSectionStructureConverted(): bool
- {
- return \Bitrix\Main\Config\Option::get('calendar', 'sectionStructureConverted', 'N') === 'Y';
- }
- /**
- * @param $date
- * @param bool $round
- * @param bool $getTime
- * @return false|float|int
- */
- public static function getTimestamp($date, $round = true, $getTime = true)
- {
- $timestamp = MakeTimeStamp($date, \CSite::getDateFormat($getTime ? "FULL" : "SHORT"));
- return $round ? (round($timestamp / 60) * 60) : $timestamp;
- }
- /**
- * @param string|null $timeZone
- * @return bool
- */
- public static function isTimezoneValid(?string $timeZone): bool
- {
- return (!is_null($timeZone) && $timeZone !== 'false' && in_array($timeZone, timezone_identifiers_list(), true));
- }
- /**
- * @param string|null $tz
- * @return \DateTimeZone
- */
- public static function prepareTimezone(?string $tz = null): \DateTimeZone
- {
- if (!$tz)
- {
- return new \DateTimeZone("UTC");
- }
- if (self::isTimezoneValid($tz))
- {
- return new \DateTimeZone($tz);
- }
- if ($timezones = MsTimezoneConverter::getValidateTimezones($tz))
- {
- return new \DateTimeZone($timezones[0]);
- }
- return new \DateTimeZone(self::getServerTimezoneName());
- }
- /**
- * @param string|null $date
- * @param bool $fullDay
- * @param string $tz
- * @return Date
- * @throws Main\ObjectException
- */
- public static function getDateObject(string $date = null, $fullDay = true, $tz = 'UTC'): Date
- {
- $preparedDate = $date;
- if ($date)
- {
- $timestamp = \CCalendar::Timestamp($date, false, !$fullDay);
- $preparedDate = \CCalendar::Date($timestamp, !$fullDay);
- }
- return $fullDay
- ? new Date($preparedDate, Date::convertFormatToPhp(FORMAT_DATE))
- : new DateTime($preparedDate, Date::convertFormatToPhp(FORMAT_DATETIME), Util::prepareTimezone($tz));
- }
- /**
- * @return string
- */
- public static function getUserSelectorContext(): string
- {
- return self::USER_SELECTOR_CONTEXT;
- }
- public static function checkRuZone(): bool
- {
- if (\Bitrix\Main\ModuleManager::isModuleInstalled('bitrix24'))
- {
- $isRussian = (\CBitrix24::getPortalZone() === 'ru');
- }
- else
- {
- $iterator = LanguageTable::getList([
- 'select' => ['ID'],
- 'filter' => ['=ID' => 'ru', '=ACTIVE' => 'Y']
- ]);
- $row = $iterator->fetch();
- if (empty($row))
- {
- $isRussian = false;
- }
- else
- {
- $iterator = LanguageTable::getList([
- 'select' => ['ID'],
- 'filter' => ['@ID' => ['ua', 'by', 'kz'], '=ACTIVE' => 'Y'],
- 'limit' => 1
- ]);
- $row = $iterator->fetch();
- $isRussian = empty($row);
- }
- }
- return $isRussian;
- }
- public static function convertEntitiesToCodes($entityList = [])
- {
- $codeList = [];
- if (is_array($entityList))
- {
- foreach($entityList as $entity)
- {
- if ($entity['entityId'] === 'meta-user' && $entity['id'] === 'all-users')
- {
- $codeList[] = 'UA';
- }
- elseif ($entity['entityId'] === 'user')
- {
- $codeList[] = 'U'.$entity['id'];
- }
- elseif ($entity['entityId'] === 'project' || $entity['entityId'] === 'project-roles')
- {
- $codeList[] = 'SG'.$entity['id'];
- }
- elseif ($entity['entityId'] === 'department')
- {
- $codeList[] = 'DR'.$entity['id'];
- }
- }
- }
- return $codeList;
- }
- public static function convertCodesToEntities($codeList = [])
- {
- $entityList = [];
- if (is_array($codeList))
- {
- foreach($codeList as $code)
- {
- if ($code === 'UA')
- {
- $entityList[] = [
- 'entityId' => 'meta-user',
- 'id' => 'all-users'
- ];
- }
- elseif (mb_substr($code, 0, 1) == 'U')
- {
- $entityList[] = [
- 'entityId' => 'user',
- 'id' => intval(mb_substr($code, 1))
- ];
- }
- if (mb_substr($code, 0, 2) == 'DR')
- {
- $entityList[] = [
- 'entityId' => 'department',
- 'id' => intval(mb_substr($code, 2))
- ];
- }
- elseif (preg_match('/^SG([0-9]+)_?([AEKMO])?$/', $code, $match) && isset($match[2]))
- {
- // todo May need to be removed/rewrite after creating new roles in projects.
- $entityList[] = [
- 'entityId' => 'project-roles',
- 'id' => mb_substr($code, 2)
- ];
- }
- elseif (mb_substr($code, 0, 2) == 'SG')
- {
- $entityList[] = [
- 'entityId' => 'project',
- 'id' => intval(mb_substr($code, 2))
- ];
- }
- }
- }
- return $entityList;
- }
- public static function getUsersByEntityList($entityList, $fetchUsers = false)
- {
- if (!Main\Loader::includeModule('socialnetwork'))
- {
- return [];
- }
- $users = \CSocNetLogDestination::getDestinationUsers(self::convertEntitiesToCodes($entityList), $fetchUsers);
- if ($fetchUsers)
- {
- for ($i = 0, $l = count($users); $i < $l; $i++)
- {
- $users[$i]['FORMATTED_NAME'] = \CCalendar::getUserName($users[$i]);
- }
- }
- return $users;
- }
- public static function getDefaultEntityList($userId, $type, $ownerId)
- {
- $entityList = [['entityId' => 'user', 'id' => $userId]];
- if ($type === 'user' && $ownerId !== $userId)
-