Example #1
0
 public static function iblock($primary)
 {
     if (!$primary) {
         throw new ArgumentException('Не указан идентификатор инфоблока');
     }
     $cache = new \CPHPCache();
     $path = self::createPath(__METHOD__);
     $cacheId = md5($primary);
     if ($cache->InitCache(86400, $cacheId, $path)) {
         $iblock = $cache->GetVars();
     } else {
         $field = is_numeric($primary) ? 'ID' : 'CODE';
         $db = IblockTable::query()->addFilter($field, $primary)->setSelect(array('*'))->exec();
         if ($db->getSelectedRowsCount() == 0) {
             $cache->AbortDataCache();
             throw new ArgumentException('Указан идентификатор несуществующего инфоблока');
         } elseif ($db->getSelectedRowsCount() > 1) {
             $cache->AbortDataCache();
             throw new ArgumentException("Существует {$db->getSelectedRowsCount()} инфоблока(ов) с {$field} = {$primary}");
         }
         $iblock = $db->fetch();
         if ($cache->StartDataCache()) {
             $cache->EndDataCache($iblock);
         }
     }
     return $iblock;
 }
Example #2
0
 /**
  * Validation code of the info block. If code not valid (empty string or code alredy used) will be throw
  * Bitrix exception.
  *
  * @param string $type
  * @param string $code
  * @param null $iblockId
  *
  * @return bool
  */
 protected static function validateCode($type, $code, $iblockId = null)
 {
     global $APPLICATION;
     if (is_null($code)) {
         // if code of info block is not updated
         return true;
     }
     try {
         $type = trim($type);
         $code = trim($code);
         if (strlen($code) <= 0) {
             throw new \Exception('EMPTY_CODE');
         }
         $rsSimilarIblock = IblockTable::query()->setFilter(['IBLOCK_TYPE_ID' => $type, 'CODE' => $code, '!ID' => $iblockId])->setSelect(['ID'])->exec();
         if ($rsSimilarIblock->getSelectedRowsCount() > 0) {
             throw new \Exception('CODE_ALREDY_USED');
         }
         return true;
     } catch (\Exception $e) {
         Loc::loadMessages(__FILE__);
         $APPLICATION->ThrowException(Loc::getMessage('BEX_TOOLS_IBLOCK_' . $e->getMessage()));
         return false;
     }
 }
Example #3
0
 /**
  * @see IblockFinder::getItems()
  *
  * @return array
  *
  * @throws ValueNotFoundException
  * @throws ArgumentException
  */
 protected function getItemsIblockShard()
 {
     $items = [];
     $rsIblocks = IblockTable::query()->setFilter(['ID' => $this->id])->setSelect(['IBLOCK_TYPE_ID', 'CODE'])->exec();
     if ($iblock = $rsIblocks->fetch()) {
         if ($iblock['CODE']) {
             $items['CODE'] = $iblock['CODE'];
         }
         $items['TYPE'] = $iblock['IBLOCK_TYPE_ID'];
     }
     if (empty($items)) {
         throw new ValueNotFoundException('Iblock', 'ID #' . $this->id);
     }
     $propIds = [];
     $rsProps = PropertyTable::query()->setFilter(['IBLOCK_ID' => $this->id])->setSelect(['ID', 'CODE', 'IBLOCK_ID'])->exec();
     while ($prop = $rsProps->fetch()) {
         $propIds[] = $prop['ID'];
         $items['PROPS_ID'][$prop['CODE']] = $prop['ID'];
     }
     if (!empty($propIds)) {
         $rsPropsEnum = PropertyEnumerationTable::query()->setFilter(['PROPERTY_ID' => $propIds])->setSelect(['ID', 'XML_ID', 'PROPERTY_ID', 'PROPERTY_CODE' => 'PROPERTY.CODE'])->exec();
         while ($propEnum = $rsPropsEnum->fetch()) {
             if ($propEnum['PROPERTY_CODE']) {
                 $items['PROPS_ENUM_ID'][$propEnum['PROPERTY_CODE']][$propEnum['XML_ID']] = $propEnum['ID'];
             }
         }
     }
     $this->registerCacheTag('bex_iblock_' . $this->id);
     return $items;
 }