CrmLeads::getData

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CrmLeads
  4. getData
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/integration/main/uiselector/crmleads.php
  • Класс: Bitrix\Crm\Integration\Main\UISelector\CrmLeads
  • Вызов: CrmLeads::getData
public function getData($params = [])
{
	$entityType = Handler::ENTITY_TYPE_CRMLEADS;

	$result = [
		'ITEMS' => [],
		'ITEMS_LAST' => [],
		'ITEMS_HIDDEN' => [],
		'ADDITIONAL_INFO' => [
			'GROUPS_LIST' => [
				'crmleads' => [
					'TITLE' => Loc::getMessage('MAIN_UI_SELECTOR_TITLE_CRMLEADS'),
					'TYPE_LIST' => [ $entityType ],
					'DESC_LESS_MODE' => 'N',
					'SORT' => 40
				]
			],
			'SORT_SELECTED' => 400
		]
	];

	$entityOptions = (!empty($params['options']) ? $params['options'] : []);
	$prefix = static::getPrefix($entityOptions);

	$lastItems = (!empty($params['lastItems']) ? $params['lastItems'] : []);
	$selectedItems = (!empty($params['selectedItems']) ? $params['selectedItems'] : []);

	$lastEntitiesIdList = [];
	if(!empty($lastItems[$entityType]))
	{
		$result["ITEMS_LAST"] = array_map(
			function($code) use ($prefix)
			{
				return preg_replace('/^'.self::PREFIX_FULL . '(\d+)$/', $prefix . '$1', $code);
			},
			array_values($lastItems[$entityType])
		);
		foreach ($lastItems[$entityType] as $value)
		{
			$lastEntitiesIdList[] = str_replace(self::PREFIX_FULL, '', $value);
		}
	}
	if(!empty($lastItems[$entityType . '_MULTI']))
	{
		$result["ITEMS_LAST"] = array_merge(
			$result["ITEMS_LAST"],
			array_map(
				function($code) use ($prefix)
				{
					return preg_replace_callback(
						'/^'.self::PREFIX_FULL . '(\d+)( . +)$/',
						function($matches) use ($prefix)
						{
							return $prefix . $matches[1].mb_strtolower($matches[2]);
						},
						$code
					);
				},
				array_values($lastItems[$entityType . '_MULTI'])
			)
		);
		foreach ($lastItems[$entityType . '_MULTI'] as $value)
		{
			$lastEntitiesIdList[] = preg_replace('/^'.self::PREFIX_FULL . '(\d+)(:([A-F0-9]{8}))$/', '$1', $value);
		}
	}

	$selectedEntitiesIdList = [];

	if(!empty($selectedItems[$entityType]))
	{
		foreach ($selectedItems[$entityType] as $value)
		{
			$selectedEntitiesIdList[] = str_replace($prefix, '', $value);
		}
	}
	if(!empty($selectedItems[$entityType . '_MULTI']))
	{
		foreach ($selectedItems[$entityType . '_MULTI'] as $value)
		{
			$selectedEntitiesIdList[] =
				preg_replace('/^'.self::PREFIX_FULL . '(\d+)(:([a-fA-F0-9]{8}))$/', '$1', $value)
			;
		}
	}

	$entitiesIdList = array_merge($selectedEntitiesIdList, $lastEntitiesIdList);
	$entitiesIdList = array_slice($entitiesIdList, 0, max(count($selectedEntitiesIdList), 20));
	$entitiesIdList = array_unique($entitiesIdList);

	$entitiesList = [];

	$filter = ['CHECK_PERMISSIONS' => 'Y'];
	$order = [];
	$select = $this->getSearchSelect();

	if (!empty($entitiesIdList))
	{
		$filter['ID'] = $entitiesIdList;
		$navParams = false;
	}
	else
	{
		$order = ['ID' => 'DESC'];
		$navParams = [ 'nTopCount' => 10 ];
	}

	if (
		isset($entityOptions['onlyWithEmail'])
		&& $entityOptions['onlyWithEmail'] == 'Y'
	)
	{
		$filter['=HAS_EMAIL'] = 'Y';
	}

	$res = CCrmLead::getListEx(
		$order,
		$filter,
		false,
		$navParams,
		$select
	);

	while ($entityFields = $res->fetch())
	{
		$entitiesList[$prefix . $entityFields['ID']] = static::prepareEntity($entityFields, $entityOptions);
	}

	if (
		!empty($entitiesIdList)
		&& count($entitiesList) < 3
	)
	{
		unset($filter['ID']);
		$res = CCrmLead::getListEx(
			[ 'ID' => 'DESC' ],
			$filter,
			false,
			[ 'nTopCount' => 10 ],
			$select
		);

		while ($entityFields = $res->fetch())
		{
			if (!isset($entitiesList[$prefix . $entityFields['ID']]))
			{
				$entitiesList[$prefix . $entityFields['ID']] = static::prepareEntity($entityFields, $entityOptions);
			}
		}
	}

	$entitiesList = static::processMultiFields($entitiesList, $entityOptions);

	if (empty($lastEntitiesIdList))
	{
		$result["ITEMS_LAST"] = array_keys($entitiesList);
	}

	$result['ITEMS'] = $entitiesList;

	if (!empty($selectedItems[$entityType]))
	{
		$hiddenItemsList = array_diff($selectedItems[$entityType], array_keys($entitiesList));
		$hiddenItemsList = array_map(
			function($code) use ($prefix)
			{
				return preg_replace('/^' . $prefix . '(\d+)$/', '$1', $code);
			},
			$hiddenItemsList
		);

		if (!empty($hiddenItemsList))
		{
			$filter = [
				'@ID' => $hiddenItemsList,
				'CHECK_PERMISSIONS' => 'N'
			];

			if (
				isset($entityOptions['onlyWithEmail'])
				&& $entityOptions['onlyWithEmail'] == 'Y'
			)
			{
				$filter['=HAS_EMAIL'] = 'Y';
			}

			$res = CCrmLead::getListEx(
				[],
				$filter,
				false,
				false,
				['ID']
			);
			while($entityFields = $res->fetch())
			{
				$result['ITEMS_HIDDEN'][] = $prefix . $entityFields["ID"];
			}
		}
	}

	return $result;
}

Добавить комментарий