• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/lib/ical/mailinvitation/helper.php
  • Класс: BitrixCalendarICalMailInvitationHelper
  • Вызов: Helper::getMailAttaches
static function getMailAttaches($fields, $userId, $parentId, &$isChangeFiles = false): AttachCollection
{
	//TODO: need refactoring
	global $USER_FIELD_MANAGER;
	$attachCollection = new AttachCollection();
	$UF = $USER_FIELD_MANAGER->GetUserFields("CALENDAR_EVENT", $parentId, LANGUAGE_ID);
	$attachedFilesIds = $UF['UF_WEBDAV_CAL_EVENT']['VALUE'];

	$fields['UF_WEBDAV_CAL_EVENT'] ??= null;
	if (is_array($fields) && is_array($fields['UF_WEBDAV_CAL_EVENT']) && is_array($attachedFilesIds))
	{
		$ufIds = array_unique(array_merge($fields['UF_WEBDAV_CAL_EVENT'], $attachedFilesIds));
	}
	elseif(is_array($fields) && is_array($fields['UF_WEBDAV_CAL_EVENT']))
	{
		$ufIds = $fields['UF_WEBDAV_CAL_EVENT'];
	}
	elseif(is_array($attachedFilesIds))
	{
		$ufIds = $attachedFilesIds;
	}
	else
	{
		return $attachCollection;
	}

	if (!empty($ufIds) && BitrixMainLoader::includeModule('disk'))
	{
		foreach ($ufIds as $item)
		{
			[$type, $realValue] = BitrixDiskUfFileUserType::detectType($item);

			if ($type == FileUserType::TYPE_ALREADY_ATTACHED)
			{
				$attachedModel = AttachedObject::loadById($realValue);
				if(!$attachedModel
					|| (!empty($fields['UF_WEBDAV_CAL_EVENT'])
						&& $item !== ''
						&& !in_array($item, $fields['UF_WEBDAV_CAL_EVENT'])))
				{
					$isChangeFiles = true;
					continue;
				}
				$file = $attachedModel->getFile();
			}
			elseif ($type == BitrixDiskUfFileUserType::TYPE_NEW_OBJECT)
			{
				$isChangeFiles = true;
				$file = BitrixDiskFile::loadById($realValue, ['STORAGE']);
			}

			if (!$file)
			{
				continue;
			}

			$externalLink = $file->addExternalLink([
				'CREATED_BY' => $userId,
				'TYPE' => BitrixDiskInternalsExternalLinkTable::TYPE_MANUAL,
			]);
			if (!$externalLink)
			{
				continue;
			}

			$name = $file->getName();
			$size = $file->getSize();
			$link = BitrixDiskDriver::getInstance()->getUrlManager()->getUrlExternalLink([
					'hash' => $externalLink->getHash(),
					'action' => 'downloadFile',
				],
				true
			);

			$attach = Attach::createInstance($link, $name, $size);

			$attachCollection->add($attach);
		}
	}

	return $attachCollection;
}