Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entities.php
1<?php
2
4
11
13{
14 const CODE_USER_REGEX = '/^U(\d+)$/i';
15 const CODE_USERALL_REGEX = '/^UA$/i';
16 const CODE_USERMANAGER_REGEX = '/^USER_MANAGER$/i';
17 const CODE_SONETGROUP_REGEX = '/^SG(\d+)$/i';
18 const CODE_GROUP_REGEX = '/^G(\d+)$/i';
19 const CODE_DEPT_REGEX = '/^D(\d+)$/i';
20 const CODE_DEPTR_REGEX = '/^DR(\d+)$/i';
21 const CODE_CRMCONTACT_REGEX = '/^CRMCONTACT(\d+)$/i';
22 const CODE_CRMCOMPANY_REGEX = '/^CRMCOMPANY(\d+)$/i';
23 const CODE_CRMLEAD_REGEX = '/^CRMLEAD(\d+)$/i';
24 const CODE_CRMDEAL_REGEX = '/^CRMDEAL(\d+)$/i';
25
26 const ENTITY_TYPE_DEPARTMENTS = 'DEPARTMENTS';
27
28 const LIST_USER_LIMIT = 11;
29
30 public static function getList($params = array())
31 {
32 $result = array();
33
34 if (empty($params['context']))
35 {
36 return $result;
37 }
38
39 if (empty($params['itemsSelected']))
40 {
41 return $result;
42 }
43
44 $event = new Event("main", "OnUISelectorEntitiesGetList", $params);
45 $event->send();
46 $eventResultList = $event->getResults();
47
48 if (is_array($eventResultList) && !empty($eventResultList))
49 {
50 foreach ($eventResultList as $eventResult)
51 {
52 if ($eventResult->getType() == EventResult::SUCCESS)
53 {
54 $resultParams = $eventResult->getParameters();
55 $result = $resultParams['result'];
56 break;
57 }
58 }
59 }
60
61 return $result;
62 }
63
64 public static function getEntityType($params)
65 {
66 if (
67 empty($params)
68 || empty($params['itemCode'])
69 )
70 {
71 return false;
72 }
73
74 $result = false;
75 $itemCode = $params['itemCode'];
76
77 if (preg_match(self::CODE_USER_REGEX, $itemCode, $matches))
78 {
79 $result = 'users';
80 }
81 elseif (preg_match(self::CODE_SONETGROUP_REGEX, $itemCode, $matches))
82 {
83 $result = 'sonetgroups';
84 }
85 elseif (
86 preg_match(self::CODE_DEPT_REGEX, $itemCode, $matches)
87 || preg_match(self::CODE_DEPTR_REGEX, $itemCode, $matches)
88 )
89 {
90 $result = 'department';
91 }
92 elseif (
93 preg_match(self::CODE_USERALL_REGEX, $itemCode, $matches)
94 || preg_match(self::CODE_USERMANAGER_REGEX, $itemCode, $matches)
95 || preg_match(self::CODE_GROUP_REGEX, $itemCode, $matches)
96 )
97 {
98 $result = 'groups';
99 }
100
101 return $result;
102 }
103
104 public static function getData($options = array(), $entityTypes = array(), $selectedItems = array())
105 {
106 $result = array(
107 'ENTITIES' => array(),
108 'SORT' => array()
109 );
110
111 $context = (!empty($options['context']) ? $options['context'] : false);
112
113 if (
114 empty($context)
115 || empty($entityTypes)
116 )
117 {
118 return $result;
119 }
120
121 $filterParams = array(
122 "DEST_CONTEXT" => $context,
123 "ALLOW_EMAIL_INVITATION" => (
124 (isset($options["allowEmailInvitation"]) && $options["allowEmailInvitation"] == "Y")
125 || (isset($options["allowSearchCrmEmailUsers"]) && $options["allowSearchCrmEmailUsers"] == "Y")
126 ),
127 "CRM" => (isset($options["enableCrm"]) && $options["enableCrm"] == "Y")
128 );
129
130 if (!empty($options['contextCode']))
131 {
132 $filterParams["CODE_TYPE"] = $options['contextCode'];
133 }
134
135 $res = self::getLastSort($filterParams);
136 $destSortData = $res['DATA'];
137
138 $dataAdditional = $res['DATA_ADDITIONAL'];
139
141 $destSortData,
142 array(
143 "CRM" => (
144 isset($options["enableCrm"])
145 && $options["enableCrm"] == 'Y'
146 ? 'Y'
147 : 'N'
148 ),
149 "EMAILS" => (
150 (
151 isset($options["allowAddUser"])
152 && $options["allowAddUser"] == 'Y'
153 )
154 || (
155 isset($options["allowSearchEmailUsers"])
156 && $options["allowSearchEmailUsers"] == 'Y'
157 )
158 || (
159 isset($options["allowEmailInvitation"])
160 && $options["allowEmailInvitation"] == 'Y'
161 )
162 ? 'Y'
163 : 'N'
164 ),
165 "CRMEMAILS" => (
166 isset($options["allowSearchCrmEmailUsers"])
167 && $options["allowSearchCrmEmailUsers"] == 'Y'
168 ? 'Y'
169 : 'N'
170 ),
171 "DATA_ADDITIONAL" => $dataAdditional,
172 "MULTI" => (
173 (
174 isset($options["returnMultiEmail"])
175 && $options["returnMultiEmail"] == 'Y'
176 )
177 || (
178 isset($options["returnMultiPhone"])
179 && $options["returnMultiPhone"] == 'Y'
180 )
181 ? 'Y'
182 : 'N'
183 )
184 )
185 );
186
187 $destSortData['UA'] = array(
188 'Y' => 9999999999,
189 'N' => 9999999999
190 );
191 $destSortData['EMPTY'] = array(
192 'Y' => 9999999998,
193 'N' => 9999999998
194 );
195
196 $lastItems = $res['LAST_DESTINATIONS'];
197 $result['SORT'] = $destSortData;
198
199 $result['TABS'] = array();
200
201 if (
202 !isset($options["disableLast"])
203 || $options["disableLast"] != 'Y'
204 )
205 {
206 $result['TABS']['last'] = array(
207 'id' => 'last',
208 'name' => Loc::getMessage('MAIN_UI_SELECTOR_TAB_LAST'),
209 'sort' => 10
210 );
211 }
212
213 $selectedItemsByEntityType = array();
214 if (!empty($selectedItems))
215 {
216 foreach($selectedItems as $key => $entityType)
217 {
218 $entityType = mb_strtoupper($entityType);
219 if (!isset($selectedItemsByEntityType[$entityType]))
220 {
221 $selectedItemsByEntityType[$entityType] = array();
222 }
223 $selectedItemsByEntityType[$entityType][] = $key;
224 }
225 }
226
227 foreach($entityTypes as $entityType => $description)
228 {
229 $provider = self::getProviderByEntityType($entityType);
230 if ($provider !== false)
231 {
232 $result['ENTITIES'][$entityType] = $provider->getData(array(
233 'options' => (!empty($description['options']) ? $description['options'] : array()),
234 'lastItems' => $lastItems,
235 'selectedItems' => $selectedItemsByEntityType
236 ));
237
238 $tabList = $provider->getTabList(array(
239 'options' => (!empty($description['options']) ? $description['options'] : array())
240 ));
241 if (!empty($tabList))
242 {
243 foreach($tabList as $tab)
244 {
245 $result['TABS'][$tab['id']] = $tab;
246 }
247 }
248 }
249 }
250
251 return $result;
252 }
253
254 public static function getProviderByEntityType($entityType)
255 {
256 $result = Handler::getProviderByEntityType($entityType);
257 if ($result)
258 {
259 return $result;
260 }
261
262 $event = new Event("main", "OnUISelectorGetProviderByEntityType", array(
263 'entityType' => $entityType
264 ));
265 $event->send();
266 $eventResultList = $event->getResults();
267 if (is_array($eventResultList) && !empty($eventResultList))
268 {
269 foreach ($eventResultList as $eventResult)
270 {
271 if ($eventResult->getType() == EventResult::SUCCESS)
272 {
273 $resultParams = $eventResult->getParameters();
274 $result = $resultParams['result'];
275 break;
276 }
277 }
278 }
279 return $result;
280 }
281
282 public static function getLastSort($params = array())
283 {
284 global $USER;
285
286 $result = array(
287 'DATA' => array(),
288 'DATA_ADDITIONAL' => array()
289 );
290
291 $userId = (
292 isset($params["USER_ID"])
293 && intval($params["USER_ID"]) > 0
294 ? intval($params["USER_ID"])
295 : false
296 );
297
298 $contextFilter = (
299 isset($params["CONTEXT_FILTER"])
300 && is_array($params["CONTEXT_FILTER"])
301 ? $params["CONTEXT_FILTER"]
302 : false
303 );
304
305 $codeFilter = (
306 $params["CODE_FILTER"] ?? false
307 );
308
309 if (
310 $codeFilter
311 && !is_array($codeFilter)
312 )
313 {
314 $codeFilter = array($codeFilter);
315 }
316
317 if (!$userId)
318 {
319 if ($USER->IsAuthorized())
320 {
321 $userId = $USER->getId();
322 }
323 else
324 {
325 return $result;
326 }
327 }
328
329 $cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
330 $cacheId = 'dest_sort_2'.$userId.serialize($params);
331 $cacheDir = self::getCacheDir([
332 'userId' => $userId,
333 ]);
334
335 $cache = new \CPHPCache;
336 if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
337 {
338 $cacheData = $cache->GetVars();
339 $destAll = $cacheData['DEST_ALL'] ?? array();
340 $dataAdditionalUsers = $cacheData['DATA_ADDITIONAL_USERS'] ?? array();
341 }
342 else
343 {
344 $dataAdditionalUsers = array();
345
346 $cache->startDataCache();
347 $filter = array(
348 "USER_ID" => $userId
349 );
350
351 if (
354 && (
355 !isset($params["ALLOW_EMAIL_INVITATION"])
356 || !$params["ALLOW_EMAIL_INVITATION"]
357 )
358 )
359 {
360 $filter["!=CODE_USER.EXTERNAL_AUTH_ID"] = 'email';
361 }
362
363 if (!empty($params["CODE_TYPE"]))
364 {
365 $filter["=CODE_TYPE"] = mb_strtoupper($params["CODE_TYPE"]);
366 }
367 elseif (
368 empty($params["CRM"])
369 || $params["CRM"] != 'Y'
370 )
371 {
372 $filter["!=CODE_TYPE"] = "CRM";
373 }
374
375 if (
376 is_array($contextFilter)
377 && !empty($contextFilter)
378 )
379 {
380 $filter["CONTEXT"] = $contextFilter;
381 }
382
383 if (
384 is_array($codeFilter)
385 && !empty($codeFilter)
386 )
387 {
388 $filter["CODE"] = $codeFilter;
389 }
390
391 $runtime = array();
392 $order = array();
393
394 if (!empty($params["DEST_CONTEXT"]))
395 {
396 $conn = \Bitrix\Main\Application::getConnection();
397 $helper = $conn->getSqlHelper();
398
399 $runtime = array(
400 new \Bitrix\Main\Entity\ExpressionField('CONTEXT_SORT', "CASE WHEN CONTEXT = '".$helper->forSql(mb_strtoupper($params["DEST_CONTEXT"]))."' THEN 1 ELSE 0 END")
401 );
402
403 $order = array(
404 'CONTEXT_SORT' => 'DESC'
405 );
406 }
407
408 $order['LAST_USE_DATE'] = 'DESC';
409
410 $emailUserCodeList = $emailCrmUserCodeList = array();
411
412 if (
415 && isset($params["ALLOW_EMAIL_INVITATION"])
416 && $params["ALLOW_EMAIL_INVITATION"]
417 )
418 {
419 $res = FinderDestTable::getList(array(
420 'order' => $order,
421 'filter' => array(
422 "USER_ID" => $USER->getId(),
423 "=CODE_USER.EXTERNAL_AUTH_ID" => 'email',
424 "=CODE_TYPE" => 'U'
425 ),
426 'select' => array('CODE'),
427 'runtime' => $runtime,
428 'limit' => self::LIST_USER_LIMIT
429 ));
430 while($dest = $res->fetch())
431 {
432 $emailUserCodeList[] = $dest['CODE'];
433 }
434 $dataAdditionalUsers['UE'] = $emailUserCodeList;
435 }
436
437 if (
438 !empty($params["DEST_CONTEXT"])
439 && $params["DEST_CONTEXT"] == "CRM_POST"
440 )
441 {
442 $res = FinderDestTable::getList(array(
443 'order' => $order,
444 'filter' => array(
445 "USER_ID" => $USER->getId(),
446 "!=CODE_USER.UF_USER_CRM_ENTITY" => false,
447 "=CODE_TYPE" => 'U'
448 ),
449 'select' => array('CODE'),
450 'runtime' => $runtime,
451 'limit' => self::LIST_USER_LIMIT
452 ));
453 while($dest = $res->fetch())
454 {
455 $emailCrmUserCodeList[] = $dest['CODE'];
456 }
457 $dataAdditionalUsers['UCRM'] = $emailCrmUserCodeList;
458 }
459
460 $res = FinderDestTable::getList(array(
461 'order' => $order,
462 'filter' => $filter,
463 'select' => array(
464 'CONTEXT',
465 'CODE',
466 'LAST_USE_DATE'
467 ),
468 'runtime' => $runtime
469 ));
470
471 $destAll = array();
472
473 while($dest = $res->fetch())
474 {
475 $dest["LAST_USE_DATE"] = MakeTimeStamp($dest["LAST_USE_DATE"]->toString());
476 $destAll[] = $dest;
477 }
478
479 $cache->endDataCache(array(
480 "DEST_ALL" => $destAll,
481 "DATA_ADDITIONAL_USERS" => $dataAdditionalUsers
482 ));
483 }
484
485 $resultData = array();
486
487 foreach ($destAll as $dest)
488 {
489 if(!isset($resultData[$dest["CODE"]]))
490 {
491 $resultData[$dest["CODE"]] = array();
492 }
493
494 $contextType = (
495 isset($params["DEST_CONTEXT"])
496 && mb_strtoupper($params["DEST_CONTEXT"]) == mb_strtoupper($dest["CONTEXT"])
497 ? "Y"
498 : "N"
499 );
500
501 if (
502 $contextType == "Y"
503 || !isset($resultData[$dest["CODE"]]["N"])
504 || $dest["LAST_USE_DATE"] > $resultData[$dest["CODE"]]["N"]
505 )
506 {
507 $resultData[$dest["CODE"]][$contextType] = $dest["LAST_USE_DATE"];
508 }
509 }
510
511 $result['DATA'] = $resultData;
512 $result['DATA_ADDITIONAL'] = $dataAdditionalUsers;
513
514 return $result;
515 }
516
517 public static function fillLastDestination($destSortData, $params = array())
518 {
519 $result = array(
520 'DATA' => array(),
521 'LAST_DESTINATIONS' => array()
522 );
523
524 global $USER;
525
526 $resultData = array();
527
528 if (!Loader::includeModule('socialnetwork'))
529 {
530 return $result;
531 }
532
533 // initialize keys for compatibility
534 $lastDestinationList = array(
535 'USERS' => array(),
536 'SONETGROUPS' => array(),
537 'DEPARTMENT' => array()
538 );
539
540 $iUCounter = $iSGCounter = $iDCounter = 0;
541
542 $bAllowEmail = (
543 is_array($params)
544 && isset($params["EMAILS"])
545 && $params["EMAILS"] == "Y"
546 );
547 $bAllowCrmEmail = (
548 is_array($params)
549 && isset($params["CRMEMAILS"])
550 && $params["CRMEMAILS"] == "Y"
552 );
553 $bAllowProject = (
554 is_array($params)
555 && isset($params["PROJECTS"])
556 && $params["PROJECTS"] == "Y"
557 );
558 $dataAdditional = (
559 is_array($params)
560 && isset($params["DATA_ADDITIONAL"])
561 && is_array($params["DATA_ADDITIONAL"])
562 ? $params["DATA_ADDITIONAL"]
563 : array()
564 );
565
566 if (is_array($destSortData))
567 {
568 $userIdList = $sonetGroupIdList = array();
569 $userLimit = self::LIST_USER_LIMIT;
570 $sonetGroupLimit = 6;
571 $departmentLimit = 6;
572
573 foreach ($destSortData as $code => $sortInfo)
574 {
575 if (
576 !$bAllowEmail
577 && !$bAllowCrmEmail
578 && !$bAllowProject
579 && ($iUCounter >= $userLimit)
580 && $iSGCounter >= $sonetGroupLimit
581 && $iDCounter >= $departmentLimit
582 )
583 {
584 break;
585 }
586
587 if (preg_match('/^U(\d+)$/i', $code, $matches))
588 {
589 if (
590 !$bAllowEmail
591 && !$bAllowCrmEmail
592 && $iUCounter >= $userLimit
593 )
594 {
595 continue;
596 }
597 if (!isset($lastDestinationList['USERS']))
598 {
599 $lastDestinationList['USERS'] = array();
600 }
601 $lastDestinationList['USERS'][$code] = $code;
602 $userIdList[] = intval($matches[1]);
603 $iUCounter++;
604 }
605 elseif (preg_match('/^SG(\d+)$/i', $code, $matches))
606 {
607 if (
608 !$bAllowProject
609 && $iSGCounter >= $sonetGroupLimit
610 )
611 {
612 continue;
613 }
614 if (!isset($lastDestinationList['SONETGROUPS']))
615 {
616 $lastDestinationList['SONETGROUPS'] = array();
617 }
618 $lastDestinationList['SONETGROUPS'][$code] = $code;
619 $sonetGroupIdList[] = intval($matches[1]);
620 $iSGCounter++;
621 }
622 elseif (
623 preg_match('/^D(\d+)$/i', $code, $matches)
624 || preg_match('/^DR(\d+)$/i', $code, $matches)
625 )
626 {
627 if ($iDCounter >= $departmentLimit)
628 {
629 continue;
630 }
631 if (!isset($lastDestinationList['DEPARTMENT']))
632 {
633 $lastDestinationList['DEPARTMENT'] = array();
634 }
635 $lastDestinationList['DEPARTMENT'][$code] = $code;
636 $iDCounter++;
637 }
638 }
639
640
641 $event = new Event("main", "OnUISelectorFillLastDestination", [
642 'params' => $params,
643 'destSortData' => $destSortData
644 ]);
645 $event->send();
646 $eventResultList = $event->getResults();
647
648 if (is_array($eventResultList) && !empty($eventResultList))
649 {
650 foreach ($eventResultList as $eventResult)
651 {
652 if ($eventResult->getType() == EventResult::SUCCESS)
653 {
654 $resultParams = $eventResult->getParameters();
655 $eventLastDestinationList = $resultParams['lastDestinationList'];
656 if (is_array($eventLastDestinationList))
657 {
658 $lastDestinationList = array_merge($lastDestinationList, $eventLastDestinationList);
659 }
660 }
661 }
662 }
663
664 if (
665 (
666 $bAllowEmail
667 || $bAllowCrmEmail
668 )
669 && !empty($userIdList)
670 )
671 {
672 $iUCounter = $iUECounter = $iUCRMCounter = 0;
673 $emailLimit = $crmLimit = 10;
674 $userId = $USER->getId();
675 $destUList = $destUEList = $destUCRMList = array();
676
677 if (
678 (
679 isset($dataAdditional['UE'])
680 && is_array($dataAdditional['UE'])
681 )
682 || (
683 isset($dataAdditional['UCRM'])
684 && is_array($dataAdditional['UCRM'])
685 )
686 )
687 {
688 if (
689 empty($dataAdditional['UE'])
690 && empty($dataAdditional['UCRM'])
691 )
692 {
693 foreach($userIdList as $uId)
694 {
695 $code = 'U'.$uId;
696 $destUList[$code] = $code;
697 }
698 }
699 else
700 {
701 foreach($userIdList as $uId)
702 {
703 if (
704 $iUCounter >= $userLimit
705 && $iUECounter >= $emailLimit
706 && $iUCRMCounter >= $crmLimit
707 )
708 {
709 break;
710 }
711
712 $code = 'U'.$uId;
713
714 if (
715 $bAllowEmail
716 && in_array($code, $dataAdditional['UE'])
717 )
718 {
719 if ($iUECounter >= $emailLimit)
720 {
721 continue;
722 }
723 $destUEList[$code] = $code;
724 $iUECounter++;
725 }
726 elseif (
727 $bAllowCrmEmail
728 && in_array($code, $dataAdditional['UCRM'])
729 )
730 {
731 if ($iUCRMCounter >= $crmLimit)
732 {
733 continue;
734 }
735 $destUCRMList[$code] = $code;
736 $iUCRMCounter++;
737 }
738 else
739 {
740 if ($iUCounter >= $userLimit)
741 {
742 continue;
743 }
744 $destUList[$code] = $code;
745 $iUCounter++;
746 }
747 }
748 }
749 }
750 else // old method
751 {
752 $cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
753 $cacheId = 'dest_sort_users'.$userId.serialize($params).intval($bAllowCrmEmail);
754 $cacheDir = self::getCacheDir([
755 'userId' => $userId,
756 ]);;
757 $cache = new \CPHPCache;
758
759 if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
760 {
761 $cacheVars = $cache->getVars();
762 $destUList = $cacheVars['U'];
763 $destUEList = $cacheVars['UE'];
764 $destUCRMList = $cacheVars['UCRM'];
765 }
766 else
767 {
768 $cache->startDataCache();
769
770 $selectList = array('ID', 'EXTERNAL_AUTH_ID');
771 if ($bAllowCrmEmail)
772 {
773 $selectList[] = 'UF_USER_CRM_ENTITY';
774 }
775 $selectList[] = new \Bitrix\Main\Entity\ExpressionField('MAX_LAST_USE_DATE', 'MAX(%s)', array('\Bitrix\Main\FinderDest:CODE_USER_CURRENT.LAST_USE_DATE'));
776
777 $res = \Bitrix\Main\UserTable::getList(array(
778 'order' => array(
779 "MAX_LAST_USE_DATE" => 'DESC',
780 ),
781 'filter' => array(
782 '@ID' => $userIdList
783 ),
784 'select' => $selectList
785 ));
786
787 while ($destUser = $res->fetch())
788 {
789 if (
790 $iUCounter >= $userLimit
791 && $iUECounter >= $emailLimit
792 && $iUCRMCounter >= $crmLimit
793 )
794 {
795 break;
796 }
797
798 $code = 'U'.$destUser['ID'];
799
800 if ($bAllowEmail && $destUser['EXTERNAL_AUTH_ID'] == 'email')
801 {
802 if ($iUECounter >= $emailLimit)
803 {
804 continue;
805 }
806 $destUEList[$code] = $code;
807 $iUECounter++;
808 }
809 elseif (
810 $bAllowCrmEmail
811 && !empty($destUser['UF_USER_CRM_ENTITY'])
812 )
813 {
814 if ($iUCRMCounter >= $crmLimit)
815 {
816 continue;
817 }
818 $destUCRMList[$code] = $code;
819 $iUCRMCounter++;
820 }
821 else
822 {
823 if ($iUCounter >= $userLimit)
824 {
825 continue;
826 }
827 $destUList[$code] = $code;
828 $iUCounter++;
829 }
830 }
831
832 $cache->endDataCache(array(
833 'U' => $destUList,
834 'UE' => $destUEList,
835 'UCRM' => $destUCRMList
836 ));
837 }
838 }
839 $destUList = array_slice($destUList, 0, self::LIST_USER_LIMIT, true);
840
841 $lastDestinationList['USERS'] = array_merge($destUList, $destUEList, $destUCRMList);
842 $tmp = array('USERS' => $lastDestinationList['USERS']);
843 self::sortDestinations($tmp, $destSortData);
844 $lastDestinationList['USERS'] = $tmp['USERS'];
845 }
846
847 if (
848 $bAllowProject
849 && !empty($sonetGroupIdList)
850 )
851 {
852 $iSGCounter = $iSGPCounter = 0;
853 $projectLimit = 10;
854 $userId = $USER->getId();
855
856 $destSGList = $destSGPList = array();
857
858 $cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
859 $cacheId = 'dest_sort_sonetgroups'.$userId.serialize($params);
860 $cacheDir = self::getCacheDir([
861 'userId' => $userId,
862 ]);
863 $cache = new \CPHPCache;
864
865 if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
866 {
867 $cacheVars = $cache->getVars();
868 $destSGList = $cacheVars['SG'];
869 $destSGPList = $cacheVars['SGP'];
870 }
871 else
872 {
873 $cache->startDataCache();
874
875 $res = \Bitrix\Socialnetwork\WorkgroupTable::getList(array(
876 'filter' => array(
877 '@ID' => $sonetGroupIdList
878 ),
879 'select' => array('ID', 'PROJECT')
880 ));
881
882 while($destSonetGroup = $res->fetch())
883 {
884 if (
885 $iSGCounter >= $sonetGroupLimit
886 && $iSGPCounter >= $projectLimit
887 )
888 {
889 break;
890 }
891
892 $code = 'SG'.$destSonetGroup['ID'];
893
894 if ($destSonetGroup['PROJECT'] == 'Y')
895 {
896 if ($iSGPCounter >= $projectLimit)
897 {
898 continue;
899 }
900 $destSGPList[$code] = $code;
901 $iSGPCounter++;
902 }
903 else
904 {
905 if ($iSGCounter >= $sonetGroupLimit)
906 {
907 continue;
908 }
909 $destSGList[$code] = $code;
910 $iSGCounter++;
911 }
912 }
913
914 $cache->endDataCache(array(
915 'SG' => $destSGList,
916 'SGP' => $destSGPList
917 ));
918 }
919
920 $tmp = array(
921 'SONETGROUPS' => $destSGList,
922 'PROJECTS' => $destSGPList
923 );
924 self::sortDestinations($tmp, $destSortData);
925 $lastDestinationList['SONETGROUPS'] = $tmp['SONETGROUPS'];
926 $lastDestinationList['PROJECTS'] = $tmp['PROJECTS'];
927 }
928 }
929
930 foreach($lastDestinationList as $groupKey => $entitiesList)
931 {
932 $result[$groupKey] = array();
933
934 if (is_array($entitiesList))
935 {
936 $tmp = array();
937 $sort = 0;
938 foreach($entitiesList as $key => $value)
939 {
940 $tmp[$key] = $sort++;
941 }
942 $result[$groupKey] = $tmp;
943 }
944 }
945
946 $result['DATA'] = $resultData;
947 $result['LAST_DESTINATIONS'] = $lastDestinationList;
948
949 return $result;
950 }
951
952 private static function compareDestinations($a, $b)
953 {
954 if(!is_array($a) && !is_array($b))
955 {
956 return 0;
957 }
958 elseif(is_array($a) && !is_array($b))
959 {
960 return -1;
961 }
962 elseif(!is_array($a) && is_array($b))
963 {
964 return 1;
965 }
966 else
967 {
968 if(isset($a["SORT"]["Y"]) && !isset($b["SORT"]["Y"]))
969 {
970 return -1;
971 }
972 elseif(!isset($a["SORT"]["Y"]) && isset($b["SORT"]["Y"]))
973 {
974 return 1;
975 }
976 elseif(isset($a["SORT"]["Y"]) && isset($b["SORT"]["Y"]))
977 {
978 if(intval($a["SORT"]["Y"]) > intval($b["SORT"]["Y"]))
979 {
980 return -1;
981 }
982 elseif(intval($a["SORT"]["Y"]) < intval($b["SORT"]["Y"]))
983 {
984 return 1;
985 }
986 else
987 {
988 return 0;
989 }
990 }
991 else
992 {
993 if(intval($a["SORT"]["N"]) > intval($b["SORT"]["N"]))
994 {
995 return -1;
996 }
997 elseif(intval($a["SORT"]["N"]) < intval($b["SORT"]["N"]))
998 {
999 return 1;
1000 }
1001 else
1002 {
1003 return 0;
1004 }
1005 }
1006 }
1007 }
1008
1009 private static function sortDestinations(&$destinationList, $destSortData)
1010 {
1011 foreach($destinationList as $type => $dest)
1012 {
1013 if (is_array($dest))
1014 {
1015 foreach($dest as $key => $value)
1016 {
1017 if (isset($destSortData[$key]))
1018 {
1019 $destinationList[$type][$key] = array(
1020 "VALUE" => $value,
1021 "SORT" => $destSortData[$key]
1022 );
1023 }
1024 }
1025
1026 uasort($destinationList[$type], array(__CLASS__, 'compareDestinations'));
1027 }
1028 }
1029
1030 foreach($destinationList as $type => $dest)
1031 {
1032 if (is_array($dest))
1033 {
1034 foreach($dest as $key => $val)
1035 {
1036 if (is_array($val))
1037 {
1038 $destinationList[$type][$key] = $val["VALUE"];
1039 }
1040 }
1041 }
1042 }
1043 }
1044
1045 public static function search($options = array(), $entityTypes = array(), $requestFields = array())
1046 {
1047 $result = array(
1048 'ENTITIES' => array()
1049 );
1050
1051 foreach($entityTypes as $entityType => $description)
1052 {
1053 $provider = self::getProviderByEntityType($entityType);
1054 if ($provider !== false)
1055 {
1056 $options = (!empty($description['options']) ? $description['options'] : array());
1057 if (
1058 !empty($requestFields['additionalData'])
1059 && !empty($requestFields['additionalData'][$entityType])
1060 )
1061 {
1062 $options['additionalData'] = $requestFields['additionalData'][$entityType];
1063 }
1064
1065 $result['ENTITIES'][$entityType] = $provider->search(array(
1066 'options' => $options,
1067 'requestFields' => $requestFields
1068 ));
1069 }
1070 }
1071
1072 return $result;
1073 }
1074
1075 public static function loadAll($entityType)
1076 {
1077 $result = array();
1078
1079 if (empty($entityType))
1080 {
1081 $entityType = 'USERS';
1082 }
1083
1084 $provider = self::getProviderByEntityType($entityType);
1085 if($provider !== false)
1086 {
1087 $result[$entityType] = $provider->loadAll();
1088 }
1089
1090 return $result;
1091 }
1092
1093 public static function save($params = [])
1094 {
1095 if (
1096 !is_array($params)
1097 || empty($params['context'])
1098 || empty($params['code'])
1099 )
1100 {
1101 return;
1102 }
1103
1104 $context = $params['context'];
1105 $code = $params['code'];
1106
1107 $event = new Event("main", "OnUISelectorBeforeSave", $params);
1108 $event->send();
1109 $eventResultList = $event->getResults();
1110
1111 if (is_array($eventResultList) && !empty($eventResultList))
1112 {
1113 foreach ($eventResultList as $eventResult)
1114 {
1115 if ($eventResult->getType() == EventResult::SUCCESS)
1116 {
1117 $resultParams = $eventResult->getParameters();
1118 $code = $resultParams['code'];
1119 break;
1120 }
1121 }
1122 }
1123
1124 \Bitrix\Main\FinderDestTable::merge(array(
1125 "CONTEXT" => $context,
1126 "CODE" => $code
1127 ));
1128 }
1129
1130 public static function getCacheDir(array $params = [])
1131 {
1132 global $USER;
1133
1134 $userId = (int)($params['userId'] ?? 0);
1135
1136 if (
1137 $userId <= 0
1138 && $USER->isAuthorized()
1139 )
1140 {
1141 $userId = $USER->getId();
1142 }
1143
1144 return '/ui_selector/dest_sort/' . substr(md5($userId), 2, 2) . '/' . $userId;
1145 }
1146
1147}
static includeModule($moduleName)
Definition loader.php:69
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static isModuleInstalled($moduleName)
static getList(array $parameters=array())
static getLastSort($params=array())
Definition entities.php:282
static getCacheDir(array $params=[])
static search($options=array(), $entityTypes=array(), $requestFields=array())
static loadAll($entityType)
static getList($params=array())
Definition entities.php:30
static getProviderByEntityType($entityType)
Definition entities.php:254
static getData($options=array(), $entityTypes=array(), $selectedItems=array())
Definition entities.php:104
static fillLastDestination($destSortData, $params=array())
Definition entities.php:517
static getProviderByEntityType($entityType)
Definition handler.php:16