CBPVirtualDocument::createDocument

  1. Bitrix24 API (v. 23.675.0)
  2. bizproc
  3. CBPVirtualDocument
  4. createDocument
  • Модуль: bizproc
  • Путь к файлу: ~/bitrix/modules/bizproc/classes/general/virtualdocument.php
  • Класс: CBPVirtualDocument
  • Вызов: CBPVirtualDocument::createDocument
static function createDocument($parentDocumentId, $arFields)
{
	if (!array_key_exists("IBLOCK_ID", $arFields) || intval($arFields["IBLOCK_ID"]) <= 0)
		throw new Exception("IBlock ID is not found");

	$arDocumentFields = self::GetDocumentFields("type_".$arFields["IBLOCK_ID"]);

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

		if ($arDocumentFields[$key]["Multiple"] && is_string($arFields[$key]))
		{
			$arFieldsTmp = explode(",", $arFields[$key]);
			$arFields[$key] = array();
			foreach ($arFieldsTmp as $value)
				$arFields[$key][] = trim($value);
		}

		$arFields[$key] = (is_array($arFields[$key]) && !CBPHelper::IsAssociativeArray($arFields[$key])) ? $arFields[$key] : array($arFields[$key]);

		if ($arDocumentFields[$key]["Type"] == "S:UserID")
		{
			$ar = array();
			foreach ($arFields[$key] as $v1)
			{
				if (mb_substr($v1, 0, mb_strlen("user_")) == "user_")
				{
					$ar[] = mb_substr($v1, mb_strlen("user_"));
				}
				else
				{
					$a1 = self::GetUsersFromUserGroup($v1, $parentDocumentId);
					foreach ($a1 as $a11)
						$ar[] = $a11;
				}
			}

			$arFields[$key] = $ar;
		}
		elseif ($arDocumentFields[$key]["Type"] == "L")
		{
			$realKey = ((mb_substr($key, 0, mb_strlen("PROPERTY_")) == "PROPERTY_")? mb_substr($key, mb_strlen("PROPERTY_")) : $key);

			$arV = array();
			$db = CIBlockProperty::GetPropertyEnum($realKey, false, array("IBLOCK_ID" => $arFields["IBLOCK_ID"]));
			while ($ar = $db->GetNext())
				$arV[$ar["XML_ID"]] = $ar["ID"];

			foreach ($arFields[$key] as &$value)
			{
				if (array_key_exists($value, $arV))
					$value = $arV[$value];
			}
		}
		elseif ($arDocumentFields[$key]["Type"] == "F")
		{
			foreach ($arFields[$key] as &$value)
				$value = CFile::MakeFileArray($value);
		}
		elseif ($arDocumentFields[$key]["Type"] == "S:HTML")
		{
			foreach ($arFields[$key] as &$value)
				$value = array("VALUE" => $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] : array($arFields[$key]);
			unset($arFields[$key]);
		}
	}

	if (count($arFieldsPropertyValues) > 0)
		$arFields["PROPERTY_VALUES"] = $arFieldsPropertyValues;

	$iblockElement = new CIBlockElement();
	$id = $iblockElement->Add($arFields, false, true, true);
	if (!$id || $id <= 0)
		throw new Exception($iblockElement->LAST_ERROR);
	return $id;
}

Добавить комментарий