/** * Move tree node * * @param Node $node * @param Node $newParent * @param Node $prevNode * @return void * @throws \Exception * @todo Use adapter for generate conditions */ public function move($node, $newParent, $prevNode = null) { $position = 1; $oldPath = $node->getData($this->_pathField); $newPath = $newParent->getData($this->_pathField); $newPath = $newPath . '/' . $node->getId(); $oldPathLength = strlen($oldPath); $newLevel = $newParent->getLevel() + 1; $levelDisposition = $newLevel - $node->getLevel(); $data = array($this->_levelField => new \Zend_Db_Expr("{$this->_levelField} + '{$levelDisposition}'"), $this->_pathField => new \Zend_Db_Expr("CONCAT('{$newPath}', RIGHT({$this->_pathField}, LENGTH({$this->_pathField}) - {$oldPathLength}))")); $condition = $this->_conn->quoteInto("{$this->_pathField} REGEXP ?", "^{$oldPath}(/|\$)"); $this->_conn->beginTransaction(); $reorderData = array($this->_orderField => new \Zend_Db_Expr("{$this->_orderField} + 1")); try { if ($prevNode && $prevNode->getId()) { $reorderCondition = "{$this->_orderField} > {$prevNode->getData($this->_orderField)}"; $position = $prevNode->getData($this->_orderField) + 1; } else { $reorderCondition = $this->_conn->quoteInto("{$this->_pathField} REGEXP ?", "^{$newParent->getData($this->_pathField)}/[0-9]+\$"); $select = $this->_conn->select()->from($this->_table, new \Zend_Db_Expr("MIN({$this->_orderField})"))->where($reorderCondition); $position = (int) $this->_conn->fetchOne($select); } $this->_conn->update($this->_table, $reorderData, $reorderCondition); $this->_conn->update($this->_table, $data, $condition); $this->_conn->update($this->_table, array($this->_orderField => $position, $this->_levelField => $newLevel), $this->_conn->quoteInto("{$this->_idField} = ?", $node->getId())); $this->_conn->commit(); } catch (\Exception $e) { $this->_conn->rollBack(); throw new \Exception("Can't move tree node due to error: " . $e->getMessage()); } }
/** * 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; }
/** * @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(); }
/** * 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; }
/** * @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; }