protected function terminateWorkflow($workflowId, $elementId) { $this->checkPermission(); if (!CBPDocument::canUserOperateDocument(CBPCanUserOperateOperation::StartWorkflow, $this->getUser(), BizProcDocument::getDocumentComplexId($this->iblockTypeId, $elementId), array("DocumentStates" => $this->documentStates))) { $this->errorCollection->add(array(new Error(Loc::getMessage('LISTS_LAC_ACCESS_DENIED')))); } if ($this->errorCollection->hasErrors()) { $this->sendJsonErrorResponse(); } if (CIBlockElementRights::userHasRightTo($this->iblockId, $elementId, "element_rights_edit")) { $errors = array(); CBPDocument::terminateWorkflow($workflowId, BizProcDocument::getDocumentComplexId($this->iblockTypeId, $elementId), $errors); foreach ($errors as $error) { $this->errorCollection->add(array(new Error($error["message"]))); } } else { $this->errorCollection->add(array(new Error(Loc::getMessage('LISTS_LAC_ACCESS_DENIED')))); } if ($this->errorCollection->hasErrors()) { $this->sendJsonErrorResponse(); } }
protected function getData() { $this->arResult['SHOW_MODE'] = 'SelectWorkflow'; $this->arResult['TEMPLATES'] = array(); $this->arResult['PARAMETERS_VALUES'] = array(); $this->arResult['ERROR_MESSAGE'] = ''; $runtime = CBPRuntime::getRuntime(); $runtime->startRuntime(); $this->arResult['DocumentService'] = $runtime->getService('DocumentService'); foreach ($this->arResult['DOCUMENT_DATA'] as $nameModule => $data) { $workflowTemplateObject = CBPWorkflowTemplateLoader::getList(array(), array('DOCUMENT_TYPE' => $data['DOCUMENT_TYPE'], 'ACTIVE' => 'Y'), false, false, array('ID', 'NAME', 'DESCRIPTION', 'MODIFIED', 'USER_ID', 'PARAMETERS')); while ($workflowTemplate = $workflowTemplateObject->getNext()) { if (!CBPDocument::canUserOperateDocument(CBPCanUserOperateOperation::StartWorkflow, $this->getUser()->getID(), $data['DOCUMENT_ID'], array())) { continue; } if ($nameModule == 'DISK') { $this->arResult['TEMPLATES'][$workflowTemplate['ID']] = $workflowTemplate; $this->arResult['TEMPLATES'][$workflowTemplate['ID']]['URL'] = htmlspecialcharsex($this->getApplication()->getCurPageParam('workflow_template_id=' . $workflowTemplate['ID'] . '&' . bitrix_sessid_get(), array('workflow_template_id', 'sessid'))); } else { $this->arResult['TEMPLATES_OLD'][$workflowTemplate['ID']] = $workflowTemplate; $this->arResult['TEMPLATES_OLD'][$workflowTemplate['ID']]['URL'] = htmlspecialcharsex($this->getApplication()->getCurPageParam('workflow_template_id=' . $workflowTemplate['ID'] . '&old=1&' . bitrix_sessid_get(), array('workflow_template_id', 'sessid'))); } } } if ($this->arParams['TEMPLATE_ID'] > 0 && strlen($this->request->getPost('CancelStartParamWorkflow')) <= 0 && (array_key_exists($this->arParams['TEMPLATE_ID'], $this->arResult['TEMPLATES']) || array_key_exists($this->arParams['TEMPLATE_ID'], $this->arResult['TEMPLATES_OLD']))) { if (array_key_exists($this->arParams['TEMPLATE_ID'], $this->arResult['TEMPLATES'])) { $templates = $this->arResult['TEMPLATES']; $documentParameters = $this->arResult['DOCUMENT_DATA']['DISK']; $this->arResult['CHECK_TEMPLATE'] = 'DISK'; } else { $templates = $this->arResult['TEMPLATES_OLD']; $documentParameters = $this->arResult['DOCUMENT_DATA']['WEBDAV']; $this->arResult['CHECK_TEMPLATE'] = 'WEBDAV'; } $workflowTemplate = $templates[$this->arParams['TEMPLATE_ID']]; $arWorkflowParameters = array(); $canStartWorkflow = false; if (count($workflowTemplate['PARAMETERS']) <= 0) { $canStartWorkflow = true; } elseif ($this->request->isPost() && strlen($this->request->getPost('DoStartParamWorkflow')) > 0 && check_bitrix_sessid()) { $errorsTemporary = array(); $request = $this->request->getPostList()->toArray(); foreach ($_FILES as $key => $value) { if (array_key_exists('name', $value)) { if (is_array($value['name'])) { $keys = array_keys($value['name']); for ($i = 0, $cnt = count($keys); $i < $cnt; $i++) { $array = array(); foreach ($value as $k1 => $v1) { $array[$k1] = $v1[$keys[$i]]; } $request[$key][] = $array; } } else { $request[$key] = $value; } } } $arWorkflowParameters = CBPWorkflowTemplateLoader::checkWorkflowParameters($workflowTemplate['PARAMETERS'], $request, $documentParameters['DOCUMENT_TYPE'], $errorsTemporary); if (count($errorsTemporary) > 0) { $canStartWorkflow = false; foreach ($errorsTemporary as $e) { $this->errorCollection->add(array(new Error($e['message']))); } } else { $canStartWorkflow = true; } } if ($canStartWorkflow) { $errorsTemporary = array(); $workflowId = CBPDocument::startWorkflow($this->arParams['TEMPLATE_ID'], $documentParameters['DOCUMENT_ID'], array_merge($arWorkflowParameters, array('TargetUser' => 'user_' . intval($this->getUser()->getID()))), $errorsTemporary); if (count($errorsTemporary) > 0) { $this->arResult['SHOW_MODE'] = 'StartWorkflowError'; foreach ($errorsTemporary as $e) { $this->errorCollection->add(array(new Error('[' . $e['code'] . '] ' . $e['message']))); } } else { $this->arResult['SHOW_MODE'] = 'StartWorkflowSuccess'; if (strlen($this->arResult['back_url']) > 0) { LocalRedirect(str_replace('#WF#', $workflowId, $this->request->getQuery('back_url'))); $this->end(true); } } } else { $doStartParam = $this->request->isPost() && strlen($this->request->getPost('DoStartParamWorkflow') && check_bitrix_sessid()) > 0; $keys = array_keys($workflowTemplate['PARAMETERS']); foreach ($keys as $key) { $value = $doStartParam ? $this->request->getQuery($key) : $workflowTemplate['PARAMETERS'][$key]['Default']; if (!is_array($value)) { $this->arResult['PARAMETERS_VALUES'][$key] = CBPHelper::convertParameterValues($value); } else { $keys1 = array_keys($value); foreach ($keys1 as $key1) { $this->arResult['PARAMETERS_VALUES'][$key][$key1] = CBPHelper::convertParameterValues($value[$key1]); } } } $this->arResult['SHOW_MODE'] = 'WorkflowParameters'; } if ($this->errorCollection->hasErrors()) { $error = array_shift($this->getErrors()); ShowError($error->getMessage()); } } else { $this->arResult['SHOW_MODE'] = 'SelectWorkflow'; } }
/** * @param string $workflowId * @param string $iblockType * @param int $elementId * @param int $iblockId * @param string $action Action stop or delete * @return string error */ public static function completeWorkflow($workflowId, $iblockType, $elementId, $iblockId, $action) { if (!Loader::includeModule('bizproc')) { return Loc::getMessage('LISTS_MODULE_BIZPROC_NOT_INSTALLED'); } global $USER; $userId = $USER->getID(); $documentType = BizprocDocument::generateDocumentComplexType($iblockType, $iblockId); $documentId = BizprocDocument::getDocumentComplexId($iblockType, $elementId); $documentStates = CBPDocument::getDocumentStates($documentType, $documentId); $permission = CBPDocument::canUserOperateDocument($action == 'stop' ? CBPCanUserOperateOperation::StartWorkflow : CBPCanUserOperateOperation::CreateWorkflow, $userId, $documentId, array("DocumentStates" => $documentStates)); if (!$permission) { return Loc::getMessage('LISTS_ACCESS_DENIED'); } $stringError = ''; if ($action == 'stop') { $errors = array(); CBPDocument::terminateWorkflow($workflowId, $documentId, $errors); if (!empty($errors)) { $stringError = ''; foreach ($errors as $error) { $stringError .= $error['message']; } $listError[] = array('id' => 'stopBizproc', 'text' => $stringError); } } else { $errors = array(); if (isset($documentStates[$workflowId]['WORKFLOW_STATUS']) && $documentStates[$workflowId]['WORKFLOW_STATUS'] !== null) { CBPDocument::terminateWorkflow($workflowId, $documentId, $errors); } if (!empty($errors)) { $stringError = ''; foreach ($errors as $error) { $stringError .= $error['message']; } $listError[] = array('id' => 'stopBizproc', 'text' => $stringError); } else { CBPTaskService::deleteByWorkflow($workflowId); CBPTrackingService::deleteByWorkflow($workflowId); CBPStateService::deleteWorkflow($workflowId); } } if (empty($listError) && Loader::includeModule('socialnetwork') && $iblockType == COption::getOptionString("lists", "livefeed_iblock_type_id")) { $sourceId = CBPStateService::getWorkflowIntegerId($workflowId); $resultQuery = CSocNetLog::getList(array(), array('EVENT_ID' => 'lists_new_element', 'SOURCE_ID' => $sourceId), false, false, array('ID')); while ($log = $resultQuery->fetch()) { CSocNetLog::delete($log['ID']); } } if (!empty($listError)) { $errorObject = new CAdminException($listError); $stringError = $errorObject->getString(); } return $stringError; }
protected function showBizProc($documentData) { $this->arResult['BIZPROC_PERMISSION'] = array(); $this->arResult['BIZPROC_PERMISSION']['START'] = CBPDocument::canUserOperateDocument(CBPCanUserOperateOperation::StartWorkflow, $this->getUser()->getId(), $documentData['DISK']['DOCUMENT_ID']); $this->arResult['BIZPROC_PERMISSION']['VIEW'] = CBPDocument::canUserOperateDocument(CBPCanUserOperateOperation::ViewWorkflow, $this->getUser()->getId(), $documentData['DISK']['DOCUMENT_ID']); $this->arResult['BIZPROC_PERMISSION']['STOP'] = $this->arResult['BIZPROC_PERMISSION']['START']; $this->arResult['BIZPROC_PERMISSION']['DROP'] = CBPDocument::canUserOperateDocument(CBPCanUserOperateOperation::CreateWorkflow, $this->getUser()->getId(), $documentData['DISK']['DOCUMENT_ID']); foreach ($documentData as $nameModuleId => $data) { $temporary[$nameModuleId] = CBPDocument::getDocumentStates($data['DOCUMENT_TYPE'], $data['DOCUMENT_ID']); } if (isset($temporary['OLD_FILE'])) { $allBizProcArray = array_merge($temporary['DISK'], $temporary['WEBDAV'], $temporary['OLD_FILE']); } else { $allBizProcArray = array_merge($temporary['DISK'], $temporary['WEBDAV']); } if (!empty($allBizProcArray)) { $userGroup = $this->getUser()->getUserGroupArray(); $userGroup[] = 'author'; if ($this->request->isPost() && intval($this->request->getPost('bizproc_index')) > 0) { $bizProcWorkflowId = array(); $bizprocIndex = intval($this->request->getPost('bizproc_index')); for ($i = 1; $i <= $bizprocIndex; $i++) { $bpId = trim($this->request->getPost("bizproc_id_" . $i)); $bpTemplateId = intval($this->request->getPost("bizproc_template_id_" . $i)); $bpEvent = trim($this->request->getPost("bizproc_event_" . $i)); if (strlen($bpId) > 0) { if (!array_key_exists($bpId, $allBizProcArray)) { continue; } } else { if (!array_key_exists($bpTemplateId, $allBizProcArray)) { continue; } $bpId = $bizProcWorkflowId[$bpTemplateId]; } if (strlen($bpEvent) > 0) { $errors = array(); CBPDocument::sendExternalEvent($bpId, $bpEvent, array("Groups" => $userGroup, "User" => $this->getUser()->getId()), $errors); } else { $errors = array(); foreach ($allBizProcArray as $idBizProc => $bizProcArray) { if ($idBizProc == $bpId) { CBPDocument::TerminateWorkflow($bpId, $bizProcArray['DOCUMENT_ID'], $errors); } } } if (!empty($errors)) { foreach ($errors as $error) { $this->arResult['ERROR_MESSAGE'] = $error['message']; } } else { LocalRedirect($this->arResult['PATH_TO_FILE_VIEW'] . "#tab-bp"); } } } $this->arResult['BIZPROC_LIST'] = array(); $count = 1; foreach ($allBizProcArray as $idBizProc => $bizProcArray) { if (intVal($bizProcArray["WORKFLOW_STATUS"]) < 0 || $idBizProc <= 0) { continue; } else { if (!CBPDocument::canUserOperateDocument(CBPCanUserOperateOperation::ViewWorkflow, $this->getUser()->getId(), $documentData['DISK']['DOCUMENT_ID'], array("DocumentStates" => $bizProcArray, "WorkflowId" => $bizProcArray["ID"] > 0 ? $bizProcArray["ID"] : $bizProcArray["TEMPLATE_ID"]))) { continue; } } $groups = CBPDocument::getAllowableUserGroups($documentData['DISK']['DOCUMENT_TYPE']); foreach ($groups as $key => $val) { $groups[strtolower($key)] = $val; } $users = array(); $dmpWorkflow = CBPTrackingService::getList(array("ID" => "DESC"), array("WORKFLOW_ID" => $idBizProc, "TYPE" => array(CBPTrackingType::Report, CBPTrackingType::Custom, CBPTrackingType::FaultActivity)), false, array("nTopCount" => 5), array("ID", "TYPE", "MODIFIED", "ACTION_NOTE", "ACTION_TITLE", "ACTION_NAME", "EXECUTION_STATUS", "EXECUTION_RESULT")); while ($track = $dmpWorkflow->getNext()) { $messageTemplate = ""; switch ($track["TYPE"]) { case 1: $messageTemplate = Loc::getMessage("DISK_FILE_VIEW_BPABL_TYPE_1"); break; case 2: $messageTemplate = Loc::getMessage("DISK_FILE_VIEW_BPABL_TYPE_2"); break; case 3: $messageTemplate = Loc::getMessage("DISK_FILE_VIEW_BPABL_TYPE_3"); break; case 4: $messageTemplate = Loc::getMessage("DISK_FILE_VIEW_BPABL_TYPE_4"); break; case 5: $messageTemplate = Loc::getMessage("DISK_FILE_VIEW_BPABL_TYPE_5"); break; default: $messageTemplate = Loc::getMessage("DISK_FILE_VIEW_BPABL_TYPE_6"); } $name = strlen($track["ACTION_TITLE"]) > 0 ? $track["ACTION_TITLE"] : $track["ACTION_NAME"]; switch ($track["EXECUTION_STATUS"]) { case CBPActivityExecutionStatus::Initialized: $status = Loc::getMessage("DISK_FILE_VIEW_BPABL_STATUS_1"); break; case CBPActivityExecutionStatus::Executing: $status = Loc::getMessage("DISK_FILE_VIEW_BPABL_STATUS_2"); break; case CBPActivityExecutionStatus::Canceling: $status = Loc::getMessage("DISK_FILE_VIEW_BPABL_STATUS_3"); break; case CBPActivityExecutionStatus::Closed: $status = Loc::getMessage("DISK_FILE_VIEW_BPABL_STATUS_4"); break; case CBPActivityExecutionStatus::Faulting: $status = Loc::getMessage("DISK_FILE_VIEW_BPABL_STATUS_5"); break; default: $status = Loc::getMessage("DISK_FILE_VIEW_BPABL_STATUS_6"); } switch ($track["EXECUTION_RESULT"]) { case CBPActivityExecutionResult::None: $result = Loc::getMessage("DISK_FILE_VIEW_BPABL_RES_1"); break; case CBPActivityExecutionResult::Succeeded: $result = Loc::getMessage("DISK_FILE_VIEW_BPABL_RES_2"); break; case CBPActivityExecutionResult::Canceled: $result = Loc::getMessage("DISK_FILE_VIEW_BPABL_RES_3"); break; case CBPActivityExecutionResult::Faulted: $result = Loc::getMessage("DISK_FILE_VIEW_BPABL_RES_4"); break; case CBPActivityExecutionResult::Uninitialized: $result = Loc::getMessage("DISK_FILE_VIEW_BPABL_RES_5"); break; default: $result = Loc::getMessage("DISK_FILE_VIEW_BPABL_RES_6"); } $note = strlen($track["ACTION_NOTE"]) > 0 ? ": " . $track["ACTION_NOTE"] : ""; $pattern = array("#ACTIVITY#", "#STATUS#", "#RESULT#", "#NOTE#"); $replaceArray = array($name, $status, $result, $note); if (!empty($track["ACTION_NAME"]) && !empty($track["ACTION_TITLE"])) { $pattern[] = $track["ACTION_NAME"]; $replaceArray[] = $track["ACTION_TITLE"]; } $messageTemplate = str_replace($pattern, $replaceArray, $messageTemplate); if (preg_match_all("/(?<=\\{\\=user\\:)([^\\}]+)(?=\\})/is", $messageTemplate, $arMatches)) { $pattern = array(); $replacement = array(); foreach ($arMatches[0] as $user) { $user = preg_quote($user); if (in_array("/\\{\\=user\\:" . $user . "\\}/is", $pattern)) { continue; } $replace = ""; if (array_key_exists(strtolower($user), $groups)) { $replace = $groups[strtolower($user)]; } elseif (array_key_exists(strtoupper($user), $groups)) { $replace = $groups[strtoupper($user)]; } else { $id = intVal(str_replace("user_", "", $user)); if (!array_key_exists($id, $users)) { $dbRes = \CUser::getByID($id); $users[$id] = false; if ($dbRes && ($arUser = $dbRes->getNext())) { $name = \CUser::formatName($this->arParams["NAME_TEMPLATE"], $arUser); $arUser["FULL_NAME"] = empty($name) ? $arUser["LOGIN"] : $name; $users[$id] = $arUser; } } if (!empty($users[$id])) { $replace = "<a href=\"" . \CComponentEngine::makePathFromTemplate('/company/personal/user/#USER_ID#/', array("USER_ID" => $id)) . "\">" . $users[$id]["FULL_NAME"] . "</a>"; } } if (!empty($replace)) { $pattern[] = "/\\{\\=user\\:" . $user . "\\}/is"; $pattern[] = "/\\{\\=user\\:user\\_" . $user . "\\}/is"; $replacement[] = $replace; $replacement[] = $replace; } } $messageTemplate = preg_replace($pattern, $replacement, $messageTemplate); } $this->arResult['BIZPROC_LIST'][$count]['DUMP_WORKFLOW'][] = $messageTemplate; } $tasks = CBPDocument::getUserTasksForWorkflow($this->getUser()->getId(), $idBizProc); $events = CBPDocument::getAllowableEvents($this->getUser()->getId(), $userGroup, $bizProcArray); if (!empty($tasks)) { foreach ($tasks as $task) { $urlTaskBizproc = \CComponentEngine::makePathFromTemplate($this->arParams['PATH_TO_DISK_TASK'], array("ID" => $task['ID'])); $urlTaskBizproc .= "?back_url=" . urlencode($this->getApplication()->getCurPage()) . "&file=" . $this->file->getName(); $this->arResult['BIZPROC_LIST'][$count]['TASK']['URL'] = $urlTaskBizproc; $this->arResult['BIZPROC_LIST'][$count]['TASK']['TASK_ID'] = $task['ID']; $this->arResult['BIZPROC_LIST'][$count]['TASK']['TASK_NAME'] = $task['NAME']; } } $this->arResult['BIZPROC_LIST'][$count]['ID'] = $bizProcArray['ID']; $this->arResult['BIZPROC_LIST'][$count]['WORKFLOW_STATUS'] = $bizProcArray["WORKFLOW_STATUS"]; $this->arResult['BIZPROC_LIST'][$count]['TEMPLATE_ID'] = $bizProcArray['TEMPLATE_ID']; $this->arResult['BIZPROC_LIST'][$count]['TEMPLATE_NAME'] = $bizProcArray['TEMPLATE_NAME']; $this->arResult['BIZPROC_LIST'][$count]['STATE_MODIFIED'] = $bizProcArray['STATE_MODIFIED']; $this->arResult['BIZPROC_LIST'][$count]['STATE_TITLE'] = $bizProcArray['STATE_TITLE']; $this->arResult['BIZPROC_LIST'][$count]['STATE_NAME'] = $bizProcArray['STATE_NAME']; $this->arResult['BIZPROC_LIST'][$count]['EVENTS'] = $events; $count++; } } }