setTemplate('premium.block', 'listings'); $this->setKey('premium'); $this->setTitle(_t('listings', 'Premium Listings')); $this->limit = 10; } protected function itemBlock(array $item) { # Replace gallery item template $block = parent::itemBlock($item); if ($block) { $block->setTemplate('lists/premium.item', 'listings'); } return $block; } public function data() { if (! $this->limit) { return false; } $data = parent::data(); $itemsId = $this->preparePremiumItemsId(); if (empty($itemsId)) { return false; } $this->loadItems($this->prepareItemsFilter($itemsId), [ 'orderBy' => 'FIELD(id,' . join(',', $itemsId) . ')', ]); if (empty($this->items)) { return false; } $data['id'] = $this->id(); return $data; } /** * @return Premium|mixed */ protected function getItemService() { if (bff::servicesEnabled('listings')) { return Listings::itemServices($this->serviceKey); } return null; } public function preparePremiumItemsId() { $service = $this->getItemService(); if (! $service) { return []; } $itemsId = $service->itemsIDs($this->order); if (empty($itemsId)) { return []; } return $itemsId; } public function prepareItemsFilter(array $itemsId) { $filter = array_merge($this->itemsFilter, [ 'id' => $itemsId, 'is_publicated' => 1, 'status' => Listings::STATUS_PUBLICATED, ]); if ($this->regionId > 0) { $filter[':region-filter'] = $this->regionId; } elseif ($this->region_filter) { $filter[':region-filter'] = Geo::filter('id'); } if ($this->categoryId > 0) { $filter[':cat-filter'] = $this->categoryId; } return $filter; } 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 Listings 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->text('title', _t('@listings', 'Title'), _t('listings', 'Premium Listings', true)); $form->images('icon', _t('@listings', 'Icon'), 1) ->preload(bff::path('/static/img/default/icon_premium.svg')); $form->number('limit', _t('@listings', 'Number'), 0, 0, 1, $this->limit); if ($this->perlineCustomizable) { $form->radio('perline', _t('@listings', 'Density'), 5) ->option(5, _t('@listings', '5 in row')) ->option(4, _t('@listings', '4 in row')); } $form->checkbox('oneline', _t('@listings', 'In one row'), $this->oneline); $form->checkbox('region_filter', _t('@listings', 'Take region filter in account'), $this->region_filter) ->tip(_t('@listings', 'display listings based on the current region')) ->hidden(! Listings::geoFilterEnabled()); $form->select('order', _t('@listings', 'Sort'), $this->order) ->option('rand', _t('@listings', 'Randomly')) ->option('desc', _t('@listings', 'In service activation order')); } }