• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/sale_helper.php
  • Класс: \CCrmSaleHelper
  • Вызов: CCrmSaleHelper::getListUserIdFromCrmRoles
static function getListUserIdFromCrmRoles($resetCash = false)
{
	$listUserId = array();

	$cacheTime = 86400;
	$cacheId = "crm-list-crm-roles";
	$cacheDir = "/crm/list_crm_roles/";
	$cache = new CPHPCache;

	if (!$resetCash && $cache->initCache($cacheTime, $cacheId, $cacheDir))
	{
		$listUserId = $cache->getVars();
	}
	else
	{
		$objectQuery = CCrmRole::getRelation();
		while ($relation = $objectQuery->fetch())
		{
			$relationCode = $relation["RELATION"];
			if (preg_match('/^(U|IU)[0-9]+$/', $relationCode, $matches))
			{
				if (!empty($matches[1]))
				{
					$listUserId[str_replace($matches[1], "", $relationCode)] = true;
				}
			}
			elseif (preg_match('/^(G)[0-9]+$/', $relationCode, $matches))
			{
				if (!empty($matches[1]))
				{
					$groupId = str_replace($matches[1], "", $relationCode);
					foreach (CGroup::getGroupUser($groupId) as $userId)
					{
						$listUserId[$userId] = true;
					}
				}
			}
			elseif (preg_match('/^(D|DR)[0-9]+$/', $relationCode, $matches))
			{
				if (!empty($matches[1]))
				{
					$listDepartmentId = array();
					$listDepartmentId[] = str_replace($matches[1], "", $relationCode);
					if ($matches[1] == "DR" && Loader::includeModule("iblock"))
					{
						$currentDepartmentId = current($listDepartmentId);
						if ($currentDepartmentId)
						{
							$parentSectionObject = CIBlockSection::getList(array(),
								array("=ID" => $currentDepartmentId));
							$parentSection = $parentSectionObject->getNext();
							$sectionFilter = array (
								"LEFT_MARGIN" => $parentSection["LEFT_MARGIN"],
								"RIGHT_MARGIN" => $parentSection["RIGHT_MARGIN"],
								"IBLOCK_ID" => $parentSection["IBLOCK_ID"]
							);
							$sectionObject = CIBlockSection::getList(array("left_margin"=>"asc"), $sectionFilter);
							while($section = $sectionObject->getNext())
							{
								$listDepartmentId[] =  $section["ID"];
							}
						}
					}
					if ($listDepartmentId)
					{
						$connection = Bitrix\Main\Application::getConnection();
						if ($connection->isTableExists("b_user") && $connection->isTableExists("b_uts_user")
							&& $connection->isTableExists("b_utm_user"))
						{
							$strSql = "
							SELECT user.ID AS ID
							FROM b_user user
							LEFT JOIN b_uts_user uts_object ON user.ID = uts_object.VALUE_ID
							WHERE user.ID IN (SELECT inner_user.ID AS ID FROM b_user inner_user
							LEFT JOIN b_utm_user utm_object ON utm_object.VALUE_ID = inner_user.ID
							WHERE (utm_object.VALUE_INT in (".implode(',', $listDepartmentId).")))
							ORDER BY user.ID DESC
						";
							$result = $connection->query($strSql);
							while ($user = $result->fetch())
							{
								$listUserId[$user["ID"]] = true;
							}
						}
					}
				}
			}
			elseif (preg_match("/^SG([0-9]+)_[A-Z]$/", $relationCode, $matches) && Loader::includeModule("socialnetwork"))
			{
				$groupId = (int)$matches[1];
				$role = ($matches[2] ?? "K");
				$userToGroup = Bitrix\Socialnetwork\UserToGroupTable::getList(array(
					"filter" => array("=GROUP_ID" => $groupId, "@ROLE" => $role),
					"select" => array("USER_ID")
				));
				while($user = $userToGroup->fetch())
				{
					$listUserId[$user["USER_ID"]] = true;
				}
			}
		}
		$listUserId = array_keys($listUserId);

		if (!empty($listUserId))
		{
			if ($cache->startDataCache())
			{
				$cache->endDataCache($listUserId);
			}
		}
	}

	return $listUserId;
}