PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Mage/DesignEditor/Block/Adminhtml/Editor/Toolbar/Buttons/Save.php

https://github.com/JackoPlane/magento2
PHP | 265 lines | 140 code | 23 blank | 102 comment | 13 complexity | 337429674ef780189985742b606a2143 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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) 2013 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. * Save button block
  28. */
  29. class Mage_DesignEditor_Block_Adminhtml_Editor_Toolbar_Buttons_Save
  30. extends Mage_Backend_Block_Widget_Button_Split
  31. {
  32. /**
  33. * Current theme used for preview
  34. *
  35. * @var Mage_Core_Model_Theme
  36. */
  37. protected $_theme;
  38. /**
  39. * Init save button
  40. *
  41. * @return $this
  42. * @throws InvalidArgumentException
  43. */
  44. public function init()
  45. {
  46. $theme = $this->getTheme();
  47. $themeType = $theme->getType();
  48. if ($themeType == Mage_Core_Model_Theme::TYPE_PHYSICAL) {
  49. $this->_initPhysical();
  50. } else if ($themeType == Mage_Core_Model_Theme::TYPE_VIRTUAL) {
  51. if ($theme->getDomainModel(Mage_Core_Model_Theme::TYPE_VIRTUAL)->isAssigned()) {
  52. $this->_initAssigned();
  53. } else {
  54. $this->_initUnAssigned();
  55. }
  56. } else {
  57. throw new InvalidArgumentException(
  58. sprintf('Invalid theme of a "%s" type passed to save button block', $themeType)
  59. );
  60. }
  61. return $this;
  62. }
  63. /**
  64. * Get current theme
  65. *
  66. * @return Mage_Core_Model_Theme
  67. * @throws InvalidArgumentException
  68. */
  69. public function getTheme()
  70. {
  71. if (null === $this->_theme) {
  72. throw new InvalidArgumentException('Current theme was not passed to save button');
  73. }
  74. return $this->_theme;
  75. }
  76. /**
  77. * Set current theme
  78. *
  79. * @param Mage_Core_Model_Theme $theme
  80. * @return Mage_DesignEditor_Block_Adminhtml_Editor_Toolbar_Buttons
  81. */
  82. public function setTheme($theme)
  83. {
  84. $this->_theme = $theme;
  85. return $this;
  86. }
  87. /**
  88. * Whether button is disabled
  89. *
  90. * @return mixed
  91. */
  92. public function getDisabled()
  93. {
  94. return false;
  95. }
  96. /**
  97. * Disable actions-split functionality if no options provided
  98. *
  99. *
  100. * @return bool
  101. */
  102. public function hasSplit()
  103. {
  104. $options = $this->getOptions();
  105. return is_array($options) && count($options) > 0;
  106. }
  107. /**
  108. * Get URL to apply changes from 'staging' theme to 'virtual' theme
  109. *
  110. * @return string
  111. */
  112. public function getSaveUrl()
  113. {
  114. return $this->getUrl('*/system_design_editor/save', array('theme_id' => $this->getTheme()->getId()));
  115. }
  116. /**
  117. * Init 'Save' button for 'physical' theme
  118. *
  119. * @return $this
  120. */
  121. protected function _initPhysical()
  122. {
  123. $this->setData(array(
  124. 'label' => $this->__('Assign'),
  125. 'data_attribute' => array('mage-init' => $this->_getAssignInitData()),
  126. 'options' => array()
  127. ));
  128. return $this;
  129. }
  130. /**
  131. * Init 'Save' button for 'virtual' theme assigned to a store
  132. *
  133. * @return $this
  134. */
  135. protected function _initAssigned()
  136. {
  137. $this->setData(array(
  138. 'label' => $this->__('Save'),
  139. 'data_attribute' => array('mage-init' => $this->_getSaveAndAssignInitData()),
  140. 'options' => array()
  141. ));
  142. return $this;
  143. }
  144. /**
  145. * Init 'Save' button for 'virtual' theme assigned to a store
  146. *
  147. * @return $this
  148. */
  149. protected function _initUnAssigned()
  150. {
  151. $this->setData(array(
  152. 'label' => $this->__('Save'),
  153. 'data_attribute' => array('mage-init' => $this->_getSaveInitData()),
  154. 'options' => array(
  155. array(
  156. 'label' => $this->__('Save'),
  157. 'data_attribute' => array('mage-init' => $this->_getSaveInitData()),
  158. 'disabled' => true
  159. ),
  160. array(
  161. 'label' => $this->__('Save and Assign'),
  162. 'data_attribute' => array('mage-init' => $this->_getSaveAndAssignInitData())
  163. ),
  164. )
  165. ));
  166. return $this;
  167. }
  168. /**
  169. * Get 'data-mage-init' attribute value for 'Save' button
  170. *
  171. * @return string
  172. */
  173. protected function _getSaveInitData()
  174. {
  175. $message = "You are about to apply current changes for your live store, are you sure want to do this?";
  176. $data = array(
  177. 'button' => array(
  178. 'event' => 'save',
  179. 'target' => 'body',
  180. 'eventData' => array(
  181. 'theme_id' => $this->getTheme()->getId(),
  182. 'save_url' => $this->getSaveUrl(),
  183. 'confirm_message' => $this->__($message)
  184. )
  185. ),
  186. );
  187. return $this->_encode($data);
  188. }
  189. /**
  190. * Get 'data-mage-init' attribute value for 'Save' button
  191. *
  192. * @return string
  193. */
  194. protected function _getAssignInitData()
  195. {
  196. $message = "You are about to apply this theme for your live store, are you sure want to do this?\n\n" .
  197. 'Note: copy of the current theme will be created automatically and assigned to your store, ' .
  198. 'so you can change your copy as you wish';
  199. $data = array(
  200. 'button' => array(
  201. 'event' => 'assign',
  202. 'target' => 'body',
  203. 'eventData' => array(
  204. 'theme_id' => $this->getTheme()->getId(),
  205. 'confirm_message' => $this->__($message)
  206. )
  207. ),
  208. );
  209. return $this->_encode($data);
  210. }
  211. /**
  212. * Get 'data-mage-init' attribute value for 'Save and Assign' button
  213. *
  214. * @return string
  215. */
  216. protected function _getSaveAndAssignInitData()
  217. {
  218. $message = "You are about to apply current changes for your live store, are you sure want to do this?";
  219. $data = array(
  220. 'button' => array(
  221. 'event' => 'save-and-assign',
  222. 'target' => 'body',
  223. 'eventData' => array(
  224. 'theme_id' => $this->getTheme()->getId(),
  225. 'save_url' => $this->getSaveUrl(),
  226. 'confirm_message' => $this->__($message)
  227. )
  228. ),
  229. );
  230. return $this->_encode($data);
  231. }
  232. /**
  233. * Get encoded data string
  234. *
  235. * @param array $data
  236. * @return string
  237. */
  238. protected function _encode($data)
  239. {
  240. return $this->helper('Mage_Backend_Helper_Data')->escapeHtml(json_encode($data));
  241. }
  242. }