29 private static $entityList = [
30 self::ENTITY_BIZPROC_MAIN => 500,
31 self::ENTITY_BIZPROC_CRM_TRIGGER => 600,
32 self::ENTITY_BIZPROC_SCRIPT => 700,
34 private static $customDealMatch =
'/^C([0-9]+):/';
35 private static $accessModules = [
'crm'];
36 private static $context;
37 private static $accessManifest = [
44 return static::$entityList;
52 if (!static::$entityList[$code])
64 $code === self::ENTITY_BIZPROC_SCRIPT
73 if (static::checkRequiredParams($code))
79 $result = static::exportBizproc($step);
82 $result = static::exportCrmTrigger($step);
85 $result = static::exportScript($step, $event->
getParameter(
'NEXT'), $itemCode);
92 $result[
'NEXT'] =
false;
93 $result[
'ERROR_ACTION'] = $e->getMessage();
95 'BIZPROC_ERROR_CONFIGURATION_EXPORT_EXCEPTION',
108 if (!static::$entityList[$code])
120 $code === self::ENTITY_BIZPROC_SCRIPT
131 if (static::checkRequiredParams($code))
136 $result = static::clearBizproc($option);
139 $result = static::clearCrmTrigger($option);
142 $result = static::clearScript($option);
149 $result[
'NEXT'] =
false;
150 $result[
'ERROR_ACTION'] = $e->getMessage();
152 'BIZPROC_ERROR_CONFIGURATION_CLEAR_EXCEPTION',
165 if (!static::$entityList[$code])
177 $code === self::ENTITY_BIZPROC_SCRIPT
188 $setting =
new Setting($contextUser);
190 $appId = (int)$app[
'ID'];
194 if (static::checkRequiredParams($code))
199 $result = static::importBizproc($data);
202 $result = static::importCrmTrigger($data);
205 $result = static::importScript($data, $userId, $appId);
212 $result[
'NEXT'] =
false;
213 $result[
'ERROR_ACTION'] = $e->getMessage();
215 'BIZPROC_ERROR_CONFIGURATION_IMPORT_EXCEPTION',
230 private static function checkRequiredParams($type)
233 if ($type == self::ENTITY_BIZPROC_CRM_TRIGGER)
235 if (!Loader::IncludeModule(
'crm'))
245 private static function exportBizproc($step)
252 $res = WorkflowTemplateTable::getList(
258 '=MODULE_ID' => static::$accessModules,
259 '<AUTO_EXECUTE' => \CBPDocumentEventType::Script,
265 if ($tpl = $res->fetchObject())
267 $result[
'NEXT'] = $step;
268 if (in_array($tpl->getModuleId(), static::$accessModules))
270 $result[
'FILE_NAME'] = $step;
271 $packer = new \Bitrix\Bizproc\Workflow\Template\Packer\Bpt();
272 $data = $packer->makePackageData($tpl);
273 $result[
'CONTENT'] = [
274 'ID' => $tpl->getId(),
275 'MODULE_ID' => $tpl->getModuleId(),
276 'ENTITY' => $tpl->getEntity(),
277 'DOCUMENT_TYPE' => $tpl->getDocumentType(),
278 'DOCUMENT_STATUS' => $tpl->getDocumentStatus(),
279 'NAME' => $tpl->getName(),
280 'AUTO_EXECUTE' => $tpl->getAutoExecute(),
281 'DESCRIPTION' => $tpl->getDescription(),
282 'SYSTEM_CODE' => $tpl->getSystemCode(),
283 'ORIGINATOR_ID' => $tpl->getOriginatorId(),
284 'ORIGIN_ID' => $tpl->getOriginId(),
285 'TEMPLATE_DATA' => $data,
293 private static function clearBizproc($option)
297 'OWNER_DELETE' => [],
299 $clearFull = $option[
'CLEAR_FULL'];
300 $prefix = $option[
'PREFIX_NAME'];
301 $pattern =
'/^\(' . $prefix .
'\)/';
303 $res = WorkflowTemplateTable::getList(
309 '=MODULE_ID' => static::$accessModules,
310 '>ID' => $option[
'NEXT'],
311 '<AUTO_EXECUTE' => \CBPDocumentEventType::Script,
317 while ($item = $res->Fetch())
319 $result[
'NEXT'] = $item[
'ID'];
321 if (!$clearFull && $item[
'DOCUMENT_TYPE'] ==
'DEAL')
325 preg_match(static::$customDealMatch, $item[
'DOCUMENT_STATUS'], $matches, PREG_OFFSET_CAPTURE);
326 if (!empty($matches))
332 if ($clearFull || !empty($item[
'DOCUMENT_STATUS']))
334 CBPDocument::DeleteWorkflowTemplate(
339 $item[
'DOCUMENT_TYPE'],
345 $result[
'OWNER_DELETE'][] = [
347 'ENTITY' => $item[
'ID'],
353 $name = $item[
'NAME'];
354 if ($prefix !=
'' && preg_match($pattern, $name) === 0)
356 $name =
"($prefix) " . $name;
358 CBPDocument::UpdateWorkflowTemplate(
363 $item[
'DOCUMENT_TYPE'],
367 'AUTO_EXECUTE' => \CBPDocumentEventType::None,
378 private static function importBizproc($importData)
382 if (!isset($importData[
'CONTENT'][
'DATA']))
386 $item = $importData[
'CONTENT'][
'DATA'];
388 in_array($item[
'MODULE_ID'], static::$accessModules)
389 && Loader::includeModule($item[
'MODULE_ID'])
390 && class_exists($item[
'ENTITY'])
393 if (is_subclass_of($item[
'ENTITY'],
'\\IBPWorkflowDocument'))
396 if (isset($importData[
'RATIO'][
'CRM_STATUS']))
398 if (is_array($item[
'TEMPLATE_DATA']))
400 $item[
'TEMPLATE_DATA'] = static::changeDealCategory($item[
'TEMPLATE_DATA'], $importData[
'RATIO'][
'CRM_STATUS']);
402 if ($item[
'DOCUMENT_TYPE'] ==
'DEAL' && $item[
'DOCUMENT_STATUS'])
404 $item[
'DOCUMENT_STATUS'] = static::changeDealCategory($item[
'DOCUMENT_STATUS'], $importData[
'RATIO'][
'CRM_STATUS']);
410 $code = static::$context .
'_xml_' . intval($item[
'ID']);
411 $id = \CBPWorkflowTemplateLoader::importTemplateFromArray(
416 $item[
'DOCUMENT_TYPE'],
418 $item[
'AUTO_EXECUTE'],
420 isset($item[
'DESCRIPTION']) ? (
string)$item[
'DESCRIPTION'] :
'',
421 $item[
'TEMPLATE_DATA'],
432 if ($item[
'DOCUMENT_STATUS'])
434 \CBPWorkflowTemplateLoader::update(
437 'DOCUMENT_STATUS' => $item[
'DOCUMENT_STATUS'],
443 catch (\Exception $e)
445 $result[
'ERROR_ACTION'] = $e->getMessage();
447 'BIZPROC_ERROR_CONFIGURATION_IMPORT_EXCEPTION_BP'
458 private static function exportCrmTrigger($step)
466 $res = TriggerTable::getList(
477 if ($item = $res->Fetch())
479 $result[
'FILE_NAME'] = $step;
480 $result[
'CONTENT'] = $item;
481 $result[
'NEXT'] = $step;
487 private static function clearCrmTrigger($option)
492 $clearFull = $option[
'CLEAR_FULL'];
494 $res = TriggerTable::getList(
500 '>ID' => $option[
'NEXT'],
505 while ($item = $res->Fetch())
507 $result[
'NEXT'] = $item[
'ID'];
508 if (!$clearFull && $item[
'ENTITY_TYPE_ID'] == \CCrmOwnerType::Deal)
512 preg_match(static::$customDealMatch, $item[
'ENTITY_STATUS'], $matches, PREG_OFFSET_CAPTURE);
513 if (!empty($matches))
518 $delete = TriggerTable::delete($item[
'ID']);
519 if ($delete->isSuccess())
521 $result[
'OWNER_DELETE'][] = [
523 'ENTITY' => $item[
'ID'],
531 private static function importCrmTrigger($importData)
534 if (!isset($importData[
'CONTENT'][
'DATA']))
538 $item = $importData[
'CONTENT'][
'DATA'];
542 && isset($item[
'CODE'])
543 && isset($item[
'ENTITY_TYPE_ID'])
544 && isset($item[
'ENTITY_STATUS'])
547 if (isset($importData[
'RATIO'][
'CRM_STATUS']))
549 if (is_array($item[
'APPLY_RULES']))
551 $item[
'APPLY_RULES'] = static::changeDealCategory(
552 $item[
'APPLY_RULES'],
553 $importData[
'RATIO'][
'CRM_STATUS']
556 if ($item[
'ENTITY_TYPE_ID'] == \CCrmOwnerType::Deal)
558 $item[
'ENTITY_STATUS'] = static::changeDealCategory(
559 $item[
'ENTITY_STATUS'],
560 $importData[
'RATIO'][
'CRM_STATUS']
566 'NAME' => $item[
'NAME'],
567 'CODE' => $item[
'CODE'],
568 'ENTITY_TYPE_ID' => $item[
'ENTITY_TYPE_ID'],
569 'ENTITY_STATUS' => $item[
'ENTITY_STATUS'],
570 'APPLY_RULES' => is_array($item[
'APPLY_RULES']) ? $item[
'APPLY_RULES'] :
null,
573 $res = TriggerTable::add($saveData);
574 if ($res->isSuccess())
578 'ENTITY' => $res->getId(),
589 private static function exportScript($step, $nextId, $docType)
597 $nextId = (int)$nextId;
601 '=MODULE_ID' => static::$accessModules,
606 $filter[
'=DOCUMENT_TYPE'] = $docType;
609 $res = ScriptTable::getList(
619 if ($tpl = $res->fetch())
621 $data = Script\Manager::exportScript($tpl[
'ID']);
624 $result[
'NEXT'] = $tpl[
'ID'];
625 $result[
'FILE_NAME'] = $step;
626 $result[
'CONTENT'] = $data;
633 private static function clearScript($option)
637 'OWNER_DELETE' => [],
640 if (!$option[
'CLEAR_FULL'])
645 $res = ScriptTable::getList(
651 '>ID' => (
int)$option[
'NEXT'],
652 '=MODULE_ID' => static::$accessModules,
659 while ($item = $res->Fetch())
661 $result[
'NEXT'] = $item[
'ID'];
663 $deletionResult = Script\Manager::deleteScript($item[
'ID']);
665 if ($deletionResult->isSuccess())
667 $result[
'OWNER_DELETE'][] = [
669 'ENTITY' => $item[
'ID'],
677 private static function importScript($importData,
int $userId,
int $appId)
681 if (!isset($importData[
'CONTENT'][
'DATA']))
685 $item = $importData[
'CONTENT'][
'DATA'];
687 in_array($item[
'MODULE_ID'], static::$accessModules)
688 && Loader::includeModule($item[
'MODULE_ID'])
689 && class_exists($item[
'ENTITY'])
692 if (is_subclass_of($item[
'ENTITY'],
'\\IBPWorkflowDocument'))
694 if (isset($importData[
'RATIO'][
'CRM_STATUS']) && $item[
'DOCUMENT_TYPE'] ===
'DEAL')
696 if (is_array($item[
'WORKFLOW_TEMPLATE']))
698 $item[
'WORKFLOW_TEMPLATE'] = static::changeDealCategory(
699 $item[
'WORKFLOW_TEMPLATE'],
700 $importData[
'RATIO'][
'CRM_STATUS']
707 $item[
'ORIGINATOR_ID'] =
'REST_APP';
708 $item[
'ORIGIN_ID'] = $appId;
711 $importResult = Script\Manager::importScript($item, $userId);
713 if ($importResult->isSuccess())
717 'ENTITY' => $importResult->getId(),
722 $result[
'ERROR_ACTION'] = $result[
'ERROR_MESSAGES'] = current($importResult->getErrorMessages());
731 private static function changeDealCategory($data, $ratio)
737 foreach ($ratio as $oldId => $newId)
739 $ratioRegEx[] =
'/^C' . $oldId .
':/';
740 $ratioReplace[] =
'C' . $newId .
':';
742 if (!empty($ratioRegEx))
744 $data = static::changeDealCategoryAction($data, $ratioRegEx, $ratioReplace, $ratio);
751 private static function changeDealCategoryAction($data, $ratioRegEx, $ratioReplace, $ratio)
753 if (is_string($data))
755 $data = preg_replace($ratioRegEx, $ratioReplace, $data);
757 elseif (is_array($data))
760 isset($data[
'field'])
761 && $data[
'field'] ==
'CATEGORY_ID'
762 && $data[
'value'] > 0
763 && $ratio[$data[
'value']] > 0
766 $data[
'value'] = $ratio[$data[
'value']];
769 foreach ($data as $key => $value)
771 $newKey = static::changeDealCategoryAction($key, $ratioRegEx, $ratioReplace, $ratio);
777 if ($newKey ==
'CATEGORY_ID')
779 if (is_array($value))
781 if (isset($value[
'Options']) && is_array($value[
'Options']))
783 $data[$newKey][
'Options'] = [];
784 foreach ($value[
'Options'] as $dealId => $title)
786 if (isset($ratio[$dealId]))
788 $data[$newKey][
'Options'][$ratio[$dealId]] =
$title;
794 $data[$newKey] = static::changeDealCategoryAction(
802 elseif (is_string($value) && isset($ratio[$value]))
804 $data[$newKey] = $ratio[$value];
808 $data[$newKey] = static::changeDealCategoryAction(
816 elseif ($newKey ==
'CategoryId' && intVal($value) > 0 && !empty($ratio[$value]))
818 $data[$newKey] = $ratio[$value];
822 $data[$newKey] = static::changeDealCategoryAction(