Form.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Newsletter Template Edit Form Block
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Newsletter\Block\Adminhtml\Template\Edit;
  12. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  13. {
  14. /**
  15. * @var \Magento\Cms\Model\Wysiwyg\Config
  16. */
  17. protected $_wysiwygConfig;
  18. /**
  19. * @param \Magento\Backend\Block\Template\Context $context
  20. * @param \Magento\Framework\Registry $registry
  21. * @param \Magento\Framework\Data\FormFactory $formFactory
  22. * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
  23. * @param array $data
  24. */
  25. public function __construct(
  26. \Magento\Backend\Block\Template\Context $context,
  27. \Magento\Framework\Registry $registry,
  28. \Magento\Framework\Data\FormFactory $formFactory,
  29. \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
  30. array $data = []
  31. ) {
  32. $this->_wysiwygConfig = $wysiwygConfig;
  33. parent::__construct($context, $registry, $formFactory, $data);
  34. }
  35. /**
  36. * Retrieve template object
  37. *
  38. * @return \Magento\Newsletter\Model\Template
  39. */
  40. public function getModel()
  41. {
  42. return $this->_coreRegistry->registry('_current_template');
  43. }
  44. /**
  45. * Prepare form before rendering HTML
  46. *
  47. * @return $this
  48. * @SuppressWarnings(PHPMD.NPathComplexity)
  49. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  50. */
  51. protected function _prepareForm()
  52. {
  53. $model = $this->getModel();
  54. $identity = $this->_scopeConfig->getValue(
  55. \Magento\Newsletter\Model\Subscriber::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY,
  56. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  57. );
  58. $identityName = $this->_scopeConfig->getValue(
  59. 'trans_email/ident_' . $identity . '/name',
  60. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  61. );
  62. $identityEmail = $this->_scopeConfig->getValue(
  63. 'trans_email/ident_' . $identity . '/email',
  64. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  65. );
  66. /** @var \Magento\Framework\Data\Form $form */
  67. $form = $this->_formFactory->create(
  68. ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
  69. );
  70. $fieldset = $form->addFieldset(
  71. 'base_fieldset',
  72. ['legend' => __('Template Information'), 'class' => 'fieldset-wide']
  73. );
  74. if ($model->getId()) {
  75. $fieldset->addField('id', 'hidden', ['name' => 'id', 'value' => $model->getId()]);
  76. }
  77. $fieldset->addField(
  78. 'code',
  79. 'text',
  80. [
  81. 'name' => 'code',
  82. 'label' => __('Template Name'),
  83. 'title' => __('Template Name'),
  84. 'required' => true,
  85. 'value' => $model->getTemplateCode()
  86. ]
  87. );
  88. $fieldset->addField(
  89. 'subject',
  90. 'text',
  91. [
  92. 'name' => 'subject',
  93. 'label' => __('Template Subject'),
  94. 'title' => __('Template Subject'),
  95. 'required' => true,
  96. 'value' => $model->getTemplateSubject()
  97. ]
  98. );
  99. $fieldset->addField(
  100. 'sender_name',
  101. 'text',
  102. [
  103. 'name' => 'sender_name',
  104. 'label' => __('Sender Name'),
  105. 'title' => __('Sender Name'),
  106. 'required' => true,
  107. 'value' => $model->getId() !== null ? $model->getTemplateSenderName() : $identityName
  108. ]
  109. );
  110. $fieldset->addField(
  111. 'sender_email',
  112. 'text',
  113. [
  114. 'name' => 'sender_email',
  115. 'label' => __('Sender Email'),
  116. 'title' => __('Sender Email'),
  117. 'class' => 'validate-email',
  118. 'required' => true,
  119. 'value' => $model->getId() !== null ? $model->getTemplateSenderEmail() : $identityEmail
  120. ]
  121. );
  122. $widgetFilters = ['is_email_compatible' => 1];
  123. $wysiwygConfig = $this->_wysiwygConfig->getConfig(['widget_filters' => $widgetFilters]);
  124. if ($model->isPlain()) {
  125. $wysiwygConfig->setEnabled(false);
  126. }
  127. $fieldset->addField(
  128. 'text',
  129. 'editor',
  130. [
  131. 'name' => 'text',
  132. 'label' => __('Template Content'),
  133. 'title' => __('Template Content'),
  134. 'required' => true,
  135. 'state' => 'html',
  136. 'style' => 'height:36em;',
  137. 'value' => $model->getTemplateText(),
  138. 'config' => $wysiwygConfig
  139. ]
  140. );
  141. if (!$model->isPlain()) {
  142. $fieldset->addField(
  143. 'template_styles',
  144. 'textarea',
  145. [
  146. 'name' => 'styles',
  147. 'label' => __('Template Styles'),
  148. 'container_id' => 'field_template_styles',
  149. 'value' => $model->getTemplateStyles()
  150. ]
  151. );
  152. }
  153. $form->setAction($this->getUrl('*/*/save'));
  154. $form->setUseContainer(true);
  155. $this->setForm($form);
  156. return parent::_prepareForm();
  157. }
  158. }