1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
appconfiguration.php
См. документацию.
1<?php
2
4
8use Bitrix\Crm\Automation\Trigger\Entity\TriggerTable;
9use Bitrix\Crm\Integration\Rest\Configuration\Helper;
16use CBPDocument;
17use CCrmOwnerType;
18use Exception;
19
21{
22 const ENTITY_BIZPROC_MAIN = 'BIZPROC_MAIN';
23 const ENTITY_BIZPROC_CRM_TRIGGER = 'BIZPROC_CRM_TRIGGER';
24 const ENTITY_BIZPROC_SCRIPT = 'BIZPROC_SCRIPT';
25
26 const OWNER_ENTITY_TYPE_BIZPROC = 'BIZPROC';
27 const OWNER_ENTITY_TYPE_TRIGGER = 'TRIGGER';
28 const OWNER_ENTITY_TYPE_BIZPROC_SCRIPT = 'BIZPROC_SCRIPT';
29
30 private static $entityList = [
31 self::ENTITY_BIZPROC_MAIN => 500,
32 self::ENTITY_BIZPROC_CRM_TRIGGER => 600,
33 self::ENTITY_BIZPROC_SCRIPT => 700,
34 ];
35 private static $customDealMatch = '/^C([0-9]+):/';
36 private static $accessModules = ['crm'];
37 private static $context = '';
38 private static $accessManifest = [
39 'total',
40 'bizproc_crm',
41 ];
42
43 public static function getEntityList()
44 {
45 return static::$entityList;
46 }
47
48 public static function onEventExportController(Event $event)
49 {
50 $result = null;
51 $code = $event->getParameter('CODE');
52 $itemCode = $event->getParameter('ITEM_CODE');
53 if (!static::$entityList[$code])
54 {
55 return $result;
56 }
57
58 $option = $event->getParameters();
59 if (
60 $code !== self::ENTITY_BIZPROC_SCRIPT
61 && !Manifest::isEntityAvailable('', $option, static::$accessManifest)
62 )
63 {
64 return $result;
65 }
66
67 if (
68 $code === self::ENTITY_BIZPROC_SCRIPT
69 && !Manifest::isEntityAvailable('', $option, ['bizproc_script'])
70 )
71 {
72 return $result;
73 }
74
75 try
76 {
77 if (static::checkRequiredParams($code))
78 {
79 $step = $event->getParameter('STEP');
80 switch ($code)
81 {
82 case self::ENTITY_BIZPROC_MAIN:
83 $result = static::exportBizproc($step);
84 break;
85 case self::ENTITY_BIZPROC_CRM_TRIGGER:
86 $result = static::exportCrmTrigger($step);
87 break;
88 case self::ENTITY_BIZPROC_SCRIPT:
89 $result = static::exportScript($step, $event->getParameter('NEXT'), $itemCode);
90 break;
91 }
92 }
93 }
94 catch (Exception $e)
95 {
96 $result['NEXT'] = false;
97 $result['ERROR_ACTION'] = $e->getMessage();
98 $result['ERROR_MESSAGES'] = Loc::getMessage(
99 'BIZPROC_ERROR_CONFIGURATION_EXPORT_EXCEPTION',
100 [
101 '#CODE#' => $code,
102 ]
103 );
104 }
105
106 return $result;
107 }
108
109 public static function onEventClearController(Event $event)
110 {
111 $code = $event->getParameter('CODE');
112 if (!static::$entityList[$code])
113 {
114 return null;
115 }
116 $option = $event->getParameters();
117
118 if (
119 $code !== self::ENTITY_BIZPROC_SCRIPT
120 && !Manifest::isEntityAvailable('', $option, static::$accessManifest)
121 )
122 {
123 return null;
124 }
125
126 if (
127 $code === self::ENTITY_BIZPROC_SCRIPT
128 && !Manifest::isEntityAvailable('', $option, ['bizproc_script'])
129 )
130 {
131 return null;
132 }
133
134 $result = null;
135
136 try
137 {
138 if (static::checkRequiredParams($code))
139 {
140 switch ($code)
141 {
142 case self::ENTITY_BIZPROC_MAIN:
143 $result = static::clearBizproc($option);
144 break;
145 case self::ENTITY_BIZPROC_CRM_TRIGGER:
146 $result = static::clearCrmTrigger($option);
147 break;
148 case self::ENTITY_BIZPROC_SCRIPT:
149 $result = static::clearScript($option);
150 break;
151 }
152 }
153 }
154 catch (Exception $e)
155 {
156 $result['NEXT'] = false;
157 $result['ERROR_ACTION'] = $e->getMessage();
158 $result['ERROR_MESSAGES'] = Loc::getMessage(
159 'BIZPROC_ERROR_CONFIGURATION_CLEAR_EXCEPTION',
160 [
161 '#CODE#' => $code,
162 ]
163 );
164 }
165
166 return $result;
167 }
168
169 public static function onEventImportController(Event $event)
170 {
171 $code = $event->getParameter('CODE');
172 if (!static::$entityList[$code])
173 {
174 return null;
175 }
176 $data = $event->getParameters();
177
178 if (
179 $code !== self::ENTITY_BIZPROC_SCRIPT
180 && !Manifest::isEntityAvailable('', $data, static::$accessManifest)
181 )
182 {
183 return null;
184 }
185
186 if (
187 $code === self::ENTITY_BIZPROC_SCRIPT
188 && !Manifest::isEntityAvailable('', $data, ['bizproc_script'])
189 )
190 {
191 return null;
192 }
193
194 $result = null;
195 $userId = (int)$event->getParameter('USER_ID');
196
197 $contextUser = $event->getParameter('CONTEXT_USER');
198 $setting = new Setting($contextUser);
199 $app = $setting->get(Setting::SETTING_APP_INFO);
200 $appId = (int)$app['ID'];
201
202 try
203 {
204 if (static::checkRequiredParams($code))
205 {
206 switch ($code)
207 {
208 case self::ENTITY_BIZPROC_MAIN:
209 $result = static::importBizproc($data);
210 break;
211 case self::ENTITY_BIZPROC_CRM_TRIGGER:
212 $result = static::importCrmTrigger($data);
213 break;
214 case self::ENTITY_BIZPROC_SCRIPT:
215 $result = static::importScript($data, $userId, $appId);
216 break;
217 }
218 }
219 }
220 catch (Exception $e)
221 {
222 $result['NEXT'] = false;
223 $result['ERROR_ACTION'] = $e->getMessage();
224 $result['ERROR_MESSAGES'] = Loc::getMessage(
225 'BIZPROC_ERROR_CONFIGURATION_IMPORT_EXCEPTION',
226 [
227 '#CODE#' => $code,
228 ]
229 );
230 }
231
232 return $result;
233 }
234
235 private static function isCrmModuleIncluded(): bool
236 {
237 return Loader::includeModule('crm');
238 }
239
245 private static function checkRequiredParams($type)
246 {
247 $return = true;
248 if ($type == self::ENTITY_BIZPROC_CRM_TRIGGER)
249 {
250 if (!static::isCrmModuleIncluded())
251 {
252 throw new SystemException('need install module: crm');
253 }
254 }
255
256 return $return;
257 }
258
259 private static function exportCrmDynamicTypesInfo(): array
260 {
261 $result = [];
262
263 if (!static::isCrmModuleIncluded())
264 {
265 return $result;
266 }
267
268 $helper = new Helper();
269 $result = $helper->exportCrmDynamicTypesInfo();
270
271 return $result;
272 }
273
274 private static function getDynamicTypeCustomSectionIdByEntityTypeId(int $entityTypeId): int
275 {
276 $result = 0;
277
278 if (!static::isCrmModuleIncluded())
279 {
280 return $result;
281 }
282
283 $helper = new Helper();
284 $result = $helper->getDynamicTypeCustomSectionIdByEntityTypeId($entityTypeId);
285
286 return $result;
287 }
288
289 private static function isDynamicDocumentType(string $documentType): bool
290 {
291 if (static::getDynamicEntityTypeIdByDocumentType($documentType) > 0)
292 {
293 return true;
294 }
295
296 return false;
297 }
298
299 private static function isDynamicEntityType(int $entityTypeId): bool
300 {
301 if (static::isCrmModuleIncluded() && CCrmOwnerType::isPossibleDynamicTypeId($entityTypeId))
302 {
303 return true;
304 }
305
306 return false;
307 }
308
309 private static function getDynamicTypeCustomSectionIdByDocumentType(string $documentType): int
310 {
311 $dynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($documentType);
312
313 return static::getDynamicTypeCustomSectionIdByEntityTypeId($dynamicEntityTypeId);
314 }
315
316 private static function checkDynamicTypeByEntityType(int $entityTypeId): bool
317 {
318 if (static::isDynamicEntityType($entityTypeId))
319 {
320 $helper = new Helper();
321
322 return $helper->checkDynamicTypeByEntityType($entityTypeId);
323 }
324
325 return false;
326 }
327
328 private static function checkDynamicTypeByDocumentType(string $documentType): bool
329 {
330 $dynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($documentType);
331
332 return static::checkDynamicTypeByEntityType($dynamicEntityTypeId);
333 }
334
335 //region bizproc
336 private static function exportBizproc($step)
337 {
338 $result = [
339 'FILE_NAME' => '',
340 'CONTENT' => [],
341 'NEXT' => false,
342 ];
343 $res = WorkflowTemplateTable::getList(
344 [
345 'order' => [
346 'ID' => 'ASC',
347 ],
348 'filter' => [
349 '=MODULE_ID' => static::$accessModules,
350 '<AUTO_EXECUTE' => \CBPDocumentEventType::Script,
351 ],
352 'limit' => 1,
353 'offset' => $step,
354 ]
355 );
356 if ($tpl = $res->fetchObject())
357 {
358 $result['NEXT'] = $step;
359 if (in_array($tpl->getModuleId(), static::$accessModules))
360 {
361 $documentType = $tpl->getDocumentType();
362 if (
363 !static::isDynamicDocumentType($documentType)
364 || (
365 static::checkDynamicTypeByDocumentType($documentType)
366 && static::getDynamicTypeCustomSectionIdByDocumentType($documentType) <= 0
367 )
368 )
369 {
370 $result['FILE_NAME'] = $step;
371 $packer = new \Bitrix\Bizproc\Workflow\Template\Packer\Bpt();
372 $data = $packer->makePackageData($tpl);
373 $result['CONTENT'] = [
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,
386 ];
387 }
388 if (static::isCrmModuleIncluded() && $step === 0)
389 {
390 $result['FILE_NAME'] = $step;
391 $result['CONTENT']['CRM_DYNAMIC_TYPES_INFO'] = static::exportCrmDynamicTypesInfo();
392 }
393 }
394 }
395
396 return $result;
397 }
398
399 private static function clearBizproc($option)
400 {
401 $result = [
402 'NEXT' => false,
403 'OWNER_DELETE' => [],
404 ];
405 $clearFull = $option['CLEAR_FULL'];
406 $prefix = $option['PREFIX_NAME'];
407 $pattern = '/^\‍(' . $prefix . '\‍)/';
408
409 $res = WorkflowTemplateTable::getList(
410 [
411 'order' => [
412 'ID' => 'ASC',
413 ],
414 'filter' => [
415 '=MODULE_ID' => static::$accessModules,
416 '>ID' => $option['NEXT'],
417 '<AUTO_EXECUTE' => \CBPDocumentEventType::Script,
418 ],
419 'select' => ['*'],
420 ]
421 );
422 $errorsTmp = [];
423 while ($item = $res->Fetch())
424 {
425 $result['NEXT'] = $item['ID'];
426
427 $documentType =
428 (
429 isset($item['DOCUMENT_TYPE'])
430 && is_string($item['DOCUMENT_TYPE'])
431 && $item['DOCUMENT_TYPE'] !== ''
432 )
433 ? $item['DOCUMENT_TYPE']
434 : ''
435 ;
436 if (static::getDynamicTypeCustomSectionIdByDocumentType($documentType) > 0)
437 {
438 continue;
439 }
440
441 if (!$clearFull && $item['DOCUMENT_TYPE'] == 'DEAL')
442 {
443 //dont off old custom deal robot
444 $matches = [];
445 preg_match(static::$customDealMatch, $item['DOCUMENT_STATUS'], $matches, PREG_OFFSET_CAPTURE);
446 if (!empty($matches))
447 {
448 continue;
449 }
450 }
451
452 if ($clearFull || !empty($item['DOCUMENT_STATUS']))
453 {
454 CBPDocument::DeleteWorkflowTemplate(
455 $item['ID'],
456 [
457 $item['MODULE_ID'],
458 $item['ENTITY'],
459 $item['DOCUMENT_TYPE'],
460 ],
461 $errorsTmp
462 );
463 if (!$errorsTmp)
464 {
465 $result['OWNER_DELETE'][] = [
466 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC,
467 'ENTITY' => $item['ID'],
468 ];
469 }
470 }
471 else
472 {
473 $name = $item['NAME'];
474 if ($prefix != '' && preg_match($pattern, $name) === 0)
475 {
476 $name = "($prefix) " . $name;
477 }
478 CBPDocument::UpdateWorkflowTemplate(
479 $item['ID'],
480 [
481 $item['MODULE_ID'],
482 $item['ENTITY'],
483 $item['DOCUMENT_TYPE'],
484 ],
485 [
486 'ACTIVE' => 'N',
487 'AUTO_EXECUTE' => \CBPDocumentEventType::None,
488 'NAME' => $name,
489 ],
490 $errorsTmp
491 );
492 }
493 }
494
495 return $result;
496 }
497
498 private static function getDynamicTypeReplacementLists(
499 array $dynamicTypesInfo,
500 array $ratioInfo,
501 bool $refresh = false
502 ): array
503 {
504 static $replacementLists = null;
505
506 if ($replacementLists === null || $refresh)
507 {
508 if (static::isCrmModuleIncluded())
509 {
510 $crmHelper = new Helper();
511 $replacementLists = $crmHelper->prepareDynamicTypeReplacementLists($dynamicTypesInfo, $ratioInfo);
512 }
513 }
514
515 return $replacementLists;
516 }
517
518 private static function getDynamicTypesInfoForImport(array $importData, array &$importResult): array
519 {
520 $result = [];
521
522 $codes = [
523 self::ENTITY_BIZPROC_MAIN,
524 self::ENTITY_BIZPROC_CRM_TRIGGER,
525 self::ENTITY_BIZPROC_SCRIPT,
526 ];
527
528 $actualCode = null;
529 foreach ($codes as $code)
530 {
531 if (
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'])
535 )
536 {
537 $actualCode = $code;
538 break;
539 }
540 }
541
542 $code = $actualCode ?? $importData['CODE'];
543
544 if (
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'])
548 && !$actualCode
549 )
550 {
551 $result = $importData['CONTENT']['DATA']['CRM_DYNAMIC_TYPES_INFO'];
552
553 // refresh info
554 $importResult['RATIO']['CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'] = $result;
555 }
556 elseif (
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'])
560 )
561 {
562 // get actual info
563 $result = $importData['RATIO'][$code]['CRM_DYNAMIC_TYPES_INFO_FOR_IMPORT'];
564 }
565
566 return $result;
567 }
568
569 private static function getDynamicEntityTypeIdByDocumentType(string $dynamicEntityTypeName): int
570 {
571 $entityTypeId = 0;
572 if (static::isCrmModuleIncluded())
573 {
574 $dynamicTypePrefix = CCrmOwnerType::DynamicTypePrefixName;
575 $dynamicTypeRegExp = "/$dynamicTypePrefix(\\d+)/u";
576 $matches = [];
577
578 if (preg_match($dynamicTypeRegExp, $dynamicEntityTypeName, $matches))
579 {
580 $entityTypeId = (int)$matches[1];
581 }
582 }
583
584 return $entityTypeId;
585 }
586
587 private static function getDynamicEntityTypeIdByOldEntityTypeId(int $oldDynamicEntityTypeId, $importData): int
588 {
589 $newDynamicEntityTypeId = 0;
590
591 if (static::isCrmModuleIncluded())
592 {
593 $isSetRatio = (isset($importData['RATIO']) && is_array($importData['RATIO']));
594 $newDynamicEntityTypeId = (new Helper())->getDynamicEntityTypeIdByOldEntityTypeId(
595 $oldDynamicEntityTypeId,
596 $isSetRatio ? $importData['RATIO'] : []
597 );
598 }
599
600 return $newDynamicEntityTypeId;
601 }
602
603 private static function importBizproc($importData)
604 {
605 $result = [];
606
607 if (!isset($importData['CONTENT']['DATA']))
608 {
609 return false;
610 }
611 $dynamicTypesInfo = static::getDynamicTypesInfoForImport($importData, $result);
612 $item = $importData['CONTENT']['DATA'];
613 if (
614 in_array($item['MODULE_ID'], static::$accessModules)
615 && Loader::includeModule($item['MODULE_ID'])
616 && class_exists($item['ENTITY'])
617 )
618 {
619 if (is_subclass_of($item['ENTITY'], '\\IBPWorkflowDocument'))
620 {
621 $oldDynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($item['DOCUMENT_TYPE']);
622 $isDynamicType = ($oldDynamicEntityTypeId > 0);
623 if ($isDynamicType)
624 {
625 $newDynamicEntityTypeId = static::getDynamicEntityTypeIdByOldEntityTypeId(
626 $oldDynamicEntityTypeId,
627 $importData
628 );
629
630 if (
631 !empty($dynamicTypesInfo)
632 && self::isCrmModuleIncluded()
633 && static::checkDynamicTypeByEntityType($newDynamicEntityTypeId)
634 )
635 {
636 $isSetRatio = (isset($importData['RATIO']) && is_array($importData['RATIO']));
637 $replacementLists = static::getDynamicTypeReplacementLists(
638 $dynamicTypesInfo,
639 $isSetRatio ? $importData['RATIO'] : []
640 );
641 $item['DOCUMENT_TYPE'] =
642 static::changeDynamicTypeIdentifiers(
643 $oldDynamicEntityTypeId,
644 $item['DOCUMENT_TYPE'],
645 $importData['RATIO'],
646 $replacementLists
647 )
648 ;
649 if (is_string($item['DOCUMENT_STATUS']) && $item['DOCUMENT_STATUS'] !== '')
650 {
651 $item['DOCUMENT_STATUS'] =
652 static::changeDynamicTypeIdentifiers(
653 $oldDynamicEntityTypeId,
654 $item['DOCUMENT_STATUS'],
655 $importData['RATIO'],
656 $replacementLists
657 )
658 ;
659 }
660 if (is_string($item['NAME']) && $item['NAME'] !== '')
661 {
662 $item['NAME'] =
663 static::changeDynamicTypeIdentifiers(
664 $oldDynamicEntityTypeId,
665 $item['NAME'],
666 $importData['RATIO'],
667 $replacementLists
668 )
669 ;
670 }
671 if(is_array($item['TEMPLATE_DATA']))
672 {
673 $item['TEMPLATE_DATA'] =
674 static::changeDynamicTypeIdentifiers(
675 $oldDynamicEntityTypeId,
676 $item['TEMPLATE_DATA'],
677 $importData['RATIO'],
678 $replacementLists
679 )
680 ;
681 }
682 }
683 }
684 else
685 {
686 if (isset($importData['RATIO']['CRM_STATUS']))
687 {
688 if (is_array($item['TEMPLATE_DATA']))
689 {
690 $item['TEMPLATE_DATA'] = static::changeDealCategory(
691 $item['TEMPLATE_DATA'],
692 $importData['RATIO']['CRM_STATUS']
693 );
694 }
695 if ($item['DOCUMENT_TYPE'] == 'DEAL' && $item['DOCUMENT_STATUS'])
696 {
697 $item['DOCUMENT_STATUS'] = static::changeDealCategory(
698 $item['DOCUMENT_STATUS'],
699 $importData['RATIO']['CRM_STATUS']
700 );
701 }
702 }
703 }
704
705 try
706 {
707 $code = static::$context . '_xml_' . intval($item['ID']);
708 $id = \CBPWorkflowTemplateLoader::importTemplateFromArray(
709 0,
710 [
711 $item['MODULE_ID'],
712 $item['ENTITY'],
713 $item['DOCUMENT_TYPE'],
714 ],
715 $item['AUTO_EXECUTE'],
716 $item['NAME'],
717 isset($item['DESCRIPTION']) ? (string)$item['DESCRIPTION'] : '',
718 $item['TEMPLATE_DATA'],
719 $code
720 );
721
722 if ($id > 0)
723 {
724 $result['OWNER'] = [
725 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC,
726 'ENTITY' => $id,
727 ];
728
729 if ($item['DOCUMENT_STATUS'])
730 {
731 \CBPWorkflowTemplateLoader::update(
732 $id,
733 [
734 'DOCUMENT_STATUS' => $item['DOCUMENT_STATUS'],
735 ]
736 );
737 }
738 }
739 }
740 catch (Exception $e)
741 {
742 $result['ERROR_ACTION'] = $e->getMessage();
743 $result['ERROR_MESSAGES'] = Loc::getMessage(
744 'BIZPROC_ERROR_CONFIGURATION_IMPORT_EXCEPTION_BP'
745 );
746 }
747 }
748 }
749
750 return $result;
751 }
752 //end region bizproc
753
754 //region trigger
755 private static function exportCrmTrigger($step)
756 {
757 $result = [
758 'FILE_NAME' => '',
759 'CONTENT' => [],
760 'NEXT' => false,
761 ];
762
763 $res = TriggerTable::getList(
764 [
765 'order' => [
766 'ID' => 'ASC',
767 ],
768 'filter' => [],
769 'select' => ['*'],
770 'limit' => 1,
771 'offset' => $step,
772 ]
773 );
774 if ($item = $res->Fetch())
775 {
776 $result['NEXT'] = $step;
777 $entityTypeId = (int)($item['ENTITY_TYPE_ID'] ?? 0);
778 if (
779 !static::isDynamicEntityType($entityTypeId)
780 || (
781 static::checkDynamicTypeByEntityType($entityTypeId)
782 && static::getDynamicTypeCustomSectionIdByEntityTypeId($entityTypeId) <= 0
783 )
784 )
785 {
786 $result['FILE_NAME'] = $step;
787 $result['CONTENT'] = $item;
788 }
789 if (static::isCrmModuleIncluded() && $step === 0)
790 {
791 $result['FILE_NAME'] = $step;
792 $result['CONTENT']['CRM_DYNAMIC_TYPES_INFO'] = static::exportCrmDynamicTypesInfo();
793 }
794 }
795
796 return $result;
797 }
798
799 private static function clearCrmTrigger($option)
800 {
801 $result = [
802 'NEXT' => false,
803 ];
804 $clearFull = $option['CLEAR_FULL'];
805
806 $res = TriggerTable::getList(
807 [
808 'order' => [
809 'ID' => 'ASC',
810 ],
811 'filter' => [
812 '>ID' => $option['NEXT'],
813 ],
814 'limit' => 10,
815 ]
816 );
817 while ($item = $res->Fetch())
818 {
819 $result['NEXT'] = $item['ID'];
820
821 if (static::getDynamicTypeCustomSectionIdByEntityTypeId((int)($item['ENTITY_TYPE_ID'] ?? 0)) > 0)
822 {
823 continue;
824 }
825
826 if (!$clearFull && $item['ENTITY_TYPE_ID'] == CCrmOwnerType::Deal)
827 {
828 //dont off old custom deal trigger
829 $matches = [];
830 preg_match(static::$customDealMatch, $item['ENTITY_STATUS'], $matches, PREG_OFFSET_CAPTURE);
831 if (!empty($matches))
832 {
833 continue;
834 }
835 }
836 $delete = TriggerTable::delete($item['ID']);
837 if ($delete->isSuccess())
838 {
839 $result['OWNER_DELETE'][] = [
840 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_TRIGGER,
841 'ENTITY' => $item['ID'],
842 ];
843 }
844 }
845
846 return $result;
847 }
848
849 private static function importCrmTrigger($importData)
850 {
851 $result = [];
852 if (!isset($importData['CONTENT']['DATA']))
853 {
854 return false;
855 }
856 $dynamicTypesInfo = static::getDynamicTypesInfoForImport($importData, $result);
857 $item = $importData['CONTENT']['DATA'];
858 if (
859 isset($item['NAME'])
860 && isset($item['CODE'])
861 && isset($item['ENTITY_TYPE_ID'])
862 && isset($item['ENTITY_STATUS'])
863 )
864 {
865 if (isset($importData['RATIO']['CRM_STATUS']))
866 {
867 $crmHelper = new Helper();
868
869 $isSetRatio = (isset($importData['RATIO']) && is_array($importData['RATIO']));
870 $oldDynamicEntityTypeId = (int)$item['ENTITY_TYPE_ID'];
871 $newDynamicEntityTypeId = static::getDynamicEntityTypeIdByOldEntityTypeId(
872 $oldDynamicEntityTypeId,
873 $importData
874 );
875 $isDynamicType = ($newDynamicEntityTypeId > 0);
876 if ($isDynamicType)
877 {
878 $item['ENTITY_TYPE_ID'] = $newDynamicEntityTypeId;
879 if (
880 !empty($dynamicTypesInfo)
881 && self::isCrmModuleIncluded()
882 && static::checkDynamicTypeByEntityType($newDynamicEntityTypeId)
883 )
884 {
885 $replacementLists = static::getDynamicTypeReplacementLists(
886 $dynamicTypesInfo,
887 $isSetRatio ? $importData['RATIO'] : []
888 );
889 if (isset($item['APPLY_RULES']) && is_array($item['APPLY_RULES']))
890 {
891 $item['APPLY_RULES'] =
892 static::changeDynamicTypeIdentifiers(
893 $oldDynamicEntityTypeId,
894 $item['APPLY_RULES'],
895 $importData['RATIO'],
896 $replacementLists
897 )
898 ;
899 }
900 if (is_string($item['ENTITY_STATUS']) && $item['ENTITY_STATUS'] !== '')
901 {
902 $item['ENTITY_STATUS'] =
903 static::changeDynamicTypeIdentifiers(
904 $oldDynamicEntityTypeId,
905 $item['ENTITY_STATUS'],
906 $importData['RATIO'],
907 $replacementLists
908 )
909 ;
910 }
911 }
912 }
913 else
914 {
915 if (is_array($item['APPLY_RULES']))
916 {
917 $item['APPLY_RULES'] = static::changeDealCategory(
918 $item['APPLY_RULES'],
919 $importData['RATIO']['CRM_STATUS']
920 );
921 }
922 if ($item['ENTITY_TYPE_ID'] == CCrmOwnerType::Deal)
923 {
924 $item['ENTITY_STATUS'] = static::changeDealCategory(
925 $item['ENTITY_STATUS'],
926 $importData['RATIO']['CRM_STATUS']
927 );
928 }
929 }
930 }
931
932 $saveData = [
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,
938 ];
939
940 $res = TriggerTable::add($saveData);
941 if ($res->isSuccess())
942 {
943 $result['OWNER'] = [
944 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_TRIGGER,
945 'ENTITY' => $res->getId(),
946 ];
947 }
948 }
949
950 return $result;
951 }
952
953 //end region trigger
954
955 //region script
956 private static function exportScript($step, $nextId, $docType)
957 {
958 $result = [
959 'FILE_NAME' => '',
960 'CONTENT' => [],
961 'NEXT' => false,
962 ];
963
964 $nextId = (int)$nextId;
965
966 $filter = [
967 '>ID' => $nextId,
968 '=MODULE_ID' => static::$accessModules,
969 ];
970
971 if ($docType)
972 {
973 $filter['=DOCUMENT_TYPE'] = $docType;
974 }
975
976 $res = ScriptTable::getList(
977 [
978 'order' => [
979 'ID' => 'ASC',
980 ],
981 'filter' => $filter,
982 'limit' => 1,
983 'select' => ['ID'],
984 ]
985 );
986 if ($tpl = $res->fetch())
987 {
988 $result['NEXT'] = $step;
989 $data = Script\Manager::exportScript($tpl['ID']);
990 if ($data)
991 {
992 $documentType =
993 (isset($data['DOCUMENT_TYPE']) && is_string($data['DOCUMENT_TYPE']))
994 ? $data['DOCUMENT_TYPE']
995 : ''
996 ;
997 if (
998 !static::isDynamicDocumentType($documentType)
999 || (
1000 static::checkDynamicTypeByDocumentType($documentType)
1001 && static::getDynamicTypeCustomSectionIdByDocumentType($documentType) <= 0
1002 )
1003 )
1004 {
1005 $result['FILE_NAME'] = $step;
1006 $result['CONTENT'] = $data;
1007 }
1008 }
1009 if (static::isCrmModuleIncluded() && $step === 0)
1010 {
1011 $result['FILE_NAME'] = $step;
1012 $result['CONTENT']['CRM_DYNAMIC_TYPES_INFO'] = static::exportCrmDynamicTypesInfo();
1013 }
1014 }
1015
1016 return $result;
1017 }
1018
1019 private static function clearScript($option)
1020 {
1021 $result = [
1022 'NEXT' => false,
1023 'OWNER_DELETE' => [],
1024 ];
1025
1026 if (!$option['CLEAR_FULL'])
1027 {
1028 return $result;
1029 }
1030
1031 $res = ScriptTable::getList(
1032 [
1033 'order' => [
1034 'ID' => 'ASC',
1035 ],
1036 'filter' => [
1037 '>ID' => (int)$option['NEXT'],
1038 '=MODULE_ID' => static::$accessModules,
1039 ],
1040 'limit' => 1,
1041 'select' => ['ID'],
1042 ]
1043 );
1044
1045 while ($item = $res->Fetch())
1046 {
1047 $result['NEXT'] = $item['ID'];
1048
1049 $documentType =
1050 (
1051 isset($item['DOCUMENT_TYPE'])
1052 && is_string($item['DOCUMENT_TYPE'])
1053 && $item['DOCUMENT_TYPE'] !== ''
1054 )
1055 ? $item['DOCUMENT_TYPE']
1056 : ''
1057 ;
1058 if (static::getDynamicTypeCustomSectionIdByDocumentType($documentType) > 0)
1059 {
1060 continue;
1061 }
1062
1063 $deletionResult = Script\Manager::deleteScript($item['ID']);
1064
1065 if ($deletionResult->isSuccess())
1066 {
1067 $result['OWNER_DELETE'][] = [
1068 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC_SCRIPT,
1069 'ENTITY' => $item['ID'],
1070 ];
1071 }
1072 }
1073
1074 return $result;
1075 }
1076
1077 private static function importScript($importData, int $userId, int $appId)
1078 {
1079 $result = [];
1080
1081 if (!isset($importData['CONTENT']['DATA']))
1082 {
1083 return false;
1084 }
1085 $dynamicTypesInfo = static::getDynamicTypesInfoForImport($importData, $result);
1086 $item = $importData['CONTENT']['DATA'];
1087 if (
1088 in_array($item['MODULE_ID'], static::$accessModules)
1089 && Loader::includeModule($item['MODULE_ID'])
1090 && class_exists($item['ENTITY'])
1091 )
1092 {
1093 if (is_subclass_of($item['ENTITY'], '\\IBPWorkflowDocument'))
1094 {
1095 if (
1096 isset($importData['RATIO']['CRM_STATUS'])
1097 && isset($item['WORKFLOW_TEMPLATE'])
1098 && is_array($item['WORKFLOW_TEMPLATE'])
1099 )
1100 {
1101 $oldDynamicEntityTypeId = static::getDynamicEntityTypeIdByDocumentType($item['DOCUMENT_TYPE']);
1102 $isDynamicType = ($oldDynamicEntityTypeId > 0);
1103 if ($isDynamicType)
1104 {
1105 $newDynamicEntityTypeId = static::getDynamicEntityTypeIdByOldEntityTypeId(
1106 $oldDynamicEntityTypeId,
1107 $importData
1108 );
1109 if (
1110 !empty($dynamicTypesInfo)
1111 && self::isCrmModuleIncluded()
1112 && static::checkDynamicTypeByEntityType($newDynamicEntityTypeId)
1113 )
1114 {
1115 $isSetRatio = (isset($importData['RATIO']) && is_array($importData['RATIO']));
1116 $replacementLists = static::getDynamicTypeReplacementLists(
1117 $dynamicTypesInfo,
1118 $isSetRatio ? $importData['RATIO'] : []
1119 );
1120 $item['DOCUMENT_TYPE'] =
1121 static::changeDynamicTypeIdentifiers(
1122 $oldDynamicEntityTypeId,
1123 $item['DOCUMENT_TYPE'],
1124 $importData['RATIO'],
1125 $replacementLists
1126 )
1127 ;
1128 $item['WORKFLOW_TEMPLATE'] =
1129 static::changeDynamicTypeIdentifiers(
1130 $oldDynamicEntityTypeId,
1131 $item['WORKFLOW_TEMPLATE'],
1132 $importData['RATIO'],
1133 $replacementLists
1134 )
1135 ;
1136 }
1137 }
1138 elseif ($item['DOCUMENT_TYPE'] === 'DEAL')
1139 {
1140 $item['WORKFLOW_TEMPLATE'] = static::changeDealCategory(
1141 $item['WORKFLOW_TEMPLATE'],
1142 $importData['RATIO']['CRM_STATUS']
1143 );
1144 }
1145 }
1146
1147 if ($appId > 0)
1148 {
1149 $item['ORIGINATOR_ID'] = 'REST_APP';
1150 $item['ORIGIN_ID'] = $appId;
1151 }
1152
1153 $importResult = Script\Manager::importScript($item, $userId);
1154
1155 if ($importResult->isSuccess())
1156 {
1157 $result['OWNER'] = [
1158 'ENTITY_TYPE' => self::OWNER_ENTITY_TYPE_BIZPROC_SCRIPT,
1159 'ENTITY' => $importResult->getId(),
1160 ];
1161 }
1162 else
1163 {
1164 $result['ERROR_ACTION'] = $result['ERROR_MESSAGES'] = current($importResult->getErrorMessages());
1165 }
1166 }
1167 }
1168
1169 return $result;
1170 }
1171 //end region script
1172
1173 private static function changeDynamicTypeIdentifiers(
1174 int $oldDynamicEntityTypeId,
1175 array|string $data,
1176 array $ratio,
1177 array $replacementLists
1178 ): array|string
1179 {
1180 if (!static::isCrmModuleIncluded())
1181 {
1182 return $data;
1183 }
1184
1185 if (
1186 is_string($data)
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'])
1193 )
1194 {
1195 $replaceMarkers = [];
1196 for ($i = 0; $i < count($replacementLists['from']); $i++)
1197 {
1198 $replaceMarkers[] = "_{<-rm[$i]->}_";
1199 }
1200 $data = str_replace($replacementLists['from'], $replaceMarkers, $data);
1201 $data = str_replace($replaceMarkers, $replacementLists['to'], $data);
1202 }
1203 elseif (is_array($data))
1204 {
1205 $crmHelper = new Helper();
1206
1207 if (
1208 isset($data['field'])
1209 && $data['field'] === 'CATEGORY_ID'
1210 && $data['value'] > 0
1211 )
1212 {
1213 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1214 $oldDynamicEntityTypeId,
1215 (int)$data['value'],
1216 $ratio
1217 );
1218 if ($newCategoryId > 0)
1219 {
1220 $data['value'] = $newCategoryId;
1221 }
1222 }
1223
1224 if (
1225 isset($data['DynamicTypeId'])
1226 && $data['DynamicTypeId'] > 0
1227 )
1228 {
1229 $oldDynamicEntityTypeId = (int)$data['DynamicTypeId'];
1230 $newEntityTypeIdRatioKey = CCrmOwnerType::DynamicTypePrefixName . $oldDynamicEntityTypeId;
1231 if (isset($ratio['CRM_DYNAMIC_TYPES'][$newEntityTypeIdRatioKey]))
1232 {
1233 $newDynamicEntityTypeId = (int)$ratio['CRM_DYNAMIC_TYPES'][$newEntityTypeIdRatioKey];
1234 $data['DynamicTypeId'] = $newDynamicEntityTypeId;
1235 if (isset($data['DynamicEntitiesFields']) && is_array($data['DynamicEntitiesFields']))
1236 {
1237 foreach ($data['DynamicEntitiesFields'] as $oldFieldKey => $value)
1238 {
1239 $oldFieldPrefix = "{$oldDynamicEntityTypeId}_";
1240 $oldFieldPrefixLength = strlen($oldFieldPrefix);
1241 $fieldName = substr($oldFieldKey, $oldFieldPrefixLength);
1242 if ($oldFieldPrefix === substr($oldFieldKey, 0, $oldFieldPrefixLength))
1243 {
1244 $newFieldKey = "{$newDynamicEntityTypeId}_$fieldName";
1245 if ($fieldName === 'CATEGORY_ID' && $value > 0)
1246 {
1247 $oldCategoryId = (int)$value;
1248 $newCategoryRatioKey = "DT{$oldDynamicEntityTypeId}_$oldCategoryId";
1249 if (
1250 isset($ratio['CRM_STATUS'][$newCategoryRatioKey])
1251 && $ratio['CRM_STATUS'][$newCategoryRatioKey] > 0
1252 )
1253 {
1254 $value = (int)$ratio['CRM_STATUS'][$newCategoryRatioKey];
1255 }
1256 }
1257 $data['DynamicEntitiesFields'][$newFieldKey] = $value;
1258 unset($data['DynamicEntitiesFields'][$oldFieldKey]);
1259 }
1260 }
1261 }
1262 }
1263 }
1264
1265 foreach ($data as $key => $value)
1266 {
1267 $newKey = static::changeDynamicTypeIdentifiers(
1268 $oldDynamicEntityTypeId,
1269 $key,
1270 $ratio,
1271 $replacementLists
1272 );
1273 if ($newKey !== $key)
1274 {
1275 unset($data[$key]);
1276 }
1277
1278 if ($newKey === 'CATEGORY_ID')
1279 {
1280 if (is_array($value))
1281 {
1282 if (isset($value['Options']) && is_array($value['Options']))
1283 {
1284 $data[$newKey]['Options'] = [];
1285 foreach ($value['Options'] as $categoryId => $title)
1286 {
1287 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1288 $oldDynamicEntityTypeId,
1289 (int)$categoryId,
1290 $ratio
1291 );
1292 if ($newCategoryId > 0)
1293 {
1294 $data[$newKey]['Options'][$newCategoryId] = $title;
1295 }
1296 }
1297 }
1298 else
1299 {
1300 $data[$newKey] = static::changeDynamicTypeIdentifiers(
1301 $oldDynamicEntityTypeId,
1302 $value,
1303 $ratio,
1304 $replacementLists
1305 );
1306 }
1307 }
1308 elseif (is_string($value))
1309 {
1310 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1311 $oldDynamicEntityTypeId,
1312 (int)$value,
1313 $ratio
1314 );
1315 if ($newCategoryId > 0)
1316 {
1317 $data[$newKey] = $newCategoryId;
1318 }
1319 else
1320 {
1321 $data[$newKey] = static::changeDynamicTypeIdentifiers(
1322 $oldDynamicEntityTypeId,
1323 $value,
1324 $ratio,
1325 $replacementLists
1326 );
1327 }
1328 }
1329 else
1330 {
1331 $data[$newKey] = $value;
1332 }
1333 }
1334 elseif (
1335 ($newKey === 'CategoryId' || $newKey === 'category_id')
1336 && $value > 0
1337 )
1338 {
1339 $newCategoryId = $crmHelper->getNewDynamicTypeCategoryIdByRatio(
1340 $oldDynamicEntityTypeId,
1341 (int)$value,
1342 $ratio
1343 );
1344 if ($newCategoryId > 0)
1345 {
1346 $data[$newKey] = $newCategoryId;
1347 }
1348 }
1349 else
1350 {
1351 if (is_string($value) || is_array($value))
1352 {
1353 $data[$newKey] = static::changeDynamicTypeIdentifiers(
1354 $oldDynamicEntityTypeId,
1355 $value,
1356 $ratio,
1357 $replacementLists
1358 );
1359 }
1360 else
1361 {
1362 $data[$newKey] = $value;
1363 }
1364 }
1365 }
1366 }
1367
1368 return $data;
1369 }
1370
1371 private static function changeDealCategory($data, $ratio)
1372 {
1373 if (!empty($ratio))
1374 {
1375 $ratioRegEx = [];
1376 $ratioReplace = [];
1377 foreach ($ratio as $oldId => $newId)
1378 {
1379 $ratioRegEx[] = '/^C' . $oldId . ':/';
1380 $ratioReplace[] = 'C' . $newId . ':';
1381 }
1382 if (!empty($ratioRegEx))
1383 {
1384 $data = static::changeDealCategoryAction($data, $ratioRegEx, $ratioReplace, $ratio);
1385 }
1386 }
1387
1388 return $data;
1389 }
1390
1391 private static function changeDealCategoryAction($data, $ratioRegEx, $ratioReplace, $ratio)
1392 {
1393 if (is_string($data))
1394 {
1395 $data = preg_replace($ratioRegEx, $ratioReplace, $data);
1396 }
1397 elseif (is_array($data))
1398 {
1399 if (
1400 isset($data['field'])
1401 && $data['field'] == 'CATEGORY_ID'
1402 && $data['value'] > 0
1403 && $ratio[$data['value']] > 0
1404 )
1405 {
1406 $data['value'] = $ratio[$data['value']];
1407 }
1408
1409 foreach ($data as $key => $value)
1410 {
1411 $newKey = static::changeDealCategoryAction($key, $ratioRegEx, $ratioReplace, $ratio);
1412 if ($newKey != $key)
1413 {
1414 unset($data[$key]);
1415 }
1416
1417 if ($newKey == 'CATEGORY_ID')
1418 {
1419 if (is_array($value))
1420 {
1421 if (isset($value['Options']) && is_array($value['Options']))
1422 {
1423 $data[$newKey]['Options'] = [];
1424 foreach ($value['Options'] as $dealId => $title)
1425 {
1426 if (isset($ratio[$dealId]))
1427 {
1428 $data[$newKey]['Options'][$ratio[$dealId]] = $title;
1429 }
1430 }
1431 }
1432 else
1433 {
1434 $data[$newKey] = static::changeDealCategoryAction(
1435 $value,
1436 $ratioRegEx,
1437 $ratioReplace,
1438 $ratio
1439 );
1440 }
1441 }
1442 elseif (is_string($value) && isset($ratio[$value]))
1443 {
1444 $data[$newKey] = $ratio[$value];
1445 }
1446 else
1447 {
1448 $data[$newKey] = static::changeDealCategoryAction(
1449 $value,
1450 $ratioRegEx,
1451 $ratioReplace,
1452 $ratio
1453 );
1454 }
1455 }
1456 elseif ($newKey == 'CategoryId' && intVal($value) > 0 && !empty($ratio[$value]))
1457 {
1458 $data[$newKey] = $ratio[$value];
1459 }
1460 else
1461 {
1462 $data[$newKey] = static::changeDealCategoryAction(
1463 $value,
1464 $ratioRegEx,
1465 $ratioReplace,
1466 $ratio
1467 );
1468 }
1469 }
1470 }
1471
1472 return $data;
1473 }
1474}
$type
Определения options.php:106
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static onEventClearController(Event $event)
Определения appconfiguration.php:109
static onEventExportController(Event $event)
Определения appconfiguration.php:48
static onEventImportController(Event $event)
Определения appconfiguration.php:169
Определения event.php:5
Определения loader.php:13
static isEntityAvailable(string $entityCode, array $option, $uses=[])
Определения manifest.php:156
const SETTING_APP_INFO
Определения setting.php:17
const Script
Определения constants.php:158
const None
Определения constants.php:152
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
$app
Определения proxy.php:8
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$name
Определения menu_edit.php:35
$value
Определения Param.php:39
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$title
Определения pdf.php:123
$option
Определения options.php:1711
if(!Loader::includeModule('sale')) $pattern
Определения index.php:20
$matches
Определения index.php:22