edit.phtml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. use Magento\Framework\App\TemplateTypesInterface;
  7. // @codingStandardsIgnoreFile
  8. /** @var $block \Magento\Email\Block\Adminhtml\Template\Edit */
  9. ?>
  10. <?php if (!$block->getEditMode()): ?>
  11. <form action="<?= $block->escapeUrl($block->getLoadUrl()) ?>" method="post" id="email_template_load_form">
  12. <?= $block->getBlockHtml('formkey') ?>
  13. <fieldset class="admin__fieldset form-inline">
  14. <legend class="admin__legend"><span><?= $block->escapeHtml(__('Load default template')) ?></span></legend><br>
  15. <div class="admin__field">
  16. <label class="admin__field-label" for="template_select"><?= $block->escapeHtml(__('Template')) ?></label>
  17. <div class="admin__field-control">
  18. <select id="template_select" name="code" class="admin__control-select required-entry">
  19. <?php foreach ($block->getTemplateOptions() as $group => $options): ?>
  20. <?php if ($group): ?>
  21. <optgroup label="<?= $block->escapeHtmlAttr($group) ?>">
  22. <?php endif; ?>
  23. <?php foreach ($options as $option): ?>
  24. <option value="<?= $block->escapeHtmlAttr($option['value']) ?>"<?= /* @noEscape */ $block->getOrigTemplateCode() == $option['value'] ? ' selected="selected"' : '' ?>><?= $block->escapeHtml($option['label']) ?></option>
  25. <?php endforeach; ?>
  26. <?php if ($group): ?>
  27. </optgroup>
  28. <?php endif; ?>
  29. <?php endforeach; ?>
  30. </select>
  31. </div>
  32. </div>
  33. <div class="admin__field required">
  34. <span class="admin__field-label"></span>
  35. <div class="admin__field-control">
  36. <?= /* @noEscape */ $block->getLoadButtonHtml() ?>
  37. </div>
  38. </div>
  39. </fieldset>
  40. </form>
  41. <?php endif ?>
  42. <form action="<?= $block->escapeUrl($block->getSaveUrl()) ?>" method="post" id="email_template_edit_form">
  43. <?= /* @noEscape */ $block->getBlockHtml('formkey') ?>
  44. <input type="hidden" id="change_flag_element" name="_change_type_flag" value="" />
  45. <input type="hidden" id="orig_template_code" name="orig_template_code" value="<?= $block->escapeHtmlAttr($block->getOrigTemplateCode()) ?>" />
  46. <?= /* @noEscape */ $block->getFormHtml() ?>
  47. </form>
  48. <form action="<?= $block->escapeUrl($block->getPreviewUrl()) ?>" method="post" id="email_template_preview_form" target="_blank">
  49. <?= /* @noEscape */ $block->getBlockHtml('formkey') ?>
  50. <div class="no-display">
  51. <input type="hidden" id="preview_type" name="type" value="<?= /* @noEscape */ $block->isTextType() ? 1 : 2 ?>" />
  52. <input type="hidden" id="preview_text" name="text" value="" />
  53. <input type="hidden" id="preview_styles" name="styles" value="" />
  54. </div>
  55. </form>
  56. <script>
  57. require([
  58. "jquery",
  59. "wysiwygAdapter",
  60. "Magento_Ui/js/modal/alert",
  61. "mage/mage",
  62. "Magento_Email/js/variables",
  63. "prototype"
  64. ], function(jQuery, tinyMCE, alert){
  65. //<![CDATA[
  66. jQuery('#email_template_edit_form').mage('form').mage('validation');
  67. jQuery('#email_template_preview_form').mage('form').mage('validation');
  68. jQuery('#email_template_load_form').mage('validation');
  69. templateControl = {
  70. unconvertedText: '',
  71. typeChange: false,
  72. variables: null,
  73. events: {
  74. 'click [data-role=template-save]' : 'save',
  75. 'click [data-role=template-preview]' : 'preview',
  76. 'click [data-role=template-unstrip]' : 'unStripTags',
  77. 'click [data-role=template-strip]' : 'stripTags',
  78. 'click [data-role=template-delete]': 'deleteTemplate',
  79. 'click [data-role=template-load]': 'load'
  80. },
  81. init: function () {
  82. if ($('convert_button_back')) {
  83. $('convert_button_back').hide();
  84. }
  85. this.bindEvents();
  86. this.renderPaths(<?= /* @noEscape */ $block->getCurrentlyUsedForPaths() ?>, 'currently_used_for');
  87. },
  88. bindEvents: function(){
  89. var events = this.events,
  90. eventName,
  91. selector,
  92. callback,
  93. key;
  94. for( key in events ){
  95. callback = this[events[key]];
  96. key = key.split(' ');
  97. eventName = key[0];
  98. selector = key.slice(1).join(' ');
  99. jQuery(selector).on(eventName, jQuery.proxy(callback, this) );
  100. }
  101. },
  102. stripTags: function () {
  103. if(!window.confirm("<?= $block->escapeJs($block->escapeHtml(__('Are you sure you want to strip tags?'))) ?>")) {
  104. return false;
  105. }
  106. this.unconvertedText = $('template_text').value;
  107. $('convert_button').hide();
  108. $('template_text').value = $('template_text').value.stripScripts().replace(
  109. new RegExp('<style[^>]*>[\\S\\s]*?</style>', 'img'), ''
  110. ).stripTags().strip();
  111. $('convert_button_back').show();
  112. $('field_template_styles').hide();
  113. this.typeChange = true;
  114. return false;
  115. },
  116. unStripTags: function () {
  117. $('convert_button').show();
  118. $('convert_button_back').hide();
  119. $('template_text').value = this.unconvertedText;
  120. $('field_template_styles').show();
  121. this.typeChange = false;
  122. return false;
  123. },
  124. save: function() {
  125. if (this.typeChange) {
  126. $('change_flag_element').value = '1';
  127. }
  128. // Temporary solution will be replaced after refactoring of email templates functionality
  129. jQuery('#email_template_edit_form').triggerHandler('save');
  130. return false;
  131. },
  132. preview: function() {
  133. if (this.typeChange) {
  134. $('preview_type').value = <?= /* @noEscape */ TemplateTypesInterface::TYPE_TEXT ?>;
  135. } else {
  136. $('preview_type').value = <?= (int) $block->getTemplateType() ?>;
  137. }
  138. if (typeof tinyMCE == 'undefined' || !tinyMCE.get('template_text')) {
  139. $('preview_text').value = $('template_text').value;
  140. } else {
  141. $('preview_text').value = tinyMCE.get('template_text').getHTML();
  142. }
  143. if ($('template_styles') != undefined) {
  144. $('preview_styles').value = $('template_styles').value;
  145. }
  146. // Temporary solution will be replaced after refactoring of email templates functionality
  147. jQuery('#email_template_preview_form').triggerHandler('save');
  148. return false;
  149. },
  150. deleteTemplate: function() {
  151. if(window.confirm("<?= $block->escapeJs($block->escapeHtml(__('Are you sure you want to delete this template?'))) ?>")) {
  152. window.location.href = '<?= $block->escapeJs($block->escapeUrl($block->getDeleteUrl())) ?>';
  153. }
  154. },
  155. load: function() {
  156. // Temporary solution will be replaced after refactoring of email templates functionality
  157. if (!jQuery('#email_template_load_form').valid()) {
  158. return;
  159. }
  160. this.variables = null;
  161. new Ajax.Request($('email_template_load_form').action, {
  162. parameters: Form.serialize($('email_template_load_form'), true),
  163. area: $('email_template_load_form'),
  164. onComplete: function (transport) {
  165. if (transport.responseText.isJSON()) {
  166. var fields = $H(transport.responseText.evalJSON());
  167. fields.each(function(pair) {
  168. if ($(pair.key)) {
  169. $(pair.key).value = pair.value.strip();
  170. }
  171. if (pair.key == 'template_type') {
  172. if (pair.value == 1) {
  173. this.typeChange = true;
  174. $('convert_button').hide();
  175. $('convert_button_back').hide();
  176. } else {
  177. this.typeChange = false;
  178. $('convert_button_back').hide();
  179. $('convert_button').show();
  180. }
  181. }
  182. if (pair.key == 'orig_template_currently_used_for') {
  183. if(pair.value.length){
  184. $('currently_used_for').show();
  185. this.renderPaths(pair.value, 'currently_used_for');
  186. }
  187. else{
  188. $('currently_used_for').hide();
  189. }
  190. }
  191. }.bind(this));
  192. } else {
  193. alert({
  194. content: '<?= $block->escapeJs($block->escapeHtml(__('The template did not load. Please review the log for details.'))) ?>'
  195. });
  196. }
  197. }.bind(this)
  198. });
  199. },
  200. renderPaths: function(paths, fieldId) {
  201. var field = $(fieldId);
  202. if (field) {
  203. field.down('div').down('div').update(this.parsePath(paths, '<span class="path-delimiter">&nbsp;-&gt;&nbsp;</span>', '<br />'));
  204. }
  205. },
  206. parsePath: function (value, pathDelimiter, lineDelimiter) {
  207. if (Object.isArray(value)) {
  208. var result = [];
  209. for (var i = 0, len = value.length; i < len; i++) {
  210. result.push(this.parsePath(value[i], pathDelimiter, pathDelimiter));
  211. }
  212. return result.join(lineDelimiter);
  213. }
  214. if(!Object.isString(value) && value.title) {
  215. value = (value.url ? '<a href="' + value.url + '">' + value.title + '</a>' : value.title) + (value.scope ? '&nbsp;&nbsp;<span class="path-scope-label">(' + value.scope + ')</span>' : '');
  216. }
  217. return value;
  218. },
  219. openVariableChooser: function() {
  220. Variables.init('template_text');
  221. if (this.variables == null) {
  222. Variables.resetData();
  223. this.variables = $('variables').value.evalJSON();
  224. var templateVariablesValue = $('template_variables').value;
  225. if (this.variables && templateVariablesValue) {
  226. if (templateVariables = templateVariablesValue.evalJSON()) {
  227. this.variables.push(templateVariables);
  228. }
  229. }
  230. }
  231. if (this.variables) {
  232. Variables.openVariableChooser(this.variables);
  233. }
  234. }
  235. };
  236. templateControl.init();
  237. //]]>
  238. });
  239. </script>