<?php
 namespace Website\Widgets\Blog; use Moto; use Website; use Illuminate; class CategoryListWidget extends Website\Widgets\Blog\AbstractPostsWidget { protected $_name = 'blog.category_list'; protected static $_defaultProperties = array( 'preset' => 'default', 'listType' => 'all', 'customList' => array(), 'hideEmpty' => false, 'enableDelimiter' => true, 'icon' => null, 'visible_on' => 'mobile-v', 'title' => [ 'content' => '', 'textStyle' => 'moto-text_system_8', 'bottomSpacing' => 'small' ], 'align' => array( 'desktop' => 'left', 'tablet' => '', 'mobile-v' => '', 'mobile-h' => '', ), 'spacing' => array( 'top' => 'auto', 'right' => 'auto', 'bottom' => 'auto', 'left' => 'auto', ), ); protected $_categories; public function getTemplatePath($preset = null) { return '@websiteWidgets/blog/templates/category_list.twig.html'; } public function getCssClasses() { $classes = 'moto-preset-' . $this->getPropertyValue('preset'); $classes .= ' ' . $this->getSpacing('classes'); $classes .= ' ' . $this->getAlignClass(); $classes .= ' ' . $this->getVisibleOn('classes'); if ($this->getPropertyValue('enableDelimiter')) { $classes .= ' moto-widget-blog-category_list_show-delimiter'; } if (!$this->getPropertyValue('icon')) { $classes .= ' moto-widget-blog-category_list_no-icon'; } return $classes; } public function getCategories() { if ($this->_categories) { return $this->_categories; } $query = Moto\Database\ContentTaxonomyBlogCategory::query(); if ($this->getPropertyValue('hideEmpty')) { $_this = $this; $query->whereHas('pages', function ($query) use ($_this) { if (!$_this->isPreviewMode()) { $query->where('published', '<=', date('Y-m-d H:i:00', time())); } $query->where('status', '=', Moto\Application\Pages\PageModel::STATUS_PUBLISH); }); } $customList = null; if ($this->getPropertyValue('listType') === 'custom') { $customList = []; foreach((array) $this->getPropertyValue('customList') as $item) { $id = $item; if (is_array($item)) { $id = Moto\Util::getValue($item, 'id'); } $customList[] = (int) $id; } $query->whereIn('id', $customList); $collection = $query->get(); $items = new Illuminate\Database\Eloquent\Collection(); foreach($customList as $id) { $item = $collection->find($id); if ($item) { $items->add($item); } } } else { $query->orderBy('name'); $items = $query->get(); } $this->_categories = $items; return $this->_categories; } } 