Form.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Adminhtml system template edit form
  8. */
  9. namespace Magento\Email\Block\Adminhtml\Template\Edit;
  10. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  11. {
  12. /**
  13. * @var \Magento\Variable\Model\Source\Variables
  14. */
  15. protected $_variables;
  16. /**
  17. * @var \Magento\Variable\Model\VariableFactory
  18. */
  19. protected $_variableFactory;
  20. /**
  21. * @var \Magento\Framework\Serialize\Serializer\Json
  22. */
  23. private $serializer;
  24. /**
  25. * @param \Magento\Backend\Block\Template\Context $context
  26. * @param \Magento\Framework\Registry $registry
  27. * @param \Magento\Framework\Data\FormFactory $formFactory
  28. * @param \Magento\Variable\Model\VariableFactory $variableFactory
  29. * @param \Magento\Variable\Model\Source\Variables $variables
  30. * @param array $data
  31. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  32. * @throws \RuntimeException
  33. */
  34. public function __construct(
  35. \Magento\Backend\Block\Template\Context $context,
  36. \Magento\Framework\Registry $registry,
  37. \Magento\Framework\Data\FormFactory $formFactory,
  38. \Magento\Variable\Model\VariableFactory $variableFactory,
  39. \Magento\Variable\Model\Source\Variables $variables,
  40. array $data = [],
  41. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  42. ) {
  43. $this->_variableFactory = $variableFactory;
  44. $this->_variables = $variables;
  45. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  46. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  47. parent::__construct($context, $registry, $formFactory, $data);
  48. }
  49. /**
  50. * Prepare layout.
  51. * Add files to use dialog windows
  52. *
  53. * @return $this
  54. */
  55. protected function _prepareLayout()
  56. {
  57. $this->pageConfig->addPageAsset('prototype/windows/themes/default.css');
  58. return parent::_prepareLayout();
  59. }
  60. /**
  61. * Add fields to form and create template info form
  62. *
  63. * @return \Magento\Backend\Block\Widget\Form
  64. * @SuppressWarnings(PHPMD.NPathComplexity)
  65. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  66. * @throws \Magento\Framework\Exception\LocalizedException
  67. */
  68. protected function _prepareForm()
  69. {
  70. /** @var \Magento\Framework\Data\Form $form */
  71. $form = $this->_formFactory->create();
  72. $fieldset = $form->addFieldset(
  73. 'base_fieldset',
  74. ['legend' => __('Template Information'), 'class' => 'fieldset-wide']
  75. );
  76. $templateId = $this->getEmailTemplate()->getId();
  77. $fieldset->addField(
  78. 'currently_used_for',
  79. 'label',
  80. [
  81. 'label' => __('Currently Used For'),
  82. 'container_id' => 'currently_used_for',
  83. 'after_element_html' => '<script>require(["prototype"], function () {' .
  84. (!$this->getEmailTemplate()->getSystemConfigPathsWhereCurrentlyUsed() ? '$(\'' .
  85. 'currently_used_for' .
  86. '\').hide(); ' : '') .
  87. '});</script>'
  88. ]
  89. );
  90. $fieldset->addField(
  91. 'template_code',
  92. 'text',
  93. ['name' => 'template_code', 'label' => __('Template Name'), 'required' => true]
  94. );
  95. $fieldset->addField(
  96. 'template_subject',
  97. 'text',
  98. ['name' => 'template_subject', 'label' => __('Template Subject'), 'required' => true]
  99. );
  100. $fieldset->addField('orig_template_variables', 'hidden', ['name' => 'orig_template_variables']);
  101. $fieldset->addField(
  102. 'variables',
  103. 'hidden',
  104. ['name' => 'variables', 'value' => $this->serializer->serialize($this->getVariables())]
  105. );
  106. $fieldset->addField('template_variables', 'hidden', ['name' => 'template_variables']);
  107. $insertVariableButton = $this->getLayout()->createBlock(
  108. \Magento\Backend\Block\Widget\Button::class,
  109. '',
  110. [
  111. 'data' => [
  112. 'type' => 'button',
  113. 'label' => __('Insert Variable...'),
  114. 'onclick' => 'templateControl.openVariableChooser();return false;',
  115. ]
  116. ]
  117. );
  118. $fieldset->addField('insert_variable', 'note', ['text' => $insertVariableButton->toHtml(), 'label' => '']);
  119. $fieldset->addField(
  120. 'template_text',
  121. 'textarea',
  122. [
  123. 'name' => 'template_text',
  124. 'label' => __('Template Content'),
  125. 'title' => __('Template Content'),
  126. 'required' => true,
  127. 'style' => 'height:24em;'
  128. ]
  129. );
  130. if (!$this->getEmailTemplate()->isPlain()) {
  131. $fieldset->addField(
  132. 'template_styles',
  133. 'textarea',
  134. [
  135. 'name' => 'template_styles',
  136. 'label' => __('Template Styles'),
  137. 'container_id' => 'field_template_styles'
  138. ]
  139. );
  140. }
  141. if ($templateId) {
  142. $form->addValues($this->getEmailTemplate()->getData());
  143. }
  144. $values = $this->_backendSession->getData('email_template_form_data', true);
  145. if ($values) {
  146. $form->setValues($values);
  147. }
  148. $this->setForm($form);
  149. return parent::_prepareForm();
  150. }
  151. /**
  152. * Return current email template model
  153. *
  154. * @return \Magento\Email\Model\Template
  155. */
  156. public function getEmailTemplate()
  157. {
  158. return $this->getData('email_template');
  159. }
  160. /**
  161. * Retrieve variables to insert into email
  162. *
  163. * @return array
  164. */
  165. public function getVariables()
  166. {
  167. $variables = $this->_variables->toOptionArray(true);
  168. $customVariables = $this->_variableFactory->create()->getVariablesOptionArray(true);
  169. if ($customVariables) {
  170. $variables = array_merge_recursive($variables, $customVariables);
  171. }
  172. $template = $this->getEmailTemplate();
  173. if ($template->getId() && ($templateVariables = $template->getVariablesOptionArray(true))) {
  174. $variables = array_merge_recursive($variables, $templateVariables);
  175. }
  176. return $variables;
  177. }
  178. }