• Модуль: lists
  • Путь к файлу: ~/bitrix/modules/lists/lib/bizprocdocument.php
  • Класс: BizprocDocument
  • Вызов: BizprocDocument::updateDocument
static function updateDocument($documentId, $arFields)
{
	$documentId = intval($documentId);
	if ($documentId <= 0)
	{
		throw new CBPArgumentNullException('documentId');
	}

	CIBlockElement::WF_CleanUpHistoryCopies($documentId, 0);

	$arFieldsPropertyValues = [];

	$dbResult = CIBlockElement::GetList(
		[],
		['ID' => $documentId, 'SHOW_NEW' => 'Y', 'SHOW_HISTORY' => 'Y'],
		false, false,
		['ID', 'IBLOCK_ID']
	);
	$arResult = $dbResult->Fetch();
	if (!$arResult)
	{
		throw new Exception('Element is not found');
	}

	$complexDocumentId = ['lists', get_called_class(), $documentId];
	$arDocumentFields = self::GetDocumentFields('iblock_' . $arResult['IBLOCK_ID']);

	$arKeys = array_keys($arFields);
	foreach ($arKeys as $key)
	{
		if (!array_key_exists($key, $arDocumentFields))
		{
			continue;
		}

		$arFields[$key] =
			(is_array($arFields[$key]) && !CBPHelper::IsAssociativeArray($arFields[$key]))
				? $arFields[$key]
				: [$arFields[$key]];
		$realKey = (
		(mb_substr($key, 0, mb_strlen('PROPERTY_')) == 'PROPERTY_')
			? mb_substr($key, mb_strlen('PROPERTY_'))
			: $key
		);

		if ($arDocumentFields[$key]['Type'] == 'user')
		{
			$arFields[$key] = CBPHelper::extractUsers($arFields[$key], $complexDocumentId);
		}
		elseif ($arDocumentFields[$key]['Type'] == 'select')
		{
			$arV = [];
			$db = CIBlockProperty::GetPropertyEnum(
				$realKey,
				false,
				['IBLOCK_ID' => $arResult['IBLOCK_ID']]
			);
			while ($ar = $db->GetNext())
			{
				$arV[$ar['XML_ID']] = $ar['ID'];
			}

			$listValue = [];
			foreach ($arFields[$key] as &$value)
			{
				if (is_array($value) && CBPHelper::isAssociativeArray($value))
				{
					$listXmlId = array_keys($value);
					foreach ($listXmlId as $xmlId)
					{
						$listValue[] = $arV[$xmlId];
					}
				}
				else
				{
					if (array_key_exists($value, $arV))
					{
						$value = $arV[$value];
					}
				}
			}
			if (!empty($listValue))
			{
				$arFields[$key] = $listValue;
			}
		}
		elseif ($arDocumentFields[$key]['Type'] == 'file')
		{
			$files = [];
			foreach ($arFields[$key] as $value)
			{
				if (is_array($value))
				{
					foreach ($value as $file)
					{
						$makeFileArray = CFile::MakeFileArray($file);
						if ($makeFileArray)
						{
							$files[] = $makeFileArray;
						}
					}
				}
				else
				{
					$makeFileArray = CFile::MakeFileArray($value);
					if ($makeFileArray)
					{
						$files[] = $makeFileArray;
					}
				}
			}
			if ($files)
			{
				$arFields[$key] = $files;
			}
			else
			{
				$arFields[$key] = [['del' => 'Y']];
			}
		}
		elseif ($arDocumentFields[$key]['Type'] == 'S:DiskFile')
		{
			foreach ($arFields[$key] as &$value)
			{
				if (!empty($value))
				{
					$value = 'n' . $value;
				}
			}
			$arFields[$key] = ['VALUE' => $arFields[$key], 'DESCRIPTION' => 'workflow'];
		}
		elseif ($arDocumentFields[$key]['Type'] == 'S:HTML')
		{
			foreach ($arFields[$key] as &$value)
			{
				$value = ['VALUE' => $value];
			}
		}

		unset($value);

		if (!$arDocumentFields[$key]["Multiple"] && is_array($arFields[$key]))
		{
			if (count($arFields[$key]) > 0)
			{
				$a = array_values($arFields[$key]);
				$arFields[$key] = $a[0];
			}
			else
			{
				$arFields[$key] = null;
			}
		}

		if (mb_substr($key, 0, mb_strlen("PROPERTY_")) == "PROPERTY_")
		{
			$realKey = mb_substr($key, mb_strlen("PROPERTY_"));
			$arFieldsPropertyValues[$realKey] = (is_array($arFields[$key])
				&& !CBPHelper::IsAssociativeArray($arFields[$key])) ? $arFields[$key] : [$arFields[$key]];
			if (empty($arFieldsPropertyValues[$realKey]))
				$arFieldsPropertyValues[$realKey] = [null];
			unset($arFields[$key]);
		}
	}

	if (count($arFieldsPropertyValues) > 0)
	{
		$arFields['PROPERTY_VALUES'] = $arFieldsPropertyValues;
	}

	$iblockElement = new CIBlockElement();
	if (isset($arFields['PROPERTY_VALUES']) && count($arFields['PROPERTY_VALUES']) > 0)
	{
		$iblockElement->SetPropertyValuesEx($documentId, $arResult['IBLOCK_ID'], $arFields['PROPERTY_VALUES']);
	}

	unset($arFields['PROPERTY_VALUES']);
	$res = $iblockElement->Update($documentId, $arFields, false, true, true);
	if (!$res)
	{
		throw new Exception($iblockElement->LAST_ERROR);
	}

	if (isset($arFields['BP_PUBLISHED']) && $arFields['BP_PUBLISHED'] === 'Y')
	{
		self::publishDocument($documentId);
	}
	elseif (isset($arFields['BP_PUBLISHED']) &&$arFields['BP_PUBLISHED'] === 'N')
	{
		self::unpublishDocument($documentId);
	}

	if (CModule::includeModule('lists'))
	{
		CLists::rebuildSeachableContentForElement($arResult['IBLOCK_ID'], $documentId);
	}
}