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_SitemapController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _initAction()
42: {
43:
44: $this->loadLayout()
45: ->_setActiveMenu('catalog/system_sitemap')
46: ->_addBreadcrumb(
47: Mage::helper('catalog')->__('Catalog'),
48: Mage::helper('catalog')->__('Catalog'))
49: ->_addBreadcrumb(
50: Mage::helper('sitemap')->__('Google Sitemap'),
51: Mage::helper('sitemap')->__('Google Sitemap'))
52: ;
53: return $this;
54: }
55:
56: 57: 58:
59: public function indexAction()
60: {
61: $this->_title($this->__('Catalog'))->_title($this->__('Google Sitemaps'));
62:
63: $this->_initAction()
64: ->_addContent($this->getLayout()->createBlock('adminhtml/sitemap'))
65: ->renderLayout();
66: }
67:
68: 69: 70:
71: public function newAction()
72: {
73:
74: $this->_forward('edit');
75: }
76:
77: 78: 79:
80: public function editAction()
81: {
82: $this->_title($this->__('Catalog'))->_title($this->__('Google Sitemaps'));
83:
84:
85: $id = $this->getRequest()->getParam('sitemap_id');
86: $model = Mage::getModel('sitemap/sitemap');
87:
88:
89: if ($id) {
90: $model->load($id);
91: if (! $model->getId()) {
92: Mage::getSingleton('adminhtml/session')->addError(
93: Mage::helper('sitemap')->__('This sitemap no longer exists.'));
94: $this->_redirect('*/*/');
95: return;
96: }
97: }
98:
99: $this->_title($model->getId() ? $model->getSitemapFilename() : $this->__('New Sitemap'));
100:
101:
102: $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
103: if (! empty($data)) {
104: $model->setData($data);
105: }
106:
107:
108: Mage::register('sitemap_sitemap', $model);
109:
110:
111: $this->_initAction()
112: ->_addBreadcrumb(
113: $id ? Mage::helper('sitemap')->__('Edit Sitemap') : Mage::helper('sitemap')->__('New Sitemap'),
114: $id ? Mage::helper('sitemap')->__('Edit Sitemap') : Mage::helper('sitemap')->__('New Sitemap'))
115: ->_addContent($this->getLayout()->createBlock('adminhtml/sitemap_edit'))
116: ->renderLayout();
117: }
118:
119: 120: 121:
122: public function saveAction()
123: {
124:
125: if ($data = $this->getRequest()->getPost()) {
126:
127: $model = Mage::getModel('sitemap/sitemap');
128:
129:
130: if (!empty($data['sitemap_filename']) && !empty($data['sitemap_path'])) {
131: $path = rtrim($data['sitemap_path'], '\\/')
132: . DS . $data['sitemap_filename'];
133:
134: $validator = Mage::getModel('core/file_validator_availablePath');
135:
136: $helper = Mage::helper('adminhtml/catalog');
137: $validator->setPaths($helper->getSitemapValidPaths());
138: if (!$validator->isValid($path)) {
139: foreach ($validator->getMessages() as $message) {
140: Mage::getSingleton('adminhtml/session')->addError($message);
141: }
142:
143: Mage::getSingleton('adminhtml/session')->setFormData($data);
144:
145: $this->_redirect('*/*/edit', array(
146: 'sitemap_id' => $this->getRequest()->getParam('sitemap_id')));
147: return;
148: }
149: }
150:
151: if ($this->getRequest()->getParam('sitemap_id')) {
152: $model ->load($this->getRequest()->getParam('sitemap_id'));
153:
154: if ($model->getSitemapFilename() && file_exists($model->getPreparedFilename())){
155: unlink($model->getPreparedFilename());
156: }
157: }
158:
159: $model->setData($data);
160:
161:
162: try {
163:
164: $model->save();
165:
166: Mage::getSingleton('adminhtml/session')->addSuccess(
167: Mage::helper('sitemap')->__('The sitemap has been saved.'));
168:
169: Mage::getSingleton('adminhtml/session')->setFormData(false);
170:
171:
172: if ($this->getRequest()->getParam('back')) {
173: $this->_redirect('*/*/edit', array('sitemap_id' => $model->getId()));
174: return;
175: }
176:
177: if ($this->getRequest()->getParam('generate')) {
178: $this->getRequest()->setParam('sitemap_id', $model->getId());
179: $this->_forward('generate');
180: return;
181: }
182: $this->_redirect('*/*/');
183: return;
184:
185: } catch (Exception $e) {
186:
187: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
188:
189: Mage::getSingleton('adminhtml/session')->setFormData($data);
190:
191: $this->_redirect('*/*/edit', array(
192: 'sitemap_id' => $this->getRequest()->getParam('sitemap_id')));
193: return;
194: }
195: }
196: $this->_redirect('*/*/');
197:
198: }
199:
200: 201: 202:
203: public function deleteAction()
204: {
205:
206: if ($id = $this->getRequest()->getParam('sitemap_id')) {
207: try {
208:
209: $model = Mage::getModel('sitemap/sitemap');
210: $model->setId($id);
211:
212:
213:
214: $model->load($id);
215:
216: if ($model->getSitemapFilename() && file_exists($model->getPreparedFilename())){
217: unlink($model->getPreparedFilename());
218: }
219: $model->delete();
220:
221: Mage::getSingleton('adminhtml/session')->addSuccess(
222: Mage::helper('sitemap')->__('The sitemap has been deleted.'));
223:
224: $this->_redirect('*/*/');
225: return;
226:
227: } catch (Exception $e) {
228:
229: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
230:
231: $this->_redirect('*/*/edit', array('sitemap_id' => $id));
232: return;
233: }
234: }
235:
236: Mage::getSingleton('adminhtml/session')->addError(
237: Mage::helper('sitemap')->__('Unable to find a sitemap to delete.'));
238:
239: $this->_redirect('*/*/');
240: }
241:
242: 243: 244:
245: public function generateAction()
246: {
247:
248: $id = $this->getRequest()->getParam('sitemap_id');
249: $sitemap = Mage::getModel('sitemap/sitemap');
250:
251: $sitemap->load($id);
252:
253: if ($sitemap->getId()) {
254: try {
255: $sitemap->generateXml();
256:
257: $this->_getSession()->addSuccess(
258: Mage::helper('sitemap')->__('The sitemap "%s" has been generated.', $sitemap->getSitemapFilename()));
259: }
260: catch (Mage_Core_Exception $e) {
261: $this->_getSession()->addError($e->getMessage());
262: }
263: catch (Exception $e) {
264: $this->_getSession()->addException($e,
265: Mage::helper('sitemap')->__('Unable to generate the sitemap.'));
266: }
267: } else {
268: $this->_getSession()->addError(
269: Mage::helper('sitemap')->__('Unable to find a sitemap to generate.'));
270: }
271:
272:
273: $this->_redirect('*/*/');
274: }
275:
276: 277: 278: 279: 280:
281: protected function _isAllowed()
282: {
283: return Mage::getSingleton('admin/session')->isAllowed('catalog/sitemap');
284: }
285: }
286: