Esempio n. 1
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;
 }
Esempio n. 2
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;
 }
Esempio n. 3
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();
 }