CBPVirtualDocument::CreateDocument: Bitrix method

      
<?php 
//  CBPVirtualDocument :: CreateDocument()
//  /dist/bitrix/modules/bizproc/classes/general/virtualdocument.php:1910

    
public 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 (
substr($v10strlen("user_")) == "user_")
                    {
                        
$ar[] = substr($v1strlen("user_"));
                    }
                    else
                    {
                        
$a1 self::GetUsersFromUserGroup($v1$parentDocumentId);
                        foreach (
$a1 as $a11)
                            
$ar[] = $a11;
                    }
                }

                
$arFields[$key] = $ar;
            }
            elseif (
$arDocumentFields[$key]["Type"] == "L")
            {
                
$realKey = ((substr($key0strlen("PROPERTY_")) == "PROPERTY_") ? substr($keystrlen("PROPERTY_")) : $key);

                
$arV = array();
                
$db CIBlockProperty::GetPropertyEnum($realKeyfalse, 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 (
substr($key0strlen("PROPERTY_")) == "PROPERTY_")
            {
                
$realKey substr($keystrlen("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($arFieldsfalsetruetrue);
        if (!
$id || $id <= 0)
            throw new 
Exception($iblockElement->LAST_ERROR);
        return 
$id;
    }