• Модуль: bizproc
  • Путь к файлу: ~/bitrix/modules/bizproc/classes/general/workflowpersister.php
  • Класс: CBPWorkflowPersister
  • Вызов: CBPWorkflowPersister::insertWorkflow
protected function insertWorkflow($id, $buffer, $status, $bUnlocked, array $creationData = [])
{
	global $DB;

	$queryCondition = $this->getLockerQueryCondition();

	if ($status == CBPWorkflowStatus::Completed || $status == CBPWorkflowStatus::Terminated)
	{
		$DB->Query(
			"DELETE FROM b_bp_workflow_instance ".
			"WHERE ID = '".$DB->ForSql($id)."'"
		);
	}
	else
	{
		$dbResult = $DB->Query(
			"SELECT ID, IF (".$queryCondition.", 'Y', 'N') as UPDATEABLE ".
			"FROM b_bp_workflow_instance ".
			"WHERE ID = '".$DB->ForSql($id)."' "
		);
		if ($arResult = $dbResult->Fetch())
		{
			if ($arResult["UPDATEABLE"] == "Y")
			{
				$DB->Query(
					"UPDATE b_bp_workflow_instance SET ".
					"	WORKFLOW = '".$DB->ForSql($buffer)."', ".
					"	STATUS = ".intval($status).", ".
					"	MODIFIED = ".$DB->CurrentTimeFunction().", ".
					"	OWNER_ID = ".($bUnlocked ? "NULL" : "'".$DB->ForSql($this->serviceInstanceId)."'").", ".
					"	OWNED_UNTIL = ".($bUnlocked ? "NULL" : $DB->CharToDateFunction(date($GLOBALS["DB"]->DateFormatToPHP(FORMAT_DATETIME), $this->GetOwnershipTimeout())))." ".
					"WHERE ID = '".$DB->ForSql($id)."' "
				);
			}
			else
			{
				throw new Exception(GetMessage('BPCGWP_WF_LOCKED'), CBPRuntime::EXCEPTION_CODE_INSTANCE_LOCKED);
			}
		}
		else
		{
			$status = (int) $status;
			$ownerId = ($bUnlocked ? "NULL" : "'".$DB->ForSql($this->serviceInstanceId)."'");
			$ownedUntil = ($bUnlocked ? "NULL" : $DB->CharToDateFunction(date($GLOBALS["DB"]->DateFormatToPHP(FORMAT_DATETIME), $this->GetOwnershipTimeout())));

			$moduleId = isset($creationData['MODULE_ID']) ? $creationData['MODULE_ID'] : '';
			$entity = isset($creationData['ENTITY']) ? $creationData['ENTITY'] : '';
			$documentId = isset($creationData['DOCUMENT_ID']) ? $creationData['DOCUMENT_ID'] : '';
			$tplId = isset($creationData['WORKFLOW_TEMPLATE_ID']) ? (int) $creationData['WORKFLOW_TEMPLATE_ID'] : 0;
			$startedBy = isset($creationData['STARTED_BY']) ? (int) $creationData['STARTED_BY'] : 0;
			$startedEventType = isset($creationData['STARTED_EVENT_TYPE']) ? (int) $creationData['STARTED_EVENT_TYPE'] : 0;

			$ro = isset($creationData['RO']) ? "'".$DB->ForSql($creationData['RO'])."'" : 'NULL';

			$DB->Query(
				sprintf(
					'INSERT INTO b_bp_workflow_instance (
					ID, WORKFLOW, WORKFLOW_RO, STATUS, MODIFIED, OWNER_ID, OWNED_UNTIL,
					MODULE_ID, ENTITY, DOCUMENT_ID, WORKFLOW_TEMPLATE_ID, STARTED, STARTED_BY, STARTED_EVENT_TYPE
					) VALUES ('%s', '%s', %s, %d, %s, %s, %s, '%s', '%s', '%s', %d, %s, %d, %d)',
					$DB->ForSql($id),
					$DB->ForSql($buffer),
					$ro,
					$status,
					$DB->CurrentTimeFunction(),
					$ownerId,
					$ownedUntil,
					$DB->ForSql($moduleId),
					$DB->ForSql($entity),
					$DB->ForSql($documentId),
					$tplId,
					$DB->CurrentTimeFunction(),
					$startedBy,
					$startedEventType
				)
			);
		}
	}
}