Skip to content

Instantly share code, notes, and snippets.

@syntheticsh
Created October 30, 2018 08:02
Show Gist options
  • Select an option

  • Save syntheticsh/c9e86cffdcff2c9ecdd0c67492fe3c72 to your computer and use it in GitHub Desktop.

Select an option

Save syntheticsh/c9e86cffdcff2c9ecdd0c67492fe3c72 to your computer and use it in GitHub Desktop.
[1c-bitrix] Инфоблок по коду
<?php
if (!function_exists("GetIBlockIDByCode")) {
function GetIBlockIDByCode($ibCode, $ibType) {
global $stackCacheManager;
if (!CModule::IncludeModule("iblock")) {
return false;
}
if (strlen($ibType) < 1 || strlen($ibCode) < 1) {
return false;
}
// Let's use the standard stack cache object
if (isset($stackCacheManager) && is_object($stackCacheManager)) {
$szCacheName = __METHOD__;
// Set the maximum number of elements in the result
$stackCacheManager->SetLength($szCacheName, 600);
// And the period of time during which the cache is considered as valid
$stackCacheManager->SetTTL($szCacheName, 86400);
// Just a utility constant
$szCacheID = "IBLOCKS_ID_BY_CODE".$ibType.$ibCode.SITE_ID;
// If necessary information is already in the cache
if ($stackCacheManager->Exist($szCacheName, $szCacheID)) {
$arResult = $stackCacheManager->Get($szCacheName, $szCacheID);
return (intval($arResult['ID']) > 0) ? intval($arResult['ID']) : false;
} else { // Otherwise get it
$rsIBlock = CIBlock::GetList(
array("SORT" => "ASC"),
array("TYPE" => $ibType, "CODE" => $ibCode, "ACTIVE" => 'Y', 'CHECK_PERMISSIONS' => 'N', "SITE_ID" => SITE_ID)
);
if (is_object($rsIBlock) && $arIBlock = $rsIBlock->GetNext()) {
// And save into the cache
$stackCacheManager->Set(
$szCacheName, $szCacheID, array("ID" => intval($arIBlock['ID']))
);
return intval($arIBlock['ID']);
}
}
} else {
// In case there is no existed cache object (which is extremely weird)
$rsIBlock = CIBlock::GetList(
array("SORT" => "ASC"), array("TYPE" => $ibType, "CODE" => $ibCode, "ACTIVE" => 'Y', 'CHECK_PERMISSIONS' => 'N', "SITE_ID" => SITE_ID)
);
if (is_object($rsIBlock) && $arIBlock = $rsIBlock->GetNext()) {
return intval($arIBlock['ID']);
}
}
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment