Your IP : 216.73.217.13


Current Path : /var/www/www.indacotrentino.com/www/app/code/Ashsmith/Blog/Block/
Upload File :
Current File : //var/www/www.indacotrentino.com/www/app/code/Ashsmith/Blog/Block/PostList.php

<?php
namespace Ashsmith\Blog\Block;

use Ashsmith\Blog\Api\Data\PostInterface;
use Ashsmith\Blog\Model\ResourceModel\Post\Collection as PostCollection;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Framework\Registry;
use Magento\Catalog\Api\CategoryRepositoryInterface;

class PostList extends \Magento\Framework\View\Element\Template implements
    \Magento\Framework\DataObject\IdentityInterface
{
    /**
     * @var \Ashsmith\Blog\Model\ResourceModel\Post\CollectionFactory
     */
    protected $_postCollectionFactory;

    /**
     * @var \Magento\Catalog\Model\CategoryFactory
     */
    protected $categoryFactory;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $_registry;

    protected $categoryRepository;

    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * * @param \Ashsmith\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory
     * * @param CategoryFactory $categoryFactory
     * * @param Registry $registry
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Ashsmith\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
        CategoryFactory $categoryFactory,
        Registry $registry,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
        array $data = []
    ) {
        $this->_postCollectionFactory = $postCollectionFactory;
        $this->categoryFactory = $categoryFactory;
        $this->_registry = $registry;
        $this->categoryRepository = $categoryRepository;
        parent::__construct($context, $data);
    }

    /**
     * @return \Ashsmith\Blog\Model\ResourceModel\Post\Collection
     */
    public function getPosts()
    {
        $categoryId = $this->getCurrentCategory()->getId();
        $allowedCategoryIds = [349, 331];

        // Check if posts has already been defined
        // makes our block nice and re-usable! We could
        // pass the 'posts' data to this block, with a collection
        // that has been filtered differently!
        if (in_array($categoryId, $allowedCategoryIds)) {
            if (!$this->hasData('posts')) {
                $posts = $this->_postCollectionFactory
                    ->create()
                    ->addFilter('is_active', 1)
                    ->addOrder(
                        PostInterface::CREATION_TIME,
                        PostCollection::SORT_ORDER_DESC
                    );
                $this->setData('posts', $posts);
            }
            return $this->getData('posts');
        }
        return [];
    }

    /**
     *
     * @return \Magento\Catalog\Model\Category
     */
    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }

    /**
     * Return identifiers for produced content
     *
     * @return array
     */
    public function getIdentities()
    {
        return [\Ashsmith\Blog\Model\Post::CACHE_TAG . '_' . 'list'];
    }

    public function getCategoryUrl($categoryId)
    {
        $category = $this->categoryRepository->get($categoryId, 1);
        return $category->getUrl();
    }
}