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_UrlrewriteController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _initRegistry()
42: {
43: $this->_title($this->__('Rewrite Rules'));
44:
45:
46: Mage::register('current_urlrewrite', Mage::getModel('core/url_rewrite')
47: ->load($this->getRequest()->getParam('id', 0))
48: );
49: $productId = $this->getRequest()->getParam('product', 0);
50: $categoryId = $this->getRequest()->getParam('category', 0);
51: if (Mage::registry('current_urlrewrite')->getId()) {
52: $productId = Mage::registry('current_urlrewrite')->getProductId();
53: $categoryId = Mage::registry('current_urlrewrite')->getCategoryId();
54: }
55:
56: Mage::register('current_product', Mage::getModel('catalog/product')->load($productId));
57: Mage::register('current_category', Mage::getModel('catalog/category')->load($categoryId));
58:
59: return $this;
60: }
61:
62: 63: 64: 65:
66: public function indexAction()
67: {
68: $this->_initRegistry();
69: $this->loadLayout();
70: $this->_setActiveMenu('catalog/urlrewrite');
71: $this->_addContent(
72: $this->getLayout()->createBlock('adminhtml/urlrewrite')
73: );
74: $this->renderLayout();
75: }
76:
77: 78: 79: 80:
81: public function editAction()
82: {
83: $this->_initRegistry();
84:
85: $this->_title($this->__('URL Rewrite'));
86:
87: $this->loadLayout();
88: $this->_setActiveMenu('catalog/urlrewrite');
89: $this->_addContent($this->getLayout()->createBlock('adminhtml/urlrewrite_edit'));
90: $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
91: $this->renderLayout();
92: }
93:
94: 95: 96: 97:
98: public function productGridAction()
99: {
100: $this->getResponse()->setBody($this->getLayout()->createBlock('adminhtml/urlrewrite_product_grid')->toHtml());
101: }
102:
103: 104: 105: 106:
107: public function categoriesJsonAction()
108: {
109: $id = $this->getRequest()->getParam('id', null);
110: $this->getResponse()->setBody(Mage::getBlockSingleton('adminhtml/urlrewrite_category_tree')
111: ->getTreeArray($id, true, 1)
112: );
113: }
114:
115: 116: 117: 118:
119: public function saveAction()
120: {
121: $this->_initRegistry();
122:
123: if ($data = $this->getRequest()->getPost()) {
124: $session = Mage::getSingleton('adminhtml/session');
125: try {
126:
127: $model = Mage::registry('current_urlrewrite');
128:
129:
130: $requestPath = $this->getRequest()->getParam('request_path');
131: Mage::helper('core/url_rewrite')->validateRequestPath($requestPath);
132:
133:
134: $model->setIdPath($this->getRequest()->getParam('id_path'))
135: ->setTargetPath($this->getRequest()->getParam('target_path'))
136: ->setOptions($this->getRequest()->getParam('options'))
137: ->setDescription($this->getRequest()->getParam('description'))
138: ->setRequestPath($requestPath);
139:
140: if (!$model->getId()) {
141: $model->setIsSystem(0);
142: }
143: if (!$model->getIsSystem()) {
144: $model->setStoreId($this->getRequest()->getParam('store_id', 0));
145: }
146:
147:
148: $category = Mage::registry('current_category')->getId() ? Mage::registry('current_category') : null;
149: if ($category) {
150: $model->setCategoryId($category->getId());
151: }
152: $product = Mage::registry('current_product')->getId() ? Mage::registry('current_product') : null;
153: if ($product) {
154: $model->setProductId($product->getId());
155: }
156: if ($product || $category) {
157: $catalogUrlModel = Mage::getSingleton('catalog/url');
158: $idPath = $catalogUrlModel->generatePath('id', $product, $category);
159:
160:
161: $found = false;
162: if (in_array($model->getOptions(), array('R', 'RP'))) {
163: $rewrite = Mage::getResourceModel('catalog/url')
164: ->getRewriteByIdPath($idPath, $model->getStoreId());
165: if (!$rewrite) {
166: $exceptionTxt = 'Chosen product does not associated with the chosen store or category.';
167: Mage::throwException($exceptionTxt);
168: }
169: if($rewrite->getId() && $rewrite->getId() != $model->getId()) {
170: $model->setIdPath($idPath);
171: $model->setTargetPath($rewrite->getRequestPath());
172: $found = true;
173: }
174: }
175:
176: if (!$found) {
177: $model->setIdPath($idPath);
178: $model->setTargetPath($catalogUrlModel->generatePath('target', $product, $category));
179: }
180: }
181:
182:
183: $model->save();
184: $session->addSuccess(Mage::helper('adminhtml')->__('The URL Rewrite has been saved.'));
185: $this->_redirect('*/*/');
186: return;
187: } catch (Mage_Core_Exception $e) {
188: $session->addError($e->getMessage())
189: ->setUrlrewriteData($data);
190: } catch (Exception $e) {
191: $session->addException($e, Mage::helper('adminhtml')->__('An error occurred while saving URL Rewrite.'))
192: ->setUrlrewriteData($data);
193:
194: }
195: }
196: $this->_redirectReferer();
197: }
198:
199: 200: 201: 202:
203: public function deleteAction()
204: {
205: $this->_initRegistry();
206:
207: if (Mage::registry('current_urlrewrite')->getId()) {
208: try {
209: Mage::registry('current_urlrewrite')->delete();
210: Mage::getSingleton('adminhtml/session')->addSuccess(
211: Mage::helper('adminhtml')->__('The URL Rewrite has been deleted.')
212: );
213: } catch (Exception $e) {
214: Mage::getSingleton('adminhtml/session')
215: ->addException($e, Mage::helper('adminhtml')->__('An error occurred while deleting URL Rewrite.'));
216: $this->_redirect('*/*/edit/', array('id'=>Mage::registry('current_urlrewrite')->getId()));
217: return;
218: }
219: }
220: $this->_redirect('*/*/');
221: }
222:
223: 224: 225: 226: 227:
228: protected function _isAllowed()
229: {
230: return Mage::getSingleton('admin/session')->isAllowed('catalog/urlrewrite');
231: }
232: }
233: