CBPVirtualDocument :: AddDocumentField: Bitrix method

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

    
public function AddDocumentField($documentType$arFields)
    {
        
$iblockId intval(substr($documentTypestrlen("type_")));
        if (
$iblockId <= 0)
            throw new 
CBPArgumentOutOfRangeException("documentType"$documentType);

        if (
substr($arFields["code"], 0strlen("PROPERTY_")) == "PROPERTY_")
            
$arFields["code"] = substr($arFields["code"], strlen("PROPERTY_"));

        
$arFieldsTmp = array(
            
"NAME" => $arFields["name"],
            
"ACTIVE" => "Y",
            
"SORT" => 150,
            
"CODE" => $arFields["code"],
            
"MULTIPLE" => $arFields["multiple"],
            
"IS_REQUIRED" => $arFields["required"],
            
"IBLOCK_ID" => $iblockId,
            
"FILTRABLE" => "Y",
        );

        if (
strpos("0123456789"substr($arFieldsTmp["CODE"], 01))!==false)
            unset(
$arFieldsTmp["CODE"]);

        if (
array_key_exists("additional_type_info"$arFields))
            
$arFieldsTmp["LINK_IBLOCK_ID"] = intval($arFields["additional_type_info"]);

        if (
strstr($arFields["type"], ":") !== false)
        {
            list(
$arFieldsTmp["PROPERTY_TYPE"], $arFieldsTmp["USER_TYPE"]) = explode(":"$arFields["type"], 2);
            if (
$arFields["type"] == "E:EList")
                
$arFieldsTmp["LINK_IBLOCK_ID"] = $arFields["options"];
        }
        else
        {
            
$arFieldsTmp["PROPERTY_TYPE"] = $arFields["type"];
            
$arFieldsTmp["USER_TYPE"] = false;
        }

        if (
$arFieldsTmp["PROPERTY_TYPE"] == "T")
        {
            
$arFieldsTmp["PROPERTY_TYPE"] = "S";
            
$arFieldsTmp["ROW_COUNT"] = 5;
        }

        if (
$arFields["type"] == "L")
        {
            if (
is_array($arFields["options"]))
            {
                
$i 10;
                foreach (
$arFields["options"] as $k => $v)
                {
                    
$arFieldsTmp["VALUES"][] = array("XML_ID" => $k"VALUE" => $v"DEF" => "N""SORT" => $i);
                    
$i $i 10;
                }
            }
            elseif (
is_string($arFields["options"]) && (strlen($arFields["options"]) > 0))
            {
                
$a explode("\n"$arFields["options"]);
                
$i 10;
                foreach (
$a as $v)
                {
                    
$v trim(trim($v), "\r\n");
                    
$v1 $v2 $v;
                    if (
substr($v01) == "[" && strpos($v"]") !== false)
                    {
                        
$v1 substr($v1strpos($v"]") - 1);
                        
$v2 trim(substr($vstrpos($v"]") + 1));
                    }
                    
$arFieldsTmp["VALUES"][] = array("XML_ID" => $v1"VALUE" => $v2"DEF" => "N""SORT" => $i);
                    
$i $i 10;
                }
            }
        }

        
$ibp = new CIBlockProperty;
        
$propId $ibp->Add($arFieldsTmp);

        if (
intval($propId) <= 0)
            throw new 
Exception($ibp->LAST_ERROR);

        return 
"PROPERTY_".$arFields["code"];
    }