00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Block_Catalog_Category_Tree extends Mage_Adminhtml_Block_Catalog_Category_Abstract
00036 {
00037
00038 protected $_withProductCount;
00039
00040 public function __construct()
00041 {
00042 parent::__construct();
00043 $this->setTemplate('catalog/category/tree.phtml');
00044 $this->setUseAjax(true);
00045 $this->_withProductCount = true;
00046 }
00047
00048 protected function _prepareLayout()
00049 {
00050 $addUrl = $this->getUrl("*/*/add", array(
00051 '_current'=>true,
00052 'id'=>null,
00053 '_query' => false
00054 ));
00055
00056 if ($this->canAddSubCategory()) {
00057 $this->setChild('add_sub_button',
00058 $this->getLayout()->createBlock('adminhtml/widget_button')
00059 ->setData(array(
00060 'label' => Mage::helper('catalog')->__('Add Subcategory'),
00061 'onclick' => "addNew('".$addUrl."', false)",
00062 'class' => 'add'
00063 ))
00064 );
00065 }
00066
00067 if ($this->canAddRootCategory()) {
00068 $this->setChild('add_root_button',
00069 $this->getLayout()->createBlock('adminhtml/widget_button')
00070 ->setData(array(
00071 'label' => Mage::helper('catalog')->__('Add Root Category'),
00072 'onclick' => "addNew('".$addUrl."', true)",
00073 'class' => 'add',
00074 'id' => 'add_root_category_button'
00075 ))
00076 );
00077 }
00078
00079 $this->setChild('store_switcher',
00080 $this->getLayout()->createBlock('adminhtml/store_switcher')
00081 ->setSwitchUrl($this->getUrl('*/*/*', array('_current'=>true, '_query'=>false, 'store'=>null)))
00082 ->setTemplate('store/switcher/enhanced.phtml')
00083 );
00084 return parent::_prepareLayout();
00085 }
00086
00087 protected function _getDefaultStoreId()
00088 {
00089 return Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
00090 }
00091
00092 public function getCategoryCollection()
00093 {
00094 $storeId = $this->getRequest()->getParam('store', $this->_getDefaultStoreId());
00095 $collection = $this->getData('category_collection');
00096 if (is_null($collection)) {
00097 $collection = Mage::getModel('catalog/category')->getCollection();
00098
00099
00100 $collection->addAttributeToSelect('name')
00101 ->addAttributeToSelect('is_active')
00102 ->setProductStoreId($storeId)
00103 ->setLoadProductCount($this->_withProductCount)
00104 ->setStoreId($storeId);
00105
00106 $this->setData('category_collection', $collection);
00107 }
00108 return $collection;
00109 }
00110
00111 public function getAddRootButtonHtml()
00112 {
00113 return $this->getChildHtml('add_root_button');
00114 }
00115
00116 public function getAddSubButtonHtml()
00117 {
00118 return $this->getChildHtml('add_sub_button');
00119 }
00120
00121 public function getExpandButtonHtml()
00122 {
00123 return $this->getChildHtml('expand_button');
00124 }
00125
00126 public function getCollapseButtonHtml()
00127 {
00128 return $this->getChildHtml('collapse_button');
00129 }
00130
00131 public function getStoreSwitcherHtml()
00132 {
00133 return $this->getChildHtml('store_switcher');
00134 }
00135
00136 public function getLoadTreeUrl($expanded=null)
00137 {
00138 $params = array('_current'=>true, 'id'=>null,'store'=>null);
00139 if (
00140 (is_null($expanded) && Mage::getSingleton('admin/session')->getIsTreeWasExpanded())
00141 || $expanded == true) {
00142 $params['expand_all'] = true;
00143 }
00144 return $this->getUrl('*/*/categoriesJson', $params);
00145 }
00146
00147 public function getNodesUrl()
00148 {
00149 return $this->getUrl('*/catalog_category/jsonTree');
00150 }
00151
00152 public function getSwitchTreeUrl()
00153 {
00154 return $this->getUrl("*/catalog_category/tree", array('_current'=>true, 'store'=>null, '_query'=>false, 'id'=>null, 'parent'=>null));
00155 }
00156
00157 public function getIsWasExpanded()
00158 {
00159 return Mage::getSingleton('admin/session')->getIsTreeWasExpanded();
00160 }
00161
00162 public function getMoveUrl()
00163 {
00164 return $this->getUrl('*/catalog_category/move', array('store'=>$this->getRequest()->getParam('store')));
00165 }
00166
00167 public function getTree($parenNodeCategory=null)
00168 {
00169 $rootArray = $this->_getNodeJson($this->getRoot($parenNodeCategory));
00170 $tree = isset($rootArray['children']) ? $rootArray['children'] : array();
00171 return $tree;
00172 }
00173
00174 public function getTreeJson($parenNodeCategory=null)
00175 {
00176 $rootArray = $this->_getNodeJson($this->getRoot($parenNodeCategory));
00177 $json = Zend_Json::encode(isset($rootArray['children']) ? $rootArray['children'] : array());
00178 return $json;
00179 }
00180
00181 public function getRootIds()
00182 {
00183 $ids = $this->getData('root_ids');
00184 if (is_null($ids)) {
00185 $ids = array();
00186 foreach (Mage::app()->getStores() as $store) {
00187 $ids[] = $store->getRootCategoryId();
00188 }
00189 $this->setData('root_ids', $ids);
00190 }
00191 return $ids;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201 public function getBreadcrumbsJavascript($path, $javascriptVarName)
00202 {
00203 if (empty($path)) {
00204 return '';
00205 }
00206 $categories = Mage::getResourceSingleton('catalog/category_tree')->loadBreadcrumbsArray($path);
00207 if (empty($categories)) {
00208 return '';
00209 }
00210 foreach ($categories as $key => $category) {
00211 $categories[$key] = $this->_getNodeJson($category);
00212 }
00213 return
00214 '<script type="text/javascript">'
00215 . $javascriptVarName . ' = ' . Zend_Json::encode($categories) . ';'
00216 . '</script>';
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226 protected function _getNodeJson($node, $level = 0)
00227 {
00228
00229 if (is_array($node)) {
00230 $node = new Varien_Data_Tree_Node($node, 'entity_id', new Varien_Data_Tree);
00231 }
00232
00233 $item = array();
00234 $item['text'] = $this->buildNodeName($node);
00235
00236
00237 $rootForStores = in_array($node->getEntityId(), $this->getRootIds());
00238
00239 $item['id'] = $node->getId();
00240 $item['store'] = (int) $this->getStore()->getId();
00241 $item['path'] = $node->getData('path');
00242
00243 $item['cls'] = 'folder ' . ($node->getIsActive() ? 'active-category' : 'no-active-category');
00244
00245 $allowMove = $this->_isCategoryMoveable($node);
00246 $item['allowDrop'] = $allowMove;
00247
00248 $item['allowDrag'] = $allowMove && (($node->getLevel()==1 && $rootForStores) ? false : true);
00249
00250 if ((int)$node->getChildrenCount()>0) {
00251 $item['children'] = array();
00252 }
00253
00254 $isParent = $this->_isParentSelectedCategory($node);
00255
00256 if ($node->hasChildren()) {
00257 $item['children'] = array();
00258 if (!($this->getUseAjax() && $node->getLevel() > 1 && !$isParent)) {
00259 foreach ($node->getChildren() as $child) {
00260 $item['children'][] = $this->_getNodeJson($child, $level+1);
00261 }
00262 }
00263 }
00264
00265 if ($isParent || $node->getLevel() < 2) {
00266 $item['expanded'] = true;
00267 }
00268
00269 return $item;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278 public function buildNodeName($node)
00279 {
00280 $result = $this->htmlEscape($node->getName());
00281 if ($this->_withProductCount) {
00282 $result .= ' (' . $node->getProductCount() . ')';
00283 }
00284 return $result;
00285 }
00286
00287 protected function _isCategoryMoveable($node)
00288 {
00289 $options = new Varien_Object(array(
00290 'is_moveable' => true,
00291 'category' => $node
00292 ));
00293
00294 Mage::dispatchEvent('adminhtml_catalog_category_tree_is_moveable',
00295 array('options'=>$options)
00296 );
00297
00298 return $options->getIsMoveable();
00299 }
00300
00301 protected function _isParentSelectedCategory($node)
00302 {
00303 if ($node && $this->getCategory()) {
00304 $pathIds = $this->getCategory()->getPathIds();
00305 if (in_array($node->getId(), $pathIds)) {
00306 return true;
00307 }
00308 }
00309
00310 return false;
00311 }
00312
00313
00314
00315
00316
00317
00318 public function isClearEdit()
00319 {
00320 return (bool) $this->getRequest()->getParam('clear');
00321 }
00322
00323
00324
00325
00326
00327
00328 public function canAddRootCategory()
00329 {
00330 $options = new Varien_Object(array('is_allow'=>true));
00331 Mage::dispatchEvent(
00332 'adminhtml_catalog_category_tree_can_add_root_category',
00333 array(
00334 'category' => $this->getCategory(),
00335 'options' => $options,
00336 'store' => $this->getStore()->getId()
00337 )
00338 );
00339
00340 return $options->getIsAllow();
00341 }
00342
00343
00344
00345
00346
00347
00348 public function canAddSubCategory()
00349 {
00350 $options = new Varien_Object(array('is_allow'=>true));
00351 Mage::dispatchEvent(
00352 'adminhtml_catalog_category_tree_can_add_sub_category',
00353 array(
00354 'category' => $this->getCategory(),
00355 'options' => $options,
00356 'store' => $this->getStore()->getId()
00357 )
00358 );
00359
00360 return $options->getIsAllow();
00361 }
00362 }