CCrmProductRestProxy::getList

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmProductRestProxy
  4. getList
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
  • Класс: \CCrmProductRestProxy
  • Вызов: CCrmProductRestProxy::getList
public function getList($order, $filter, $select, $start)
{
	if(!CModule::IncludeModule('iblock'))
	{
		throw new RestException('Could not load iblock module.');
	}

	if(!CCrmProduct::CheckReadPermission(0))
	{
		throw new RestException('Access denied.');
	}

	$catalogID = CCrmCatalog::GetDefaultID();
	if($catalogID <= 0)
	{
		$result = array();
		$dbResult = new CDBResult();
		$dbResult->InitFromArray($result);
		return CCrmRestService::setNavData($result, $dbResult);
	}

	$navigation = CCrmRestService::getNavData($start);

	if(!is_array($order) || empty($order))
	{
		$order = array('sort' => 'asc', 'id' => 'asc');
	}
	else
	{
		$idInSort = false;
		foreach (array_keys($order) as $by)
		{
			if (mb_strtoupper($by) === 'ID')
			{
				$idInSort = true;
				break;
			}
		}
		if (!$idInSort)
		{
			$order['id'] = 'asc';
		}
		unset($idInSort, $by);
	}

	if(!isset($navigation['bShowAll']))
	{
		$navigation['bShowAll'] = false;
	}

	$enableCatalogData = false;
	$catalogSelect = null;
	$priceSelect = null;
	$vatSelect = null;
	$propertiesSelect = array();

	$selectAll = false;
	if(is_array($select))
	{
		if(!empty($select))
		{
			// Remove '*' for get rid of inefficient construction of price data
			foreach($select as $k => $v)
			{
				if (!is_string($v))
				{
					unset($select[$k]);
					continue;
				}
				if($v === '*')
				{
					$selectAll = true;
					unset($select[$k]);
				}
				else if (preg_match('/^PROPERTY_(\d+|\*)$/', $v))
				{
					$propertiesSelect[] = $v;
					unset($select[$k]);
				}
			}
		}

		if (!empty($propertiesSelect) && empty($select) && !$selectAll)
			$select = array('ID');

		if(empty($select))
		{
			$priceSelect = array('PRICE', 'CURRENCY_ID');
			$vatSelect = array('VAT_ID', 'VAT_INCLUDED', 'MEASURE');
		}
		else
		{
			$priceSelect = array();
			$vatSelect = array();

			$select = CCrmProduct::DistributeProductSelect($select, $priceSelect, $vatSelect);
		}

		$catalogSelect = array_merge($priceSelect, $vatSelect);
		$enableCatalogData = !empty($catalogSelect);
	}

	if (empty($propertiesSelect) && $selectAll)
	{
		$propertiesSelect[] = 'PROPERTY_*';
	}

	$fieldsInfo = $this->getFieldsInfo();
	$this->internalizeFilterFields($filter, $fieldsInfo);

	$filter['CATALOG_ID'] = $catalogID;
	$dbResult = CCrmProduct::GetList($order, $filter, $select, $navigation);
	if(!$enableCatalogData)
	{
		$result = array();
		$fieldsInfo = $this->getFieldsInfo();
		while($fields = $dbResult->Fetch())
		{
			$selectedFields = array();
			if (!empty($select))
			{
				$selectedFields['ID'] = $fields['ID'];
				foreach ($select as $k)
					$selectedFields[$k] = &$fields[$k];
				$fields = &$selectedFields;
			}
			unset($selectedFields);

			$this->getProperties($catalogID, $fields, $propertiesSelect);
			$this->externalizeFields($fields, $fieldsInfo);
			$result[] = $fields;
		}
	}
	else
	{
		$itemMap = array();
		$itemIDs = array();
		while($fields = $dbResult->Fetch())
		{
			$selectedFields = array();
			if (!empty($select))
			{
				$selectedFields['ID'] = $fields['ID'];
				foreach ($select as $k)
					$selectedFields[$k] = &$fields[$k];
				$fields = &$selectedFields;
			}
			unset($selectedFields);

			foreach ($catalogSelect as $fieldName)
			{
				$fields[$fieldName] = null;
			}

			$itemID = isset($fields['ID']) ? intval($fields['ID']) : 0;
			if($itemID > 0)
			{
				$itemIDs[] = $itemID;
				$itemMap[$itemID] = $fields;
			}

		}
		CCrmProduct::ObtainPricesVats($itemMap, $itemIDs, $priceSelect, $vatSelect, true);

		$result = array_values($itemMap);
		$fieldsInfo = $this->getFieldsInfo();
		foreach($result as &$fields)
		{
			$this->getProperties($catalogID, $fields, $propertiesSelect);
			$this->externalizeFields($fields, $fieldsInfo);
		}
		unset($fields);
	}

	return CCrmRestService::setNavData($result, $dbResult);
}

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