List * @copyright Tamaranga */ class ListBlock extends Block { use HasBanners; /** @var array Items list data */ public $items = []; /** @var int Total items counter */ public $total = 0; /** @var string Pagination pages */ public $pages = ''; /** @var int Items per page */ public $pageSize = 20; /** @var string */ public $listView = ListFactory::LIST; /** @var FilterBlock */ public $filter; /** @var bool Расположение вертикального блока */ public $verticalBlockPosition = 'right'; /** @var bool */ public $bannerRightSticky = false; /** @var int */ public $pagesLimit = 0; public function init() { parent::init(); $this->setTemplate('search/list', 'listings'); $this->setTitle(_t('@listings', 'List')); $this->setKey('ListBlock'); } public function blocks() { $this->addBlock('sortBlock', function () { return $this->filter->getSortBlock(); }); $this->addBlock('fixedBlock', FixedBlock::class, function (FixedBlock $block) { $block->filter = $this->filter; }); $this->addBlock('list', function () { return ListFactory::create($this->listView); }, function (ListListBlock $block) { $block->showBanners(true); }); $this->addBlock('premiumBlock', PremiumItemsBlock::class, function (PremiumItemsBlock $block) { $block->categoryId = $this->filter->categoryId; # prerender to draw right block $block->prerenderable(true); }); $this->addBlock('rssBlock', RssBlock::class, function (RssBlock $block) { $block->regionId = $this->filter->region; $block->category = $this->filter->categoryData; }); $this->addBanner('bannerRight', 'listings_search_right', function (Banner $banner) { $banner->setBannerSettings('cat', $this->filter->categoryId); $banner->setBannerSettings('region', $this->filter->region); }); } public function onSubmitAction() { $countOnly = $this->filter->countOnly; $this->loadList($countOnly); if (! $countOnly) { $this->respond('list', $this->getList()); $this->respond('pages', $this->pages); } $this->respond('total', $this->total); $this->respond('total_declension', ( $this->total ? tpl::declension($this->total, _t('listings', 'listing;listings;listings'), false) : '' )); return $this->getActionResponse(); } public function data() { $data = parent::data(); if ($data['filterVertical'] = $this->filter->isVertical()) { $data['filterVerticalBlock'] = $this->filter->getPropsBlock(); } $this->filter->setVisibleProps(! $data['filterVertical']); return $data; } public function loadList($countOnly = false) { # Search Query $this->filter->singleList = $this->getBlock('fixedBlock')->singleList; $filterQuery = $this->prepareSearchQuery(); # Count $this->total = Listings::model()->itemsList($filterQuery['filter'], true, ['context' => static::class]); if ($countOnly) { return; } # Load if ($this->total > 0) { # Pagination $pages = new Pagination($this->total, $this->pageSize, [ 'link' => Listings::url('items.search', [ 'keyword' => $this->filter->categoryKeyword, 'landing_url' => $this->filter->categoryData['landing_url'], ]), 'query' => $this->filter->toUrlQuery(), ]); if ($this->pagesLimit) { $pages->setPagesLimit($this->pagesLimit); } if ($this->request->isPhone()) { $pages->pageNeighbours = 1; } $this->filter->page = $pages->getCurrentPage(); # Items $this->items = Listings::model()->itemsList($filterQuery['filter'], false, [ 'context' => static::class, 'orderBy' => $filterQuery['orderBy'], 'limit' => $pages->getLimit(), 'offset' => $pages->getOffset(), 'listCurrency' => $this->filter->currencyId, ]); $this->pages = $pages->view(['pagelast' => false]); } $this->filter->total = $this->total; $this->getList()->setItems($this->items); } public function prepareSearchQuery(): array { return $this->filter->toSqlQuery(); } /** * List block * @return ListListBlock|mixed */ public function getList() { return $this->getBlock('list'); } /** * Fixed items block * @return FixedBlock|mixed */ public function getFixed() { return $this->getBlock('fixedBlock'); } public function renderList() { $list = $this->getList(); $list->showBanners(true); return $list; } public function settingsForm($form) { $form ->number('pageSize', _t('@listings', 'Search Results'), 1, 100, 1, 20) ->tip(_t('@listings', 'Number of listings per page in search results')) ->label(_t('@', 'per page')) ->width(100) ->number('pagesLimit', _t('@listings', 'Pages Limit'), 0) ->tip(_t('@', '0 - No Limit')) ->width(100) ->radio('verticalBlockPosition', _t('@listings', 'Vertical Block Position'), 'right', function () { return [ 'left' => _t('@', 'Left'), 'right' => _t('@', 'Right'), ]; }) ->checkbox('bannerRightSticky', _t('@listings', 'Sticky Side Banner'), false) ; } }