• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/integration/filediskproperty.php
  • Класс: BitrixDiskIntegrationFileDiskProperty
  • Вызов: FileDiskProperty::attachFilesWorkflow
static function attachFilesWorkflow($iblockId, $fileId)
{
	if(!(int)$iblockId)
	{
		return null;
	}

	$userFieldManager = Driver::getInstance()->getUserFieldManager();
	list($connectorClass, $moduleId) = $userFieldManager->getConnectorDataByEntityType("iblock_workflow");
	list($type, $realId) = FileUserType::detectType($fileId);

	if($type == FileUserType::TYPE_ALREADY_ATTACHED)
	{
		$attachedModel = AttachedObject::loadById($realId);
		if(!$attachedModel)
		{
			return null;
		}
		else
		{
			return $realId;
		}
	}

	$errorCollection = new ErrorCollection();
	$fileModel = File::loadById($realId, array('STORAGE'));
	if(!$fileModel)
	{
		return null;
	}

	$attachedModel = AttachedObject::load(array(
		'OBJECT_ID' => $fileModel->getId(),
		'=ENTITY_TYPE' => $connectorClass,
		'=ENTITY_ID' => $iblockId,
		'=MODULE_ID' => $moduleId
	));
	if($attachedModel)
	{
		return $fileModel->getId();
	}

	global $USER;
	if($USER instanceof CUser && $USER->getId())
	{
		$userId = $USER->getId();
	}
	else
	{
		$userId = SystemUser::SYSTEM_USER_ID;
	}
	$securityContext = $fileModel->getStorage()->getSecurityContext($userId);
	if(!$fileModel->canRead($securityContext))
	{
		return null;
	}
	$canUpdate = $fileModel->canUpdate($securityContext);

	$attachedModel = AttachedObject::add(array(
		'MODULE_ID' => $moduleId,
		'OBJECT_ID' => $fileModel->getId(),
		'ENTITY_ID' => $iblockId,
		'ENTITY_TYPE' => $connectorClass,
		'IS_EDITABLE' => (int)$canUpdate,
		'ALLOW_EDIT' => (int) $canUpdate,
		'CREATED_BY' => $userId,
	), $errorCollection);
	if(!$attachedModel || $errorCollection->hasErrors())
	{
		return null;
	}

	return $fileModel->getId();
}