Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Template.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Email\Model;
7 
9 
42 {
48  const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
49 
55  const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
56 
61  const XML_PATH_SYSTEM_SMTP_DISABLE = 'system/smtp/disable';
62 
68  protected $_bcc = [];
69 
75  protected $_returnPath = '';
76 
82  protected $_replyTo = '';
83 
87  protected $_vars = [];
88 
92  protected $_sendingException = null;
93 
99  private $filterFactory;
100 
104  private $serializer;
105 
128  public function __construct(
129  \Magento\Framework\Model\Context $context,
130  \Magento\Framework\View\DesignInterface $design,
131  \Magento\Framework\Registry $registry,
132  \Magento\Store\Model\App\Emulation $appEmulation,
134  \Magento\Framework\View\Asset\Repository $assetRepo,
135  \Magento\Framework\Filesystem $filesystem,
136  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
137  \Magento\Email\Model\Template\Config $emailConfig,
138  \Magento\Email\Model\TemplateFactory $templateFactory,
139  \Magento\Framework\Filter\FilterManager $filterManager,
140  \Magento\Framework\UrlInterface $urlModel,
141  \Magento\Email\Model\Template\FilterFactory $filterFactory,
142  array $data = [],
143  \Magento\Framework\Serialize\Serializer\Json $serializer = null
144  ) {
145  $this->filterFactory = $filterFactory;
146  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
147  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
148  parent::__construct(
149  $context,
150  $design,
151  $registry,
154  $assetRepo,
155  $filesystem,
156  $scopeConfig,
157  $emailConfig,
160  $urlModel,
161  $data
162  );
163  }
164 
170  protected function _construct()
171  {
172  $this->_init(\Magento\Email\Model\ResourceModel\Template::class);
173  }
174 
180  public function getId()
181  {
182  return $this->getTemplateId();
183  }
184 
191  public function setId($value)
192  {
193  return $this->setTemplateId($value);
194  }
195 
201  public function isValidForSend()
202  {
203  return $this->getSenderName() && $this->getSenderEmail() && $this->getTemplateSubject();
204  }
205 
211  public function getType()
212  {
213  $templateType = $this->getTemplateType();
214  if (null === $templateType && $this->getId()) {
215  $templateType = $this->emailConfig->getTemplateType($this->getId());
217  }
218  return $templateType !== null ? $templateType : self::TYPE_HTML;
219  }
220 
227  public function getSendingException()
228  {
230  }
231 
239  public function getProcessedTemplateSubject(array $variables)
240  {
241  $processor = $this->getTemplateFilter();
242 
243  $variables['this'] = $this;
244 
245  $processor->setVariables($variables);
246 
247  $this->applyDesignConfig();
248  $storeId = $this->getDesignConfig()->getStore();
249  try {
250  $processedResult = $processor->setStoreId($storeId)->filter(__($this->getTemplateSubject()));
251  } catch (\Exception $e) {
252  $this->cancelDesignConfig();
253  throw new \Magento\Framework\Exception\MailException(__($e->getMessage()), $e);
254  }
255  $this->cancelDesignConfig();
256  return $processedResult;
257  }
258 
266  public function addBcc($bcc)
267  {
268  $this->_bcc[] = $bcc;
269  return $this;
270  }
271 
279  public function setReturnPath($email)
280  {
281  $this->_returnPath = $email;
282  return $this;
283  }
284 
292  public function setReplyTo($email)
293  {
294  $this->_replyTo = $email;
295  return $this;
296  }
297 
304  protected function _parseVariablesString($variablesString)
305  {
306  $variables = [];
307  if ($variablesString && is_string($variablesString)) {
308  $variablesString = str_replace("\n", '', $variablesString);
309  $variables = $this->serializer->unserialize($variablesString);
310  }
311  return $variables;
312  }
313 
320  public function getVariablesOptionArray($withGroup = false)
321  {
322  $optionArray = [];
323  $variables = $this->_parseVariablesString($this->getData('orig_template_variables'));
324  if ($variables) {
325  foreach ($variables as $value => $label) {
326  $optionArray[] = ['value' => '{{' . $value . '}}', 'label' => __('%1', $label)];
327  }
328  if ($withGroup) {
329  $optionArray = ['label' => __('Template Variables'), 'value' => $optionArray];
330  }
331  }
332  return $optionArray;
333  }
334 
341  public function beforeSave()
342  {
343  $code = $this->getTemplateCode();
344  if (empty($code)) {
345  throw new \Magento\Framework\Exception\MailException(__('Please enter a template name.'));
346  }
347  if ($this->_getResource()->checkCodeUsage($this)) {
348  throw new \Magento\Framework\Exception\MailException(__('Duplicate Of Template Name'));
349  }
350  parent::beforeSave();
351  return $this;
352  }
353 
360  public function processTemplate()
361  {
362  // Support theme fallback for email templates
363  $isDesignApplied = $this->applyDesignConfig();
364 
365  $templateId = $this->getId();
366  if (is_numeric($templateId)) {
367  $this->load($templateId);
368  } else {
369  $this->loadDefault($templateId);
370  }
371 
372  if (!$this->getId()) {
373  throw new \Magento\Framework\Exception\MailException(
374  __('Invalid transactional email code: %1', $templateId)
375  );
376  }
377 
378  $this->setUseAbsoluteLinks(true);
379  $text = $this->getProcessedTemplate($this->_getVars());
380 
381  if ($isDesignApplied) {
382  $this->cancelDesignConfig();
383  }
384  return $text;
385  }
386 
392  public function getSubject()
393  {
394  return $this->getProcessedTemplateSubject($this->_getVars());
395  }
396 
403  public function setVars(array $vars)
404  {
405  $this->_vars = $vars;
406  return $this;
407  }
408 
415  public function setOptions(array $options)
416  {
417  return $this->setDesignConfig($options);
418  }
419 
423  protected function getFilterFactory()
424  {
425  return $this->filterFactory;
426  }
427 
433  protected function _getVars()
434  {
435  return $this->_vars;
436  }
437 }
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\View\DesignInterface $design, \Magento\Framework\Registry $registry, \Magento\Store\Model\App\Emulation $appEmulation, StoreManagerInterface $storeManager, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Email\Model\Template\Config $emailConfig, \Magento\Email\Model\TemplateFactory $templateFactory, \Magento\Framework\Filter\FilterManager $filterManager, \Magento\Framework\UrlInterface $urlModel, \Magento\Email\Model\Template\FilterFactory $filterFactory, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Template.php:128
getData($key='', $index=null)
Definition: DataObject.php:119
$email
Definition: details.phtml:13
$processor
Definition: 404.php:10
getProcessedTemplateSubject(array $variables)
Definition: Template.php:239
__()
Definition: __.php:13
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$templateType
Definition: list.phtml:37
$templateId
Definition: queue.php:15
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
getVariablesOptionArray($withGroup=false)
Definition: Template.php:320
setOptions(array $options)
Definition: Template.php:415
const XML_PATH_SENDING_RETURN_PATH_EMAIL
Definition: Template.php:55
_parseVariablesString($variablesString)
Definition: Template.php:304
$code
Definition: info.phtml:12