├── LICENSE ├── README.md └── iblock_tools.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Alexandr Yakovlev and other contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Bitrix IBlock Tools 2 | =================== 3 | 4 | Представляет класс CIBlockTools для CMS "1С-Битрикс", позволяющий без обращения к БД: 5 | 6 | 1. Получить ID инфоблока по его коду (CODE), 7 | 2. Получить ID свойства инфоблока по его коду (CODE), 8 | 3. Получить ID значения свойства типа "список" по его XML_ID. 9 | 10 | Класс хранит информацию о инфоблоках и их свойствах в кэше битрикса. 11 | При любом изменении инфоблоков, свойств инфоблоков данный кэш автоматически обновляется. 12 | 13 | Использование 14 | ------------- 15 | 16 | Для использования достаточно подключить iblock_tools.php в /bitrix/php_interface/init.php 17 | 18 | `````php 19 | // Получение ID инфоблока по коду 20 | $iblockId = CIBlockTools::GetIBlockId('код инфоблока'); 21 | 22 | // Получение ID свойства по коду инфоблока и коду свойства 23 | $propertyId = CIBlockTools::GetPropertyId('код инфоблока', 'код свойства'); 24 | 25 | // Получение ID значение свойства типа "список" 26 | // по коду инфоблока, коду свойства и XML_ID значения свойства 27 | $propertyValueId = CIBlockTools::GetPropertyEnumValueId('код инфоблока', 'код свойства', 'XML_ID значения свойства'); 28 | ````` -------------------------------------------------------------------------------- /iblock_tools.php: -------------------------------------------------------------------------------- 1 | GetIBlockIdPr($iblockCode); 31 | } 32 | 33 | /** 34 | * Get IBlock property ID 35 | * @param string $iblockCode IBlock CODE 36 | * @param string $propCode Property CODE 37 | * @return integer|null 38 | */ 39 | public static function GetPropertyId($iblockCode, $propCode){ 40 | return self::Init()->GetPropertyIdPr($iblockCode, $propCode); 41 | } 42 | 43 | /** 44 | * Get IBlock property enum value ID 45 | * @param string $iblockCode IBlock CODE 46 | * @param string $propCode Property CODE 47 | * @param string $xmlId Property value XML_ID 48 | * @return integer|null 49 | */ 50 | public static function GetPropertyEnumValueId($iblockCode, $propCode, $xmlId){ 51 | return self::Init()->GetPropertyEnumValueIdPr($iblockCode, $propCode, $xmlId); 52 | } 53 | 54 | /** 55 | * Clear service cache 56 | * @return boolean 57 | */ 58 | public static function Update(){ 59 | $obCache = new CPHPCache(); 60 | $obCache->CleanDir('/'.self::$cacheKey.'/'); 61 | return true; 62 | } 63 | 64 | private function __construct() { 65 | $cache = new CPHPCache(); 66 | $cache_time = 2592000; // month 67 | $cache_id = self::$cacheKey; 68 | $cache_path = '/'.self::$cacheKey.'/'; 69 | 70 | if($cache->InitCache($cache_time, $cache_id, $cache_path)) 71 | { 72 | $vars = $cache->GetVars(); 73 | $this->arIBlockIds = $vars['arIBlockIds']; 74 | $this->arPropertyIds = $vars['arPropertyIds']; 75 | $this->arPropertyValueIds = $vars['arPropertyValueIds']; 76 | } 77 | else 78 | { 79 | $cache->StartDataCache($cache_time, $cache_id, $cache_path); 80 | 81 | $this->SetIBlocks(); 82 | $this->SetProperties(); 83 | 84 | $cache->EndDataCache(array( 85 | 'arIBlockIds' => $this->arIBlockIds, 86 | 'arPropertyIds' => $this->arPropertyIds, 87 | 'arPropertyValueIds' => $this->arPropertyValueIds, 88 | )); 89 | } 90 | } 91 | 92 | private function SetIBlocks(){ 93 | $this->arIBlockIds = array(); 94 | 95 | if(!CModule::IncludeModule("iblock")) return; 96 | 97 | $db = CIBlock::GetList( 98 | array('ID' => 'ASC'), 99 | array( 100 | 'ACTIVE' => 'Y', 101 | 'CHECK_PERMISSIONS' => 'N' 102 | ) 103 | ); 104 | while($arr = $db->Fetch()){ 105 | if($arr['CODE']){ 106 | $this->arIBlockIds[$arr['CODE']] = intval($arr['ID']); 107 | } 108 | } 109 | } 110 | 111 | private function SetProperties(){ 112 | $this->arPropertyIds = array(); 113 | $this->arPropertyValueIds = array(); 114 | 115 | if(!CModule::IncludeModule("iblock")) return; 116 | 117 | $db = CIBlockProperty::GetList( 118 | array(), 119 | array('ACTIVE' => 'Y') 120 | ); 121 | while($arr = $db->Fetch()){ 122 | if(is_null($this->arPropertyIds[$arr['IBLOCK_ID']])) 123 | $this->arPropertyIds[$arr['IBLOCK_ID']] = array(); 124 | 125 | if($arr['CODE']){ 126 | $this->arPropertyIds[$arr['IBLOCK_ID']][$arr['CODE']] = intval($arr['ID']); 127 | 128 | if($arr['PROPERTY_TYPE'] == 'L'){ 129 | if(is_null($this->arPropertyValueIds[$arr['ID']])) 130 | $this->arPropertyValueIds[$arr['ID']] = array(); 131 | 132 | $resProp = CIBlockPropertyEnum::GetList( 133 | array(), 134 | array('PROPERTY_ID' => $arr['ID']) 135 | ); 136 | while($arrProp=$resProp->Fetch()){ 137 | if($arrProp['XML_ID']){ 138 | $this->arPropertyValueIds[$arr['ID']][$arrProp['XML_ID']] = intval($arrProp['ID']); 139 | } 140 | } 141 | } 142 | } 143 | } 144 | } 145 | 146 | private function GetIBlockIdPr($iblockCode){ 147 | if(isset($this->arIBlockIds[$iblockCode])) 148 | return $this->arIBlockIds[$iblockCode]; 149 | 150 | return null; 151 | } 152 | 153 | private function GetPropertyIdPr($iblockCode, $propCode){ 154 | $iblockId = $this->GetIBlockId($iblockCode); 155 | if(!$iblockId) return null; 156 | 157 | if(isset($this->arPropertyIds[$iblockId]) && isset($this->arPropertyIds[$iblockId][$propCode])) 158 | return $this->arPropertyIds[$iblockId][$propCode]; 159 | 160 | return null; 161 | } 162 | 163 | private function GetPropertyEnumValueIdPr($iblockCode, $propCode, $xmlId){ 164 | $propId = $this->GetPropertyId($iblockCode, $propCode); 165 | if(!$propId) return null; 166 | 167 | if(isset($this->arPropertyValueIds[$propId]) && isset($this->arPropertyValueIds[$propId][$xmlId])) 168 | return $this->arPropertyValueIds[$propId][$xmlId]; 169 | 170 | return null; 171 | } 172 | } 173 | 174 | // IBlock events 175 | AddEventHandler('iblock', 'OnAfterIBlockAdd', array('CIBlockTools', 'Update')); 176 | AddEventHandler('iblock', 'OnAfterIBlockUpdate', array('CIBlockTools', 'Update')); 177 | AddEventHandler('iblock', 'OnBeforeIBlockDelete', array('CIBlockTools', 'Update')); 178 | 179 | // IBlock property events 180 | AddEventHandler('iblock', 'OnAfterIBlockPropertyAdd', array('CIBlockTools', 'Update')); 181 | AddEventHandler('iblock', 'OnAfterIBlockPropertyUpdate', array('CIBlockTools', 'Update')); 182 | AddEventHandler('iblock', 'OnBeforeIBlockPropertyDelete', array('CIBlockTools', 'Update')); 183 | --------------------------------------------------------------------------------