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 class Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Categories extends Mage_Adminhtml_Block_Catalog_Category_Tree
00035 {
00036 protected $_categoryIds;
00037 protected $_selectedNodes = null;
00038
00039 public function __construct()
00040 {
00041 parent::__construct();
00042 $this->setTemplate('catalog/product/edit/categories.phtml');
00043 }
00044
00045
00046
00047
00048
00049
00050 public function getProduct()
00051 {
00052 return Mage::registry('current_product');
00053 }
00054
00055
00056
00057
00058
00059
00060 public function isReadonly()
00061 {
00062 return $this->getProduct()->getCategoriesReadonly();
00063 }
00064
00065 protected function getCategoryIds()
00066 {
00067 return $this->getProduct()->getCategoryIds();
00068 }
00069
00070 public function getIdsString()
00071 {
00072 return implode(',', $this->getCategoryIds());
00073 }
00074
00075 public function getRootNode()
00076 {
00077
00078 $root = $this->getRoot();
00079 if ($root && in_array($root->getId(), $this->getCategoryIds())) {
00080 $root->setChecked(true);
00081 }
00082 return $root;
00083 }
00084
00085 public function getRoot($parentNodeCategory=null, $recursionLevel=3)
00086 {
00087 if (!is_null($parentNodeCategory) && $parentNodeCategory->getId()) {
00088 return $this->getNode($parentNodeCategory, $recursionLevel);
00089 }
00090 $root = Mage::registry('root');
00091 if (is_null($root)) {
00092 $storeId = (int) $this->getRequest()->getParam('store');
00093
00094 if ($storeId) {
00095 $store = Mage::app()->getStore($storeId);
00096 $rootId = $store->getRootCategoryId();
00097 }
00098 else {
00099 $rootId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
00100 }
00101
00102 $ids = $this->getSelectedCategoriesPathIds($rootId);
00103 $tree = Mage::getResourceSingleton('catalog/category_tree')
00104 ->loadByIds($ids, false, false);
00105
00106 if ($this->getCategory()) {
00107 $tree->loadEnsuredNodes($this->getCategory(), $tree->getNodeById($rootId));
00108 }
00109
00110 $tree->addCollectionData($this->getCategoryCollection());
00111
00112 $root = $tree->getNodeById($rootId);
00113
00114 if ($root && $rootId != Mage_Catalog_Model_Category::TREE_ROOT_ID) {
00115 $root->setIsVisible(true);
00116 if ($this->isReadonly()) {
00117 $root->setDisabled(true);
00118 }
00119 }
00120 elseif($root && $root->getId() == Mage_Catalog_Model_Category::TREE_ROOT_ID) {
00121 $root->setName(Mage::helper('catalog')->__('Root'));
00122 }
00123
00124 Mage::register('root', $root);
00125 }
00126
00127 return $root;
00128 }
00129
00130 protected function _getNodeJson($node, $level=1)
00131 {
00132 $item = parent::_getNodeJson($node, $level);
00133
00134 $isParent = $this->_isParentSelectedCategory($node);
00135
00136 if ($isParent) {
00137 $item['expanded'] = true;
00138 }
00139
00140
00141
00142
00143
00144
00145 if (in_array($node->getId(), $this->getCategoryIds())) {
00146 $item['checked'] = true;
00147 }
00148
00149 if ($this->isReadonly()) {
00150 $item['disabled'] = true;
00151 }
00152 return $item;
00153 }
00154
00155 protected function _isParentSelectedCategory($node)
00156 {
00157 foreach ($this->_getSelectedNodes() as $selected) {
00158 if ($selected) {
00159 $pathIds = explode('/', $selected->getPathId());
00160 if (in_array($node->getId(), $pathIds)) {
00161 return true;
00162 }
00163 }
00164 }
00165
00166 return false;
00167 }
00168
00169 protected function _getSelectedNodes()
00170 {
00171 if ($this->_selectedNodes === null) {
00172 $this->_selectedNodes = array();
00173 foreach ($this->getCategoryIds() as $categoryId) {
00174 $this->_selectedNodes[] = $this->getRoot()->getTree()->getNodeById($categoryId);
00175 }
00176 }
00177
00178 return $this->_selectedNodes;
00179 }
00180
00181 public function getCategoryChildrenJson($categoryId)
00182 {
00183 $category = Mage::getModel('catalog/category')->load($categoryId);
00184 $node = $this->getRoot($category, 1)->getTree()->getNodeById($categoryId);
00185
00186 if (!$node || !$node->hasChildren()) {
00187 return '[]';
00188 }
00189
00190 $children = array();
00191 foreach ($node->getChildren() as $child) {
00192 $children[] = $this->_getNodeJson($child);
00193 }
00194
00195 return Zend_Json::encode($children);
00196 }
00197
00198 public function getLoadTreeUrl($expanded=null)
00199 {
00200 return $this->getUrl('*/*/categoriesJson', array('_current'=>true));
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 public function getSelectedCategoriesPathIds($rootId = false)
00210 {
00211 $ids = array();
00212 $collection = Mage::getModel('catalog/category')->getCollection()
00213 ->addFieldToFilter('entity_id', array('in'=>$this->getCategoryIds()));
00214 foreach ($collection as $item) {
00215 if ($rootId && !in_array($rootId, $item->getPathIds())) {
00216 continue;
00217 }
00218 foreach ($item->getPathIds() as $id) {
00219 if (!in_array($id, $ids)) {
00220 $ids[] = $id;
00221 }
00222 }
00223 }
00224 return $ids;
00225 }
00226 }