<?php
/**
* Landofcoder
*
* NOTICE OF LICENSE
*
* This source file is subject to the Landofcoder.com license that is
* available through the world-wide-web at this URL:
* https://landofcoder.com/license-agreement.html
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Landofcoder
* @package Lofmp_Blog
* @copyright Copyright (c) 2016 Landofcoder (https://landofcoder.com/)
* @license https://landofcoder.com/LICENSE-1.0.html
*/
namespace Lofmp\Blog\Block\Category;
use Lofmp\Blog\Block\AbstractBlock;
use Lofmp\Blog\Model\Category;
class CategoryList extends AbstractBlock
{
/**
* @var \Lofmp\Blog\Model\CategoryFactory
*/
protected $_categoryFactory;
/**
* @var Lofmp\Blog\Model\ResourceModel\Category\Collection
*/
protected $_colleciton;
/**
* @param \Magento\Framework\View\Element\Template\Context
* @param \Lofmp\Blog\Helper\Data $blogHelper
* @param \Magento\Framework\Registry $registry
* @param \Lofmp\Blog\Model\CategoryFactory $category
* @param \Lofmp\Blog\Model\Url $url
* @param array
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Lofmp\Blog\Helper\Data $blogHelper,
\Magento\Framework\Registry $registry,
\Lofmp\Blog\Model\CategoryFactory $categoryFactory,
\Lofmp\Blog\Model\Url $url,
array $data = []
)
{
parent::__construct($context, $registry, $blogHelper, $url, $data);
$this->_categoryFactory = $categoryFactory;
}
/**
* draw item
*
* @param mixed $collection
* @param mixed $cat
* @param int $level
* @return mixed
*/
public function drawItems($collection, $cat, $level = 0)
{
foreach ($collection as $_cat) {
if ($_cat->getParentId() == $cat->getId()) {
$children[] = $this->drawItems($collection, $_cat, $level+1);
$cat->setChildren($children);
}
}
$cat->setSelevel($level);
return $cat;
}
/**
* @inheritdoc
*/
public function _toHtml()
{
if(!$this->_blogHelper->getConfig('general_settings/enable')) return;
$store = $this->_storeManager->getStore();
$collection = $this->_categoryFactory->create()
->getCollection()
->addFieldToFilter('is_active', Category::STATUS_ENABLED)
->addFieldToFilter('is_approved', Category::STATUS_APPROVED)
->addStoreFilter($store)
->setOrder("cat_position", "ASC");
if ($this->getAuthor()) {
$collection->addFieldToFilter("seller_id", array(
array('finset'=> array('0')),
array('finset'=> array($this->getAuthor()->getData('seller_id'))),
)
);
}
$cats = [];
foreach ($collection as $_cat) {
if (!$_cat->getParentId()) {
$cats[] = $this->drawItems($collection, $_cat);
}
}
$this->setCollection($cats);
return parent::_toHtml();
}
/**
* draw item
*
* @param mixed $collection
* @param string $html
* @return string
*/
public function drawItem($collection, $html = '')
{
foreach ($collection as $_cat) {
$children = $_cat->getChildren();
$class = '';
if ($children) $class = "class='level" . $_cat->getLevel() . "' parent";
$html .= '<li ' . $class . ' >';
$html .= '<a href="' . $_cat->getUrl() .'">';
$html .= $_cat->getName();
$html .= '</a>';
if ($children) {
$html .= '<span class="opener"><i class="fa fa-plus-square-o"></i></span>';
$html .= '<ul class="level' . $_cat->getLevel() . ' children">';
$html .= $this->drawItem($children);
$html .= '</ul>';
}
$html .= '</li>';
}
return $html;
}
/**
* get category html
*
* @return string
*/
public function getCategoryHtml()
{
$collection = $this->getCollection();
$html = $this->drawItem($collection, '');
return $html;
}
}
|