Skip to content

13299 category tree #674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
321 changes: 321 additions & 0 deletions Block/Adminhtml/Category/AbstractCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/

namespace Magefan\Blog\Block\Adminhtml\Category;

use Magento\Framework\Data\Tree\Node;
use Magento\Store\Model\Store;
use Magefan\Blog\Model\ResourceModel\Category\CollectionFactory;


/**
* Class AbstractCategory
*/
class AbstractCategory extends \Magento\Backend\Block\Template
{
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry = null;

/**
* @var \Magefan\Blog\Model\ResourceModel\Category\Tree
*/
protected $_categoryTree;

/**
* @var \Magefan\Blog\Model\CategoryFactory
*/
protected $_categoryFactory;

protected $collectionFactory;

/**
* @var bool
*/
protected $_withProductCount;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magefan\Blog\Model\ResourceModel\Category\Tree $categoryTree
* @param \Magento\Framework\Registry $registry
* @param \Magefan\Blog\Model\CategoryFactory $categoryFactory
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magefan\Blog\Model\ResourceModel\Category\Tree $categoryTree,
\Magento\Framework\Registry $registry,
\Magefan\Blog\Model\CategoryFactory $categoryFactory,
CollectionFactory $collectionFactory,
array $data = []
) {
$this->_categoryTree = $categoryTree;
$this->_coreRegistry = $registry;
$this->_categoryFactory = $categoryFactory;
$this->collectionFactory = $collectionFactory;
$this->_withProductCount = true;
parent::__construct($context, $data);
}

/**
* Retrieve current category instance
*
* @return array|null
*/
public function getCategory()
{
$categoryId = (int)$this->getRequest()->getParam('id');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$catRepo = $objectManager->create( \Magefan\Blog\Api\CategoryRepositoryInterface::class);

return $catRepo->getById($categoryId);
}

/**
* Get category id
*
* @return int|string|null
*/
public function getCategoryId()
{
if ($this->getCategory()) {
return $this->getCategory()->getId();
}
return \Magefan\Blog\Model\Category::TREE_ROOT_ID;
}

/**
* Get category name
*
* @return string
*/
public function getCategoryName()
{
return $this->getCategory()->getName();
}

/**
* Get category path
*
* @return mixed
*/
public function getCategoryPath()
{
if ($this->getCategory()) {
return $this->getCategory()->getPath();
}
return \Magefan\Blog\Model\Category::TREE_ROOT_ID;
}

/**
* Check store root category
*
* @return bool
*/
public function hasStoreRootCategory()
{
$root = $this->getRoot();
if ($root && $root->getId()) {
return true;
}
return false;
}

/**
* Get store from request
*
* @return Store
*/
public function getStore()
{
$storeId = (int)$this->getRequest()->getParam('store');
return $this->_storeManager->getStore($storeId);
}

/**
* Get root category for tree
*
* @param mixed|null $parentNodeCategory
* @param int $recursionLevel
* @return Node|array|null
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getRoot($parentNodeCategory = null, $recursionLevel = 3)
{
if ($parentNodeCategory !== null && $parentNodeCategory->getId()) {
return $this->getNode($parentNodeCategory, $recursionLevel);
}

$root = $this->_coreRegistry->registry('root');
if ($root === null) {
$storeId = (int)$this->getRequest()->getParam('store');

if ($storeId) {
$store = $this->_storeManager->getStore($storeId);
$rootId = $store->getRootCategoryId();
} else {
$rootId = \Magefan\Blog\Model\Category::TREE_ROOT_ID;
}

$tree = $this->_categoryTree->load(null, $recursionLevel);

if ($this->getCategory()) {
$tree->loadEnsuredNodes($this->getCategory(), $tree->getNodeById($rootId));
}

$tree->addCollectionData($this->getCategoryCollection());

$root = $tree->getNodeById($rootId);

if ($root) {
$root->setIsVisible(true);
if ($root->getId() == \Magefan\Blog\Model\Category::TREE_ROOT_ID) {
$root->setName(__('Root'));
}
}


$this->_coreRegistry->register('root', $root);
}

return $root;
}

/**
* Get Default Store Id
*
* @return int
*/
protected function _getDefaultStoreId()
{
return \Magento\Store\Model\Store::DEFAULT_STORE_ID;
}

/**
* Get category collection
*
* @return \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
*/
public function getCategoryCollection()
{
$storeId = $this->getRequest()->getParam('store', $this->_getDefaultStoreId());
$collection = $this->getData('category_collection');
if ($collection === null) {
$collection = $this->collectionFactory->create();

$collection
->addFieldToSelect('category_id')
->addFieldToSelect('title')
->addFieldToSelect('is_active');

$this->setData('category_collection', $collection);
}
return $collection;
}

/**
* Get and register categories root by specified categories IDs
*
* IDs can be arbitrary set of any categories ids.
* Tree with minimal required nodes (all parents and neighbours) will be built.
* If ids are empty, default tree with depth = 2 will be returned.
*
* @param array $ids
* @return mixed
*/
public function getRootByIds($ids)
{
$root = $this->_coreRegistry->registry('root');
if (null === $root) {
$ids = $this->_categoryTree->getExistingCategoryIdsBySpecifiedIds($ids);
$tree = $this->_categoryTree->loadByIds($ids);
$rootId = \Magefan\Blog\Model\Category::TREE_ROOT_ID;
$root = $tree->getNodeById($rootId);
if ($root && $rootId != \Magefan\Blog\Model\Category::TREE_ROOT_ID) {
$root->setIsVisible(true);
} elseif ($root && $root->getId() == \Magefan\Blog\Model\Category::TREE_ROOT_ID) {
$root->setName(__('Root'));
}

$tree->addCollectionData($this->getCategoryCollection());
$this->_coreRegistry->register('root', $root);
}
return $root;
}

/**
* Get category node for tree
*
* @param mixed $parentNodeCategory
* @param int $recursionLevel
* @return Node
*/
public function getNode($parentNodeCategory, $recursionLevel = 2)
{
$nodeId = $parentNodeCategory->getId();
$node = $this->_categoryTree->loadNode($nodeId);
$node->loadChildren($recursionLevel);

if ($node && $nodeId != \Magefan\Blog\Model\Category::TREE_ROOT_ID) {
$node->setIsVisible(true);
} elseif ($node && $node->getId() == \Magefan\Blog\Model\Category::TREE_ROOT_ID) {
$node->setName(__('Root'));
}

$this->_categoryTree->addCollectionData($this->getCategoryCollection());
return $node;
}

/**
* Get category save url
*
* @param array $args
* @return string
*/
public function getSaveUrl(array $args = [])
{
$params = ['_current' => false, '_query' => false, 'store' => $this->getStore()->getId()];
$params = array_merge($params, $args);
return $this->getUrl('catalog/*/save', $params);
}

/**
* Get category edit url
*
* @return string
*/
public function getEditUrl()
{
return $this->getUrl(
'blog/category/edit',
['store' => null, '_query' => false, 'id' => null, 'parent' => null]
);
}

/**
* Return ids of root categories as array
*
* @return array
*/
public function getRootIds()
{
$ids = $this->getData('root_ids');
if ($ids === null) {
$ids = [\Magefan\Blog\Model\Category::TREE_ROOT_ID];
foreach ($this->_storeManager->getGroups() as $store) {
$ids[] = $store->getRootCategoryId();
}
$this->setData('root_ids', $ids);
}
return $ids;
}
}
Loading