PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/DesignEditor/controllers/EditorController.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 218 lines | 165 code | 8 blank | 45 comment | 4 complexity | 242f235ca7dc8af1f1324d7673d1cdbe MD5 | raw file
  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_DesignEditor
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Controller that allows to display arbitrary page in design editor mode
  28. */
  29. class Mage_DesignEditor_EditorController extends Mage_Core_Controller_Front_Action
  30. {
  31. /**
  32. * @var Mage_DesignEditor_Model_Session
  33. */
  34. protected $_session;
  35. /**
  36. * Variable to store full action name
  37. *
  38. * @var string
  39. */
  40. protected $_fullActionName = '';
  41. /**
  42. * Enforce admin session with the active design editor mode
  43. *
  44. * @return Mage_DesignEditor_EditorController
  45. */
  46. public function preDispatch()
  47. {
  48. parent::preDispatch();
  49. $this->_session = Mage::getSingleton('Mage_DesignEditor_Model_Session');
  50. if (!$this->_session->isDesignEditorActive()) {
  51. Mage::getSingleton('Mage_Core_Model_Session')->addError(
  52. $this->__('Design editor is not initialized by administrator.')
  53. );
  54. $this->norouteAction();
  55. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  56. }
  57. return $this;
  58. }
  59. /**
  60. * Display an arbitrary page by specified page type
  61. */
  62. public function pageAction()
  63. {
  64. try {
  65. $handle = $this->getRequest()->getParam('handle');
  66. // page type format
  67. if (!$handle || !preg_match('/^[a-z][a-z\d]*(_[a-z][a-z\d]*)*$/i', $handle)) {
  68. Mage::throwException($this->__('Invalid page handle specified.'));
  69. }
  70. // whether such page type exists
  71. if (!$this->getLayout()->getUpdate()->pageHandleExists($handle)) {
  72. Mage::throwException(
  73. $this->__("Specified page type or page fragment type doesn't exist: '{$handle}'.")
  74. );
  75. }
  76. $this->_fullActionName = $handle;
  77. $this->addPageLayoutHandles();
  78. $this->loadLayoutUpdates();
  79. $this->generateLayoutXml();
  80. Mage::getModel('Mage_DesignEditor_Model_Layout')->sanitizeLayout($this->getLayout()->getNode());
  81. $this->generateLayoutBlocks();
  82. $blockHierarchy = $this->getLayout()->getBlock('design_editor_toolbar_handles_hierarchy');
  83. if ($blockHierarchy) {
  84. $blockHierarchy->setSelectedHandle($handle);
  85. }
  86. $blockBreadcrumbs = $this->getLayout()->getBlock('design_editor_toolbar_breadcrumbs');
  87. if ($blockBreadcrumbs) {
  88. $blockBreadcrumbs->setOmitCurrentPage(true);
  89. }
  90. $this->renderLayout();
  91. } catch (Mage_Core_Exception $e) {
  92. $this->getResponse()->setBody($e->getMessage());
  93. $this->getResponse()->setHeader('Content-Type', 'text/plain; charset=UTF-8')->setHttpResponseCode(503);
  94. }
  95. }
  96. /**
  97. * Hack the "full action name" in order to render emulated layout
  98. *
  99. * @param string $delimiter
  100. * @return string
  101. */
  102. public function getFullActionName($delimiter = '_')
  103. {
  104. if ($this->_fullActionName) {
  105. return $this->_fullActionName;
  106. }
  107. return parent::getFullActionName($delimiter);
  108. }
  109. /**
  110. * Sets new theme for viewed store and returns customer back to the previous address
  111. */
  112. public function themeAction()
  113. {
  114. $backUrl = $this->_getRefererUrl();
  115. $themeId = $this->getRequest()->get('theme_id');
  116. /** @var $theme Mage_Core_Model_Theme */
  117. $theme = Mage::getModel('Mage_Core_Model_Theme');
  118. try {
  119. $theme->load($themeId);
  120. if (!$theme->getId()) {
  121. Mage::throwException($this->__('The theme was not found.'));
  122. }
  123. $this->_session->setThemeId($themeId);
  124. } catch (Mage_Core_Exception $e) {
  125. $this->_session->addError($e->getMessage());
  126. }
  127. $this->getResponse()->setRedirect($backUrl);
  128. }
  129. /**
  130. * Compact history
  131. */
  132. public function compactHistoryAction()
  133. {
  134. $historyData = Mage::app()->getRequest()->getPost();
  135. /** @var $helper Mage_Core_Helper_Data */
  136. $helper = Mage::helper('Mage_Core_Helper_Data');
  137. if (!$historyData) {
  138. $this->getResponse()->setBody($helper->jsonEncode(
  139. array(Mage_Core_Model_Message::ERROR => array($this->__('Invalid post data')))
  140. ));
  141. return;
  142. }
  143. try {
  144. $historyModel = $this->_compactHistory($historyData);
  145. $response = array(Mage_Core_Model_Message::SUCCESS => array($historyModel->getChanges()->toArray()));
  146. } catch (Mage_Core_Exception $e) {
  147. $response = array(
  148. Mage_Core_Model_Message::ERROR => array($e->getMessage())
  149. );
  150. }
  151. $this->getResponse()->setBody($helper->jsonEncode($response));
  152. }
  153. /**
  154. * Compact history
  155. *
  156. * @param array $historyData
  157. * @return Mage_DesignEditor_Model_History
  158. */
  159. protected function _compactHistory($historyData)
  160. {
  161. /** @var $historyModel Mage_DesignEditor_Model_History */
  162. $historyModel = Mage::getModel('Mage_DesignEditor_Model_History');
  163. /** @var $historyCompactModel Mage_DesignEditor_Model_History_Compact */
  164. $historyCompactModel = Mage::getModel('Mage_DesignEditor_Model_History_Compact');
  165. /** @var $collection Mage_DesignEditor_Model_Change_Collection */
  166. $collection = $historyModel->setChanges($historyData)->getChanges();
  167. $historyCompactModel->compact($collection);
  168. return $historyModel;
  169. }
  170. /**
  171. * Get layout xml
  172. */
  173. public function getLayoutUpdateAction()
  174. {
  175. $historyData = Mage::app()->getRequest()->getPost();
  176. if (!$historyData) {
  177. $this->getResponse()->setBody(Mage::helper('Mage_Core_Helper_Data')->jsonEncode(
  178. array(Mage_Core_Model_Message::ERROR => array($this->__('Invalid post data')))
  179. ));
  180. return;
  181. }
  182. try {
  183. $historyModel = $this->_compactHistory($historyData);
  184. /** @var $layoutRenderer Mage_DesignEditor_Model_History_Renderer_LayoutUpdate */
  185. $layoutRenderer = Mage::getModel('Mage_DesignEditor_Model_History_Renderer_LayoutUpdate');
  186. $layoutUpdate = $historyModel->output($layoutRenderer);
  187. $this->getResponse()->setBody(Mage::helper('Mage_Core_Helper_Data')->jsonEncode(array(
  188. Mage_Core_Model_Message::SUCCESS => array($layoutUpdate)
  189. )));
  190. } catch (Mage_Core_Exception $e) {
  191. $this->getResponse()->setBody(Mage::helper('Mage_Core_Helper_Data')->jsonEncode(
  192. array(Mage_Core_Model_Message::ERROR => array($e->getMessage()))
  193. ));
  194. }
  195. }
  196. }