Fixed items block * @copyright Tamaranga */ class FixedBlock extends Block { /** @var bool */ public $singleList = false; /** @var int */ public $limit = 5; /** @var bool */ public $visible = false; /** @var array */ public $items = []; /** @var bool */ public $loaded = false; /** @var bool */ public $more = false; /** @var FilterBlock */ public $filter; protected $serviceKey = 'fix'; public function init() { parent::init(); $this->setKey('fixed'); $this->setTitle(_t('listings', 'Top listings')); $this->setTemplate('search/fixed.block', 'listings'); } public function blocks() { $this->addBlock('list', function () { return ListFactory::create($this->filter->listView); }, function (ListListBlock $block) { $block->showBanners(false); }); } public function data() { if (! Listings::itemServices('fix')) { return false; } if ($this->singleList) { return false; } $data = parent::data(); $data['active'] = $this->filter->fixedListActive; if (! $data['active']) { $this->loadFixedItems(); } $this->getBlock('list')->setItems($this->items); return $data; } public function loadFixedItems(): array { if ($this->loaded) { return $this->items; } $this->loaded = true; $query = $this->filter->toSqlQuery(); if ($this->limit < 1) { $this->limit = 5; } $query['filter']['svc'] = $this->serviceKey; $this->items = Listings::model()->itemsList($query['filter'], false, [ 'context' => 'search', 'orderBy' => 'RAND()', 'listCurrency' => $this->filter->currencyId, 'limit' => $this->limit + 1, ]); $this->more = (count($this->items) > $this->limit); if ($this->more) { array_pop($this->items); } return $this->items; } /** * @return Fix|mixed */ protected function getItemService() { if (bff::servicesEnabled('listings')) { return Listings::itemServices($this->serviceKey); } return null; } public function settingsForm($form) { $plugin = bff::plugin('services\listings'); if (! $plugin) { $form->alert(_t('@listings', 'Make sure plugin "[services-listings]" is installed and enabled', ['services-listings' => _t('listings', 'Paid Listing Services')]), 'info', false); } elseif (! $this->getItemService()) { $form->alert(_t('@listings', 'Make sure that this service is enabled in [a]settings[/a]', [ 'a' => '', '/a' => '' ]), 'info', false); } $form->checkbox('singleList', _t('@listings', 'Single List'), $this->singleList); $form->number('limit', _t('@', 'Number'), 0, 0, 1, $this->limit) ->visibleIf('singleList', false) ; } }