CAllCrmCatalog::CreateCatalog

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmCatalog
  4. CreateCatalog
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_catalog.php
  • Класс: \CAllCrmCatalog
  • Вызов: CAllCrmCatalog::CreateCatalog
static function CreateCatalog($originatorID = '', $name = '', $siteID = null)
{
	if(!is_string($originatorID) || $originatorID == '')
	{
		$originatorID = null;
	}

	if ($siteID == null)
	{
		$siteID = SITE_ID;
	}

	$langID = LANGUAGE_ID;
	$dbSite = CSite::GetById($siteID);
	if ($arSite = $dbSite->Fetch())
	{
		$langID = $arSite['LANGUAGE_ID'];
	}

	//check type type
	$typeID = self::GetCatalogTypeID();
	//$rsIBlockTypes = CIBlockType::GetByID($typeID); // CIBlockType::GetByID() is unstable
	$rsIBlockTypes = CIBlockType::GetList(array(), array("=ID" => $typeID));
	if (!$rsIBlockTypes->Fetch())
	{
		$iblocktype = new CIBlockType();

		$result = $iblocktype->Add(
			array(
				'ID' => $typeID,
				'SECTIONS' => 'Y',
				'IN_RSS'=>'N',
				'SORT' => 100,
				'LANG' => array(
					$langID => array(
						'NAME' => GetMessage('CRM_PRODUCT_CATALOG_TYPE_TITLE'),
						'SECTION_NAME'=> GetMessage('CRM_PRODUCT_CATALOG_SECTION_NAME'),
						'ELEMENT_NAME'=> GetMessage('CRM_PRODUCT_CATALOG_PRODUCT_NAME')
					)
				)
			)
		);

		if(!$result)
		{
			self::RegisterError($iblocktype->LAST_ERROR);
			return false;
		}
	}

	$catalogTitle = ($name != '' ? $name : GetMessage('CRM_PRODUCT_CATALOG_TITLE'));
	$offersTitle = GetMessage(
		'CRM_PRODUCT_CATALOG_OFFERS_TITLE_FORMAT',
		['#CATALOG#' => $catalogTitle]
	);

	$fields = \CIBlock::GetFieldsDefaults();

	$code = $fields['CODE'];
	$code['DEFAULT_VALUE'] = unserialize($code['DEFAULT_VALUE'], ['allowed_classes' => false]);
	$code['DEFAULT_VALUE']['TRANSLITERATION'] = 'Y';
	$code['DEFAULT_VALUE']['USE_GOOGLE'] = 'N';
	$code['DEFAULT_VALUE']['TRANS_LEN'] = 255;

	$sectionCode = $fields['SECTION_CODE'];
	$sectionCode['DEFAULT_VALUE'] = unserialize($sectionCode['DEFAULT_VALUE'], ['allowed_classes' => false]);
	$sectionCode['DEFAULT_VALUE']['TRANSLITERATION'] = 'Y';
	$sectionCode['DEFAULT_VALUE']['USE_GOOGLE'] = 'N';
	$sectionCode['DEFAULT_VALUE']['TRANS_LEN'] = 255;

	$fields['CODE'] = $code;
	$fields['SECTION_CODE'] = $sectionCode;
	unset($sectionCode, $code);

	//creation of iblock
	$iblock = new CIBlock();
	$iblockID = $iblock->Add(
		array(
			'NAME' => $catalogTitle,
			'ACTIVE' => 'Y',
			'IBLOCK_TYPE_ID' => $typeID,
			'LID' => $siteID,
			'SORT' => 100,
			'XML_ID' => 'crm_external_'.$originatorID,
			'INDEX_ELEMENT' => 'N',
			'WORKFLOW' => 'N',
			'BIZPROC' => 'N',
			'VERSION' => 1,
			'GROUP_ID' => array(1 => 'X', 2 => 'R'),
			'LIST_MODE' => Iblock\IblockTable::LIST_MODE_COMBINED,
			'FIELDS' => $fields
		)
	);

	if($iblockID === false)
	{
		self::RegisterError($iblock->LAST_ERROR);
		return false;
	}

	self::createMorePhoto($iblockID);

	//creation of catalog
	$result = CCrmCatalog::Add(
		array
		(
			'ID' => $iblockID,
			'ORIGINATOR_ID' => $originatorID
		)
	);

	if($result === false)
	{
		self::RegisterError('Catalog creation error');
		return false;
	}

	if (Loader::includeModule('catalog'))
	{
		$offersId = $iblock->Add(
			[
				'NAME' => $offersTitle,
				'ACTIVE' => 'Y',
				'IBLOCK_TYPE_ID' => $typeID,
				'LID' => $siteID,
				'SORT' => 200,
				'XML_ID' => 'crm_external_offers_'.$originatorID,
				'INDEX_ELEMENT' => 'N',
				'WORKFLOW' => 'N',
				'BIZPROC' => 'N',
				'VERSION' => 1,
				'GROUP_ID' => array(1 => 'X', 2 => 'R'),
				'LIST_MODE' => 'S',
				'FIELDS' => $fields
			]
		);
		if ($offersId === false)
		{
			self::RegisterError($iblock->LAST_ERROR);
			return false;
		}

		$propertyId = \CIBlockPropertyTools::createProperty(
			$offersId,
			\CIBlockPropertyTools::CODE_SKU_LINK,
			['LINK_IBLOCK_ID' => $iblockID]
		);
		if (!$propertyId)
		{
			foreach (CIBlockPropertyTools::getErrors() as $propertyError)
				self::RegisterError($propertyError);
			return false;
		}

		self::createMorePhoto($offersId);

		$offersFields = [
			'IBLOCK_ID' => $offersId,
			'PRODUCT_IBLOCK_ID' => $iblockID,
			'SKU_PROPERTY_ID' => $propertyId
		];
		// get default vat
		$iterator = Catalog\VatTable::getList([
			'select' => ['ID', 'SORT'],
			'order' => ['SORT' => 'ASC'],
			'limit' => 1
		]);
		$row = $iterator->fetch();
		unset($iterator);
		if (!empty($row))
			$offersFields['VAT_ID'] = (int)$row['ID'];
		unset($row);

		if (!\CCatalog::Add($offersFields))
		{
			self::RegisterError(GetMessage('CRM_ERR_REGISTER_OFFERS'));
			return false;
		}
	}

	return $iblockID;
}

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