30 private static $entityList = [
31 self::ENTITY_BIZPROC_MAIN => 500,
32 self::ENTITY_BIZPROC_CRM_TRIGGER => 600,
33 self::ENTITY_BIZPROC_SCRIPT => 700,
35 private static $customDealMatch =
'/^C([0-9]+):/';
36 private static $accessModules = [
'crm'];
37 private static $context =
'';
38 private static $accessManifest = [
45 return static::$entityList;
52 $itemCode =
$event->getParameter(
'ITEM_CODE');
53 if (!static::$entityList[
$code])
60 $code !== self::ENTITY_BIZPROC_SCRIPT
68 $code === self::ENTITY_BIZPROC_SCRIPT
77 if (static::checkRequiredParams(
$code))
79 $step =
$event->getParameter(
'STEP');
82 case self::ENTITY_BIZPROC_MAIN:
83 $result = static::exportBizproc($step);
85 case self::ENTITY_BIZPROC_CRM_TRIGGER:
86 $result = static::exportCrmTrigger($step);
88 case self::ENTITY_BIZPROC_SCRIPT:
89 $result = static::exportScript($step,
$event->getParameter(
'NEXT'), $itemCode);
97 $result[
'ERROR_ACTION'] = $e->getMessage();
98 $result[
'ERROR_MESSAGES'] = Loc::getMessage(
99 'BIZPROC_ERROR_CONFIGURATION_EXPORT_EXCEPTION',
112 if (!static::$entityList[
$code])
119 $code !== self::ENTITY_BIZPROC_SCRIPT
127 $code === self::ENTITY_BIZPROC_SCRIPT
138 if (static::checkRequiredParams(
$code))
142 case self::ENTITY_BIZPROC_MAIN:
145 case self::ENTITY_BIZPROC_CRM_TRIGGER:
148 case self::ENTITY_BIZPROC_SCRIPT:
157 $result[
'ERROR_ACTION'] = $e->getMessage();
158 $result[
'ERROR_MESSAGES'] = Loc::getMessage(
159 'BIZPROC_ERROR_CONFIGURATION_CLEAR_EXCEPTION',
172 if (!static::$entityList[
$code])
179 $code !== self::ENTITY_BIZPROC_SCRIPT
187 $code === self::ENTITY_BIZPROC_SCRIPT
197 $contextUser =
$event->getParameter(
'CONTEXT_USER');
198 $setting =
new Setting($contextUser);
200 $appId = (int)
$app[
'ID'];
204 if (static::checkRequiredParams(
$code))
208 case self::ENTITY_BIZPROC_MAIN:
211 case self::ENTITY_BIZPROC_CRM_TRIGGER:
214 case self::ENTITY_BIZPROC_SCRIPT:
223 $result[
'ERROR_ACTION'] = $e->getMessage();
224 $result[
'ERROR_MESSAGES'] = Loc::getMessage(
225 'BIZPROC_ERROR_CONFIGURATION_IMPORT_EXCEPTION',
235 private static function isCrmModuleIncluded(): bool
237 return Loader::includeModule(
'crm');
245 private static function checkRequiredParams(
$type)
248 if (
$type == self::ENTITY_BIZPROC_CRM_TRIGGER)
250 if (!static::isCrmModuleIncluded())
259 private static function exportCrmDynamicTypesInfo():
array
263 if (!static::isCrmModuleIncluded())
268 $helper =
new Helper();
269 $result = $helper->exportCrmDynamicTypesInfo();
274 private static function getDynamicTypeCustomSectionIdByEntityTypeId(
int $entityTypeId): int
278 if (!static::isCrmModuleIncluded())
283 $helper =
new Helper();
284 $result = $helper->getDynamicTypeCustomSectionIdByEntityTypeId($entityTypeId);
289 private static function isDynamicDocumentType(
string $documentType): bool
291 if (static::getDynamicEntityTypeIdByDocumentType($documentType) > 0)
299 private static function isDynamicEntityType(
int $entityTypeId): bool
301 if (static::isCrmModuleIncluded() && CCrmOwnerType::isPossibleDynamicTypeId($entityTypeId))
309 private static function getDynamicTypeCustomSectionIdByDocumentType(
string $documentType): int
311 $dynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($documentType);
313 return static::getDynamicTypeCustomSectionIdByEntityTypeId($dynamicEntityTypeId);
316 private static function checkDynamicTypeByEntityType(
int $entityTypeId): bool
318 if (static::isDynamicEntityType($entityTypeId))
320 $helper =
new Helper();
322 return $helper->checkDynamicTypeByEntityType($entityTypeId);
328 private static function checkDynamicTypeByDocumentType(
string $documentType): bool
330 $dynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($documentType);
332 return static::checkDynamicTypeByEntityType($dynamicEntityTypeId);
336 private static function exportBizproc($step)
343 $res = WorkflowTemplateTable::getList(
349 '=MODULE_ID' => static::$accessModules,
356 if ($tpl =
$res->fetchObject())
359 if (in_array($tpl->getModuleId(), static::$accessModules))
361 $documentType = $tpl->getDocumentType();
363 !static::isDynamicDocumentType($documentType)
365 static::checkDynamicTypeByDocumentType($documentType)
366 && static::getDynamicTypeCustomSectionIdByDocumentType($documentType) <= 0
371 $packer = new \Bitrix\Bizproc\Workflow\Template\Packer\Bpt();
372 $data = $packer->makePackageData($tpl);
374 'ID' => $tpl->getId(),
375 'MODULE_ID' => $tpl->getModuleId(),
376 'ENTITY' => $tpl->getEntity(),
377 'DOCUMENT_TYPE' => $tpl->getDocumentType(),
378 'DOCUMENT_STATUS' => $tpl->getDocumentStatus(),
379 'NAME' => $tpl->getName(),
380 'AUTO_EXECUTE' => $tpl->getAutoExecute(),
381 'DESCRIPTION' => $tpl->getDescription(),
382 'SYSTEM_CODE' => $tpl->getSystemCode(),
383 'ORIGINATOR_ID' => $tpl->getOriginatorId(),
384 'ORIGIN_ID' => $tpl->getOriginId(),
385 'TEMPLATE_DATA' =>
$data,
388 if (static::isCrmModuleIncluded() && $step === 0)
391 $result[
'CONTENT'][
'CRM_DYNAMIC_TYPES_INFO'] = static::exportCrmDynamicTypesInfo();
399 private static function clearBizproc(
$option)
403 'OWNER_DELETE' => [],
405 $clearFull =
$option[
'CLEAR_FULL'];
406 $prefix =
$option[
'PREFIX_NAME'];
407 $pattern =
'/^\(' . $prefix .
'\)/';
409 $res = WorkflowTemplateTable::getList(
415 '=MODULE_ID' => static::$accessModules,
423 while ($item =
$res->Fetch())
429 isset($item[
'DOCUMENT_TYPE'])
430 && is_string($item[
'DOCUMENT_TYPE'])
431 && $item[
'DOCUMENT_TYPE'] !==
''
433 ? $item[
'DOCUMENT_TYPE']
436 if (static::getDynamicTypeCustomSectionIdByDocumentType($documentType) > 0)
441 if (!$clearFull && $item[
'DOCUMENT_TYPE'] ==
'DEAL')
445 preg_match(static::$customDealMatch, $item[
'DOCUMENT_STATUS'],
$matches, PREG_OFFSET_CAPTURE);
452 if ($clearFull || !empty($item[
'DOCUMENT_STATUS']))
454 CBPDocument::DeleteWorkflowTemplate(
459 $item[
'DOCUMENT_TYPE'],
466 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC,
467 'ENTITY' => $item[
'ID'],
473 $name = $item[
'NAME'];
478 CBPDocument::UpdateWorkflowTemplate(
483 $item[
'DOCUMENT_TYPE'],
498 private static function getDynamicTypeReplacementLists(
499 array $dynamicTypesInfo,
501 bool $refresh =
false
504 static $replacementLists =
null;
506 if ($replacementLists ===
null || $refresh)
508 if (static::isCrmModuleIncluded())
510 $crmHelper =
new Helper();
511 $replacementLists = $crmHelper->prepareDynamicTypeReplacementLists($dynamicTypesInfo, $ratioInfo);
515 return $replacementLists;
518 private static function getDynamicTypesInfoForImport(
array $importData,
array &$importResult):
array
523 self::ENTITY_BIZPROC_MAIN,
524 self::ENTITY_BIZPROC_CRM_TRIGGER,
525 self::ENTITY_BIZPROC_SCRIPT,
529 foreach ($codes as
$code)
532 isset($importData[
'RATIO'][
$code][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'])
533 && is_array($importData[
'RATIO'][
$code][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'])
534 && !empty($importData[
'RATIO'][
$code][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'])
542 $code = $actualCode ?? $importData[
'CODE'];
545 isset($importData[
'CONTENT'][
'DATA'][
'CRM_DYNAMIC_TYPES_INFO'])
546 && is_array($importData[
'CONTENT'][
'DATA'][
'CRM_DYNAMIC_TYPES_INFO'])
547 && !empty($importData[
'CONTENT'][
'DATA'][
'CRM_DYNAMIC_TYPES_INFO'])
551 $result = $importData[
'CONTENT'][
'DATA'][
'CRM_DYNAMIC_TYPES_INFO'];
554 $importResult[
'RATIO'][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'] =
$result;
557 isset($importData[
'RATIO'][
$code][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'])
558 && is_array($importData[
'RATIO'][
$code][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'])
559 && !empty($importData[
'RATIO'][
$code][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'])
563 $result = $importData[
'RATIO'][
$code][
'CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'];
569 private static function getDynamicEntityTypeIdByDocumentType(
string $dynamicEntityTypeName): int
572 if (static::isCrmModuleIncluded())
574 $dynamicTypePrefix = CCrmOwnerType::DynamicTypePrefixName;
575 $dynamicTypeRegExp =
"/$dynamicTypePrefix(\\d+)/u";
578 if (preg_match($dynamicTypeRegExp, $dynamicEntityTypeName,
$matches))
584 return $entityTypeId;
587 private static function getDynamicEntityTypeIdByOldEntityTypeId(
int $oldDynamicEntityTypeId, $importData): int
589 $newDynamicEntityTypeId = 0;
591 if (static::isCrmModuleIncluded())
593 $isSetRatio = (isset($importData[
'RATIO']) && is_array($importData[
'RATIO']));
594 $newDynamicEntityTypeId = (
new Helper())->getDynamicEntityTypeIdByOldEntityTypeId(
595 $oldDynamicEntityTypeId,
596 $isSetRatio ? $importData[
'RATIO'] : []
600 return $newDynamicEntityTypeId;
603 private static function importBizproc($importData)
607 if (!isset($importData[
'CONTENT'][
'DATA']))
611 $dynamicTypesInfo = static::getDynamicTypesInfoForImport($importData,
$result);
612 $item = $importData[
'CONTENT'][
'DATA'];
614 in_array($item[
'MODULE_ID'], static::$accessModules)
615 && Loader::includeModule($item[
'MODULE_ID'])
616 && class_exists($item[
'ENTITY'])
619 if (is_subclass_of($item[
'ENTITY'],
'\\IBPWorkflowDocument'))
621 $oldDynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($item[
'DOCUMENT_TYPE']);
622 $isDynamicType = ($oldDynamicEntityTypeId > 0);
625 $newDynamicEntityTypeId = static::getDynamicEntityTypeIdByOldEntityTypeId(
626 $oldDynamicEntityTypeId,
631 !empty($dynamicTypesInfo)
632 && self::isCrmModuleIncluded()
633 && static::checkDynamicTypeByEntityType($newDynamicEntityTypeId)
636 $isSetRatio = (isset($importData[
'RATIO']) && is_array($importData[
'RATIO']));
637 $replacementLists = static::getDynamicTypeReplacementLists(
639 $isSetRatio ? $importData[
'RATIO'] : []
641 $item[
'DOCUMENT_TYPE'] =
642 static::changeDynamicTypeIdentifiers(
643 $oldDynamicEntityTypeId,
644 $item[
'DOCUMENT_TYPE'],
645 $importData[
'RATIO'],
649 if (is_string($item[
'DOCUMENT_STATUS']) && $item[
'DOCUMENT_STATUS'] !==
'')
651 $item[
'DOCUMENT_STATUS'] =
652 static::changeDynamicTypeIdentifiers(
653 $oldDynamicEntityTypeId,
654 $item[
'DOCUMENT_STATUS'],
655 $importData[
'RATIO'],
660 if (is_string($item[
'NAME']) && $item[
'NAME'] !==
'')
663 static::changeDynamicTypeIdentifiers(
664 $oldDynamicEntityTypeId,
666 $importData[
'RATIO'],
671 if(is_array($item[
'TEMPLATE_DATA']))
673 $item[
'TEMPLATE_DATA'] =
674 static::changeDynamicTypeIdentifiers(
675 $oldDynamicEntityTypeId,
676 $item[
'TEMPLATE_DATA'],
677 $importData[
'RATIO'],
686 if (isset($importData[
'RATIO'][
'CRM_STATUS']))
688 if (is_array($item[
'TEMPLATE_DATA']))
690 $item[
'TEMPLATE_DATA'] = static::changeDealCategory(
691 $item[
'TEMPLATE_DATA'],
692 $importData[
'RATIO'][
'CRM_STATUS']
695 if ($item[
'DOCUMENT_TYPE'] ==
'DEAL' && $item[
'DOCUMENT_STATUS'])
697 $item[
'DOCUMENT_STATUS'] = static::changeDealCategory(
698 $item[
'DOCUMENT_STATUS'],
699 $importData[
'RATIO'][
'CRM_STATUS']
707 $code = static::$context .
'_xml_' . intval($item[
'ID']);
708 $id = \CBPWorkflowTemplateLoader::importTemplateFromArray(
713 $item[
'DOCUMENT_TYPE'],
715 $item[
'AUTO_EXECUTE'],
717 isset($item[
'DESCRIPTION']) ? (
string)$item[
'DESCRIPTION'] :
'',
718 $item[
'TEMPLATE_DATA'],
725 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC,
729 if ($item[
'DOCUMENT_STATUS'])
731 \CBPWorkflowTemplateLoader::update(
734 'DOCUMENT_STATUS' => $item[
'DOCUMENT_STATUS'],
742 $result[
'ERROR_ACTION'] = $e->getMessage();
743 $result[
'ERROR_MESSAGES'] = Loc::getMessage(
744 'BIZPROC_ERROR_CONFIGURATION_IMPORT_EXCEPTION_BP'
755 private static function exportCrmTrigger($step)
763 $res = TriggerTable::getList(
774 if ($item =
$res->Fetch())
777 $entityTypeId = (int)($item[
'ENTITY_TYPE_ID'] ?? 0);
779 !static::isDynamicEntityType($entityTypeId)
781 static::checkDynamicTypeByEntityType($entityTypeId)
782 && static::getDynamicTypeCustomSectionIdByEntityTypeId($entityTypeId) <= 0
789 if (static::isCrmModuleIncluded() && $step === 0)
792 $result[
'CONTENT'][
'CRM_DYNAMIC_TYPES_INFO'] = static::exportCrmDynamicTypesInfo();
799 private static function clearCrmTrigger(
$option)
804 $clearFull =
$option[
'CLEAR_FULL'];
806 $res = TriggerTable::getList(
817 while ($item =
$res->Fetch())
821 if (static::getDynamicTypeCustomSectionIdByEntityTypeId((
int)($item[
'ENTITY_TYPE_ID'] ?? 0)) > 0)
826 if (!$clearFull && $item[
'ENTITY_TYPE_ID'] == CCrmOwnerType::Deal)
830 preg_match(static::$customDealMatch, $item[
'ENTITY_STATUS'],
$matches, PREG_OFFSET_CAPTURE);
836 $delete = TriggerTable::delete($item[
'ID']);
837 if ($delete->isSuccess())
840 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_TRIGGER,
841 'ENTITY' => $item[
'ID'],
849 private static function importCrmTrigger($importData)
852 if (!isset($importData[
'CONTENT'][
'DATA']))
856 $dynamicTypesInfo = static::getDynamicTypesInfoForImport($importData,
$result);
857 $item = $importData[
'CONTENT'][
'DATA'];
860 && isset($item[
'CODE'])
861 && isset($item[
'ENTITY_TYPE_ID'])
862 && isset($item[
'ENTITY_STATUS'])
865 if (isset($importData[
'RATIO'][
'CRM_STATUS']))
867 $crmHelper =
new Helper();
869 $isSetRatio = (isset($importData[
'RATIO']) && is_array($importData[
'RATIO']));
870 $oldDynamicEntityTypeId = (int)$item[
'ENTITY_TYPE_ID'];
871 $newDynamicEntityTypeId = static::getDynamicEntityTypeIdByOldEntityTypeId(
872 $oldDynamicEntityTypeId,
875 $isDynamicType = ($newDynamicEntityTypeId > 0);
878 $item[
'ENTITY_TYPE_ID'] = $newDynamicEntityTypeId;
880 !empty($dynamicTypesInfo)
881 && self::isCrmModuleIncluded()
882 && static::checkDynamicTypeByEntityType($newDynamicEntityTypeId)
885 $replacementLists = static::getDynamicTypeReplacementLists(
887 $isSetRatio ? $importData[
'RATIO'] : []
889 if (isset($item[
'APPLY_RULES']) && is_array($item[
'APPLY_RULES']))
891 $item[
'APPLY_RULES'] =
892 static::changeDynamicTypeIdentifiers(
893 $oldDynamicEntityTypeId,
894 $item[
'APPLY_RULES'],
895 $importData[
'RATIO'],
900 if (is_string($item[
'ENTITY_STATUS']) && $item[
'ENTITY_STATUS'] !==
'')
902 $item[
'ENTITY_STATUS'] =
903 static::changeDynamicTypeIdentifiers(
904 $oldDynamicEntityTypeId,
905 $item[
'ENTITY_STATUS'],
906 $importData[
'RATIO'],
915 if (is_array($item[
'APPLY_RULES']))
917 $item[
'APPLY_RULES'] = static::changeDealCategory(
918 $item[
'APPLY_RULES'],
919 $importData[
'RATIO'][
'CRM_STATUS']
922 if ($item[
'ENTITY_TYPE_ID'] == CCrmOwnerType::Deal)
924 $item[
'ENTITY_STATUS'] = static::changeDealCategory(
925 $item[
'ENTITY_STATUS'],
926 $importData[
'RATIO'][
'CRM_STATUS']
933 'NAME' => $item[
'NAME'],
934 'CODE' => $item[
'CODE'],
935 'ENTITY_TYPE_ID' => $item[
'ENTITY_TYPE_ID'],
936 'ENTITY_STATUS' => $item[
'ENTITY_STATUS'],
937 'APPLY_RULES' => is_array($item[
'APPLY_RULES']) ? $item[
'APPLY_RULES'] :
null,
940 $res = TriggerTable::add($saveData);
941 if (
$res->isSuccess())
944 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_TRIGGER,
945 'ENTITY' =>
$res->getId(),
956 private static function exportScript($step, $nextId, $docType)
964 $nextId = (int)$nextId;
968 '=MODULE_ID' => static::$accessModules,
973 $filter[
'=DOCUMENT_TYPE'] = $docType;
976 $res = ScriptTable::getList(
986 if ($tpl =
$res->fetch())
989 $data = Script\Manager::exportScript($tpl[
'ID']);
993 (isset(
$data[
'DOCUMENT_TYPE']) && is_string(
$data[
'DOCUMENT_TYPE']))
994 ?
$data[
'DOCUMENT_TYPE']
998 !static::isDynamicDocumentType($documentType)
1000 static::checkDynamicTypeByDocumentType($documentType)
1001 && static::getDynamicTypeCustomSectionIdByDocumentType($documentType) <= 0
1009 if (static::isCrmModuleIncluded() && $step === 0)
1012 $result[
'CONTENT'][
'CRM_DYNAMIC_TYPES_INFO'] = static::exportCrmDynamicTypesInfo();
1019 private static function clearScript(
$option)
1023 'OWNER_DELETE' => [],
1031 $res = ScriptTable::getList(
1037 '>ID' => (
int)
$option[
'NEXT'],
1038 '=MODULE_ID' => static::$accessModules,
1045 while ($item =
$res->Fetch())
1047 $result[
'NEXT'] = $item[
'ID'];
1051 isset($item[
'DOCUMENT_TYPE'])
1052 && is_string($item[
'DOCUMENT_TYPE'])
1053 && $item[
'DOCUMENT_TYPE'] !==
''
1055 ? $item[
'DOCUMENT_TYPE']
1058 if (static::getDynamicTypeCustomSectionIdByDocumentType($documentType) > 0)
1063 $deletionResult = Script\Manager::deleteScript($item[
'ID']);
1065 if ($deletionResult->isSuccess())
1068 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC_SCRIPT,
1069 'ENTITY' => $item[
'ID'],
1077 private static function importScript($importData,
int $userId,
int $appId)
1081 if (!isset($importData[
'CONTENT'][
'DATA']))
1085 $dynamicTypesInfo = static::getDynamicTypesInfoForImport($importData,
$result);
1086 $item = $importData[
'CONTENT'][
'DATA'];
1088 in_array($item[
'MODULE_ID'], static::$accessModules)
1089 && Loader::includeModule($item[
'MODULE_ID'])
1090 && class_exists($item[
'ENTITY'])
1093 if (is_subclass_of($item[
'ENTITY'],
'\\IBPWorkflowDocument'))
1096 isset($importData[
'RATIO'][
'CRM_STATUS'])
1097 && isset($item[
'WORKFLOW_TEMPLATE'])
1098 && is_array($item[
'WORKFLOW_TEMPLATE'])
1101 $oldDynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($item[
'DOCUMENT_TYPE']);
1102 $isDynamicType = ($oldDynamicEntityTypeId > 0);
1105 $newDynamicEntityTypeId = static::getDynamicEntityTypeIdByOldEntityTypeId(
1106 $oldDynamicEntityTypeId,
1110 !empty($dynamicTypesInfo)
1111 && self::isCrmModuleIncluded()
1112 && static::checkDynamicTypeByEntityType($newDynamicEntityTypeId)
1115 $isSetRatio = (isset($importData[
'RATIO']) && is_array($importData[
'RATIO']));
1116 $replacementLists = static::getDynamicTypeReplacementLists(
1118 $isSetRatio ? $importData[
'RATIO'] : []
1120 $item[
'DOCUMENT_TYPE'] =
1121 static::changeDynamicTypeIdentifiers(
1122 $oldDynamicEntityTypeId,
1123 $item[
'DOCUMENT_TYPE'],
1124 $importData[
'RATIO'],
1128 $item[
'WORKFLOW_TEMPLATE'] =
1129 static::changeDynamicTypeIdentifiers(
1130 $oldDynamicEntityTypeId,
1131 $item[
'WORKFLOW_TEMPLATE'],
1132 $importData[
'RATIO'],
1138 elseif ($item[
'DOCUMENT_TYPE'] ===
'DEAL')
1140 $item[
'WORKFLOW_TEMPLATE'] = static::changeDealCategory(
1141 $item[
'WORKFLOW_TEMPLATE'],
1142 $importData[
'RATIO'][
'CRM_STATUS']
1149 $item[
'ORIGINATOR_ID'] =
'REST_APP';
1150 $item[
'ORIGIN_ID'] = $appId;
1153 $importResult = Script\Manager::importScript($item,
$userId);
1155 if ($importResult->isSuccess())
1158 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC_SCRIPT,
1159 'ENTITY' => $importResult->getId(),
1164 $result[
'ERROR_ACTION'] =
$result[
'ERROR_MESSAGES'] = current($importResult->getErrorMessages());
1173 private static function changeDynamicTypeIdentifiers(
1174 int $oldDynamicEntityTypeId,
1177 array $replacementLists
1180 if (!static::isCrmModuleIncluded())
1187 && isset($replacementLists[
'from'])
1188 && is_array($replacementLists[
'from'])
1189 && !empty($replacementLists[
'from'])
1190 && isset($replacementLists[
'to'])
1191 && is_array($replacementLists[
'to'])
1192 && !empty($replacementLists[
'to'])
1195 $replaceMarkers = [];
1196 for (
$i = 0;
$i <
count($replacementLists[
'from']);
$i++)
1198 $replaceMarkers[] =
"_{<-rm[$i]->}_";
1200 $data = str_replace($replacementLists[
'from'], $replaceMarkers,
$data);
1201 $data = str_replace($replaceMarkers, $replacementLists[
'to'],
$data);
1205 $crmHelper =
new Helper();
1208 isset(
$data[
'field'])
1209 &&
$data[
'field'] ===
'CATEGORY_ID'
1210 &&
$data[
'value'] > 0
1213 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1214 $oldDynamicEntityTypeId,
1215 (
int)
$data[
'value'],
1218 if ($newCategoryId > 0)
1220 $data[
'value'] = $newCategoryId;
1225 isset(
$data[
'DynamicTypeId'])
1226 &&
$data[
'DynamicTypeId'] > 0
1229 $oldDynamicEntityTypeId = (int)
$data[
'DynamicTypeId'];
1230 $newEntityTypeIdRatioKey = CCrmOwnerType::DynamicTypePrefixName . $oldDynamicEntityTypeId;
1231 if (isset($ratio[
'CRM_DYNAMIC_TYPES'][$newEntityTypeIdRatioKey]))
1233 $newDynamicEntityTypeId = (int)$ratio[
'CRM_DYNAMIC_TYPES'][$newEntityTypeIdRatioKey];
1234 $data[
'DynamicTypeId'] = $newDynamicEntityTypeId;
1235 if (isset(
$data[
'DynamicEntitiesFields']) && is_array(
$data[
'DynamicEntitiesFields']))
1237 foreach (
$data[
'DynamicEntitiesFields'] as $oldFieldKey => $value)
1239 $oldFieldPrefix =
"{$oldDynamicEntityTypeId}_";
1240 $oldFieldPrefixLength = strlen($oldFieldPrefix);
1241 $fieldName = substr($oldFieldKey, $oldFieldPrefixLength);
1242 if ($oldFieldPrefix === substr($oldFieldKey, 0, $oldFieldPrefixLength))
1244 $newFieldKey =
"{$newDynamicEntityTypeId}_$fieldName";
1245 if ($fieldName ===
'CATEGORY_ID' && $value > 0)
1247 $oldCategoryId = (int)$value;
1248 $newCategoryRatioKey =
"DT{$oldDynamicEntityTypeId}_$oldCategoryId";
1250 isset($ratio[
'CRM_STATUS'][$newCategoryRatioKey])
1251 && $ratio[
'CRM_STATUS'][$newCategoryRatioKey] > 0
1254 $value = (int)$ratio[
'CRM_STATUS'][$newCategoryRatioKey];
1257 $data[
'DynamicEntitiesFields'][$newFieldKey] =
$value;
1258 unset(
$data[
'DynamicEntitiesFields'][$oldFieldKey]);
1267 $newKey = static::changeDynamicTypeIdentifiers(
1268 $oldDynamicEntityTypeId,
1273 if ($newKey !==
$key)
1278 if ($newKey ===
'CATEGORY_ID')
1280 if (is_array($value))
1282 if (isset($value[
'Options']) && is_array($value[
'Options']))
1284 $data[$newKey][
'Options'] = [];
1285 foreach ($value[
'Options'] as $categoryId =>
$title)
1287 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1288 $oldDynamicEntityTypeId,
1292 if ($newCategoryId > 0)
1294 $data[$newKey][
'Options'][$newCategoryId] =
$title;
1300 $data[$newKey] = static::changeDynamicTypeIdentifiers(
1301 $oldDynamicEntityTypeId,
1308 elseif (is_string($value))
1310 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1311 $oldDynamicEntityTypeId,
1315 if ($newCategoryId > 0)
1317 $data[$newKey] = $newCategoryId;
1321 $data[$newKey] = static::changeDynamicTypeIdentifiers(
1322 $oldDynamicEntityTypeId,
1335 ($newKey ===
'CategoryId' || $newKey ===
'category_id')
1339 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1340 $oldDynamicEntityTypeId,
1344 if ($newCategoryId > 0)
1346 $data[$newKey] = $newCategoryId;
1351 if (is_string($value) || is_array($value))
1353 $data[$newKey] = static::changeDynamicTypeIdentifiers(
1354 $oldDynamicEntityTypeId,
1371 private static function changeDealCategory(
$data, $ratio)
1377 foreach ($ratio as $oldId => $newId)
1379 $ratioRegEx[] =
'/^C' . $oldId .
':/';
1380 $ratioReplace[] =
'C' . $newId .
':';
1382 if (!empty($ratioRegEx))
1384 $data = static::changeDealCategoryAction(
$data, $ratioRegEx, $ratioReplace, $ratio);
1391 private static function changeDealCategoryAction(
$data, $ratioRegEx, $ratioReplace, $ratio)
1393 if (is_string(
$data))
1395 $data = preg_replace($ratioRegEx, $ratioReplace,
$data);
1400 isset(
$data[
'field'])
1401 &&
$data[
'field'] ==
'CATEGORY_ID'
1402 &&
$data[
'value'] > 0
1403 && $ratio[
$data[
'value']] > 0
1411 $newKey = static::changeDealCategoryAction(
$key, $ratioRegEx, $ratioReplace, $ratio);
1412 if ($newKey !=
$key)
1417 if ($newKey ==
'CATEGORY_ID')
1419 if (is_array($value))
1421 if (isset($value[
'Options']) && is_array($value[
'Options']))
1423 $data[$newKey][
'Options'] = [];
1424 foreach ($value[
'Options'] as $dealId =>
$title)
1426 if (isset($ratio[$dealId]))
1428 $data[$newKey][
'Options'][$ratio[$dealId]] =
$title;
1434 $data[$newKey] = static::changeDealCategoryAction(
1442 elseif (is_string($value) && isset($ratio[$value]))
1448 $data[$newKey] = static::changeDealCategoryAction(
1456 elseif ($newKey ==
'CategoryId' && intVal($value) > 0 && !empty($ratio[$value]))
1462 $data[$newKey] = static::changeDealCategoryAction(