PageRenderTime 443ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/controllers/Cms/PageController.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 276 lines | 155 code | 28 blank | 93 comment | 15 complexity | 1d8d78b016d3f37c04fcaf64013e4fad MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Cms manage pages controller
  28. *
  29. * @category Mage
  30. * @package Mage_Cms
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Cms_PageController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Init actions
  37. *
  38. * @return Mage_Adminhtml_Cms_PageController
  39. */
  40. protected function _initAction()
  41. {
  42. // load layout, set active menu and breadcrumbs
  43. $this->loadLayout()
  44. ->_setActiveMenu('cms/page')
  45. ->_addBreadcrumb(Mage::helper('cms')->__('CMS'), Mage::helper('cms')->__('CMS'))
  46. ->_addBreadcrumb(Mage::helper('cms')->__('Manage Pages'), Mage::helper('cms')->__('Manage Pages'))
  47. ;
  48. return $this;
  49. }
  50. /**
  51. * Index action
  52. */
  53. public function indexAction()
  54. {
  55. $this->_title($this->__('CMS'))
  56. ->_title($this->__('Pages'))
  57. ->_title($this->__('Manage Content'));
  58. $this->_initAction();
  59. $this->renderLayout();
  60. }
  61. /**
  62. * Create new CMS page
  63. */
  64. public function newAction()
  65. {
  66. // the same form is used to create and edit
  67. $this->_forward('edit');
  68. }
  69. /**
  70. * Edit CMS page
  71. */
  72. public function editAction()
  73. {
  74. $this->_title($this->__('CMS'))
  75. ->_title($this->__('Pages'))
  76. ->_title($this->__('Manage Content'));
  77. // 1. Get ID and create model
  78. $id = $this->getRequest()->getParam('page_id');
  79. $model = Mage::getModel('cms/page');
  80. // 2. Initial checking
  81. if ($id) {
  82. $model->load($id);
  83. if (! $model->getId()) {
  84. Mage::getSingleton('adminhtml/session')->addError(
  85. Mage::helper('cms')->__('This page no longer exists.'));
  86. $this->_redirect('*/*/');
  87. return;
  88. }
  89. }
  90. $this->_title($model->getId() ? $model->getTitle() : $this->__('New Page'));
  91. // 3. Set entered data if was error when we do save
  92. $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
  93. if (! empty($data)) {
  94. $model->setData($data);
  95. }
  96. // 4. Register model to use later in blocks
  97. Mage::register('cms_page', $model);
  98. // 5. Build edit form
  99. $this->_initAction()
  100. ->_addBreadcrumb(
  101. $id ? Mage::helper('cms')->__('Edit Page')
  102. : Mage::helper('cms')->__('New Page'),
  103. $id ? Mage::helper('cms')->__('Edit Page')
  104. : Mage::helper('cms')->__('New Page'));
  105. $this->renderLayout();
  106. }
  107. /**
  108. * Save action
  109. */
  110. public function saveAction()
  111. {
  112. // check if data sent
  113. if ($data = $this->getRequest()->getPost()) {
  114. $data = $this->_filterPostData($data);
  115. //init model and set data
  116. $model = Mage::getModel('cms/page');
  117. if ($id = $this->getRequest()->getParam('page_id')) {
  118. $model->load($id);
  119. }
  120. $model->setData($data);
  121. Mage::dispatchEvent('cms_page_prepare_save', array('page' => $model, 'request' => $this->getRequest()));
  122. //validating
  123. if (!$this->_validatePostData($data)) {
  124. $this->_redirect('*/*/edit', array('page_id' => $model->getId(), '_current' => true));
  125. return;
  126. }
  127. // try to save it
  128. try {
  129. // save the data
  130. $model->save();
  131. // display success message
  132. Mage::getSingleton('adminhtml/session')->addSuccess(
  133. Mage::helper('cms')->__('The page has been saved.'));
  134. // clear previously saved data from session
  135. Mage::getSingleton('adminhtml/session')->setFormData(false);
  136. // check if 'Save and Continue'
  137. if ($this->getRequest()->getParam('back')) {
  138. $this->_redirect('*/*/edit', array('page_id' => $model->getId(), '_current'=>true));
  139. return;
  140. }
  141. // go to grid
  142. $this->_redirect('*/*/');
  143. return;
  144. } catch (Mage_Core_Exception $e) {
  145. $this->_getSession()->addError($e->getMessage());
  146. }
  147. catch (Exception $e) {
  148. $this->_getSession()->addException($e,
  149. Mage::helper('cms')->__('An error occurred while saving the page.'));
  150. }
  151. $this->_getSession()->setFormData($data);
  152. $this->_redirect('*/*/edit', array('page_id' => $this->getRequest()->getParam('page_id')));
  153. return;
  154. }
  155. $this->_redirect('*/*/');
  156. }
  157. /**
  158. * Delete action
  159. */
  160. public function deleteAction()
  161. {
  162. // check if we know what should be deleted
  163. if ($id = $this->getRequest()->getParam('page_id')) {
  164. $title = "";
  165. try {
  166. // init model and delete
  167. $model = Mage::getModel('cms/page');
  168. $model->load($id);
  169. $title = $model->getTitle();
  170. $model->delete();
  171. // display success message
  172. Mage::getSingleton('adminhtml/session')->addSuccess(
  173. Mage::helper('cms')->__('The page has been deleted.'));
  174. // go to grid
  175. Mage::dispatchEvent('adminhtml_cmspage_on_delete', array('title' => $title, 'status' => 'success'));
  176. $this->_redirect('*/*/');
  177. return;
  178. } catch (Exception $e) {
  179. Mage::dispatchEvent('adminhtml_cmspage_on_delete', array('title' => $title, 'status' => 'fail'));
  180. // display error message
  181. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  182. // go back to edit form
  183. $this->_redirect('*/*/edit', array('page_id' => $id));
  184. return;
  185. }
  186. }
  187. // display error message
  188. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('cms')->__('Unable to find a page to delete.'));
  189. // go to grid
  190. $this->_redirect('*/*/');
  191. }
  192. /**
  193. * Check the permission to run it
  194. *
  195. * @return boolean
  196. */
  197. protected function _isAllowed()
  198. {
  199. switch ($this->getRequest()->getActionName()) {
  200. case 'new':
  201. case 'save':
  202. return Mage::getSingleton('admin/session')->isAllowed('cms/page/save');
  203. break;
  204. case 'delete':
  205. return Mage::getSingleton('admin/session')->isAllowed('cms/page/delete');
  206. break;
  207. default:
  208. return Mage::getSingleton('admin/session')->isAllowed('cms/page');
  209. break;
  210. }
  211. }
  212. /**
  213. * Filtering posted data. Converting localized data if needed
  214. *
  215. * @param array
  216. * @return array
  217. */
  218. protected function _filterPostData($data)
  219. {
  220. $data = $this->_filterDates($data, array('custom_theme_from', 'custom_theme_to'));
  221. return $data;
  222. }
  223. /**
  224. * Validate post data
  225. *
  226. * @param array $data
  227. * @return bool Return FALSE if someone item is invalid
  228. */
  229. protected function _validatePostData($data)
  230. {
  231. $errorNo = true;
  232. if (!empty($data['layout_update_xml']) || !empty($data['custom_layout_update_xml'])) {
  233. /** @var $validatorCustomLayout Mage_Adminhtml_Model_LayoutUpdate_Validator */
  234. $validatorCustomLayout = Mage::getModel('adminhtml/layoutUpdate_validator');
  235. if (!empty($data['layout_update_xml']) && !$validatorCustomLayout->isValid($data['layout_update_xml'])) {
  236. $errorNo = false;
  237. }
  238. if (!empty($data['custom_layout_update_xml'])
  239. && !$validatorCustomLayout->isValid($data['custom_layout_update_xml'])) {
  240. $errorNo = false;
  241. }
  242. foreach ($validatorCustomLayout->getMessages() as $message) {
  243. $this->_getSession()->addError($message);
  244. }
  245. }
  246. return $errorNo;
  247. }
  248. }