288 'DATA_ADDITIONAL' => array()
292 isset($params[
"USER_ID"])
293 && intval($params[
"USER_ID"]) > 0
294 ? intval($params[
"USER_ID"])
299 isset($params[
"CONTEXT_FILTER"])
300 && is_array($params[
"CONTEXT_FILTER"])
301 ? $params[
"CONTEXT_FILTER"]
306 $params[
"CODE_FILTER"] ?? false
311 && !is_array($codeFilter)
314 $codeFilter = array($codeFilter);
319 if ($USER->IsAuthorized())
321 $userId = $USER->getId();
329 $cacheTtl = defined(
"BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
330 $cacheId =
'dest_sort_2'.$userId.serialize($params);
335 $cache = new \CPHPCache;
336 if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
338 $cacheData = $cache->GetVars();
339 $destAll = $cacheData[
'DEST_ALL'] ?? array();
340 $dataAdditionalUsers = $cacheData[
'DATA_ADDITIONAL_USERS'] ?? array();
344 $dataAdditionalUsers = array();
346 $cache->startDataCache();
355 !isset($params[
"ALLOW_EMAIL_INVITATION"])
356 || !$params[
"ALLOW_EMAIL_INVITATION"]
360 $filter[
"!=CODE_USER.EXTERNAL_AUTH_ID"] =
'email';
363 if (!empty($params[
"CODE_TYPE"]))
365 $filter[
"=CODE_TYPE"] = mb_strtoupper($params[
"CODE_TYPE"]);
368 empty($params[
"CRM"])
369 || $params[
"CRM"] !=
'Y'
372 $filter[
"!=CODE_TYPE"] =
"CRM";
376 is_array($contextFilter)
377 && !empty($contextFilter)
380 $filter[
"CONTEXT"] = $contextFilter;
384 is_array($codeFilter)
385 && !empty($codeFilter)
388 $filter[
"CODE"] = $codeFilter;
394 if (!empty($params[
"DEST_CONTEXT"]))
396 $conn = \Bitrix\Main\Application::getConnection();
397 $helper = $conn->getSqlHelper();
400 new \
Bitrix\Main\
Entity\
ExpressionField(
'CONTEXT_SORT',
"CASE WHEN CONTEXT = '".$helper->forSql(mb_strtoupper($params[
"DEST_CONTEXT"])).
"' THEN 1 ELSE 0 END")
404 'CONTEXT_SORT' =>
'DESC'
408 $order[
'LAST_USE_DATE'] =
'DESC';
410 $emailUserCodeList = $emailCrmUserCodeList = array();
415 && isset($params[
"ALLOW_EMAIL_INVITATION"])
416 && $params[
"ALLOW_EMAIL_INVITATION"]
422 "USER_ID" => $USER->getId(),
423 "=CODE_USER.EXTERNAL_AUTH_ID" =>
'email',
426 'select' => array(
'CODE'),
427 'runtime' => $runtime,
428 'limit' => self::LIST_USER_LIMIT
430 while($dest = $res->fetch())
432 $emailUserCodeList[] = $dest[
'CODE'];
434 $dataAdditionalUsers[
'UE'] = $emailUserCodeList;
438 !empty($params[
"DEST_CONTEXT"])
439 && $params[
"DEST_CONTEXT"] ==
"CRM_POST"
445 "USER_ID" => $USER->getId(),
446 "!=CODE_USER.UF_USER_CRM_ENTITY" =>
false,
449 'select' => array(
'CODE'),
450 'runtime' => $runtime,
451 'limit' => self::LIST_USER_LIMIT
453 while($dest = $res->fetch())
455 $emailCrmUserCodeList[] = $dest[
'CODE'];
457 $dataAdditionalUsers[
'UCRM'] = $emailCrmUserCodeList;
468 'runtime' => $runtime
473 while($dest = $res->fetch())
475 $dest[
"LAST_USE_DATE"] = MakeTimeStamp($dest[
"LAST_USE_DATE"]->toString());
479 $cache->endDataCache(array(
480 "DEST_ALL" => $destAll,
481 "DATA_ADDITIONAL_USERS" => $dataAdditionalUsers
485 $resultData = array();
487 foreach ($destAll as $dest)
489 if(!isset($resultData[$dest[
"CODE"]]))
491 $resultData[$dest[
"CODE"]] = array();
495 isset($params[
"DEST_CONTEXT"])
496 && mb_strtoupper($params[
"DEST_CONTEXT"]) == mb_strtoupper($dest[
"CONTEXT"])
503 || !isset($resultData[$dest[
"CODE"]][
"N"])
504 || $dest[
"LAST_USE_DATE"] > $resultData[$dest[
"CODE"]][
"N"]
507 $resultData[$dest[
"CODE"]][$contextType] = $dest[
"LAST_USE_DATE"];
511 $result[
'DATA'] = $resultData;
512 $result[
'DATA_ADDITIONAL'] = $dataAdditionalUsers;
521 'LAST_DESTINATIONS' => array()
526 $resultData = array();
534 $lastDestinationList = array(
536 'SONETGROUPS' => array(),
537 'DEPARTMENT' => array()
540 $iUCounter = $iSGCounter = $iDCounter = 0;
544 && isset($params[
"EMAILS"])
545 && $params[
"EMAILS"] ==
"Y"
549 && isset($params[
"CRMEMAILS"])
550 && $params[
"CRMEMAILS"] ==
"Y"
555 && isset($params[
"PROJECTS"])
556 && $params[
"PROJECTS"] ==
"Y"
560 && isset($params[
"DATA_ADDITIONAL"])
561 && is_array($params[
"DATA_ADDITIONAL"])
562 ? $params[
"DATA_ADDITIONAL"]
566 if (is_array($destSortData))
568 $userIdList = $sonetGroupIdList = array();
570 $sonetGroupLimit = 6;
571 $departmentLimit = 6;
573 foreach ($destSortData as $code => $sortInfo)
579 && ($iUCounter >= $userLimit)
580 && $iSGCounter >= $sonetGroupLimit
581 && $iDCounter >= $departmentLimit
587 if (preg_match(
'/^U(\d+)$/i', $code, $matches))
592 && $iUCounter >= $userLimit
597 if (!isset($lastDestinationList[
'USERS']))
599 $lastDestinationList[
'USERS'] = array();
601 $lastDestinationList[
'USERS'][$code] = $code;
602 $userIdList[] = intval($matches[1]);
605 elseif (preg_match(
'/^SG(\d+)$/i', $code, $matches))
609 && $iSGCounter >= $sonetGroupLimit
614 if (!isset($lastDestinationList[
'SONETGROUPS']))
616 $lastDestinationList[
'SONETGROUPS'] = array();
618 $lastDestinationList[
'SONETGROUPS'][$code] = $code;
619 $sonetGroupIdList[] = intval($matches[1]);
623 preg_match(
'/^D(\d+)$/i', $code, $matches)
624 || preg_match(
'/^DR(\d+)$/i', $code, $matches)
627 if ($iDCounter >= $departmentLimit)
631 if (!isset($lastDestinationList[
'DEPARTMENT']))
633 $lastDestinationList[
'DEPARTMENT'] = array();
635 $lastDestinationList[
'DEPARTMENT'][$code] = $code;
641 $event =
new Event(
"main",
"OnUISelectorFillLastDestination", [
643 'destSortData' => $destSortData
646 $eventResultList = $event->getResults();
648 if (is_array($eventResultList) && !empty($eventResultList))
650 foreach ($eventResultList as $eventResult)
652 if ($eventResult->getType() == EventResult::SUCCESS)
654 $resultParams = $eventResult->getParameters();
655 $eventLastDestinationList = $resultParams[
'lastDestinationList'];
656 if (is_array($eventLastDestinationList))
658 $lastDestinationList = array_merge($lastDestinationList, $eventLastDestinationList);
669 && !empty($userIdList)
672 $iUCounter = $iUECounter = $iUCRMCounter = 0;
673 $emailLimit = $crmLimit = 10;
674 $userId = $USER->getId();
675 $destUList = $destUEList = $destUCRMList = array();
679 isset($dataAdditional[
'UE'])
680 && is_array($dataAdditional[
'UE'])
683 isset($dataAdditional[
'UCRM'])
684 && is_array($dataAdditional[
'UCRM'])
689 empty($dataAdditional[
'UE'])
690 && empty($dataAdditional[
'UCRM'])
693 foreach($userIdList as $uId)
696 $destUList[$code] = $code;
701 foreach($userIdList as $uId)
704 $iUCounter >= $userLimit
705 && $iUECounter >= $emailLimit
706 && $iUCRMCounter >= $crmLimit
716 && in_array($code, $dataAdditional[
'UE'])
719 if ($iUECounter >= $emailLimit)
723 $destUEList[$code] = $code;
728 && in_array($code, $dataAdditional[
'UCRM'])
731 if ($iUCRMCounter >= $crmLimit)
735 $destUCRMList[$code] = $code;
740 if ($iUCounter >= $userLimit)
744 $destUList[$code] = $code;
752 $cacheTtl = defined(
"BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
753 $cacheId =
'dest_sort_users'.$userId.serialize($params).intval($bAllowCrmEmail);
757 $cache = new \CPHPCache;
759 if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
761 $cacheVars = $cache->getVars();
762 $destUList = $cacheVars[
'U'];
763 $destUEList = $cacheVars[
'UE'];
764 $destUCRMList = $cacheVars[
'UCRM'];
768 $cache->startDataCache();
770 $selectList = array(
'ID',
'EXTERNAL_AUTH_ID');
773 $selectList[] =
'UF_USER_CRM_ENTITY';
775 $selectList[] = new \Bitrix\Main\Entity\ExpressionField(
'MAX_LAST_USE_DATE',
'MAX(%s)', array(
'\Bitrix\Main\FinderDest:CODE_USER_CURRENT.LAST_USE_DATE'));
777 $res = \Bitrix\Main\UserTable::getList(array(
779 "MAX_LAST_USE_DATE" =>
'DESC',
784 'select' => $selectList
787 while ($destUser = $res->fetch())
790 $iUCounter >= $userLimit
791 && $iUECounter >= $emailLimit
792 && $iUCRMCounter >= $crmLimit
798 $code =
'U'.$destUser[
'ID'];
800 if ($bAllowEmail && $destUser[
'EXTERNAL_AUTH_ID'] ==
'email')
802 if ($iUECounter >= $emailLimit)
806 $destUEList[$code] = $code;
811 && !empty($destUser[
'UF_USER_CRM_ENTITY'])
814 if ($iUCRMCounter >= $crmLimit)
818 $destUCRMList[$code] = $code;
823 if ($iUCounter >= $userLimit)
827 $destUList[$code] = $code;
832 $cache->endDataCache(array(
835 'UCRM' => $destUCRMList
839 $destUList = array_slice($destUList, 0, self::LIST_USER_LIMIT,
true);
841 $lastDestinationList[
'USERS'] = array_merge($destUList, $destUEList, $destUCRMList);
842 $tmp = array(
'USERS' => $lastDestinationList[
'USERS']);
843 self::sortDestinations($tmp, $destSortData);
844 $lastDestinationList[
'USERS'] = $tmp[
'USERS'];
849 && !empty($sonetGroupIdList)
852 $iSGCounter = $iSGPCounter = 0;
854 $userId = $USER->getId();
856 $destSGList = $destSGPList = array();
858 $cacheTtl = defined(
"BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
859 $cacheId =
'dest_sort_sonetgroups'.$userId.serialize($params);
863 $cache = new \CPHPCache;
865 if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
867 $cacheVars = $cache->getVars();
868 $destSGList = $cacheVars[
'SG'];
869 $destSGPList = $cacheVars[
'SGP'];
873 $cache->startDataCache();
875 $res = \Bitrix\Socialnetwork\WorkgroupTable::getList(array(
877 '@ID' => $sonetGroupIdList
879 'select' => array(
'ID',
'PROJECT')
882 while($destSonetGroup = $res->fetch())
885 $iSGCounter >= $sonetGroupLimit
886 && $iSGPCounter >= $projectLimit
892 $code =
'SG'.$destSonetGroup[
'ID'];
894 if ($destSonetGroup[
'PROJECT'] ==
'Y')
896 if ($iSGPCounter >= $projectLimit)
900 $destSGPList[$code] = $code;
905 if ($iSGCounter >= $sonetGroupLimit)
909 $destSGList[$code] = $code;
914 $cache->endDataCache(array(
916 'SGP' => $destSGPList
921 'SONETGROUPS' => $destSGList,
922 'PROJECTS' => $destSGPList
924 self::sortDestinations($tmp, $destSortData);
925 $lastDestinationList[
'SONETGROUPS'] = $tmp[
'SONETGROUPS'];
926 $lastDestinationList[
'PROJECTS'] = $tmp[
'PROJECTS'];
930 foreach($lastDestinationList as $groupKey => $entitiesList)
932 $result[$groupKey] = array();
934 if (is_array($entitiesList))
938 foreach($entitiesList as $key => $value)
940 $tmp[$key] = $sort++;
942 $result[$groupKey] = $tmp;
946 $result[
'DATA'] = $resultData;
947 $result[
'LAST_DESTINATIONS'] = $lastDestinationList;