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:
35: class Mage_Adminhtml_Cms_PageController extends Mage_Adminhtml_Controller_Action
36: {
37:
38: 39: 40: 41: 42:
43: protected function _initAction()
44: {
45:
46: $this->loadLayout()
47: ->_setActiveMenu('cms/page')
48: ->_addBreadcrumb(Mage::helper('cms')->__('CMS'), Mage::helper('cms')->__('CMS'))
49: ->_addBreadcrumb(Mage::helper('cms')->__('Manage Pages'), Mage::helper('cms')->__('Manage Pages'))
50: ;
51: return $this;
52: }
53:
54: 55: 56:
57: public function indexAction()
58: {
59: $this->_title($this->__('CMS'))
60: ->_title($this->__('Pages'))
61: ->_title($this->__('Manage Content'));
62:
63: $this->_initAction();
64: $this->renderLayout();
65: }
66:
67: 68: 69:
70: public function newAction()
71: {
72:
73: $this->_forward('edit');
74: }
75:
76: 77: 78:
79: public function editAction()
80: {
81: $this->_title($this->__('CMS'))
82: ->_title($this->__('Pages'))
83: ->_title($this->__('Manage Content'));
84:
85:
86: $id = $this->getRequest()->getParam('page_id');
87: $model = Mage::getModel('cms/page');
88:
89:
90: if ($id) {
91: $model->load($id);
92: if (! $model->getId()) {
93: Mage::getSingleton('adminhtml/session')->addError(
94: Mage::helper('cms')->__('This page no longer exists.'));
95: $this->_redirect('*/*/');
96: return;
97: }
98: }
99:
100: $this->_title($model->getId() ? $model->getTitle() : $this->__('New Page'));
101:
102:
103: $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
104: if (! empty($data)) {
105: $model->setData($data);
106: }
107:
108:
109: Mage::register('cms_page', $model);
110:
111:
112: $this->_initAction()
113: ->_addBreadcrumb(
114: $id ? Mage::helper('cms')->__('Edit Page')
115: : Mage::helper('cms')->__('New Page'),
116: $id ? Mage::helper('cms')->__('Edit Page')
117: : Mage::helper('cms')->__('New Page'));
118:
119: $this->renderLayout();
120: }
121:
122: 123: 124:
125: public function saveAction()
126: {
127:
128: if ($data = $this->getRequest()->getPost()) {
129: $data = $this->_filterPostData($data);
130:
131: $model = Mage::getModel('cms/page');
132:
133: if ($id = $this->getRequest()->getParam('page_id')) {
134: $model->load($id);
135: }
136:
137: $model->setData($data);
138:
139: Mage::dispatchEvent('cms_page_prepare_save', array('page' => $model, 'request' => $this->getRequest()));
140:
141:
142: if (!$this->_validatePostData($data)) {
143: $this->_redirect('*/*/edit', array('page_id' => $model->getId(), '_current' => true));
144: return;
145: }
146:
147:
148: try {
149:
150: $model->save();
151:
152:
153: Mage::getSingleton('adminhtml/session')->addSuccess(
154: Mage::helper('cms')->__('The page has been saved.'));
155:
156: Mage::getSingleton('adminhtml/session')->setFormData(false);
157:
158: if ($this->getRequest()->getParam('back')) {
159: $this->_redirect('*/*/edit', array('page_id' => $model->getId(), '_current'=>true));
160: return;
161: }
162:
163: $this->_redirect('*/*/');
164: return;
165:
166: } catch (Mage_Core_Exception $e) {
167: $this->_getSession()->addError($e->getMessage());
168: }
169: catch (Exception $e) {
170: $this->_getSession()->addException($e,
171: Mage::helper('cms')->__('An error occurred while saving the page.'));
172: }
173:
174: $this->_getSession()->setFormData($data);
175: $this->_redirect('*/*/edit', array('page_id' => $this->getRequest()->getParam('page_id')));
176: return;
177: }
178: $this->_redirect('*/*/');
179: }
180:
181: 182: 183:
184: public function deleteAction()
185: {
186:
187: if ($id = $this->getRequest()->getParam('page_id')) {
188: $title = "";
189: try {
190:
191: $model = Mage::getModel('cms/page');
192: $model->load($id);
193: $title = $model->getTitle();
194: $model->delete();
195:
196: Mage::getSingleton('adminhtml/session')->addSuccess(
197: Mage::helper('cms')->__('The page has been deleted.'));
198:
199: Mage::dispatchEvent('adminhtml_cmspage_on_delete', array('title' => $title, 'status' => 'success'));
200: $this->_redirect('*/*/');
201: return;
202:
203: } catch (Exception $e) {
204: Mage::dispatchEvent('adminhtml_cmspage_on_delete', array('title' => $title, 'status' => 'fail'));
205:
206: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
207:
208: $this->_redirect('*/*/edit', array('page_id' => $id));
209: return;
210: }
211: }
212:
213: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('cms')->__('Unable to find a page to delete.'));
214:
215: $this->_redirect('*/*/');
216: }
217:
218: 219: 220: 221: 222:
223: protected function _isAllowed()
224: {
225: switch ($this->getRequest()->getActionName()) {
226: case 'new':
227: case 'save':
228: return Mage::getSingleton('admin/session')->isAllowed('cms/page/save');
229: break;
230: case 'delete':
231: return Mage::getSingleton('admin/session')->isAllowed('cms/page/delete');
232: break;
233: default:
234: return Mage::getSingleton('admin/session')->isAllowed('cms/page');
235: break;
236: }
237: }
238:
239: 240: 241: 242: 243: 244:
245: protected function _filterPostData($data)
246: {
247: $data = $this->_filterDates($data, array('custom_theme_from', 'custom_theme_to'));
248: return $data;
249: }
250:
251: 252: 253: 254: 255: 256:
257: protected function _validatePostData($data)
258: {
259: $errorNo = true;
260: if (!empty($data['layout_update_xml']) || !empty($data['custom_layout_update_xml'])) {
261:
262: $validatorCustomLayout = Mage::getModel('adminhtml/layoutUpdate_validator');
263: if (!empty($data['layout_update_xml']) && !$validatorCustomLayout->isValid($data['layout_update_xml'])) {
264: $errorNo = false;
265: }
266: if (!empty($data['custom_layout_update_xml'])
267: && !$validatorCustomLayout->isValid($data['custom_layout_update_xml'])) {
268: $errorNo = false;
269: }
270: foreach ($validatorCustomLayout->getMessages() as $message) {
271: $this->_getSession()->addError($message);
272: }
273: }
274: return $errorNo;
275: }
276: }
277: