• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/ui/selector/entities.php
  • Класс: BitrixMainUISelectorEntities
  • Вызов: Entities::fillLastDestination
static function fillLastDestination($destSortData, $params = array())
{
	$result = array(
		'DATA' => array(),
		'LAST_DESTINATIONS' => array()
	);

	global $USER;

	$resultData = array();

	if (!Loader::includeModule('socialnetwork'))
	{
		return $result;
	}

	// initialize keys for compatibility
	$lastDestinationList = array(
		'USERS' => array(),
		'SONETGROUPS' => array(),
		'DEPARTMENT' => array()
	);

	$iUCounter = $iSGCounter = $iDCounter = 0;

	$bAllowEmail = (
		is_array($params)
		&& isset($params["EMAILS"])
		&& $params["EMAILS"] == "Y"
	);
	$bAllowCrmEmail = (
		is_array($params)
		&& isset($params["CRMEMAILS"])
		&& $params["CRMEMAILS"] == "Y"
		&& ModuleManager::isModuleInstalled('crm')
	);
	$bAllowProject = (
		is_array($params)
		&& isset($params["PROJECTS"])
		&& $params["PROJECTS"] == "Y"
	);
	$dataAdditional = (
		is_array($params)
		&& isset($params["DATA_ADDITIONAL"])
		&& is_array($params["DATA_ADDITIONAL"])
			? $params["DATA_ADDITIONAL"]
			: array()
	);

	if (is_array($destSortData))
	{
		$userIdList = $sonetGroupIdList = array();
		$userLimit = self::LIST_USER_LIMIT;
		$sonetGroupLimit = 6;
		$departmentLimit = 6;

		foreach ($destSortData as $code => $sortInfo)
		{
			if (
				!$bAllowEmail
				&& !$bAllowCrmEmail
				&& !$bAllowProject
				&& ($iUCounter >= $userLimit)
				&& $iSGCounter >= $sonetGroupLimit
				&& $iDCounter >= $departmentLimit
			)
			{
				break;
			}

			if (preg_match('/^U(d+)$/i', $code, $matches))
			{
				if (
					!$bAllowEmail
					&& !$bAllowCrmEmail
					&& $iUCounter >= $userLimit
				)
				{
					continue;
				}
				if (!isset($lastDestinationList['USERS']))
				{
					$lastDestinationList['USERS'] = array();
				}
				$lastDestinationList['USERS'][$code] = $code;
				$userIdList[] = intval($matches[1]);
				$iUCounter++;
			}
			elseif (preg_match('/^SG(d+)$/i', $code, $matches))
			{
				if (
					!$bAllowProject
					&& $iSGCounter >= $sonetGroupLimit
				)
				{
					continue;
				}
				if (!isset($lastDestinationList['SONETGROUPS']))
				{
					$lastDestinationList['SONETGROUPS'] = array();
				}
				$lastDestinationList['SONETGROUPS'][$code] = $code;
				$sonetGroupIdList[] = intval($matches[1]);
				$iSGCounter++;
			}
			elseif (
				preg_match('/^D(d+)$/i', $code, $matches)
				|| preg_match('/^DR(d+)$/i', $code, $matches)
			)
			{
				if ($iDCounter >= $departmentLimit)
				{
					continue;
				}
				if (!isset($lastDestinationList['DEPARTMENT']))
				{
					$lastDestinationList['DEPARTMENT'] = array();
				}
				$lastDestinationList['DEPARTMENT'][$code] = $code;
				$iDCounter++;
			}
		}


		$event = new Event("main", "OnUISelectorFillLastDestination", [
			'params' => $params,
			'destSortData' => $destSortData
		]);
		$event->send();
		$eventResultList = $event->getResults();

		if (is_array($eventResultList) && !empty($eventResultList))
		{
			foreach ($eventResultList as $eventResult)
			{
				if ($eventResult->getType() == EventResult::SUCCESS)
				{
					$resultParams = $eventResult->getParameters();
					$eventLastDestinationList = $resultParams['lastDestinationList'];
					if (is_array($eventLastDestinationList))
					{
						$lastDestinationList = array_merge($lastDestinationList, $eventLastDestinationList);
					}
				}
			}
		}

		if (
			(
				$bAllowEmail
				|| $bAllowCrmEmail
			)
			&& !empty($userIdList)
		)
		{
			$iUCounter = $iUECounter = $iUCRMCounter = 0;
			$emailLimit = $crmLimit = 10;
			$userId = $USER->getId();
			$destUList = $destUEList = $destUCRMList = array();

			if (
				(
					isset($dataAdditional['UE'])
					&& is_array($dataAdditional['UE'])
				)
				|| (
					isset($dataAdditional['UCRM'])
					&& is_array($dataAdditional['UCRM'])
				)
			)
			{
				if (
					empty($dataAdditional['UE'])
					&& empty($dataAdditional['UCRM'])
				)
				{
					foreach($userIdList as $uId)
					{
						$code = 'U'.$uId;
						$destUList[$code] = $code;
					}
				}
				else
				{
					foreach($userIdList as $uId)
					{
						if (
							$iUCounter >= $userLimit
							&& $iUECounter >= $emailLimit
							&& $iUCRMCounter >= $crmLimit
						)
						{
							break;
						}

						$code = 'U'.$uId;

						if (
							$bAllowEmail
							&& in_array($code, $dataAdditional['UE'])
						)
						{
							if ($iUECounter >= $emailLimit)
							{
								continue;
							}
							$destUEList[$code] = $code;
							$iUECounter++;
						}
						elseif (
							$bAllowCrmEmail
							&& in_array($code, $dataAdditional['UCRM'])
						)
						{
							if ($iUCRMCounter >= $crmLimit)
							{
								continue;
							}
							$destUCRMList[$code] = $code;
							$iUCRMCounter++;
						}
						else
						{
							if ($iUCounter >= $userLimit)
							{
								continue;
							}
							$destUList[$code] = $code;
							$iUCounter++;
						}
					}
				}
			}
			else // old method
			{
				$cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
				$cacheId = 'dest_sort_users'.$userId.serialize($params).intval($bAllowCrmEmail);
				$cacheDir = self::getCacheDir([
					'userId' => $userId,
				]);;
				$cache = new CPHPCache;

				if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
				{
					$cacheVars = $cache->getVars();
					$destUList = $cacheVars['U'];
					$destUEList = $cacheVars['UE'];
					$destUCRMList = $cacheVars['UCRM'];
				}
				else
				{
					$cache->startDataCache();

					$selectList = array('ID', 'EXTERNAL_AUTH_ID');
					if ($bAllowCrmEmail)
					{
						$selectList[] = 'UF_USER_CRM_ENTITY';
					}
					$selectList[] = new BitrixMainEntityExpressionField('MAX_LAST_USE_DATE', 'MAX(%s)', array('BitrixMainFinderDest:CODE_USER_CURRENT.LAST_USE_DATE'));

					$res = BitrixMainUserTable::getList(array(
						'order' => array(
							"MAX_LAST_USE_DATE" => 'DESC',
						),
						'filter' => array(
							'@ID' => $userIdList
						),
						'select' => $selectList
					));

					while ($destUser = $res->fetch())
					{
						if (
							$iUCounter >= $userLimit
							&& $iUECounter >= $emailLimit
							&& $iUCRMCounter >= $crmLimit
						)
						{
							break;
						}

						$code = 'U'.$destUser['ID'];

						if ($bAllowEmail && $destUser['EXTERNAL_AUTH_ID'] == 'email')
						{
							if ($iUECounter >= $emailLimit)
							{
								continue;
							}
							$destUEList[$code] = $code;
							$iUECounter++;
						}
						elseif (
							$bAllowCrmEmail
							&& !empty($destUser['UF_USER_CRM_ENTITY'])
						)
						{
							if ($iUCRMCounter >= $crmLimit)
							{
								continue;
							}
							$destUCRMList[$code] = $code;
							$iUCRMCounter++;
						}
						else
						{
							if ($iUCounter >= $userLimit)
							{
								continue;
							}
							$destUList[$code] = $code;
							$iUCounter++;
						}
					}

					$cache->endDataCache(array(
						'U' => $destUList,
						'UE' => $destUEList,
						'UCRM' => $destUCRMList
					));
				}
			}
			$destUList = array_slice($destUList, 0, self::LIST_USER_LIMIT, true);

			$lastDestinationList['USERS'] = array_merge($destUList, $destUEList, $destUCRMList);
			$tmp = array('USERS' => $lastDestinationList['USERS']);
			self::sortDestinations($tmp, $destSortData);
			$lastDestinationList['USERS'] = $tmp['USERS'];
		}

		if (
			$bAllowProject
			&& !empty($sonetGroupIdList)
		)
		{
			$iSGCounter = $iSGPCounter = 0;
			$projectLimit = 10;
			$userId = $USER->getId();

			$destSGList = $destSGPList = array();

			$cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
			$cacheId = 'dest_sort_sonetgroups'.$userId.serialize($params);
			$cacheDir = self::getCacheDir([
				'userId' => $userId,
			]);
			$cache = new CPHPCache;

			if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
			{
				$cacheVars = $cache->getVars();
				$destSGList = $cacheVars['SG'];
				$destSGPList = $cacheVars['SGP'];
			}
			else
			{
				$cache->startDataCache();

				$res = BitrixSocialnetworkWorkgroupTable::getList(array(
					'filter' => array(
						'@ID' => $sonetGroupIdList
					),
					'select' => array('ID', 'PROJECT')
				));

				while($destSonetGroup = $res->fetch())
				{
					if (
						$iSGCounter >= $sonetGroupLimit
						&& $iSGPCounter >= $projectLimit
					)
					{
						break;
					}

					$code = 'SG'.$destSonetGroup['ID'];

					if ($destSonetGroup['PROJECT'] == 'Y')
					{
						if ($iSGPCounter >= $projectLimit)
						{
							continue;
						}
						$destSGPList[$code] = $code;
						$iSGPCounter++;
					}
					else
					{
						if ($iSGCounter >= $sonetGroupLimit)
						{
							continue;
						}
						$destSGList[$code] = $code;
						$iSGCounter++;
					}
				}

				$cache->endDataCache(array(
					'SG' => $destSGList,
					'SGP' => $destSGPList
				));
			}

			$tmp = array(
				'SONETGROUPS' => $destSGList,
				'PROJECTS' => $destSGPList
			);
			self::sortDestinations($tmp, $destSortData);
			$lastDestinationList['SONETGROUPS'] = $tmp['SONETGROUPS'];
			$lastDestinationList['PROJECTS'] = $tmp['PROJECTS'];
		}
	}

	foreach($lastDestinationList as $groupKey => $entitiesList)
	{
		$result[$groupKey] = array();

		if (is_array($entitiesList))
		{
			$tmp = array();
			$sort = 0;
			foreach($entitiesList as $key => $value)
			{
				$tmp[$key] = $sort++;
			}
			$result[$groupKey] = $tmp;
		}
	}

	$result['DATA'] = $resultData;
	$result['LAST_DESTINATIONS'] = $lastDestinationList;

	return $result;
}