1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Adminhtml_Block_Urlrewrite_Category_Tree extends Mage_Adminhtml_Block_Catalog_Category_Abstract
35: {
36: 37: 38: 39: 40:
41: protected $_allowedCategoryIds = null;
42:
43: 44: 45:
46: public function __construct()
47: {
48: parent::__construct();
49: $this->setTemplate('urlrewrite/categories.phtml');
50: }
51:
52: 53: 54: 55: 56: 57: 58: 59:
60: public function getTreeArray($parentId = null, $asJson = false, $recursionLevel = 3)
61: {
62: $productId = Mage::app()->getRequest()->getParam('product');
63: if ($productId) {
64: $product = Mage::getModel('catalog/product')->setId($productId);
65: $this->_allowedCategoryIds = $product->getCategoryIds();
66: unset($product);
67: }
68:
69: $result = array();
70: if ($parentId) {
71: $category = Mage::getModel('catalog/category')->load($parentId);
72: if (!empty($category)) {
73: $tree = $this->_getNodesArray($this->getNode($category, $recursionLevel));
74: if (!empty($tree) && !empty($tree['children'])) {
75: $result = $tree['children'];
76: }
77: }
78: } else {
79: $result = $this->_getNodesArray($this->getRoot(null, $recursionLevel));
80: }
81:
82: if ($asJson) {
83: return Mage::helper('core')->jsonEncode($result);
84: }
85:
86: $this->_allowedCategoryIds = null;
87:
88: return $result;
89: }
90:
91: 92: 93: 94: 95:
96: public function getCategoryCollection()
97: {
98: $collection = $this->_getData('category_collection');
99: if (is_null($collection)) {
100: $collection = Mage::getModel('catalog/category')->getCollection()
101: ->addAttributeToSelect(array('name', 'is_active'))
102: ->setLoadProductCount(true);
103: $this->setData('category_collection', $collection);
104: }
105:
106: return $collection;
107: }
108:
109: 110: 111: 112: 113: 114:
115: protected function _getNodesArray($node)
116: {
117: $result = array(
118: 'id' => (int)$node->getId(),
119: 'parent_id' => (int)$node->getParentId(),
120: 'children_count' => (int)$node->getChildrenCount(),
121: 'is_active' => (bool)$node->getIsActive(),
122: 'name' => $node->getName(),
123: 'level' => (int)$node->getLevel(),
124: 'product_count' => (int)$node->getProductCount()
125: );
126:
127: if (is_array($this->_allowedCategoryIds) && !in_array($result['id'], $this->_allowedCategoryIds)) {
128: $result['disabled'] = true;
129: }
130:
131: if ($node->hasChildren()) {
132: $result['children'] = array();
133: foreach ($node->getChildren() as $childNode) {
134: $result['children'][] = $this->_getNodesArray($childNode);
135: }
136: }
137: $result['cls'] = ($result['is_active'] ? '' : 'no-') . 'active-category';
138: $result['expanded'] = (!empty($result['children']));
139:
140: return $result;
141: }
142:
143: 144: 145: 146: 147:
148: public function getLoadTreeUrl()
149: {
150: return Mage::helper('adminhtml')->getUrl('*/*/categoriesJson');
151: }
152: }
153: