/** * @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; }
/** * 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); } } }