• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/integration/filediskproperty.php
  • Класс: BitrixDiskIntegrationFileDiskProperty
  • Вызов: FileDiskProperty::convertToDB
static function convertToDB($property, $value)
{
	$listId = self::prepareValue($value);

	if(empty($property['ELEMENT_ID']))
	{
		$value['VALUE'] = implode(',', $listId);
		return $value;
	}

	global $USER;
	if($USER instanceof CUser && $USER->getId())
	{
		$userId = $USER->getId();
	}
	else
	{
		$userId = SystemUser::SYSTEM_USER_ID;
	}

	if(isset($value['DESCRIPTION']) && $value['DESCRIPTION'] == 'workflow')
	{
		$workFlow = true;
	}
	else
	{
		$workFlow = false;
	}

	$value['VALUE'] = array();
	$userFieldManager = Driver::getInstance()->getUserFieldManager();
	list($connectorClass, $moduleId) = $userFieldManager->getConnectorDataByEntityType("iblock_element");

	foreach($listId as $id)
	{
		list($type, $realId) = FileUserType::detectType($id);
		if($type == FileUserType::TYPE_NEW_OBJECT)
		{
			$errorCollection = new ErrorCollection();
			$fileModel = File::loadById($realId, array('STORAGE'));
			if(!$fileModel)
			{
				continue;
			}

			if($workFlow)
			{
				$canUpdate = true;
			}
			else
			{
				$securityContext = $fileModel->getStorage()->getSecurityContext($userId);
				if(!$fileModel->canRead($securityContext))
				{
					continue;
				}
				$canUpdate = $fileModel->canUpdate($securityContext);
			}

			$attachedModel = AttachedObject::add(array(
				'MODULE_ID' => $moduleId,
				'OBJECT_ID' => $fileModel->getId(),
				'ENTITY_ID' => $property['ELEMENT_ID'],
				'ENTITY_TYPE' => $connectorClass,
				'IS_EDITABLE' => (int) $canUpdate,
				'ALLOW_EDIT' => (int) $canUpdate,
				'CREATED_BY' => $userId,
			), $errorCollection);
			if(!$attachedModel || $errorCollection->hasErrors())
			{
				continue;
			}
			$value['VALUE'][] = $attachedModel->getId();
		}
		else
		{
			$value['VALUE'][] = $realId;
		}
	}

	$query = CIblockElement::getPropertyValues($property['IBLOCK_ID'], array('ID' => $property['ELEMENT_ID']));
	$oldPropertyValues = array();
	if($propertyValues = $query->fetch())
	{
		if(is_array($propertyValues[$property['ID']]) && !empty($propertyValues[$property['ID']]))
		{
			$oldValues = current($propertyValues[$property['ID']]);
		}
		else
		{
			$oldValues = $propertyValues[$property['ID']];
		}

		if(!empty($oldValues))
		{
			$oldPropertyValues = explode(',', $oldValues);
		}
	}
	$attachedIdForDelete = array_diff($oldPropertyValues, $value['VALUE']);

	if(!empty($attachedIdForDelete))
	{
		foreach($attachedIdForDelete as $idAttached)
		{
			list($type, $realId) = FileUserType::detectType($idAttached);
			if($type == FileUserType::TYPE_ALREADY_ATTACHED)
			{
				$attachedModel = AttachedObject::loadById($realId);
				if(!$attachedModel)
				{
					continue;
				}
				if($userFieldManager->belongsToEntity($attachedModel, "iblock_element", $property['ELEMENT_ID']))
				{
					$attachedModel->delete();
				}
			}
		}
	}

	$value['VALUE'] = implode(',', $value['VALUE']);

	return $value;
}