CBPWorkflowTemplateLoader :: ImportTemplate: Bitrix method

      
<?php 
//  CBPWorkflowTemplateLoader :: ImportTemplate()
//  /dist/bitrix/modules/bizproc/classes/general/workflowtemplateloader.php:810

    
public static function ImportTemplate($id$documentType$autoExecute$name$description$datum$systemCode null$systemImport false)
    {
        
$id intval($id);
        if (
$id <= 0)
            
$id 0;

        
$packer = new \Bitrix\Bizproc\Workflow\Template\Packer\Bpt();
        
$unpackResult $packer->unpack($datum);

        if (!
$unpackResult->isSuccess())
        {
            throw new \
Bitrix\Main\ArgumentException(reset($unpackResult->getErrorMessages()));
        }

        
$templateFields $unpackResult->getTpl()->collectValues();

        if (!
$systemImport)
        {
            if (!
self::WalkThroughWorkflowTemplate($templateFields["TEMPLATE"], array("CBPWorkflowTemplateLoader""ImportTemplateChecker"), new CBPWorkflowTemplateUser(CBPWorkflowTemplateUser::CurrentUser)))
                return 
false;
        }
        elseif (
$id && !empty($templateFields["CONSTANTS"]))
        {
            
$userConstants self::getTemplateConstants($id);
            if (!empty(
$userConstants))
            {
                foreach (
$userConstants as $constantName => $constantData)
                {
                    if (isset(
$templateFields["CONSTANTS"][$constantName]))
                    {
                        
$templateFields["CONSTANTS"][$constantName]['Default'] = $constantData['Default'];
                    }
                }
            }
        }

        
$templateData = array(
            
"DOCUMENT_TYPE" => $documentType,
            
"AUTO_EXECUTE" => $autoExecute,
            
"NAME" => $name,
            
"DESCRIPTION" => $description,
            
"TEMPLATE" => $templateFields["TEMPLATE"],
            
"PARAMETERS" => $templateFields["PARAMETERS"],
            
"VARIABLES" => $templateFields["VARIABLES"],
            
"CONSTANTS" => $templateFields["CONSTANTS"],
            
"USER_ID" => $systemImport $GLOBALS["USER"]->GetID(),
            
"MODIFIER_USER" => new CBPWorkflowTemplateUser($systemImport CBPWorkflowTemplateUser::CurrentUser),
        );
        if (!
is_null($systemCode))
            
$templateData["SYSTEM_CODE"] = $systemCode;
        if (
$id <= 0)
            
$templateData['ACTIVE'] = 'Y';

        if (
$id 0)
            
self::Update($id$templateData$systemImport);
        else
            
$id self::Add($templateData$systemImport);

        
$documentFields $unpackResult->getDocumentFields();
        if (
$documentFields)
        {
            
$documentService CBPRuntime::GetRuntime(true)->getDocumentService();
            
$currentDocumentFields $documentService->GetDocumentFields($documentTypetrue);

            \
Bitrix\Main\Type\Collection::sortByColumn($documentFields"sort");
            
$len strlen("_PRINTABLE");

            foreach (
$documentFields as $code => $field)
            {
                
//skip printable
                
if (strtoupper(substr($code, -$len)) == "_PRINTABLE")
                {
                    continue;
                }

                
//skip references
                
if (strpos($code'.') !== false)
                {
                    continue;
                }

                
$documentField = array(
                    
"name" => $field["Name"],
                    
"code" => $code,
                    
"type" => $field["Type"],
                    
"multiple" => $field["Multiple"],
                    
"required" => $field["Required"],
                );

                if (
is_array($field["Options"]) && count($field["Options"]) > 0)
                {
                    foreach (
$field["Options"] as $k => $v)
                        
$documentField["options"] .= "[".$k."]".$v."\n";
                }

                unset(
$field["Name"], $field["Type"], $field["Multiple"], $field["Required"], $field["Options"]);
                
$documentField array_merge($documentField$field);

                if (!
array_key_exists($code$currentDocumentFields))
                {
                    
$documentService->AddDocumentField($documentType$documentField);
                }
                else
                {
                    
$documentService->UpdateDocumentField($documentType$documentField);
                }
            }
        }

        return 
$id;
    }