component.php 1.91 KB
<?
use \Cpeople\Classes\Block;

if ($this->StartResultCache(COMPONENTS_CACHE_TTL))
{
    /** @var $item \SH\Entities\Partner */
    /** @var $type \SH\Entities\PartnerType */

    $arResult['ITEMS'] = Block\Getter::instance()
        ->setFilter(array('IBLOCK_ID' => IBLOCK_PARTNERS, 'ACTIVE' => 'Y'))
        ->setOrder(array('SORT' => 'ASC', 'NAME' => 'ASC'))
        ->setClassName('\SH\Entities\Partner')
        ->get();

    $arResult['TYPES'] = Block\Getter::instance()
        ->setFilter(array('IBLOCK_ID' => IBLOCK_PARTNERS_TYPE, 'ACTIVE' => 'Y'))
        ->setOrder(array('SORT' => 'ASC', 'NAME' => 'ASC'))
        ->setClassName('\SH\Entities\PartnerType')
        ->get();

    $arResult['SEPARATE'] = array();
    $arResult['SEPARATE_BY_ID'] = array();
    foreach($arResult['TYPES'] as $i => $type) {
        if(!$type->isSeparate()) continue;

        $arResult['SEPARATE_BY_ID'][$type->ID] = $arResult['TYPES'][$i];
        $arResult['SEPARATE'][] = $arResult['SEPARATE_BY_ID'][$type->ID];
        $arResult['SEPARATE_BY_ID'][$type->ID]->items = array();
        unset($arResult['TYPES'][$i]);
    }

    foreach($arResult['ITEMS'] as $i => $item) {
        $item->typesId = $item->getPartnersTypeIds();

        $isSeparated = false;
        foreach($item->typesId as $j => $typeId) {
            if(!isset($arResult['SEPARATE_BY_ID'][$typeId])) continue;

            $arResult['SEPARATE_BY_ID'][$typeId]->items[] = $arResult['ITEMS'][$i];
            unset($item->typesId[$j]);
            $isSeparated = true;
        }

        if(!$item->typesId) {
            if($isSeparated) {
                unset($arResult['ITEMS'][$i]);
            } else {
                $item->typesId = array(0);
            }
        }
    }

    $arResult['ITEMS'] = array_values($arResult['ITEMS']);

    $this->SetResultCacheKeys(array());
    $this->IncludeComponentTemplate();
}

$APPLICATION->SetPageProperty('CONTAINER_EXTRA_CLASS', '_gray');