List * @copyright Tamaranga */ class ListListBlock extends Block { /** @var int Limit */ public $limit = 10; /** @var array Items data */ public $items = []; /** @var string Item block view class */ public $itemView = ListItemBlock::class; /** @var array Item block default settings */ public $itemSettings = []; /** @var bool Show banners inside list */ public $showBanners = false; /** @var mixed|null Empty list text (false - do not show) */ public $emptyText; /** @var string Item images view: cover, contain */ public $imagesView = 'cover'; public function init() { parent::init(); $this->setTemplate('lists/list.list', 'listings'); $this->imagesView = $this->config('listings.items.images.lists.view', 'cover'); } /** * Create item blocks from items data * @return mixed|void */ protected function beforeRender() { if (parent::beforeRender() === false) { return false; } $this->prepareItems(); $this->prepareEmptyText(); foreach ($this->items as $key => $item) { $this->items[$key] = $this->itemBlock($item); } } /** * Set items data * @param array $items */ public function setItems(array $items) { $this->items = $items; } /** * Load items data * @param array $filter * @param array $opts * @return array|int */ public function loadItems(array $filter = [], array $opts = []) { $opts = $this->defaults($opts, [ 'count' => false, 'limit' => $this->limit, 'context' => static::class, ]); # count only if ($opts['count']) { return Listings::model()->itemsList($filter, true, $opts); } # load $this->items = Listings::model()->itemsList($filter, false, $opts); if (empty($this->items)) { $this->items = []; } return $this->items; } /** * Prepare items data for view * @return void */ public function prepareItems() { if (empty($this->items)) { return; } # Items images $images = Listings::itemImages(); $sizes = [ItemImages::szSmall, ItemImages::szMedium]; $extensions = ['svg']; $urlDefaults = []; foreach ($sizes as $size) { $urlDefaults['img_' . $size] = $images->urlDefault($size); foreach ($extensions as $sizeExt) { $urlDefaults['img_' . $size . ':' . $sizeExt] = $images->urlDefault($size . ':' . $sizeExt); } } # Default image foreach ($this->items as &$item) { if (! $item['imgs']) { foreach ($urlDefaults as $defField => $defUrl) { $item[$defField] = $defUrl; } } } unset($item); } /** * Set item view class * @param string $view */ public function setItemView(string $view) { $this->itemView = $view; } /** * Create item block * @param array $item item data * @return ListItemBlock|mixed */ protected function itemBlock(array $item) { $class = $this->itemView; return new $class($this->defaults([ 'item' => $item, 'imagesView' => $this->imagesView, ], $this->itemSettings)); } /** * Show/hide banners * @param bool $show */ public function showBanners($show = true) { $this->showBanners = $show; } /** * Set empty list text * @param mixed $text */ public function setEmptyText($text) { $this->emptyText = $text; } /** * Prepare empty text before render */ public function prepareEmptyText() { if (is_callable($this->emptyText)) { $this->emptyText = ($this->emptyText)(); } elseif ($this->emptyText === false) { $this->emptyText = ''; } else { $this->emptyText = Site::showInlineMessage( $this->emptyText ?: _t('listings', 'No listings found for your query') ); } } }