Edit.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Block\Adminhtml\Queue;
  7. use Magento\Framework\View\Element\AbstractBlock;
  8. use Magento\Newsletter\Model\Queue as ModelQueue;
  9. /**
  10. * Newsletter queue edit block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Edit extends \Magento\Backend\Block\Template
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $_template = 'Magento_Newsletter::queue/edit.phtml';
  21. /**
  22. * Core registry
  23. *
  24. * @var \Magento\Framework\Registry
  25. */
  26. protected $_coreRegistry = null;
  27. /**
  28. * @param \Magento\Backend\Block\Template\Context $context
  29. * @param \Magento\Framework\Registry $registry
  30. * @param array $data
  31. */
  32. public function __construct(
  33. \Magento\Backend\Block\Template\Context $context,
  34. \Magento\Framework\Registry $registry,
  35. array $data = []
  36. ) {
  37. $this->_coreRegistry = $registry;
  38. parent::__construct($context, $data);
  39. }
  40. /**
  41. * @return void
  42. */
  43. protected function _construct()
  44. {
  45. parent::_construct();
  46. $templateId = $this->getRequest()->getParam('template_id');
  47. if ($templateId) {
  48. $this->setTemplateId($templateId);
  49. }
  50. }
  51. /**
  52. * Retrieve current Newsletter Queue Object
  53. *
  54. * @return ModelQueue
  55. */
  56. public function getQueue()
  57. {
  58. return $this->_coreRegistry->registry('current_queue');
  59. }
  60. /**
  61. * Before rendering html, but after trying to load cache
  62. *
  63. * @return AbstractBlock
  64. */
  65. protected function _beforeToHtml()
  66. {
  67. $this->setChild(
  68. 'form',
  69. $this->getLayout()->createBlock(\Magento\Newsletter\Block\Adminhtml\Queue\Edit\Form::class, 'form')
  70. );
  71. return parent::_beforeToHtml();
  72. }
  73. /**
  74. * Get the url for save
  75. *
  76. * @return string
  77. */
  78. public function getSaveUrl()
  79. {
  80. if ($this->getTemplateId()) {
  81. $params = ['template_id' => $this->getTemplateId()];
  82. } else {
  83. $params = ['id' => $this->getRequest()->getParam('id')];
  84. }
  85. return $this->getUrl('*/*/save', $params);
  86. }
  87. /**
  88. * Prepare for the layout
  89. *
  90. * @return AbstractBlock
  91. */
  92. protected function _prepareLayout()
  93. {
  94. $this->getToolbar()->addChild(
  95. 'back_button',
  96. \Magento\Backend\Block\Widget\Button::class,
  97. [
  98. 'label' => __('Back'),
  99. 'onclick' => "window.location.href = '" . $this->getUrl(
  100. $this->getTemplateId() ? '*/template' : '*/*'
  101. ) . "'",
  102. 'class' => 'action-back'
  103. ]
  104. );
  105. $this->getToolbar()->addChild(
  106. 'reset_button',
  107. \Magento\Backend\Block\Widget\Button::class,
  108. ['label' => __('Reset'), 'class' => 'reset', 'onclick' => 'window.location = window.location']
  109. );
  110. $this->getToolbar()->addChild(
  111. 'preview_button',
  112. \Magento\Backend\Block\Widget\Button::class,
  113. ['label' => __('Preview Template'), 'onclick' => 'queueControl.preview();', 'class' => 'preview']
  114. );
  115. $this->getToolbar()->addChild(
  116. 'save_button',
  117. \Magento\Backend\Block\Widget\Button::class,
  118. [
  119. 'label' => __('Save Newsletter'),
  120. 'class' => 'save primary',
  121. 'data_attribute' => [
  122. 'mage-init' => ['button' => ['event' => 'save', 'target' => '#queue_edit_form']],
  123. ]
  124. ]
  125. );
  126. $this->getToolbar()->addChild(
  127. 'save_and_resume',
  128. \Magento\Backend\Block\Widget\Button::class,
  129. [
  130. 'label' => __('Save and Resume'),
  131. 'class' => 'save',
  132. 'data_attribute' => [
  133. 'mage-init' => [
  134. 'button' => [
  135. 'event' => 'save',
  136. 'target' => '#queue_edit_form',
  137. 'eventData' => ['action' => ['args' => ['_resume' => 1]]],
  138. ],
  139. ],
  140. ]
  141. ]
  142. );
  143. return parent::_prepareLayout();
  144. }
  145. /**
  146. * Return preview action url for form
  147. *
  148. * @return string
  149. */
  150. public function getPreviewUrl()
  151. {
  152. if ($this->getTemplateId()) {
  153. $params = ['template_id' => $this->getTemplateId()];
  154. } else {
  155. $params = ['id' => $this->getRequest()->getParam('id')];
  156. }
  157. return $this->getUrl('*/*/preview', $params);
  158. }
  159. /**
  160. * Retrieve Preview Button HTML
  161. *
  162. * @return string
  163. */
  164. public function getPreviewButtonHtml()
  165. {
  166. return $this->getChildHtml('preview_button');
  167. }
  168. /**
  169. * Retrieve Save Button HTML
  170. *
  171. * @return string
  172. */
  173. public function getSaveButtonHtml()
  174. {
  175. return $this->getChildHtml('save_button');
  176. }
  177. /**
  178. * Retrieve Reset Button HTML
  179. *
  180. * @return string
  181. */
  182. public function getResetButtonHtml()
  183. {
  184. return $this->getChildHtml('reset_button');
  185. }
  186. /**
  187. * Retrieve Back Button HTML
  188. *
  189. * @return string
  190. */
  191. public function getBackButtonHtml()
  192. {
  193. return $this->getChildHtml('back_button');
  194. }
  195. /**
  196. * Retrieve Resume Button HTML
  197. *
  198. * @return string
  199. */
  200. public function getResumeButtonHtml()
  201. {
  202. return $this->getChildHtml('save_and_resume');
  203. }
  204. /**
  205. * Getter for availability preview mode
  206. *
  207. * @return bool
  208. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  209. */
  210. public function getIsPreview()
  211. {
  212. return !in_array(
  213. $this->getQueue()->getQueueStatus(),
  214. [ModelQueue::STATUS_NEVER, ModelQueue::STATUS_PAUSE]
  215. );
  216. }
  217. /**
  218. * Getter for single store mode check
  219. *
  220. * @return bool
  221. */
  222. protected function isSingleStoreMode()
  223. {
  224. return $this->_storeManager->isSingleStoreMode();
  225. }
  226. /**
  227. * Getter for id of current store (the only one in single-store mode and current in multi-stores mode)
  228. *
  229. * @return bool
  230. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  231. */
  232. protected function getStoreId()
  233. {
  234. return $this->_storeManager->getStore(true)->getId();
  235. }
  236. /**
  237. * Getter for check is this newsletter the plain text.
  238. *
  239. * @return bool
  240. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  241. */
  242. public function getIsTextType()
  243. {
  244. return $this->getQueue()->isPlain();
  245. }
  246. /**
  247. * Getter for availability resume action
  248. *
  249. * @return bool
  250. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  251. */
  252. public function getCanResume()
  253. {
  254. return in_array($this->getQueue()->getQueueStatus(), [ModelQueue::STATUS_PAUSE]);
  255. }
  256. /**
  257. * Getter for header text
  258. *
  259. * @return bool
  260. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  261. */
  262. public function getHeaderText()
  263. {
  264. return $this->getIsPreview() ? __('View Newsletter') : __('Edit Newsletter');
  265. }
  266. }