Ejemplo 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;
 }
 /**
  * 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
 /**
  * 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.º 4
0
 /**
  * Load ensured nodes
  *
  * @param object $category
  * @param Node $rootNode
  * @return void
  */
 public function loadEnsuredNodes($category, $rootNode)
 {
     $pathIds = $category->getPathIds();
     $rootNodeId = $rootNode->getId();
     $rootNodePath = $rootNode->getData($this->_pathField);
     $select = clone $this->_select;
     $select->order($this->_table . '.' . $this->_orderField . ' ASC');
     if ($pathIds) {
         $condition = $this->_conn->quoteInto("{$this->_table}.{$this->_idField} in (?)", $pathIds);
         $select->where($condition);
     }
     $arrNodes = $this->_conn->fetchAll($select);
     if ($arrNodes) {
         $childrenItems = array();
         foreach ($arrNodes as $nodeInfo) {
             $nodeId = $nodeInfo[$this->_idField];
             if ($nodeId <= $rootNodeId) {
                 continue;
             }
             $pathToParent = explode('/', $nodeInfo[$this->_pathField]);
             array_pop($pathToParent);
             $pathToParent = implode('/', $pathToParent);
             $childrenItems[$pathToParent][] = $nodeInfo;
         }
         $this->_addChildNodes($childrenItems, $rootNodePath, $rootNode, true);
     }
 }
Ejemplo n.º 5
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.º 6
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.º 7
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();
 }