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_Catalog_Category_Abstract extends Mage_Adminhtml_Block_Template
35: {
36:
37: public function __construct()
38: {
39: parent::__construct();
40: }
41:
42: 43: 44: 45: 46:
47: public function getCategory()
48: {
49: return Mage::registry('category');
50: }
51:
52: public function getCategoryId()
53: {
54: if ($this->getCategory()) {
55: return $this->getCategory()->getId();
56: }
57: return Mage_Catalog_Model_Category::TREE_ROOT_ID;
58: }
59:
60: public function getCategoryName()
61: {
62: return $this->getCategory()->getName();
63: }
64:
65: public function getCategoryPath()
66: {
67: if ($this->getCategory()) {
68: return $this->getCategory()->getPath();
69: }
70: return Mage_Catalog_Model_Category::TREE_ROOT_ID;
71: }
72:
73: public function hasStoreRootCategory()
74: {
75: $root = $this->getRoot();
76: if ($root && $root->getId()) {
77: return true;
78: }
79: return false;
80: }
81:
82: public function getStore()
83: {
84: $storeId = (int) $this->getRequest()->getParam('store');
85: return Mage::app()->getStore($storeId);
86: }
87:
88: public function getRoot($parentNodeCategory=null, $recursionLevel=3)
89: {
90: if (!is_null($parentNodeCategory) && $parentNodeCategory->getId()) {
91: return $this->getNode($parentNodeCategory, $recursionLevel);
92: }
93: $root = Mage::registry('root');
94: if (is_null($root)) {
95: $storeId = (int) $this->getRequest()->getParam('store');
96:
97: if ($storeId) {
98: $store = Mage::app()->getStore($storeId);
99: $rootId = $store->getRootCategoryId();
100: }
101: else {
102: $rootId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
103: }
104:
105: $tree = Mage::getResourceSingleton('catalog/category_tree')
106: ->load(null, $recursionLevel);
107:
108: if ($this->getCategory()) {
109: $tree->loadEnsuredNodes($this->getCategory(), $tree->getNodeById($rootId));
110: }
111:
112: $tree->addCollectionData($this->getCategoryCollection());
113:
114: $root = $tree->getNodeById($rootId);
115:
116: if ($root && $rootId != Mage_Catalog_Model_Category::TREE_ROOT_ID) {
117: $root->setIsVisible(true);
118: }
119: elseif($root && $root->getId() == Mage_Catalog_Model_Category::TREE_ROOT_ID) {
120: $root->setName(Mage::helper('catalog')->__('Root'));
121: }
122:
123: Mage::register('root', $root);
124: }
125:
126: return $root;
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136: 137:
138: public function getRootByIds($ids)
139: {
140: $root = Mage::registry('root');
141: if (null === $root) {
142: $categoryTreeResource = Mage::getResourceSingleton('catalog/category_tree');
143: $ids = $categoryTreeResource->getExistingCategoryIdsBySpecifiedIds($ids);
144: $tree = $categoryTreeResource->loadByIds($ids);
145: $rootId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
146: $root = $tree->getNodeById($rootId);
147: if ($root && $rootId != Mage_Catalog_Model_Category::TREE_ROOT_ID) {
148: $root->setIsVisible(true);
149: } else if($root && $root->getId() == Mage_Catalog_Model_Category::TREE_ROOT_ID) {
150: $root->setName(Mage::helper('catalog')->__('Root'));
151: }
152:
153: $tree->addCollectionData($this->getCategoryCollection());
154: Mage::register('root', $root);
155: }
156: return $root;
157: }
158:
159: public function getNode($parentNodeCategory, $recursionLevel=2)
160: {
161: $tree = Mage::getResourceModel('catalog/category_tree');
162:
163: $nodeId = $parentNodeCategory->getId();
164: $parentId = $parentNodeCategory->getParentId();
165:
166: $node = $tree->loadNode($nodeId);
167: $node->loadChildren($recursionLevel);
168:
169: if ($node && $nodeId != Mage_Catalog_Model_Category::TREE_ROOT_ID) {
170: $node->setIsVisible(true);
171: } elseif($node && $node->getId() == Mage_Catalog_Model_Category::TREE_ROOT_ID) {
172: $node->setName(Mage::helper('catalog')->__('Root'));
173: }
174:
175: $tree->addCollectionData($this->getCategoryCollection());
176:
177: return $node;
178: }
179:
180: public function getSaveUrl(array $args = array())
181: {
182: $params = array('_current'=>true);
183: $params = array_merge($params, $args);
184: return $this->getUrl('*/*/save', $params);
185: }
186:
187: public function getEditUrl()
188: {
189: return $this->getUrl("*/catalog_category/edit", array('_current'=>true, 'store'=>null, '_query'=>false, 'id'=>null, 'parent'=>null));
190: }
191:
192: 193: 194: 195: 196:
197: public function getRootIds()
198: {
199: $ids = $this->getData('root_ids');
200: if (is_null($ids)) {
201: $ids = array();
202: foreach (Mage::app()->getGroups() as $store) {
203: $ids[] = $store->getRootCategoryId();
204: }
205: $this->setData('root_ids', $ids);
206: }
207: return $ids;
208: }
209: }
210: