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:
35: class Mage_Adminhtml_Block_Catalog_Category_Tree extends Mage_Adminhtml_Block_Catalog_Category_Abstract
36: {
37:
38: protected $_withProductCount;
39:
40: public function __construct()
41: {
42: parent::__construct();
43: $this->setTemplate('catalog/category/tree.phtml');
44: $this->setUseAjax(true);
45: $this->_withProductCount = true;
46: }
47:
48: protected function _prepareLayout()
49: {
50: $addUrl = $this->getUrl("*/*/add", array(
51: '_current'=>true,
52: 'id'=>null,
53: '_query' => false
54: ));
55:
56: $this->setChild('add_sub_button',
57: $this->getLayout()->createBlock('adminhtml/widget_button')
58: ->setData(array(
59: 'label' => Mage::helper('catalog')->__('Add Subcategory'),
60: 'onclick' => "addNew('".$addUrl."', false)",
61: 'class' => 'add',
62: 'id' => 'add_subcategory_button',
63: 'style' => $this->canAddSubCategory() ? '' : 'display: none;'
64: ))
65: );
66:
67: if ($this->canAddRootCategory()) {
68: $this->setChild('add_root_button',
69: $this->getLayout()->createBlock('adminhtml/widget_button')
70: ->setData(array(
71: 'label' => Mage::helper('catalog')->__('Add Root Category'),
72: 'onclick' => "addNew('".$addUrl."', true)",
73: 'class' => 'add',
74: 'id' => 'add_root_category_button'
75: ))
76: );
77: }
78:
79: $this->setChild('store_switcher',
80: $this->getLayout()->createBlock('adminhtml/store_switcher')
81: ->setSwitchUrl($this->getUrl('*/*/*', array('_current'=>true, '_query'=>false, 'store'=>null)))
82: ->setTemplate('store/switcher/enhanced.phtml')
83: );
84: return parent::_prepareLayout();
85: }
86:
87: protected function _getDefaultStoreId()
88: {
89: return Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
90: }
91:
92: public function getCategoryCollection()
93: {
94: $storeId = $this->getRequest()->getParam('store', $this->_getDefaultStoreId());
95: $collection = $this->getData('category_collection');
96: if (is_null($collection)) {
97: $collection = Mage::getModel('catalog/category')->getCollection();
98:
99:
100: $collection->addAttributeToSelect('name')
101: ->addAttributeToSelect('is_active')
102: ->setProductStoreId($storeId)
103: ->setLoadProductCount($this->_withProductCount)
104: ->setStoreId($storeId);
105:
106: $this->setData('category_collection', $collection);
107: }
108: return $collection;
109: }
110:
111: public function getAddRootButtonHtml()
112: {
113: return $this->getChildHtml('add_root_button');
114: }
115:
116: public function getAddSubButtonHtml()
117: {
118: return $this->getChildHtml('add_sub_button');
119: }
120:
121: public function getExpandButtonHtml()
122: {
123: return $this->getChildHtml('expand_button');
124: }
125:
126: public function getCollapseButtonHtml()
127: {
128: return $this->getChildHtml('collapse_button');
129: }
130:
131: public function getStoreSwitcherHtml()
132: {
133: return $this->getChildHtml('store_switcher');
134: }
135:
136: public function getLoadTreeUrl($expanded=null)
137: {
138: $params = array('_current'=>true, 'id'=>null,'store'=>null);
139: if (
140: (is_null($expanded) && Mage::getSingleton('admin/session')->getIsTreeWasExpanded())
141: || $expanded == true) {
142: $params['expand_all'] = true;
143: }
144: return $this->getUrl('*/*/categoriesJson', $params);
145: }
146:
147: public function getNodesUrl()
148: {
149: return $this->getUrl('*/catalog_category/jsonTree');
150: }
151:
152: public function getSwitchTreeUrl()
153: {
154: return $this->getUrl("*/catalog_category/tree", array('_current'=>true, 'store'=>null, '_query'=>false, 'id'=>null, 'parent'=>null));
155: }
156:
157: public function getIsWasExpanded()
158: {
159: return Mage::getSingleton('admin/session')->getIsTreeWasExpanded();
160: }
161:
162: public function getMoveUrl()
163: {
164: return $this->getUrl('*/catalog_category/move', array('store'=>$this->getRequest()->getParam('store')));
165: }
166:
167: public function getTree($parenNodeCategory=null)
168: {
169: $rootArray = $this->_getNodeJson($this->getRoot($parenNodeCategory));
170: $tree = isset($rootArray['children']) ? $rootArray['children'] : array();
171: return $tree;
172: }
173:
174: public function getTreeJson($parenNodeCategory=null)
175: {
176: $rootArray = $this->_getNodeJson($this->getRoot($parenNodeCategory));
177: $json = Mage::helper('core')->jsonEncode(isset($rootArray['children']) ? $rootArray['children'] : array());
178: return $json;
179: }
180:
181: 182: 183: 184: 185: 186: 187:
188: public function getBreadcrumbsJavascript($path, $javascriptVarName)
189: {
190: if (empty($path)) {
191: return '';
192: }
193:
194: $categories = Mage::getResourceSingleton('catalog/category_tree')
195: ->setStoreId($this->getStore()->getId())->loadBreadcrumbsArray($path);
196: if (empty($categories)) {
197: return '';
198: }
199: foreach ($categories as $key => $category) {
200: $categories[$key] = $this->_getNodeJson($category);
201: }
202: return
203: '<script type="text/javascript">'
204: . $javascriptVarName . ' = ' . Mage::helper('core')->jsonEncode($categories) . ';'
205: . ($this->canAddSubCategory() ? '$("add_subcategory_button").show();' : '$("add_subcategory_button").hide();')
206: . '</script>';
207: }
208:
209: 210: 211: 212: 213: 214: 215:
216: protected function _getNodeJson($node, $level = 0)
217: {
218:
219: if (is_array($node)) {
220: $node = new Varien_Data_Tree_Node($node, 'entity_id', new Varien_Data_Tree);
221: }
222:
223: $item = array();
224: $item['text'] = $this->buildNodeName($node);
225:
226:
227: $rootForStores = in_array($node->getEntityId(), $this->getRootIds());
228:
229: $item['id'] = $node->getId();
230: $item['store'] = (int) $this->getStore()->getId();
231: $item['path'] = $node->getData('path');
232:
233: $item['cls'] = 'folder ' . ($node->getIsActive() ? 'active-category' : 'no-active-category');
234:
235: $allowMove = $this->_isCategoryMoveable($node);
236: $item['allowDrop'] = $allowMove;
237:
238: $item['allowDrag'] = $allowMove && (($node->getLevel()==1 && $rootForStores) ? false : true);
239:
240: if ((int)$node->getChildrenCount()>0) {
241: $item['children'] = array();
242: }
243:
244: $isParent = $this->_isParentSelectedCategory($node);
245:
246: if ($node->hasChildren()) {
247: $item['children'] = array();
248: if (!($this->getUseAjax() && $node->getLevel() > 1 && !$isParent)) {
249: foreach ($node->getChildren() as $child) {
250: $item['children'][] = $this->_getNodeJson($child, $level+1);
251: }
252: }
253: }
254:
255: if ($isParent || $node->getLevel() < 2) {
256: $item['expanded'] = true;
257: }
258:
259: return $item;
260: }
261:
262: 263: 264: 265: 266: 267:
268: public function buildNodeName($node)
269: {
270: $result = $this->htmlEscape($node->getName());
271: if ($this->_withProductCount) {
272: $result .= ' (' . $node->getProductCount() . ')';
273: }
274: return $result;
275: }
276:
277: protected function _isCategoryMoveable($node)
278: {
279: $options = new Varien_Object(array(
280: 'is_moveable' => true,
281: 'category' => $node
282: ));
283:
284: Mage::dispatchEvent('adminhtml_catalog_category_tree_is_moveable',
285: array('options'=>$options)
286: );
287:
288: return $options->getIsMoveable();
289: }
290:
291: protected function _isParentSelectedCategory($node)
292: {
293: if ($node && $this->getCategory()) {
294: $pathIds = $this->getCategory()->getPathIds();
295: if (in_array($node->getId(), $pathIds)) {
296: return true;
297: }
298: }
299:
300: return false;
301: }
302:
303: 304: 305: 306: 307:
308: public function isClearEdit()
309: {
310: return (bool) $this->getRequest()->getParam('clear');
311: }
312:
313: 314: 315: 316: 317:
318: public function canAddRootCategory()
319: {
320: $options = new Varien_Object(array('is_allow'=>true));
321: Mage::dispatchEvent(
322: 'adminhtml_catalog_category_tree_can_add_root_category',
323: array(
324: 'category' => $this->getCategory(),
325: 'options' => $options,
326: 'store' => $this->getStore()->getId()
327: )
328: );
329:
330: return $options->getIsAllow();
331: }
332:
333: 334: 335: 336: 337:
338: public function canAddSubCategory()
339: {
340: $options = new Varien_Object(array('is_allow'=>true));
341: Mage::dispatchEvent(
342: 'adminhtml_catalog_category_tree_can_add_sub_category',
343: array(
344: 'category' => $this->getCategory(),
345: 'options' => $options,
346: 'store' => $this->getStore()->getId()
347: )
348: );
349:
350: return $options->getIsAllow();
351: }
352: }
353: