• Модуль: report
  • Путь к файлу: ~/bitrix/modules/report/classes/general/report_helper.php
  • Класс: CReportHelper
  • Вызов: CReportHelper::collectUFValues
static function collectUFValues($rows, $columnInfo, $total)
{
	// uf columns
	$fileColumns = array();
	$diskFileColumns = array();
	$employeeColumns = array();
	$crmColumns = array();
	$crmStatusColumns = array();
	$iblockElementColumns = array();
	$iblockSectionColumns = array();
	if (is_array($columnInfo))
	{
		foreach ($columnInfo as $k => $cInfo)
		{
			if ($cInfo['isUF'] && is_array($cInfo['ufInfo']) && isset($cInfo['ufInfo']['USER_TYPE_ID']))
			{
				switch ($cInfo['ufInfo']['USER_TYPE_ID'])
				{
					case 'file':
						$fileColumns[$k] = true;
						break;
					case 'disk_file':
						$diskFileColumns[$k] = true;
						break;
					case 'employee':
						$employeeColumns[$k] = true;
						break;
					case 'crm':
						$crmColumns[$k] = true;
						break;
					case 'crm_status':
						$crmStatusColumns[$k] = true;
						break;
					case 'iblock_element':
						$iblockElementColumns[$k] = true;
						break;
					case 'iblock_section':
						$iblockSectionColumns[$k] = true;
						break;
				}
			}
		}
	}

	$arFileID = array();
	$arDiskFileID = array();
	$arEmployeeID = array();
	$arCrmID = array();
	$arCrmStatusID = array();
	$arCrmStatusEntityType = array();
	$arIblockElementID = array();
	$arIblockSectionID = array();
	if (count($fileColumns) > 0 || count($diskFileColumns) > 0 || count($employeeColumns) > 0
		|| count($crmColumns) > 0 || count($crmStatusColumns) > 0 || count($iblockElementColumns) > 0
		|| count($iblockSectionColumns) > 0)
	{
		foreach ($rows as $row)
		{
			foreach ($row as $k => $v)
			{
				// file
				if (isset($fileColumns[$k]))
				{
					if (is_array($v))
						foreach ($v as $subv)
						{
							$value = intval($subv);
							if ($value > 0)
								$arFileID[] = $value;
						}
					else
					{
						$value = intval($v);
						if ($value > 0)
							$arFileID[] = $value;
					}
				}

				// disk file
				if (isset($diskFileColumns[$k]))
				{
					if (is_array($v))
						foreach ($v as $subv)
						{
							$value = intval($subv);
							if ($value > 0)
								$arDiskFileID[] = $value;
						}
					else
					{
						$value = intval($v);
						if ($value > 0)
							$arDiskFileID[] = $value;
					}
				}

				// employee
				if (isset($employeeColumns[$k]))
				{
					if (is_array($v))
						foreach ($v as $subv)
						{
							$value = intval($subv);
							if ($value > 0)
								$arEmployeeID[] = $value;
						}
					else
					{
						$value = intval($v);
						if ($value > 0)
							$arEmployeeID[] = $value;
					}
				}
				
				// crm
				if (isset($crmColumns[$k]))
				{
					$prefixByType = static::getUserFieldCrmTypePrefixMap();

					$maxPrefixLength = 3;    // 'SSI'
					$singleTypePrefix = '';
					if (is_array($columnInfo[$k]['ufInfo']['SETTINGS']))
					{
						$supportedTypes = array();
						foreach ($columnInfo[$k]['ufInfo']['SETTINGS'] as $type => $supported)
						{
							if ($supported === 'Y')
								$supportedTypes[$type] = true;
						}
						$supportedTypes = array_keys($supportedTypes);
						if (count($supportedTypes) === 1)
						{
							if (isset($prefixByType[mb_strtolower($supportedTypes[0])]))
								$singleTypePrefix = $prefixByType[mb_strtolower($supportedTypes[0])];
						}
						unset($supportedTypes, $type, $supported);
					}

					if (is_array($v))
					{
						foreach ($v as $subv)
						{
							$subv = strval($subv);
							if ($subv <> '')
							{
								$prefix = '';
								if (($pos = mb_strpos(mb_substr($subv, 0, $maxPrefixLength + 1), '_')) !== false && $pos > 0)
									$prefix = mb_substr($subv, 0, $pos);
								if (empty($prefix))
									$subv = $singleTypePrefix . '_' . $subv;
								unset($prefix, $pos);

								$value = explode('_', trim($subv));
								if ($value[0] <> '' && $value[1] <> '')
								{
									if (!is_array($arCrmID[$value[0]]))
										$arCrmID[$value[0]] = array();
									$arCrmID[$value[0]][] = $value[1];
								}
							}
						}
					}
					else
					{
						$v = strval($v);
						if ($v <> '')
						{
							$prefix = '';
							if (($pos = mb_strpos(mb_substr($v, 0, $maxPrefixLength + 1), '_')) !== false && $pos > 0)
								$prefix = mb_substr($v, 0, $pos);
							if (empty($prefix))
								$v = $singleTypePrefix . '_' . $v;
							unset($prefix, $pos);

							$value = explode('_', trim($v));
							if ($value[0] <> '' && $value[1] <> '')
							{
								if (!is_array($arCrmID[$value[0]]))
									$arCrmID[$value[0]] = array();
								$arCrmID[$value[0]][] = $value[1];
							}
						}
					}

					unset($maxPrefixLength);
				}

				// crm_status
				if (isset($crmStatusColumns[$k]))
				{
					if (!isset($arCrmStatusEntityType[$k]))
					{
						if (isset($columnInfo[$k]['ufInfo']['SETTINGS']['ENTITY_TYPE']))
						{
							$arCrmStatusEntityType[$k] =
								strval($columnInfo[$k]['ufInfo']['SETTINGS']['ENTITY_TYPE']);
						}
					}
					if (!empty($arCrmStatusEntityType[$k]))
					{
						if (is_array($v))
							foreach ($v as $subv)
							{
								if ($subv <> '')
								{
									if (!is_array($arCrmStatusID[$arCrmStatusEntityType[$k]]))
										$arCrmStatusID[$arCrmStatusEntityType[$k]] = array();
									$arCrmStatusID[$arCrmStatusEntityType[$k]][] = $subv;
								}
							}
						else
						{
							if ($v <> '')
							{
								if (!is_array($arCrmStatusID[$arCrmStatusEntityType[$k]]))
									$arCrmStatusID[$arCrmStatusEntityType[$k]] = array();
								$arCrmStatusID[$arCrmStatusEntityType[$k]][] = $v;
							}
						}
					}
				}

				// iblock_element
				if (isset($iblockElementColumns[$k]))
				{
					if (is_array($v))
						foreach ($v as $subv)
						{
							$value = intval($subv);
							if ($value > 0)
								$arIblockElementID[] = $value;
						}
					else
					{
						$value = intval($v);
						if ($value > 0)
							$arIblockElementID[] = $value;
					}
				}

				// iblock_section
				if (isset($iblockSectionColumns[$k]))
				{
					if (is_array($v))
						foreach ($v as $subv)
						{
							$value = intval($subv);
							if ($value > 0)
								$arIblockSectionID[] = $value;
						}
					else
					{
						$value = intval($v);
						if ($value > 0)
							$arIblockSectionID[] = $value;
					}
				}
			}
		}
	}
	
	// collect files
	if (count($fileColumns) > 0)
	{
		if (count($arFileID) > 0)
			$arFileID = array_unique($arFileID);

		$i = 0;
		$cnt = 0;
		$stepCnt = 500;
		$nIDs = count($arFileID);
		$arID = array();
		$file = new CFile();
		foreach ($arFileID as $fileID)
		{
			$arID[$cnt++] = $fileID;
			$i++;

			if ($cnt === $stepCnt || $i === $nIDs)
			{
				$res = $file->GetList(array(), array('@ID' => implode(',', $arID)));
				if (is_object($res))
				{
					while ($arFile = $res->Fetch())
					{
						if($arFile)
						{
							if(array_key_exists("~src", $arFile))
							{
								if($arFile["~src"])
									$arFile["SRC"] = $arFile["~src"];
								else
									$arFile["SRC"] = $file->GetFileSRC($arFile, false, false);
							}
							else
							{
								$arFile["SRC"] = $file->GetFileSRC($arFile, false);
							}

							self::$ufFiles[intval($arFile['ID'])] = $arFile;
						}
					}
				}

				$cnt = 0;
				$arID = array();
			}
		}
	}

	// collect disk files
	if (count($diskFileColumns) > 0)
	{
		if (count($arDiskFileID) > 0)
			$arDiskFileID = array_unique($arDiskFileID);

		$i = 0;
		$cnt = 0;
		$stepCnt = 500;
		$nIDs = count($arDiskFileID);
		$arID = array();
		foreach ($arDiskFileID as $diskFileID)
		{
			$arID[$cnt++] = $diskFileID;
			$i++;

			if ($cnt === $stepCnt || $i === $nIDs)
			{
				$res = BitrixDiskAttachedObject::getList(array(
					'filter' => array('ID' => $arID),
					'select' => array(
						'ID', 'NAME' => 'OBJECT.NAME', 'SIZE' => 'OBJECT.SIZE'
					),
				));
				$urlManager = BitrixDiskDriver::getInstance()->getUrlManager();
				if (is_object($res))
				{
					while ($arDiskFile = $res->Fetch())
					{
						if($arDiskFile)
						{
							$arDiskFile['DOWNLOAD_URL'] = $urlManager->getUrlUfController(
								'download',
								array('attachedId' => $arDiskFile['ID'])
							);
							self::$ufDiskFiles[intval($arDiskFile['ID'])] = $arDiskFile;
						}
					}
				}

				$cnt = 0;
				$arID = array();
			}
		}
	}

	// collect employees
	if (count($employeeColumns) > 0)
	{
		if (count($arEmployeeID) > 0)
			$arEmployeeID = array_unique($arEmployeeID);

		$i = 0;
		$cnt = 0;
		$stepCnt = 500;
		$nIDs = count($arEmployeeID);
		$arID = array();
		foreach ($arEmployeeID as $employeeID)
		{
			$arID[$cnt++] = $employeeID;
			$i++;

			if ($cnt === $stepCnt || $i === $nIDs)
			{
				$res = BitrixMainUserTable::getList(
					array(
						'filter' => array('ID' => $arID),
						'select' => array('ID', 'LOGIN', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'TITLE')
					)
				);
				if (is_object($res))
				{
					while ($arUser = $res->fetch())
						self::$ufEmployees[intval($arUser['ID'])] = $arUser;
				}

				$cnt = 0;
				$arID = array();
			}
		}
	}

	// collect crm elements
	if (count($crmColumns) > 0 && Loader::includeModule('crm'))
	{
		foreach ($arCrmID as $typeIndex => $arSubID)
		{
			if (count($arSubID) > 0)
				$arCrmID[$typeIndex] = array_unique($arSubID);

			$i = 0;
			$cnt = 0;
			$stepCnt = 500;
			$nIDs = count($arSubID);
			$arID = array();
			foreach ($arSubID as $crmID)
			{
				$arID[$cnt++] = $crmID;
				$i++;

				if ($cnt === $stepCnt || $i === $nIDs)
				{
					$entityTypeName = CCrmOwnerTypeAbbr::ResolveName($typeIndex);
					$settings = [mb_strtoupper($entityTypeName) => 'Y'];
					$selectorParams = ElementType::getDestSelectorParametersForFilter($settings, true);
					$selectorEntityTypeOptions = ElementType::getDestSelectorOptions($selectorParams);
					if ($description = reset($selectorEntityTypeOptions))
					{
						$provider = Entities::getProviderByEntityType(key($selectorEntityTypeOptions));
						if (
							$provider !== false
							&& is_callable([$provider, 'getByIds'])
						)
						{
							$options = (!empty($description['options']) ? $description['options'] : array());
							foreach ($provider->getByIds($arID, $options) as $item)
							{
								self::$ufCrmElements[$typeIndex . '_' . $item['entityId']] = [
									'id' => $item['entityId'],
									'type' => $entityTypeName,
									'title' => htmlspecialcharsback($item['name']) ?? '',
									'url' => $item['url'] ?? '',
								];
							}
						}
					}

					$cnt = 0;
					$arID = array();
				}
			}
		}
	}

	// collect crm statuses
	if (count($crmStatusColumns) > 0 && CModule::IncludeModule('crm'))
	{
		foreach ($arCrmStatusID as $entityType => $arSubID)
		{
			if (count($arSubID) > 0)
				$arCrmID[$entityType] = array_unique($arSubID);

			$res = null;
			$res = CCrmStatus::GetStatusList($entityType);
			if (is_array($res) && count($res) > 0)
			{
				foreach ($arSubID as $crmStatusID)
				{
					if (isset($res[$crmStatusID]))
					if (!isset(self::$ufCrmStatuses[$entityType]))
						self::$ufCrmStatuses[$entityType] = array();
					self::$ufCrmStatuses[$entityType][$crmStatusID] = $res[$crmStatusID];
				}
			}
		}
	}
	
	// collect iblock elements
	if (count($iblockElementColumns) > 0 && CModule::IncludeModule('iblock'))
	{
		if (count($arIblockElementID) > 0)
			$arIblockElementID = array_unique($arIblockElementID);

		$i = 0;
		$cnt = 0;
		$stepCnt = 500;
		$nIDs = count($arIblockElementID);
		$arID = array();
		foreach ($arIblockElementID as $iblockElementID)
		{
			$arID[$cnt++] = $iblockElementID;
			$i++;

			if ($cnt === $stepCnt || $i === $nIDs)
			{
				$res = CIBlockElement::GetList(array('SORT'=>'ASC'), array('=ID' => $arID));
				if (is_object($res))
				{
					while ($arIblockElement = $res->GetNext())
						self::$ufIblockElements[intval($arIblockElement['ID'])] = $arIblockElement;
				}

				$cnt = 0;
				$arID = array();
			}
		}
	}
	
	// collect iblock sections
	if (count($iblockSectionColumns) > 0 && CModule::IncludeModule('iblock'))
	{
		if (count($arIblockSectionID) > 0)
			$arIblockSectionID = array_unique($arIblockSectionID);

		$i = 0;
		$cnt = 0;
		$stepCnt = 500;
		$nIDs = count($arIblockSectionID);
		$arID = array();
		foreach ($arIblockSectionID as $iblockSectionID)
		{
			$arID[$cnt++] = $iblockSectionID;
			$i++;

			if ($cnt === $stepCnt || $i === $nIDs)
			{
				$res = CIBlockSection::GetList(
					array('left_margin' => 'asc'),
					array('ID' => $arID),
					false, array('ID', 'NAME', 'SECTION_PAGE_URL')
				);
				if (is_object($res))
				{
					while ($arIblockSection = $res->GetNext())
						self::$ufIblockSections[intval($arIblockSection['ID'])] = $arIblockSection;
				}

				$cnt = 0;
				$arID = array();
			}
		}
	}
}