Ejemplo n.º 1
0
 public function testGetTreeHasLevelField()
 {
     $rootId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
     $storeGroups = [];
     $storeId = 1;
     $rootLevel = 2;
     $level = 3;
     $this->collection->expects($this->any())->method('addAttributeToSelect')->willReturnMap([['url_key', false, $this->collection], ['is_anchor', false, $this->collection]]);
     $this->childNode->expects($this->atLeastOnce())->method('getLevel')->willReturn($level);
     $this->rootNode->expects($this->atLeastOnce())->method('getLevel')->willReturn($rootLevel);
     $this->rootNode->expects($this->once())->method('hasChildren')->willReturn(true);
     $this->rootNode->expects($this->once())->method('getChildren')->willReturn([$this->childNode]);
     $this->categoryTree->expects($this->once())->method('load')->with(null, 3)->willReturnSelf();
     $this->categoryTree->expects($this->atLeastOnce())->method('addCollectionData')->with($this->collection)->willReturnSelf();
     $this->categoryTree->expects($this->once())->method('getNodeById')->with($rootId)->willReturn($this->rootNode);
     $this->store->expects($this->atLeastOnce())->method('getId')->willReturn($storeId);
     $this->storeManager->expects($this->once())->method('getGroups')->willReturn($storeGroups);
     $this->storeManager->expects($this->atLeastOnce())->method('getStore')->willReturn($this->store);
     $this->context->expects($this->once())->method('getStoreManager')->willReturn($this->storeManager);
     $this->context->expects($this->once())->method('getRequest')->willReturn($this->request);
     $this->context->expects($this->once())->method('getEscaper')->willReturn($this->escaper);
     $this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager);
     /** @var \Magento\Widget\Block\Adminhtml\Widget\Catalog\Category\Chooser $chooser */
     $chooser = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject('Magento\\Widget\\Block\\Adminhtml\\Widget\\Catalog\\Category\\Chooser', ['categoryTree' => $this->categoryTree, 'context' => $this->context]);
     $chooser->setData('category_collection', $this->collection);
     $result = $chooser->getTree();
     $this->assertEquals($level, $result[0]['level']);
 }
 /**
  * Checks whether category belongs to active category's path
  *
  * @param \Magento\Framework\Data\Tree\Node $category
  * @return bool
  */
 protected function hasActive($category)
 {
     $catalogLayer = $this->getCatalogLayer();
     if (!$catalogLayer) {
         return false;
     }
     $currentCategory = $catalogLayer->getCurrentCategory();
     if (!$currentCategory) {
         return false;
     }
     $categoryPathIds = explode(',', $currentCategory->getPathInStore());
     return in_array($category->getId(), $categoryPathIds);
 }
Ejemplo n.º 3
0
 /**
  * @param array $prevNodes
  * @return array
  */
 public function getPath(&$prevNodes = [])
 {
     if ($this->_parent) {
         $prevNodes[] = $this;
         $this->_parent->getPath($prevNodes);
     }
     return $prevNodes;
 }
 /**
  * Recursively adds categories to top menu
  *
  * @param \Magento\Framework\Data\Tree\Node\Collection|array $categories
  * @param \Magento\Framework\Data\Tree\Node $parentCategoryNode
  * @param \Magento\Theme\Block\Html\Topmenu $block
  * @return void
  */
 protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block)
 {
     foreach ($categories as $category) {
         if (!$category->getIsActive()) {
             continue;
         }
         $block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId());
         $tree = $parentCategoryNode->getTree();
         $categoryData = $this->menuCategoryData->getMenuCategoryData($category);
         $categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode);
         $parentCategoryNode->addChild($categoryNode);
         if ($this->categoryFlatState->isFlatEnabled() && $category->getUseFlatResource()) {
             $subcategories = (array) $category->getChildrenNodes();
         } else {
             $subcategories = $category->getChildren();
         }
         $this->_addCategoriesToMenu($subcategories, $categoryNode, $block);
     }
 }
Ejemplo n.º 5
0
 /**
  * @param array|Node $node
  * @param int $level
  * @return array
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _getNodeJson($node, $level = 1)
 {
     $item = [];
     $item['text'] = $this->escapeHtml($node->getName());
     if ($this->_withProductCount) {
         $item['text'] .= ' (' . $node->getProductCount() . ')';
     }
     $item['id'] = $node->getId();
     $item['path'] = $node->getData('path');
     $item['cls'] = 'folder ' . ($node->getIsActive() ? 'active-category' : 'no-active-category');
     $item['allowDrop'] = false;
     $item['allowDrag'] = false;
     if (in_array($node->getId(), $this->getCategoryIds())) {
         $this->setExpandedPath($node->getData('path'));
         $item['checked'] = true;
     }
     if ($node->getLevel() < 2) {
         $this->setExpandedPath($node->getData('path'));
     }
     if ($node->hasChildren()) {
         $item['children'] = [];
         foreach ($node->getChildren() as $child) {
             $item['children'][] = $this->_getNodeJson($child, $level + 1);
         }
     }
     if (empty($item['children']) && (int) $node->getChildrenCount() > 0) {
         $item['children'] = [];
     }
     $item['expanded'] = in_array($node->getId(), $this->getExpandedPath());
     return $item;
 }
Ejemplo n.º 6
0
 /**
  * Delete
  *
  * @param Node $node
  * @return $this
  */
 public function delete($node)
 {
     if (isset($this->_nodes[$node->getId()])) {
         unset($this->_nodes[$node->getId()]);
     }
     return $this;
 }
Ejemplo n.º 7
0
 /**
  * Add child nodes
  *
  * @param array $children
  * @param string $path
  * @param Node $parentNode
  * @param bool $withChildren
  * @param int $level
  * @return void
  */
 protected function _addChildNodes($children, $path, $parentNode, $withChildren = false, $level = 0)
 {
     if (isset($children[$path])) {
         foreach ($children[$path] as $child) {
             $nodeId = isset($child[$this->_idField]) ? $child[$this->_idField] : false;
             if ($parentNode && $nodeId && ($node = $parentNode->getChildren()->searchById($nodeId))) {
                 $node->addData($child);
             } else {
                 $node = new Node($child, $this->_idField, $this, $parentNode);
                 $node->setLevel($node->getData($this->_levelField));
                 $node->setPathId($node->getData($this->_pathField));
                 $this->addNode($node, $parentNode);
             }
             if ($withChildren) {
                 $this->_loaded = false;
                 $node->loadChildren(1);
                 $this->_loaded = false;
             }
             if ($path) {
                 $childrenPath = explode('/', $path);
             } else {
                 $childrenPath = array();
             }
             $childrenPath[] = $node->getId();
             $childrenPath = implode('/', $childrenPath);
             $this->_addChildNodes($children, $childrenPath, $node, $withChildren, $level + 1);
         }
     }
 }
Ejemplo n.º 8
0
 /**
  * @param Node|array $node
  * @return bool
  */
 protected function _isParentSelectedCategory($node)
 {
     if ($node && $this->getCategory()) {
         $pathIds = $this->getCategory()->getPathIds();
         if (in_array($node->getId(), $pathIds)) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 9
0
 /**
  * Returns array of menu item's classes
  *
  * @param \Magento\Framework\Data\Tree\Node $item
  * @return array
  */
 protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
 {
     $classes = [];
     $classes[] = 'level' . $item->getLevel();
     $classes[] = $item->getPositionClass();
     if ($item->getIsFirst()) {
         $classes[] = 'first';
     }
     if ($item->getIsActive()) {
         $classes[] = 'active';
     } elseif ($item->getHasActive()) {
         $classes[] = 'has-active';
     }
     if ($item->getIsLast()) {
         $classes[] = 'last';
     }
     if ($item->getClass()) {
         $classes[] = $item->getClass();
     }
     if ($item->hasChildren()) {
         $classes[] = 'parent';
     }
     return $classes;
 }
Ejemplo n.º 10
0
 /**
  * Convert categories tree to array recursively
  *
  * @param  \Magento\Framework\Data\Tree\Node $node
  * @return array
  */
 protected function _getNodesArray($node)
 {
     $result = ['id' => (int) $node->getId(), 'parent_id' => (int) $node->getParentId(), 'children_count' => (int) $node->getChildrenCount(), 'is_active' => (bool) $node->getIsActive(), 'name' => $node->getName(), 'level' => (int) $node->getLevel(), 'product_count' => (int) $node->getProductCount()];
     if ($node->getParentId() == Category::TREE_ROOT_ID && !in_array($result['id'], $this->_allowedCategoryIds)) {
         $result['disabled'] = true;
     }
     if ($node->hasChildren()) {
         $result['children'] = [];
         foreach ($node->getChildren() as $childNode) {
             $result['children'][] = $this->_getNodesArray($childNode);
         }
     }
     $result['cls'] = ($result['is_active'] ? '' : 'no-') . 'active-category';
     $result['expanded'] = !empty($result['children']);
     return $result;
 }
Ejemplo n.º 11
0
 /**
  * @param \Magento\Framework\Data\Tree\Node $node
  * @param int $depth
  * @param int $currentLevel
  * @return \Magento\Catalog\Service\V1\Data\Eav\Category\Tree[]
  */
 public function getTree($node, $depth = null, $currentLevel = 0)
 {
     $builder = $this->treeBuilderFactory->create();
     $builder->setId($node->getId())->setParentId($node->getParentId())->setName($node->getName())->setPosition($node->getPosition())->setLevel($node->getLevel())->setActive($node->getIsActive())->setProductCount($node->getProductCount())->setChildren([]);
     if ($node->hasChildren()) {
         $children = array();
         foreach ($node->getChildren() as $child) {
             if (!is_null($depth) && $depth <= $currentLevel) {
                 break;
             }
             $children[] = $this->getTree($child, $depth, $currentLevel + 1);
         }
         $builder->setChildren($children);
     }
     return $builder->create();
 }
Ejemplo n.º 12
0
 /**
  * @param \Magento\Framework\Data\Tree\Node $node
  * @param int $depth
  * @param int $currentLevel
  * @return \Magento\Catalog\Api\Data\CategoryTreeInterface[]|[]
  */
 protected function getChildren($node, $depth, $currentLevel)
 {
     if ($node->hasChildren()) {
         $children = [];
         foreach ($node->getChildren() as $child) {
             if ($depth !== null && $depth <= $currentLevel) {
                 break;
             }
             $children[] = $this->getTree($child, $depth, $currentLevel + 1);
         }
         return $children;
     }
     return [];
 }
Ejemplo n.º 13
0
 /**
  * Get child categories
  *
  * @param \Magento\Framework\Data\Tree\Node $tree
  * @param string $name
  * @return mixed
  */
 protected function findTreeChild($tree, $name)
 {
     $foundChild = null;
     if ($name) {
         foreach ($tree->getChildren() as $child) {
             if ($child->getName() == $name) {
                 $foundChild = $child;
                 break;
             }
         }
     }
     return $foundChild;
 }