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

/app/code/core/Mage/DesignEditor/Model/Observer.php

https://bitbucket.org/jokusafet/magento2
PHP | 196 lines | 103 code | 17 blank | 76 comment | 20 complexity | 0e2a4407a67dce96ac42b52db3d0453f 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. * Observer for design editor module
  28. */
  29. class Mage_DesignEditor_Model_Observer
  30. {
  31. /**#@+
  32. * VDE specific layout update handles
  33. */
  34. const HANDLE_PAGE = 'design_editor_page';
  35. const HANDLE_TOOLBAR = 'design_editor_toolbar';
  36. /**#@-*/
  37. /**
  38. * Renderer for wrapping html to be shown at frontend
  39. *
  40. * @var Mage_Core_Block_Template
  41. */
  42. protected $_wrappingRenderer = null;
  43. /**
  44. * Handler for 'controller_action_predispatch' event
  45. */
  46. public function preDispatch()
  47. {
  48. /* Deactivate the design editor, if the admin session has been already expired */
  49. if (!$this->_getSession()->isLoggedIn()) {
  50. $this->_getSession()->deactivateDesignEditor();
  51. }
  52. /* Deactivate the design editor, if the theme cannot be loaded */
  53. if ($this->_getSession()->isDesignEditorActive()) {
  54. /** @var $theme Mage_Core_Model_Theme */
  55. $theme = Mage::getModel('Mage_Core_Model_Theme');
  56. try {
  57. $theme->load($this->_getSession()->getThemeId());
  58. if (!$theme->getId()) {
  59. Mage::throwException(Mage::helper('Mage_DesignEditor_Helper_Data')->__('The theme was not found.'));
  60. }
  61. Mage::register('vde_theme', $theme);
  62. } catch (Exception $e) {
  63. $this->_getSession()->deactivateDesignEditor();
  64. Mage::logException($e);
  65. }
  66. }
  67. /* Apply custom design to the current page */
  68. if ($this->_getSession()->isDesignEditorActive() && $theme->getThemePath()) {
  69. Mage::getDesign()->setDesignTheme($theme->getThemePath());
  70. }
  71. }
  72. /**
  73. * Add the design editor toolbar to the current page
  74. *
  75. * @param Varien_Event_Observer $observer
  76. */
  77. public function addToolbar(Varien_Event_Observer $observer)
  78. {
  79. if (!$this->_getSession()->isDesignEditorActive()) {
  80. return;
  81. }
  82. /** @var $update Mage_Core_Model_Layout_Merge */
  83. $update = $observer->getEvent()->getLayout()->getUpdate();
  84. $handles = $update->getHandles();
  85. $handle = reset($handles);
  86. if ($handle && $update->getPageHandleType($handle) == Mage_Core_Model_Layout_Merge::TYPE_FRAGMENT) {
  87. $update->addHandle(self::HANDLE_PAGE);
  88. }
  89. $update->addHandle(self::HANDLE_TOOLBAR);
  90. }
  91. /**
  92. * Disable blocks HTML output caching
  93. */
  94. public function disableBlocksOutputCaching()
  95. {
  96. if (!$this->_getSession()->isDesignEditorActive()) {
  97. return;
  98. }
  99. Mage::app()->getCacheInstance()->banUse(Mage_Core_Block_Abstract::CACHE_GROUP);
  100. }
  101. /**
  102. * Set design_editor_active flag, which allows to load DesignEditor's CSS or JS scripts
  103. *
  104. * @param Varien_Event_Observer $observer
  105. */
  106. public function setDesignEditorFlag(Varien_Event_Observer $observer)
  107. {
  108. if (!$this->_getSession()->isDesignEditorActive()) {
  109. return;
  110. }
  111. /** @var $block Mage_Page_Block_Html_Head */
  112. $block = $observer->getEvent()->getLayout()->getBlock('head');
  113. if ($block) {
  114. $block->setDesignEditorActive(true);
  115. }
  116. }
  117. /**
  118. * Retrieve session instance for the design editor
  119. *
  120. * @return Mage_DesignEditor_Model_Session
  121. */
  122. protected function _getSession()
  123. {
  124. return Mage::getSingleton('Mage_DesignEditor_Model_Session');
  125. }
  126. /**
  127. * Wrap each element of a page that is being rendered, with a block-level HTML-element to highlight it in VDE
  128. *
  129. * Subscriber to event 'core_layout_render_element'
  130. *
  131. * @param Varien_Event_Observer $observer
  132. */
  133. public function wrapPageElement(Varien_Event_Observer $observer)
  134. {
  135. if (!$this->_getSession()->isDesignEditorActive()) {
  136. return;
  137. }
  138. if (!$this->_wrappingRenderer) {
  139. $this->_wrappingRenderer = Mage::getModel('Mage_DesignEditor_Block_Template', array('data' => array(
  140. 'template' => 'wrapping.phtml'
  141. )));
  142. }
  143. $event = $observer->getEvent();
  144. /** @var $layout Mage_Core_Model_Layout */
  145. $layout = $event->getData('layout');
  146. $elementName = $event->getData('element_name');
  147. /** @var $transport Varien_Object */
  148. $transport = $event->getData('transport');
  149. $block = $layout->getBlock($elementName);
  150. $isVde = ($block && 0 === strpos(get_class($block), 'Mage_DesignEditor_Block_'));
  151. $manipulationAllowed = $layout->isManipulationAllowed($elementName) && !$isVde;
  152. $isContainer = $layout->isContainer($elementName);
  153. if ($manipulationAllowed || $isContainer) {
  154. $elementId = 'vde_element_' . rtrim(strtr(base64_encode($elementName), '+/', '-_'), '=');
  155. $this->_wrappingRenderer->setData(array(
  156. 'element_id' => $elementId,
  157. 'element_title' => $layout->getElementProperty($elementName, 'label') ?: $elementName,
  158. 'element_html' => $transport->getData('output'),
  159. 'is_manipulation_allowed' => $manipulationAllowed,
  160. 'is_container' => $isContainer,
  161. 'element_name' => $elementName,
  162. ));
  163. $transport->setData('output', $this->_wrappingRenderer->toHtml());
  164. }
  165. /* Inject toolbar at the very beginning of the page */
  166. if ($elementName == 'after_body_start') {
  167. $elementHtml = $transport->getData('output');
  168. $toolbarHtml = $layout->renderElement('design_editor_toolbar');
  169. $transport->setData('output', $toolbarHtml . $elementHtml);
  170. }
  171. }
  172. /**
  173. * Deactivate the design editor
  174. */
  175. public function adminSessionUserLogout()
  176. {
  177. $this->_getSession()->deactivateDesignEditor();
  178. }
  179. }