Relink block * @copyright Tamaranga */ class RelinkBlock extends Block { /** @var FilterBlock */ public $filter; /** @var int Кеширование */ public $countersCache = 60; public function init() { parent::init(); $this->setTemplate('search/relink.block', 'listings'); } public function data() { if (! Listings::geoFilterEnabled()) { return false; } if ($this->filter->hasSearchQuery()) { return false; } $data = parent::data(); $model = Listings::model(); $geo = Geo::filter(); # выбраный регион $coveringType = Geo::coveringType(); # тип покрытия $geoTitle = ''; # название региона, если есть if (! empty($geo['title']) && $coveringType != Geo::COVERING_CITY) { $geoTitle = ' ' . (!empty($geo['declension']) ? $geo['declension']['where'] : $geo['title']); } # категории $cats = [ Listings::CATS_ROOTID => [ 't' => _t('listings', 'Main categories[region]', ['region' => $geoTitle]) ] ]; if (! empty($this->filter->categoryData['crumbs'])) { foreach ($this->filter->categoryData['crumbs'] as $v) { if (! $v['id']) { continue; } if ($this->filter->categoryData['id'] == $v['id'] && empty($this->filter->categoryData['subs'])) { continue; } $cats[ $v['id'] ] = ['t' => $v['title']]; /* .$geoTitle */ } } $catIDs = []; foreach ($cats as $k => &$v) { $v['data'] = $model->catsDataByFilter( ['pid' => $k, 'enabled' => 1], ['id','pid','keyword','landing_url','title'], ['cache' => 60] ); foreach ($v['data'] as &$vv) { $vv['link'] = Listings::url('items.search', ['keyword' => $vv['keyword'], 'landing_url' => $vv['landing_url']]); //$vv['title'] .= $geoTitle; $catIDs[] = $vv['id']; } unset($vv); } unset($v); # посчитаем количество объявлений в категориях $catsItems = $model->catsItemsCountersByID($catIDs, $geo); # удалим категории в которых нет объявлений foreach ($cats as $k => &$v) { foreach ($v['data'] as $kk => &$vv) { if (empty($catsItems[ $vv['id'] ])) { unset($v['data'][$kk]); } } unset($vv); } unset($v); $data['cats'] = &$cats; # регионы $linkParam = [ 'keyword' => $this->filter->categoryData['keyword'], 'landing_url' => $this->filter->categoryData['landing_url'], ]; $coveringRegion = Geo::coveringRegion(); if ($geo['id']) { # есть фильтр по региону if (empty($geo['city'])) { # выбрана страна или регион, выведем регионы или города $regionsFilter = [ 'cat_id' => $this->filter->categoryData['id'], 'numlevel' => $geo['numlevel'] + 1, 'numleft' => ['>', $geo['numleft']], 'numright' => ['<', $geo['numright']], ]; $data['regs'] = $model->regionsItemsCounters($regionsFilter); foreach ($data['regs'] as &$v) { $v['link'] = Listings::url('items.search', array_merge($linkParam, $v['url'] ?? [])); } unset($v); } $crumb = []; # хлебные крошки для выбранного региона if (! empty($geo['parents'])) { $reg = []; foreach ($geo['parents'] as $v) { switch ($coveringType) { case Geo::COVERING_COUNTRY: if ($v['numlevel'] == 1) { continue 2; } case Geo::COVERING_REGION: if ($v['id'] == $coveringRegion) { continue 2; } break; case Geo::COVERING_CITIES: case Geo::COVERING_CITY: continue 2; } $reg[] = $v['id']; } if (! empty($reg)) { $reg = Geo::model()->regionsListing(['id' => $reg], ['fields' => ['declension'], 'orderBy' => 'numlevel']); foreach ($reg as $v) { $crumb[$v['numlevel']] = [ 'id' => $v['id'], 'title' => $v['title'], 'title.in' => ! empty($v['declension']['where']) ? $v['declension']['where'] : $v['title'], 'link' => Listings::url('items.search', array_merge($linkParam, ['region' . $v['numlevel'] => $v['keyword']])), 'region' => $v, ]; } } } $data['crumb'] = $crumb; } else { # нет фильтра по региону, выбираем регионы в зависимости от настроек покрытия switch ($coveringType) { case Geo::COVERING_COUNTRIES: case Geo::COVERING_CITIES: $data['regs'] = $model->regionsItemsCounters([ 'cat_id' => $this->filter->categoryData['id'], 'id' => $coveringRegion ?: 0], ['cache' => $this->countersCache]); break; case Geo::COVERING_COUNTRY: case Geo::COVERING_REGION: $regionID = $coveringRegion; $regionData = Geo::regionData($regionID); $data['regs'] = $model->regionsItemsCounters([ 'cat_id' => $this->filter->categoryData['id'], 'numlevel' => $regionData['numlevel'] + 1, 'numleft' => ['>', $regionData['numleft']], 'numright' => ['<', $regionData['numright']], ], ['cache' => $this->countersCache]); break; } foreach ($data['regs'] as &$v) { $v['link'] = Listings::url('items.search', array_merge($linkParam, $v['url'] ?? [])); } unset($v); } return $data; } }