Mage_Adminhtml_Catalog_CategoryController Class Reference

Inheritance diagram for Mage_Adminhtml_Catalog_CategoryController:

Mage_Adminhtml_Controller_Action Mage_Core_Controller_Varien_Action

List of all members.

Public Member Functions

 indexAction ()
 addAction ()
 editAction ()
 categoriesJsonAction ()
 saveAction ()
 moveAction ()
 deleteAction ()
 gridAction ()
 treeAction ()

Protected Member Functions

 _initCategory ($getRootInstead=false)
 _isAllowed ()


Detailed Description

Definition at line 34 of file CategoryController.php.


Member Function Documentation

_initCategory ( getRootInstead = false  )  [protected]

Initialize requested category and put it into registry. Root category can be returned, if inappropriate store/category is specified

Parameters:
bool $getRootInstead
Returns:
Mage_Catalog_Model_Category

Definition at line 43 of file CategoryController.php.

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                     // load root category instead wrong one
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     }

_isAllowed (  )  [protected]

Reimplemented from Mage_Adminhtml_Controller_Action.

Definition at line 387 of file CategoryController.php.

00388     {
00389         return Mage::getSingleton('admin/session')->isAllowed('catalog/categories');
00390     }

addAction (  ) 

Add new category form

Definition at line 86 of file CategoryController.php.

00087     {
00088         $this->_forward('edit');
00089     }

categoriesJsonAction (  ) 

Get tree node (Ajax version)

Definition at line 195 of file CategoryController.php.

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     }

deleteAction (  ) 

Delete category action

Definition at line 325 of file CategoryController.php.

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     }

editAction (  ) 

Edit category page

Check if we have data in session (if duering category save was exceprion)

Build response for ajax request

Definition at line 94 of file CategoryController.php.

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            // $params['id'] = $_prevCategoryId;
00118              $this->getRequest()->setParam('id',$_prevCategoryId);
00119             //$redirect = true;
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          * Check if we have data in session (if duering category save was exceprion)
00138          */
00139         $data = Mage::getSingleton('adminhtml/session')->getCategoryData(true);
00140         if (isset($data['general'])) {
00141             $category->addData($data['general']);
00142         }
00143 
00144         /**
00145          * Build response for ajax request
00146          */
00147         if ($this->getRequest()->getQuery('isAjax')) {
00148             // prepare breadcrumbs of selected category, if any
00149             $breadcrumbsPath = $category->getPath();
00150             if (empty($breadcrumbsPath)) {
00151                 // but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
00152                 $breadcrumbsPath = Mage::getSingleton('admin/session')->getDeletedPath(true);
00153                 if (!empty($breadcrumbsPath)) {
00154                     $breadcrumbsPath = explode('/', $breadcrumbsPath);
00155                     // no need to get parent breadcrumbs if deleting category level 1
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 //            $this->_initLayoutMessages('adminhtml/session');
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     }

gridAction (  ) 

Definition at line 346 of file CategoryController.php.

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     }

indexAction (  ) 

Catalog categories index action

Definition at line 78 of file CategoryController.php.

00079     {
00080         $this->_forward('edit');
00081     }

moveAction (  ) 

Move category tree node action

Definition at line 283 of file CategoryController.php.

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     }

saveAction (  ) 

Category save

Check "Use Default Value" checkboxes values

Definition at line 218 of file CategoryController.php.

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              * Check "Use Default Value" checkboxes values
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     }

treeAction (  ) 

Definition at line 356 of file CategoryController.php.

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     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:10 2009 for Magento by  doxygen 1.5.8