Preview.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Adminhtml system template preview block
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Email\Block\Adminhtml\Template;
  12. /**
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Preview extends \Magento\Backend\Block\Widget
  17. {
  18. /**
  19. * @var \Magento\Framework\Filter\Input\MaliciousCode
  20. */
  21. protected $_maliciousCode;
  22. /**
  23. * @var \Magento\Email\Model\TemplateFactory
  24. */
  25. protected $_emailFactory;
  26. /**
  27. * @var string
  28. */
  29. protected $profilerName = 'email_template_proccessing';
  30. /**
  31. * @param \Magento\Backend\Block\Template\Context $context
  32. * @param \Magento\Framework\Filter\Input\MaliciousCode $maliciousCode
  33. * @param \Magento\Email\Model\TemplateFactory $emailFactory
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Backend\Block\Template\Context $context,
  38. \Magento\Framework\Filter\Input\MaliciousCode $maliciousCode,
  39. \Magento\Email\Model\TemplateFactory $emailFactory,
  40. array $data = []
  41. ) {
  42. $this->_maliciousCode = $maliciousCode;
  43. $this->_emailFactory = $emailFactory;
  44. parent::__construct($context, $data);
  45. }
  46. /**
  47. * Prepare html output
  48. *
  49. * @return string
  50. */
  51. protected function _toHtml()
  52. {
  53. $storeId = $this->getAnyStoreView()->getId();
  54. /** @var $template \Magento\Email\Model\Template */
  55. $template = $this->_emailFactory->create();
  56. if ($id = (int)$this->getRequest()->getParam('id')) {
  57. $template->load($id);
  58. } else {
  59. $template->setTemplateType($this->getRequest()->getParam('type'));
  60. $template->setTemplateText($this->getRequest()->getParam('text'));
  61. $template->setTemplateStyles($this->getRequest()->getParam('styles'));
  62. }
  63. $template->setTemplateText($this->_maliciousCode->filter($template->getTemplateText()));
  64. \Magento\Framework\Profiler::start($this->profilerName);
  65. $template->emulateDesign($storeId);
  66. $templateProcessed = $this->_appState->emulateAreaCode(
  67. \Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA,
  68. [$template, 'getProcessedTemplate']
  69. );
  70. $template->revertDesign();
  71. if ($template->isPlain()) {
  72. $templateProcessed = "<pre>" . htmlspecialchars($templateProcessed) . "</pre>";
  73. }
  74. \Magento\Framework\Profiler::stop($this->profilerName);
  75. return $templateProcessed;
  76. }
  77. /**
  78. * Get either default or any store view
  79. *
  80. * @return \Magento\Store\Model\Store|null
  81. */
  82. protected function getAnyStoreView()
  83. {
  84. $store = $this->_storeManager->getDefaultStoreView();
  85. if ($store) {
  86. return $store;
  87. }
  88. foreach ($this->_storeManager->getStores() as $store) {
  89. return $store;
  90. }
  91. return null;
  92. }
  93. }