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_Catalog_CategoryController extends Mage_Adminhtml_Controller_Action
00035 {
00036
00037
00038
00039
00040
00041
00042
00043 protected function _initCategory($getRootInstead = false)
00044 {
00045 $categoryId = (int) $this->getRequest()->getParam('id',false);
00046 $storeId = (int) $this->getRequest()->getParam('store');
00047 $category = Mage::getModel('catalog/category');
00048 $category->setStoreId($storeId);
00049
00050 if ($categoryId) {
00051 $category->load($categoryId);
00052 if ($storeId) {
00053 $rootId = Mage::app()->getStore($storeId)->getRootCategoryId();
00054 if (!in_array($rootId, $category->getPathIds())) {
00055
00056 if ($getRootInstead) {
00057 $category->load($rootId);
00058 }
00059 else {
00060 $this->_redirect('*/*/', array('_current'=>true, 'id'=>null));
00061 return false;
00062 }
00063 }
00064 }
00065 }
00066
00067 if ($activeTabId = (string) $this->getRequest()->getParam('active_tab_id')) {
00068 Mage::getSingleton('admin/session')->setActiveTabId($activeTabId);
00069 }
00070
00071 Mage::register('category', $category);
00072 Mage::register('current_category', $category);
00073 return $category;
00074 }
00075
00076
00077
00078 public function indexAction()
00079 {
00080 $this->_forward('edit');
00081 }
00082
00083
00084
00085
00086 public function addAction()
00087 {
00088 $this->_forward('edit');
00089 }
00090
00091
00092
00093
00094 public function editAction()
00095 {
00096 $params['_current'] = true;
00097 $redirect = false;
00098
00099 $storeId = (int) $this->getRequest()->getParam('store');
00100 $parentId = (int) $this->getRequest()->getParam('parent');
00101 $_prevStoreId = Mage::getSingleton('admin/session')
00102 ->getLastViewedStore(true);
00103
00104 if ($_prevStoreId != null && !$this->getRequest()->getQuery('isAjax')) {
00105 $params['store'] = $_prevStoreId;
00106 $redirect = true;
00107 }
00108
00109 $categoryId = (int) $this->getRequest()->getParam('id');
00110 $_prevCategoryId = Mage::getSingleton('admin/session')
00111 ->getLastEditedCategory(true);
00112
00113
00114 if ($_prevCategoryId
00115 && !$this->getRequest()->getQuery('isAjax')
00116 && !$this->getRequest()->getParam('clear')) {
00117
00118 $this->getRequest()->setParam('id',$_prevCategoryId);
00119
00120 }
00121
00122 if ($redirect) {
00123 $this->_redirect('*/*/edit', $params);
00124 return;
00125 }
00126
00127 if ($storeId && !$categoryId && !$parentId) {
00128 $store = Mage::app()->getStore($storeId);
00129 $_prevCategoryId = (int) $store->getRootCategoryId();
00130 $this->getRequest()->setParam('id', $_prevCategoryId);
00131 }
00132
00133 if (!($category = $this->_initCategory(true))) {
00134 return;
00135 }
00136
00137
00138
00139 $data = Mage::getSingleton('adminhtml/session')->getCategoryData(true);
00140 if (isset($data['general'])) {
00141 $category->addData($data['general']);
00142 }
00143
00144
00145
00146
00147 if ($this->getRequest()->getQuery('isAjax')) {
00148
00149 $breadcrumbsPath = $category->getPath();
00150 if (empty($breadcrumbsPath)) {
00151
00152 $breadcrumbsPath = Mage::getSingleton('admin/session')->getDeletedPath(true);
00153 if (!empty($breadcrumbsPath)) {
00154 $breadcrumbsPath = explode('/', $breadcrumbsPath);
00155
00156 if (count($breadcrumbsPath) <= 1) {
00157 $breadcrumbsPath = '';
00158 }
00159 else {
00160 array_pop($breadcrumbsPath);
00161 $breadcrumbsPath = implode('/', $breadcrumbsPath);
00162 }
00163 }
00164 }
00165
00166 Mage::getSingleton('admin/session')
00167 ->setLastViewedStore($this->getRequest()->getParam('store'));
00168 Mage::getSingleton('admin/session')
00169 ->setLastEditedCategory($category->getId());
00170
00171 $this->loadLayout();
00172 $this->getResponse()->setBody(
00173 $this->getLayout()->getMessagesBlock()->getGroupedHtml()
00174 . $this->getLayout()->getBlock('category.edit')->getFormHtml()
00175 . $this->getLayout()->getBlock('category.tree')
00176 ->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs')
00177 );
00178 return;
00179 }
00180
00181 $this->loadLayout();
00182 $this->_setActiveMenu('catalog/categories');
00183 $this->getLayout()->getBlock('head')->setCanLoadExtJs(true)
00184 ->setContainerCssClass('catalog-categories');
00185
00186 $this->_addBreadcrumb(Mage::helper('catalog')->__('Manage Catalog Categories'),
00187 Mage::helper('catalog')->__('Manage Categories')
00188 );
00189 $this->renderLayout();
00190 }
00191
00192
00193
00194
00195 public function categoriesJsonAction()
00196 {
00197 if ($this->getRequest()->getParam('expand_all')) {
00198 Mage::getSingleton('admin/session')->setIsTreeWasExpanded(true);
00199 } else {
00200 Mage::getSingleton('admin/session')->setIsTreeWasExpanded(false);
00201 }
00202 if ($categoryId = (int) $this->getRequest()->getPost('id')) {
00203 $this->getRequest()->setParam('id', $categoryId);
00204
00205 if (!$category = $this->_initCategory()) {
00206 return;
00207 }
00208 $this->getResponse()->setBody(
00209 $this->getLayout()->createBlock('adminhtml/catalog_category_tree')
00210 ->getTreeJson($category)
00211 );
00212 }
00213 }
00214
00215
00216
00217
00218 public function saveAction()
00219 {
00220 if (!$category = $this->_initCategory()) {
00221 return;
00222 }
00223
00224 $storeId = $this->getRequest()->getParam('store');
00225 if ($data = $this->getRequest()->getPost()) {
00226 $category->addData($data['general']);
00227 if (!$category->getId()) {
00228 $parentId = $this->getRequest()->getParam('parent');
00229 if (!$parentId) {
00230 if ($storeId) {
00231 $parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
00232 }
00233 else {
00234 $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
00235 }
00236 }
00237 $parentCategory = Mage::getModel('catalog/category')->load($parentId);
00238 $category->setPath($parentCategory->getPath());
00239 }
00240
00241
00242
00243 if ($useDefaults = $this->getRequest()->getPost('use_default')) {
00244 foreach ($useDefaults as $attributeCode) {
00245 $category->setData($attributeCode, null);
00246 }
00247 }
00248
00249 $category->setAttributeSetId($category->getDefaultAttributeSetId());
00250
00251 if (isset($data['category_products']) &&
00252 !$category->getProductsReadonly()) {
00253 $products = array();
00254 parse_str($data['category_products'], $products);
00255 $category->setPostedProducts($products);
00256 }
00257
00258 Mage::dispatchEvent('catalog_category_prepare_save', array(
00259 'category' => $category,
00260 'request' => $this->getRequest()
00261 ));
00262
00263 try {
00264 $category->save();
00265 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('Category saved'));
00266 $refreshTree = 'true';
00267 }
00268 catch (Exception $e){
00269 $this->_getSession()->addError($e->getMessage())
00270 ->setCategoryData($data);
00271 $refreshTree = 'false';
00272 }
00273 }
00274 $url = $this->getUrl('*/*/edit', array('_current' => true, 'id' => $category->getId()));
00275 $this->getResponse()->setBody(
00276 '<script type="text/javascript">parent.updateContent("' . $url . '", {}, '.$refreshTree.');</script>'
00277 );
00278 }
00279
00280
00281
00282
00283 public function moveAction()
00284 {
00285 $nodeId = $this->getRequest()->getPost('id', false);
00286 $parentNodeId = $this->getRequest()->getPost('pid', false);
00287 $prevNodeId = $this->getRequest()->getPost('aid', false);
00288 $prevParentNodeId = $this->getRequest()->getPost('paid', false);
00289
00290 try {
00291 $tree = Mage::getResourceModel('catalog/category_tree')
00292 ->load();
00293
00294 $node = $tree->getNodeById($nodeId);
00295 $newParentNode = $tree->getNodeById($parentNodeId);
00296 $prevNode = $tree->getNodeById($prevNodeId);
00297
00298 if (!$prevNode || !$prevNode->getId()) {
00299 $prevNode = null;
00300 }
00301
00302 $tree->move($node, $newParentNode, $prevNode);
00303
00304 Mage::dispatchEvent('category_move',
00305 array(
00306 'category_id' => $nodeId,
00307 'prev_parent_id' => $prevParentNodeId,
00308 'parent_id' => $parentNodeId
00309 ));
00310
00311 $this->getResponse()->setBody("SUCCESS");
00312 }
00313 catch (Mage_Core_Exception $e) {
00314 $this->getResponse()->setBody($e->getMessage());
00315 }
00316 catch (Exception $e){
00317 $this->getResponse()->setBody(Mage::helper('catalog')->__('Category move error'));
00318 }
00319
00320 }
00321
00322
00323
00324
00325 public function deleteAction()
00326 {
00327 if ($id = (int) $this->getRequest()->getParam('id')) {
00328 try {
00329 $category = Mage::getModel('catalog/category')->load($id);
00330 Mage::dispatchEvent('catalog_controller_category_delete', array('category'=>$category));
00331
00332 Mage::getSingleton('admin/session')->setDeletedPath($category->getPath());
00333
00334 $category->delete();
00335 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('Category deleted'));
00336 }
00337 catch (Exception $e){
00338 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Category delete error'));
00339 $this->getResponse()->setRedirect($this->getUrl('*/*/edit', array('_current'=>true)));
00340 return;
00341 }
00342 }
00343 $this->getResponse()->setRedirect($this->getUrl('*/*/', array('_current'=>true, 'id'=>null)));
00344 }
00345
00346 public function gridAction()
00347 {
00348 if (!$category = $this->_initCategory(true)) {
00349 return;
00350 }
00351 $this->getResponse()->setBody(
00352 $this->getLayout()->createBlock('adminhtml/catalog_category_tab_product')->toHtml()
00353 );
00354 }
00355
00356 public function treeAction()
00357 {
00358 $storeId = (int) $this->getRequest()->getParam('store');
00359 $categoryId = (int) $this->getRequest()->getParam('id');
00360
00361 if ($storeId) {
00362 if (!$categoryId) {
00363 $store = Mage::app()->getStore($storeId);
00364 $rootId = $store->getRootCategoryId();
00365 $this->getRequest()->setParam('id', $rootId);
00366 }
00367 }
00368
00369 $category = $this->_initCategory(true);
00370
00371 $block = $this->getLayout()->createBlock('adminhtml/catalog_category_tree');
00372 $root = $block->getRoot();
00373 $this->getResponse()->setBody(Zend_Json::encode(array(
00374 'data' => $block->getTree(),
00375 'parameters' => array(
00376 'text' => $block->buildNodeName($root),
00377 'draggable' => false,
00378 'allowDrop' => ($root->getIsVisible()) ? true : false,
00379 'id' => (int) $root->getId(),
00380 'expanded' => (int) $block->getIsWasExpanded(),
00381 'store_id' => (int) $block->getStore()->getId(),
00382 'category_id' => (int) $category->getId(),
00383 'root_visible'=> (int) $root->getIsVisible()
00384 ))));
00385 }
00386
00387 protected function _isAllowed()
00388 {
00389 return Mage::getSingleton('admin/session')->isAllowed('catalog/categories');
00390 }
00391 }