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: class Mage_Adminhtml_Catalog_CategoryController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40: 41: 42:
43: protected function _initCategory($getRootInstead = false)
44: {
45: $this->_title($this->__('Catalog'))
46: ->_title($this->__('Categories'))
47: ->_title($this->__('Manage Categories'));
48:
49: $categoryId = (int) $this->getRequest()->getParam('id',false);
50: $storeId = (int) $this->getRequest()->getParam('store');
51: $category = Mage::getModel('catalog/category');
52: $category->setStoreId($storeId);
53:
54: if ($categoryId) {
55: $category->load($categoryId);
56: if ($storeId) {
57: $rootId = Mage::app()->getStore($storeId)->getRootCategoryId();
58: if (!in_array($rootId, $category->getPathIds())) {
59:
60: if ($getRootInstead) {
61: $category->load($rootId);
62: }
63: else {
64: $this->_redirect('*/*/', array('_current'=>true, 'id'=>null));
65: return false;
66: }
67: }
68: }
69: }
70:
71: if ($activeTabId = (string) $this->getRequest()->getParam('active_tab_id')) {
72: Mage::getSingleton('admin/session')->setActiveTabId($activeTabId);
73: }
74:
75: Mage::register('category', $category);
76: Mage::register('current_category', $category);
77: Mage::getSingleton('cms/wysiwyg_config')->setStoreId($this->getRequest()->getParam('store'));
78: return $category;
79: }
80: 81: 82:
83: public function indexAction()
84: {
85: $this->_forward('edit');
86: }
87:
88: 89: 90:
91: public function addAction()
92: {
93: Mage::getSingleton('admin/session')->unsActiveTabId();
94: $this->_forward('edit');
95: }
96:
97: 98: 99:
100: public function editAction()
101: {
102: $params['_current'] = true;
103: $redirect = false;
104:
105: $storeId = (int) $this->getRequest()->getParam('store');
106: $parentId = (int) $this->getRequest()->getParam('parent');
107: $_prevStoreId = Mage::getSingleton('admin/session')
108: ->getLastViewedStore(true);
109:
110: if (!empty($_prevStoreId) && !$this->getRequest()->getQuery('isAjax')) {
111: $params['store'] = $_prevStoreId;
112: $redirect = true;
113: }
114:
115: $categoryId = (int) $this->getRequest()->getParam('id');
116: $_prevCategoryId = Mage::getSingleton('admin/session')
117: ->getLastEditedCategory(true);
118:
119:
120: if ($_prevCategoryId
121: && !$this->getRequest()->getQuery('isAjax')
122: && !$this->getRequest()->getParam('clear')) {
123:
124: $this->getRequest()->setParam('id',$_prevCategoryId);
125:
126: }
127:
128: if ($redirect) {
129: $this->_redirect('*/*/edit', $params);
130: return;
131: }
132:
133: if ($storeId && !$categoryId && !$parentId) {
134: $store = Mage::app()->getStore($storeId);
135: $_prevCategoryId = (int) $store->getRootCategoryId();
136: $this->getRequest()->setParam('id', $_prevCategoryId);
137: }
138:
139: if (!($category = $this->_initCategory(true))) {
140: return;
141: }
142:
143: $this->_title($categoryId ? $category->getName() : $this->__('New Category'));
144:
145: 146: 147:
148: $data = Mage::getSingleton('adminhtml/session')->getCategoryData(true);
149: if (isset($data['general'])) {
150: $category->addData($data['general']);
151: }
152:
153: 154: 155:
156: if ($this->getRequest()->getQuery('isAjax')) {
157:
158: $breadcrumbsPath = $category->getPath();
159: if (empty($breadcrumbsPath)) {
160:
161: $breadcrumbsPath = Mage::getSingleton('admin/session')->getDeletedPath(true);
162: if (!empty($breadcrumbsPath)) {
163: $breadcrumbsPath = explode('/', $breadcrumbsPath);
164:
165: if (count($breadcrumbsPath) <= 1) {
166: $breadcrumbsPath = '';
167: }
168: else {
169: array_pop($breadcrumbsPath);
170: $breadcrumbsPath = implode('/', $breadcrumbsPath);
171: }
172: }
173: }
174:
175: Mage::getSingleton('admin/session')
176: ->setLastViewedStore($this->getRequest()->getParam('store'));
177: Mage::getSingleton('admin/session')
178: ->setLastEditedCategory($category->getId());
179:
180: $this->loadLayout();
181:
182: $eventResponse = new Varien_Object(array(
183: 'content' => $this->getLayout()->getBlock('category.edit')->getFormHtml()
184: . $this->getLayout()->getBlock('category.tree')
185: ->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
186: 'messages' => $this->getLayout()->getMessagesBlock()->getGroupedHtml(),
187: ));
188:
189: Mage::dispatchEvent('category_prepare_ajax_response', array(
190: 'response' => $eventResponse,
191: 'controller' => $this
192: ));
193:
194: $this->getResponse()->setBody(
195: Mage::helper('core')->jsonEncode($eventResponse->getData())
196: );
197:
198: return;
199: }
200:
201: $this->loadLayout();
202: $this->_setActiveMenu('catalog/categories');
203: $this->getLayout()->getBlock('head')->setCanLoadExtJs(true)
204: ->setContainerCssClass('catalog-categories');
205:
206: $this->_addBreadcrumb(Mage::helper('catalog')->__('Manage Catalog Categories'),
207: Mage::helper('catalog')->__('Manage Categories')
208: );
209:
210: $block = $this->getLayout()->getBlock('catalog.wysiwyg.js');
211: if ($block) {
212: $block->setStoreId($storeId);
213: }
214:
215: $this->renderLayout();
216: }
217:
218: 219: 220: 221:
222: public function wysiwygAction()
223: {
224: $elementId = $this->getRequest()->getParam('element_id', md5(microtime()));
225: $storeId = $this->getRequest()->getParam('store_id', 0);
226: $storeMediaUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
227:
228: $content = $this->getLayout()->createBlock('adminhtml/catalog_helper_form_wysiwyg_content', '', array(
229: 'editor_element_id' => $elementId,
230: 'store_id' => $storeId,
231: 'store_media_url' => $storeMediaUrl,
232: ));
233:
234: $this->getResponse()->setBody($content->toHtml());
235: }
236:
237: 238: 239:
240: public function categoriesJsonAction()
241: {
242: if ($this->getRequest()->getParam('expand_all')) {
243: Mage::getSingleton('admin/session')->setIsTreeWasExpanded(true);
244: } else {
245: Mage::getSingleton('admin/session')->setIsTreeWasExpanded(false);
246: }
247: if ($categoryId = (int) $this->getRequest()->getPost('id')) {
248: $this->getRequest()->setParam('id', $categoryId);
249:
250: if (!$category = $this->_initCategory()) {
251: return;
252: }
253: $this->getResponse()->setBody(
254: $this->getLayout()->createBlock('adminhtml/catalog_category_tree')
255: ->getTreeJson($category)
256: );
257: }
258: }
259:
260: 261: 262:
263: public function saveAction()
264: {
265: if (!$category = $this->_initCategory()) {
266: return;
267: }
268:
269: $storeId = $this->getRequest()->getParam('store');
270: $refreshTree = 'false';
271: if ($data = $this->getRequest()->getPost()) {
272: $category->addData($data['general']);
273: if (!$category->getId()) {
274: $parentId = $this->getRequest()->getParam('parent');
275: if (!$parentId) {
276: if ($storeId) {
277: $parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
278: }
279: else {
280: $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
281: }
282: }
283: $parentCategory = Mage::getModel('catalog/category')->load($parentId);
284: $category->setPath($parentCategory->getPath());
285: }
286:
287: 288: 289:
290: if ($useConfig = $this->getRequest()->getPost('use_config')) {
291: foreach ($useConfig as $attributeCode) {
292: $category->setData($attributeCode, null);
293: }
294: }
295:
296: 297: 298:
299: if ($category->getId() && isset($data['general']['url_key_create_redirect']))
300:
301: {
302: $category->setData('save_rewrites_history', (bool)$data['general']['url_key_create_redirect']);
303: }
304:
305: $category->setAttributeSetId($category->getDefaultAttributeSetId());
306:
307: if (isset($data['category_products']) &&
308: !$category->getProductsReadonly()) {
309: $products = array();
310: parse_str($data['category_products'], $products);
311: $category->setPostedProducts($products);
312: }
313:
314: Mage::dispatchEvent('catalog_category_prepare_save', array(
315: 'category' => $category,
316: 'request' => $this->getRequest()
317: ));
318:
319: 320: 321: 322:
323: $category->setData("use_post_data_config", $this->getRequest()->getPost('use_config'));
324:
325: try {
326: $validate = $category->validate();
327: if ($validate !== true) {
328: foreach ($validate as $code => $error) {
329: if ($error === true) {
330: Mage::throwException(Mage::helper('catalog')->__('Attribute "%s" is required.', $category->getResource()->getAttribute($code)->getFrontend()->getLabel()));
331: }
332: else {
333: Mage::throwException($error);
334: }
335: }
336: }
337:
338: 339: 340:
341: if ($useDefaults = $this->getRequest()->getPost('use_default')) {
342: foreach ($useDefaults as $attributeCode) {
343: $category->setData($attributeCode, false);
344: }
345: }
346:
347: 348: 349:
350: $category->unsetData('use_post_data_config');
351:
352: $category->save();
353: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('The category has been saved.'));
354: $refreshTree = 'true';
355: }
356: catch (Exception $e){
357: $this->_getSession()->addError($e->getMessage())
358: ->setCategoryData($data);
359: $refreshTree = 'false';
360: }
361: }
362: $url = $this->getUrl('*/*/edit', array('_current' => true, 'id' => $category->getId()));
363: $this->getResponse()->setBody(
364: '<script type="text/javascript">parent.updateContent("' . $url . '", {}, '.$refreshTree.');</script>'
365: );
366: }
367:
368: 369: 370:
371: public function moveAction()
372: {
373: $category = $this->_initCategory();
374: if (!$category) {
375: $this->getResponse()->setBody(Mage::helper('catalog')->__('Category move error'));
376: return;
377: }
378: 379: 380:
381: $parentNodeId = $this->getRequest()->getPost('pid', false);
382: 383: 384:
385: $prevNodeId = $this->getRequest()->getPost('aid', false);
386:
387: try {
388: $category->move($parentNodeId, $prevNodeId);
389: $this->getResponse()->setBody("SUCCESS");
390: }
391: catch (Mage_Core_Exception $e) {
392: $this->getResponse()->setBody($e->getMessage());
393: }
394: catch (Exception $e){
395: $this->getResponse()->setBody(Mage::helper('catalog')->__('Category move error'.$e));
396: Mage::logException($e);
397: }
398:
399: }
400:
401: 402: 403:
404: public function deleteAction()
405: {
406: if ($id = (int) $this->getRequest()->getParam('id')) {
407: try {
408: $category = Mage::getModel('catalog/category')->load($id);
409: Mage::dispatchEvent('catalog_controller_category_delete', array('category'=>$category));
410:
411: Mage::getSingleton('admin/session')->setDeletedPath($category->getPath());
412:
413: $category->delete();
414: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('The category has been deleted.'));
415: }
416: catch (Mage_Core_Exception $e){
417: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
418: $this->getResponse()->setRedirect($this->getUrl('*/*/edit', array('_current'=>true)));
419: return;
420: }
421: catch (Exception $e){
422: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('An error occurred while trying to delete the category.'));
423: $this->getResponse()->setRedirect($this->getUrl('*/*/edit', array('_current'=>true)));
424: return;
425: }
426: }
427: $this->getResponse()->setRedirect($this->getUrl('*/*/', array('_current'=>true, 'id'=>null)));
428: }
429:
430: 431: 432: 433: 434: 435:
436: public function gridAction()
437: {
438: if (!$category = $this->_initCategory(true)) {
439: return;
440: }
441: $this->getResponse()->setBody(
442: $this->getLayout()->createBlock('adminhtml/catalog_category_tab_product', 'category.product.grid')
443: ->toHtml()
444: );
445: }
446:
447: 448: 449: 450: 451: 452:
453: public function treeAction()
454: {
455: $storeId = (int) $this->getRequest()->getParam('store');
456: $categoryId = (int) $this->getRequest()->getParam('id');
457:
458: if ($storeId) {
459: if (!$categoryId) {
460: $store = Mage::app()->getStore($storeId);
461: $rootId = $store->getRootCategoryId();
462: $this->getRequest()->setParam('id', $rootId);
463: }
464: }
465:
466: $category = $this->_initCategory(true);
467:
468: $block = $this->getLayout()->createBlock('adminhtml/catalog_category_tree');
469: $root = $block->getRoot();
470: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode(array(
471: 'data' => $block->getTree(),
472: 'parameters' => array(
473: 'text' => $block->buildNodeName($root),
474: 'draggable' => false,
475: 'allowDrop' => ($root->getIsVisible()) ? true : false,
476: 'id' => (int) $root->getId(),
477: 'expanded' => (int) $block->getIsWasExpanded(),
478: 'store_id' => (int) $block->getStore()->getId(),
479: 'category_id' => (int) $category->getId(),
480: 'root_visible'=> (int) $root->getIsVisible()
481: ))));
482: }
483:
484: 485: 486:
487: public function refreshPathAction()
488: {
489: if ($id = (int) $this->getRequest()->getParam('id')) {
490: $category = Mage::getModel('catalog/category')->load($id);
491: $this->getResponse()->setBody(
492: Mage::helper('core')->jsonEncode(array(
493: 'id' => $id,
494: 'path' => $category->getPath(),
495: ))
496: );
497: }
498: }
499:
500: 501: 502: 503: 504:
505: protected function _isAllowed()
506: {
507: return Mage::getSingleton('admin/session')->isAllowed('catalog/categories');
508: }
509: }
510: