private function nodeToArray(Varien_Data_Tree_Node $node, $mediaUrl, $baseUrl) { $result = array(); $thumbnail = ''; try { $thumbImg = $node->getThumbnail(); if ($thumbImg != null) { $thumbnail = $mediaUrl . 'catalog/category/' . $node->getThumbnail(); } } catch (Exception $e) { } $result['category_id'] = $node->getId(); $result['image'] = $mediaUrl . 'catalog/category/' . $node->getImage(); $result['thumbnail'] = $thumbnail; $result['description'] = strip_tags($node->getDescription()); $result['parent_id'] = $node->getParentId(); $result['name'] = $node->getName(); $result['is_active'] = $node->getIsActive(); $result['children'] = array(); if (method_exists('Mage', 'getEdition') && Mage::getEdition() == Mage::EDITION_COMMUNITY) { $result['url_path'] = $baseUrl . $node->getData('url_path'); } else { $category = Mage::getModel('catalog/category')->load($node->getId()); $result['url_path'] = $category->getUrl(); } foreach ($node->getChildren() as $child) { $result['children'][] = $this->nodeToArray($child, $mediaUrl, $baseUrl); } return $result; }
/** * Get JSON of a tree node or an associative array * * @param Varien_Data_Tree_Node|array $node * @param int $level * @return string */ protected function _getNodeJson($node, $level = 1) { $item = array(); $item['text'] = $this->htmlEscape($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 ($node->hasChildren()) { $item['children'] = array(); foreach ($node->getChildren() as $child) { $item['children'][] = $this->_getNodeJson($child, $level + 1); } } if (empty($item['children']) && (int) $node->getChildrenCount() > 0) { $item['children'] = array(); } if (!empty($item['children'])) { $item['expanded'] = true; } if (in_array($node->getId(), $this->getCategoryIds())) { $item['checked'] = true; } return $item; }
/** * Check if click able enabled for category. * * @param Varien_Data_Tree_Node|Mage_Catalog_Model_Category $category * @return bool */ protected function _isCategoryClickAble($category) { $isCategoryClickAbleStatus = true; $isCategoryClickAble = $category->getData(Monsoon_Test_Helper_Data::IS_CLICK_ABLE_LINK_CODE); if ($isCategoryClickAble !== null && (bool) $isCategoryClickAble === false) { $isCategoryClickAbleStatus = false; } return $isCategoryClickAbleStatus; }
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 Varien_Data_Tree_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); } } }
/** * Return the number of products assigned to the category * * @param Mage_Catalog_Model_Category|Varien_Data_Tree_Node $category * @return int */ protected function _getProductCount($category) { if (null === ($count = $category->getData('product_count'))) { $count = 0; if ($category instanceof Mage_Catalog_Model_Category) { $count = $category->getProductCount(); } elseif ($category instanceof Varien_Data_Tree_Node) { $count = $this->_getProductCountFromTreeNode($category); } } return $count; }
/** * Move tree node * * @param Varien_Data_Tree_Node $node * @param Varien_Data_Tree_Node $parentNode * @param Varien_Data_Tree_Node $prevNode */ public function moveNodeTo($node, $parentNode, $prevNode = null) { $data = array(); $data[$this->_parentField] = $parentNode->getId(); $data[$this->_levelField] = $parentNode->getData($this->_levelField) + 1; // New node order if (is_null($prevNode) || is_null($prevNode->getData($this->_orderField))) { $data[$this->_orderField] = 1; } else { $data[$this->_orderField] = $prevNode->getData($this->_orderField) + 1; } $condition = $this->_conn->quoteInto("{$this->_idField}=?", $node->getId()); // For reorder new node branch $dataReorderNew = array($this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) . '+1')); $conditionReorderNew = $this->_conn->quoteIdentifier($this->_parentField) . '=' . $parentNode->getId() . ' AND ' . $this->_conn->quoteIdentifier($this->_orderField) . '>=' . $data[$this->_orderField]; // For reorder old node branch $dataReorderOld = array($this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) . '-1')); $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField) . '=' . $node->getData($this->_parentField) . ' AND ' . $this->_conn->quoteIdentifier($this->_orderField) . '>' . $node->getData($this->_orderField); $this->_conn->beginTransaction(); try { // Prepare new node branch $this->_conn->update($this->_table, $dataReorderNew, $conditionReorderNew); // Move node $this->_conn->update($this->_table, $data, $condition); // Update old node branch $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld); $this->_updateChildLevels($node->getId(), $data[$this->_levelField]); $this->_conn->commit(); } catch (Exception $e) { $this->_conn->rollBack(); throw new Exception('Can\'t move tree node'); } }
/** * Move tree node * * @param Varien_Data_Tree_Node $node * @param Varien_Data_Tree_Node $parentNode * @param Varien_Data_Tree_Node $prevNode */ public function move($category, $newParent, $prevNode = null) { $position = 1; $oldPath = $category->getData($this->_pathField); $newPath = $newParent->getData($this->_pathField); $newPath = $newPath . '/' . $category->getId(); $oldPathLength = strlen($oldPath); $data = array($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(); try { if ($prevNode && $prevNode->getId()) { $reorderData = array($this->_orderField => new Zend_Db_Expr("{$this->_orderField} + 1")); $reorderCondition = "{$this->_orderField} > {$prevNode->getData($this->_orderField)}"; $this->_conn->update($this->_table, $reorderData, $reorderCondition); $position = $prevNode->getData($this->_orderField) + 1; } $this->_conn->update($this->_table, $data, $condition); $this->_conn->update($this->_table, array($this->_orderField => $position), $this->_conn->quoteInto("{$this->_idField} = ?", $category->getId())); $this->_conn->commit(); } catch (Exception $e) { $this->_conn->rollBack(); throw new Exception("Can't move tree node due to error: " . $e->getMessage()); } }
function getNodeChildrenData(Varien_Data_Tree_Node $node) { foreach ($node->getChildren() as $childNode) { $result[] = array('category_id' => $childNode->getData('entity_id'), 'parent_id' => $node->getData('entity_id'), 'name' => $childNode->getData('name'), 'categories' => getNodeChildrenData($childNode)); } return $result; }