<?php
 namespace Website\Widgets\Blog; use Moto; use Website; use Zend\Db\Sql\Where; use Zend; class PostListWidget extends Website\Widgets\Blog\AbstractPostsWidget { protected $_name = 'blog.post_list'; protected static $_defaultProperties = array( 'spacing' => array( 'top' => 'auto', 'right' => 'auto', 'bottom' => 'auto', 'left' => 'auto', ), 'item_count' => 5, 'read_more_label' => '', 'style' => array( 'title' => array( 'font_style' => 'moto-text_system_7', ), 'meta' => array( 'font_style' => 'moto-text_system_11', ), 'button' => array( 'preset' => '5', 'size' => 'small', ), 'feature_image' => array( 'preset' => 'default', ), 'divider' => array( 'preset' => 'default', ), ), 'contentAlignment' => array( 'title' => array( 'desktop' => 'left', 'tablet' => '', 'mobile-v' => '', 'mobile-h' => '' ), 'date' => array( 'desktop' => 'left', 'tablet' => '', 'mobile-v' => '', 'mobile-h' => '' ), 'button' => array( 'desktop' => 'left', 'tablet' => '', 'mobile-v' => '', 'mobile-h' => '' ) ) ); protected $_paginator = null; public function getTemplatePath($preset = null) { return '@websiteWidgets/blog/templates/post_list.twig.html'; } public function getPaginator() { if (!$this->_paginator) { $table = $this->_getTable(); $where = new Where(); if (!$this->isPreviewMode()) { $where->lessThanOrEqualTo('published', date('Y-m-d H:i:00', time())); } $where->equalTo('type', 'blog.post'); $where->equalTo('status', Moto\Application\Pages\PageModel::STATUS_PUBLISH); $this->_updatePaginatorQueryFilter($where); $order = array( 'published' => 'DESC' ); $this->_paginator = $table->getPaginator($where, $order); $this->_paginator->setCurrentPageNumber(Moto\System\Request::getQuery('page', 1)); $this->_paginator->setItemCountPerPage($this->getPropertyValue('item_count', 5)); $this->_paginator->setPageRange(5); } return $this->_paginator; } protected function _updatePaginatorQueryFilter($where) { $currentPage = $this->getCurrentPage(); if ($currentPage instanceof Moto\Website\ContentTaxonomyPage) { $currentTaxonomy = $currentPage->getContentEntity(); if (!$currentTaxonomy) { return; } $query = $currentTaxonomy->blogPosts(); $query->where('pages.status', '=', Moto\Application\Pages\PageModel::STATUS_PUBLISH); $posts = $query->get(['id']); $postsId = Moto\Database\Util::listsOrPluck($posts, 'id'); if (count($postsId) > 0) { $where->in('id', $postsId); } else { $where->equalTo('id', 0); } } } public function getPaginatorPages() { return $this->getPaginator()->getPages(); } public function isAllowShowContent() { $currentPage = $this->getCurrentPage(); if ($currentPage && $currentPage->isTemplate() && $this->isPreviewMode()) { return false; } return true; } } 