Edit.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Newsletter Template Edit Block
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Newsletter\Block\Adminhtml\Template;
  12. use Magento\Backend\Block\Widget;
  13. use Magento\Framework\App\TemplateTypesInterface;
  14. /**
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Edit extends Widget
  19. {
  20. /**
  21. * Core registry
  22. *
  23. * @var \Magento\Framework\Registry
  24. */
  25. protected $_coreRegistry = null;
  26. /**
  27. * @var \Magento\Cms\Model\Wysiwyg\Config
  28. */
  29. protected $_wysiwygConfig;
  30. /**
  31. * @param \Magento\Backend\Block\Template\Context $context
  32. * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
  33. * @param \Magento\Framework\Registry $registry
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Backend\Block\Template\Context $context,
  38. \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
  39. \Magento\Framework\Registry $registry,
  40. array $data = []
  41. ) {
  42. $this->_coreRegistry = $registry;
  43. $this->_wysiwygConfig = $wysiwygConfig;
  44. parent::__construct($context, $data);
  45. }
  46. /**
  47. * Retrieve template object
  48. *
  49. * @return \Magento\Newsletter\Model\Template
  50. */
  51. public function getModel()
  52. {
  53. return $this->_coreRegistry->registry('_current_template');
  54. }
  55. /**
  56. * Preparing block layout
  57. *
  58. * @return $this
  59. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  60. */
  61. protected function _prepareLayout()
  62. {
  63. $this->getToolbar()->addChild(
  64. 'back_button',
  65. \Magento\Backend\Block\Widget\Button::class,
  66. [
  67. 'label' => __('Back'),
  68. 'onclick' => "window.location.href = '" . $this->getUrl('*/*') . "'",
  69. 'class' => 'action-back'
  70. ]
  71. );
  72. $this->getToolbar()->addChild(
  73. 'reset_button',
  74. \Magento\Backend\Block\Widget\Button::class,
  75. [
  76. 'label' => __('Reset'),
  77. 'onclick' => 'window.location.href = window.location.href',
  78. 'class' => 'reset'
  79. ]
  80. );
  81. if (!$this->isTextType()) {
  82. $this->getToolbar()->addChild(
  83. 'to_plain_button',
  84. \Magento\Backend\Block\Widget\Button::class,
  85. [
  86. 'label' => __('Convert to Plain Text'),
  87. 'data_attribute' => [
  88. 'role' => 'template-strip',
  89. ],
  90. 'id' => 'convert_button',
  91. 'class' => 'convert'
  92. ]
  93. );
  94. $this->getToolbar()->addChild(
  95. 'to_html_button',
  96. \Magento\Backend\Block\Widget\Button::class,
  97. [
  98. 'label' => __('Return HTML Version'),
  99. 'data_attribute' => [
  100. 'role' => 'template-unstrip',
  101. ],
  102. 'id' => 'convert_button_back',
  103. 'style' => 'display:none',
  104. 'class' => 'return'
  105. ]
  106. );
  107. }
  108. $this->getToolbar()->addChild(
  109. 'preview_button',
  110. \Magento\Backend\Block\Widget\Button::class,
  111. [
  112. 'label' => __('Preview Template'),
  113. 'data_attribute' => [
  114. 'role' => 'template-preview',
  115. ],
  116. 'class' => 'preview'
  117. ]
  118. );
  119. if ($this->getEditMode()) {
  120. $this->getToolbar()->addChild(
  121. 'delete_button',
  122. \Magento\Backend\Block\Widget\Button::class,
  123. [
  124. 'label' => __('Delete Template'),
  125. 'data_attribute' => [
  126. 'role' => 'template-delete',
  127. ],
  128. 'class' => 'delete'
  129. ]
  130. );
  131. $this->getToolbar()->addChild(
  132. 'save_as_button',
  133. \Magento\Backend\Block\Widget\Button::class,
  134. [
  135. 'label' => __('Save As'),
  136. 'data_attribute' => [
  137. 'role' => 'template-save-as',
  138. ],
  139. 'class' => 'save-as'
  140. ]
  141. );
  142. }
  143. $this->getToolbar()->addChild(
  144. 'save_button',
  145. \Magento\Backend\Block\Widget\Button::class,
  146. [
  147. 'label' => __('Save Template'),
  148. 'data_attribute' => [
  149. 'role' => 'template-save',
  150. ],
  151. 'class' => 'save primary'
  152. ]
  153. );
  154. return parent::_prepareLayout();
  155. }
  156. /**
  157. * Return edit flag for block
  158. *
  159. * @return boolean
  160. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  161. */
  162. public function getEditMode()
  163. {
  164. if ($this->getModel()->getId()) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * Return header text for form
  171. *
  172. * @return \Magento\Framework\Phrase
  173. */
  174. public function getHeaderText()
  175. {
  176. if ($this->getEditMode()) {
  177. return __('Edit Newsletter Template');
  178. }
  179. return __('New Newsletter Template');
  180. }
  181. /**
  182. * Return form block HTML
  183. *
  184. * @return string
  185. */
  186. public function getForm()
  187. {
  188. return $this->getLayout()->createBlock(
  189. \Magento\Newsletter\Block\Adminhtml\Template\Edit\Form::class
  190. )->toHtml();
  191. }
  192. /**
  193. * Return template name for JS
  194. *
  195. * @return string
  196. */
  197. public function getJsTemplateName()
  198. {
  199. return addcslashes($this->getModel()->getTemplateCode(), "\"\r\n\\");
  200. }
  201. /**
  202. * Return action url for form
  203. *
  204. * @return string
  205. */
  206. public function getSaveUrl()
  207. {
  208. return $this->getUrl('*/*/save');
  209. }
  210. /**
  211. * Return preview action url for form
  212. *
  213. * @return string
  214. */
  215. public function getPreviewUrl()
  216. {
  217. return $this->getUrl('*/*/preview', ['id' => $this->getRequest()->getParam('id')]);
  218. }
  219. /**
  220. * Check Template Type is Plain Text
  221. *
  222. * @return bool
  223. */
  224. public function isTextType()
  225. {
  226. return $this->getModel()->isPlain();
  227. }
  228. /**
  229. * Return template type from template object or TYPE_HTML by default
  230. *
  231. * @return int
  232. */
  233. public function getTemplateType()
  234. {
  235. if ($this->getModel()->getTemplateType()) {
  236. return $this->getModel()->getTemplateType();
  237. }
  238. return TemplateTypesInterface::TYPE_HTML;
  239. }
  240. /**
  241. * Return delete url for customer group
  242. *
  243. * @return string
  244. */
  245. public function getDeleteUrl()
  246. {
  247. return $this->getUrl('*/*/delete', ['id' => $this->getRequest()->getParam('id')]);
  248. }
  249. /**
  250. * Retrieve Save As Flag
  251. *
  252. * @return int
  253. */
  254. public function getSaveAsFlag()
  255. {
  256. return $this->getRequest()->getParam('_save_as_flag') ? '1' : '';
  257. }
  258. /**
  259. * Getter for single store mode check
  260. *
  261. * @return boolean
  262. */
  263. protected function isSingleStoreMode()
  264. {
  265. return $this->_storeManager->isSingleStoreMode();
  266. }
  267. /**
  268. * Getter for id of current store (the only one in single-store mode and current in multi-stores mode)
  269. *
  270. * @return int
  271. */
  272. protected function getStoreId()
  273. {
  274. return $this->_storeManager->getStore(true)->getId();
  275. }
  276. }