Подсчет количества элементов в разделе

Модуль расширенного управления меню для битрикс

Август 14, 2024

Теги: Инфоблоки, ORM

Подсчет количества элементов в разделе у учетом нахождения элемента в нескольких разделах при помощи "\Bitrix\Iblock\SectionElementTable".

Подсчитать количество элементов в разделе с ID=989 инфоблока с ID=10 с учетом активности элементов:


\Bitrix\Main\Loader::includeModule('iblock');


$resultCount \Bitrix\Iblock\SectionElementTable::getList(array(
    
'runtime' => array(
        new 
\Bitrix\Main\ORM\Fields\ExpressionField('COUNT',  'COUNT(*)' )
    ),
    
'filter' => array(
        
'IBLOCK_SECTION_ID' => 989,
        
'IBLOCK_ELEMENT.ACTIVE' => 'Y',
        
'IBLOCK_ELEMENT.IBLOCK_ID' => 10,
    ),
    
'select' => array(
        
'COUNT'
    
),
));

$count 0;
if(
$countElement $resultCount->fetch())
    
$count = (int)$countElement['COUNT'];

функция агента для подсчета количества активных элементов в разделах (с учетом вложенности и без) и сохранения его в свойствах раздела "UF_ELEMENT_COUNT","UF_ELEMENT_RECURSIVE_COUNT" в инфоблоке с ID=10:


<?php


namespace Partner;

class 
SectionElementCount 
{
    public static function 
saveElementCountAgent()
    {
        
Loader::includeModule('iblock');
        
$iblockId 10;
        
$stack = array();
        
$entity \Bitrix\Iblock\Model\Section::compileEntityByIblock($iblockId);
        
$rsSection $entity::getList(array(
            
'order' => array(
                
'DEPTH_LEVEL' => 'DESC',
            ),
            
'filter' => array(
                
'IBLOCK_ID' => $iblockId,
                
'ACTIVE' => 'Y',
            ), 
            
'select' =>  array(
                
'ID',
                
'DEPTH_LEVEL',
                
'IBLOCK_SECTION_ID',
                
'UF_ELEMENT_COUNT',
                
'UF_ELEMENT_RECURSIVE_COUNT',
            ),
        ));
        
        while (
$section=$rsSection->fetch()) 
        {            
            
$resultCount \Bitrix\Iblock\SectionElementTable::getList(array(
                
'runtime' => array(
                    new 
\Bitrix\Main\ORM\Fields\ExpressionField('COUNT',  'COUNT(*)' )
                ),
                
'filter' => array(
                    
'IBLOCK_SECTION_ID' => $section['ID'],
                    
'IBLOCK_ELEMENT.ACTIVE' => 'Y',
                    
'IBLOCK_ELEMENT.IBLOCK_ID' => $iblockId,
                ),
                
'select' => array(
                    
'COUNT'
                
),
            ));
            
            
$count 0;
            if(
$elementCount=$resultCount->fetch())
                
$count = (int)$elementCount['COUNT'];

            if(!
array_key_exists((int)$section['IBLOCK_SECTION_ID'],$stack))
                
$stack[(int)$section['IBLOCK_SECTION_ID']] = array('count'=>0,'recursiveCount'=>0,'subsections'=>0);
            if(!
array_key_exists((int)$section['ID'],$stack))
                
$stack[(int)$section['ID']] = array('count'=>0,'recursiveCount'=>0,'subsections'=>0);

            
$stack[(int)$section['IBLOCK_SECTION_ID']]['subsections']++;
            
$stack[(int)$section['ID']]['count'] = $count;
            
$stack[(int)$section['ID']]['section'] = $section;            
        }        

        
$parentChain = array();
        
$c 0;
        
$getParentId = function($section,$chain=array()) use (&$getParentId,$stack,&$c)
        {
            if(!(
$parentSectionId=(int)$section['section']['IBLOCK_SECTION_ID']) || !($parentSection=$stack[$parentSectionId]))
                return 
$chain;
            
$c++;
            if(
$c>20)
                return;
            
$chain[] = $parentSectionId;
            
$chain $getParentId($parentSection,$chain);
            return 
$chain;
        };

        foreach(
$stack as $sectionId=>$section)
        {
            
$c=0;
            
$stack[$sectionId]['recursiveCount'] += $section['count'];
            
$parentIds $getParentId($section);
            foreach(
$parentIds as $parentId)
            {
                
$stack[$parentId]['recursiveCount'] += $section['count'];
            }
        }

        foreach(
$stack as $sectionId=>$section)
        {
            
$sectionUpdateFields = array();
            if(
$section['count']!=(int)$section['section']['UF_ELEMENT_COUNT'])
                
$sectionUpdateFields['UF_ELEMENT_COUNT'] = $section['count'];
            if(
$section['recursiveCount']!=(int)$section['section']['UF_ELEMENT_COUNT'])
                
$sectionUpdateFields['UF_ELEMENT_RECURSIVE_COUNT'] = $section['recursiveCount'];
            if(
$sectionUpdateFields)
            {
                
$sectionUpdate = new \CIBlockSection;
                
$sectionUpdate->update($sectionId,$sectionUpdateFields);
            }
        }

        return 
'\\'.__METHOD__.'();';
    }
}

← Переход к списку