CBPAllStateService::SetState: Bitrix method

      
<?php 
//  CBPAllStateService :: SetState()
//  /dist/bitrix/modules/bizproc/classes/general/stateservice.php:505

    
public function SetState($workflowId$arState$arStatePermissions = array())
    {
        global 
$DB;

        
$workflowId trim($workflowId);
        if (
strlen($workflowId) <= 0)
            throw new 
Exception("workflowId");

        
$state trim($arState["STATE"]);
        
$stateTitle trim($arState["TITLE"]);
        
$stateParameters "";
        if (
count($arState["PARAMETERS"]) > 0)
            
$stateParameters serialize($arState["PARAMETERS"]);

        
$DB->Query(
            
"UPDATE b_bp_workflow_state SET ".
            
"    STATE = ".(strlen($state) > "'".$DB->ForSql($state)."'" "NULL").", ".
            
"    STATE_TITLE = ".(strlen($stateTitle) > "'".$DB->ForSql($stateTitle)."'" "NULL").", ".
            
"    STATE_PARAMETERS = ".(strlen($stateParameters) > "'".$DB->ForSql($stateParameters)."'" "NULL").", ".
            
"    MODIFIED = ".$DB->CurrentTimeFunction()." ".
            
"WHERE ID = '".$DB->ForSql($workflowId)."' "
        
);

        if (
$arStatePermissions !== false)
        {
            
$arState self::GetWorkflowState($workflowId);
            
$runtime $this->runtime;
            if (!isset(
$runtime) || !is_object($runtime))
                
$runtime CBPRuntime::GetRuntime();
            
$documentService $runtime->GetService("DocumentService");
            
$documentService->SetPermissions($arState["DOCUMENT_ID"], $workflowId$arStatePermissionstrue);
            
$documentType $documentService->GetDocumentType($arState["DOCUMENT_ID"]);
            if (
$documentType)
                
$arStatePermissions $documentService->toInternalOperations($documentType$arStatePermissions);

            
$DB->Query(
                
"DELETE FROM b_bp_workflow_permissions ".
                
"WHERE WORKFLOW_ID = '".$DB->ForSql($workflowId)."' "
            
);

            foreach (
$arStatePermissions as $permission => $arObjects)
            {
                foreach (
$arObjects as $object)
                {
                    
$DB->Query(
                        
"INSERT INTO b_bp_workflow_permissions (WORKFLOW_ID, OBJECT_ID, PERMISSION) ".
                        
"VALUES ('".$DB->ForSql($workflowId)."', '".$DB->ForSql($object)."', '".$DB->ForSql($permission)."')"
                    
);
                }
            }
        }
    }